jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject jena git commit: Tidy some more try-resource-QueryExecution
Date Sun, 03 Dec 2017 12:32:52 GMT
Repository: jena
Updated Branches:
  refs/heads/master 9d2a65785 -> 601681dc4


Tidy some more try-resource-QueryExecution


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/601681dc
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/601681dc
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/601681dc

Branch: refs/heads/master
Commit: 601681dc4abd7bd677a5e3eb02fd7d1d040f14d0
Parents: 9d2a657
Author: Andy Seaborne <andy@apache.org>
Authored: Sun Dec 3 12:32:36 2017 +0000
Committer: Andy Seaborne <andy@apache.org>
Committed: Sun Dec 3 12:32:36 2017 +0000

----------------------------------------------------------------------
 .../core/describe/DescribeBNodeClosure.java     | 18 ++---
 .../apache/jena/sparql/util/QueryExecUtils.java | 72 +++++++++-----------
 .../jena/sparql/util/graph/GraphUtils.java      | 14 ++--
 3 files changed, 46 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/601681dc/jena-arq/src/main/java/org/apache/jena/sparql/core/describe/DescribeBNodeClosure.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/core/describe/DescribeBNodeClosure.java
b/jena-arq/src/main/java/org/apache/jena/sparql/core/describe/DescribeBNodeClosure.java
index d718a7a..601194e 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/core/describe/DescribeBNodeClosure.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/core/describe/DescribeBNodeClosure.java
@@ -61,15 +61,15 @@ public class DescribeBNodeClosure implements DescribeHandler
         
         QuerySolutionMap qsm = new QuerySolutionMap() ;
         qsm.add("s", r) ;
-        QueryExecution qExec = QueryExecutionFactory.create(query, dataset, qsm) ;
-        ResultSet rs = qExec.execSelect() ;
-        for ( ; rs.hasNext() ; )
-        {
-            QuerySolution qs = rs.next() ;
-            String gName = qs.getResource("g").getURI() ;
-            Model model =  dataset.getNamedModel(gName) ;
-            Resource r2 = otherModel(r, model) ;
-            Closure.closure(r2, false, acc) ;
+        try(QueryExecution qExec = QueryExecutionFactory.create(query, dataset, qsm)) {
+            ResultSet rs = qExec.execSelect() ;
+            for ( ; rs.hasNext() ; ) {
+                QuerySolution qs = rs.next() ;
+                String gName = qs.getResource("g").getURI() ;
+                Model model =  dataset.getNamedModel(gName) ;
+                Resource r2 = otherModel(r, model) ;
+                Closure.closure(r2, false, acc) ;
+            }
         }
         
 //        // Named graphs

http://git-wip-us.apache.org/repos/asf/jena/blob/601681dc/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryExecUtils.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryExecUtils.java b/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryExecUtils.java
index 995dbbb..c76d1d4 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryExecUtils.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryExecUtils.java
@@ -310,58 +310,50 @@ public class QueryExecUtils {
         if ( q.getResultVars().size() != 1 )
             throw new ARQException("getExactlyOne: Must have exactly one result columns")
;
         String varname = q.getResultVars().get(0) ;
-        QueryExecution qExec = QueryExecutionFactory.create(q, ds) ;
-        return getExactlyOne(qExec, varname) ;
+        try ( QueryExecution qExec = QueryExecutionFactory.create(q, ds) ) {
+            return getExactlyOne(qExec, varname) ;
+        }
     }
 
     /**
-     * Execute, expecting the result to be one row, one column. Return that one
-     * RDFNode or throw an exception
+     * Execute, expecting the result to be one row, one column. Return that one.
+     * RDFNode or throw an exception.
+     * Use with {@code try ( QueryExecution qExec = ....)}.
      */
     public static RDFNode getExactlyOne(QueryExecution qExec, String varname) {
-        try {
-            ResultSet rs = qExec.execSelect() ;
+        ResultSet rs = qExec.execSelect() ;
 
-            if ( !rs.hasNext() )
-                throw new ARQException("Not found: var ?" + varname) ;
+        if ( !rs.hasNext() )
+            throw new ARQException("Not found: var ?" + varname) ;
 
-            QuerySolution qs = rs.nextSolution() ;
-            RDFNode r = qs.get(varname) ;
-            if ( rs.hasNext() )
-                throw new ARQException("More than one: var ?" + varname) ;
-            return r ;
-        }
-        finally {
-            qExec.close() ;
-        }
+        QuerySolution qs = rs.nextSolution() ;
+        RDFNode r = qs.get(varname) ;
+        if ( rs.hasNext() )
+            throw new ARQException("More than one: var ?" + varname) ;
+        return r ;
     }
 
     /**
      * Execute, expecting the result to be one row, one column. Return that one
-     * RDFNode or null Throw exception if more than one.
+     * RDFNode or null. Throw exception if more than one.
+     * Use with {@code try ( QueryExecution qExec = ....)}.
      */
-    public static RDFNode getOne(QueryExecution qExec, String varname) {
-        try {
-            ResultSet rs = qExec.execSelect() ;
-
-            if ( !rs.hasNext() )
-                return null ;
-
-            QuerySolution qs = rs.nextSolution() ;
-            RDFNode r = qs.get(varname) ;
-            if ( rs.hasNext() ) {
-                QuerySolution qs2 = rs.next() ;
-                RDFNode r2 = qs2.get(varname) ;
-                if ( rs.hasNext() )
-                    throw new ARQException("More than one: var ?" + varname + " -> " +
r + ", " + r2 + ", ...") ;
-                else
-                    throw new ARQException("Found two matches: var ?" + varname + " ->
" + r + ", " + r2) ;
-            }
-            return r ;
-        }
-        finally {
-            qExec.close() ;
+    public static RDFNode getAtMostOne(QueryExecution qExec, String varname) {
+        ResultSet rs = qExec.execSelect() ;
+
+        if ( !rs.hasNext() )
+            return null ;
+
+        QuerySolution qs = rs.nextSolution() ;
+        RDFNode r = qs.get(varname) ;
+        if ( rs.hasNext() ) {
+            QuerySolution qs2 = rs.next() ;
+            RDFNode r2 = qs2.get(varname) ;
+            if ( rs.hasNext() )
+                throw new ARQException("More than one: var ?" + varname + " -> " + r +
", " + r2 + ", ...") ;
+            else
+                throw new ARQException("Found two matches: var ?" + varname + " -> " +
r + ", " + r2) ;
         }
+        return r ;
     }
-
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/601681dc/jena-arq/src/main/java/org/apache/jena/sparql/util/graph/GraphUtils.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/util/graph/GraphUtils.java b/jena-arq/src/main/java/org/apache/jena/sparql/util/graph/GraphUtils.java
index 719fe21..c15fdf7 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/util/graph/GraphUtils.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/util/graph/GraphUtils.java
@@ -18,11 +18,7 @@
 
 package org.apache.jena.sparql.util.graph ;
 
-import java.util.ArrayList ;
-import java.util.HashSet ;
-import java.util.Iterator ;
-import java.util.List ;
-import java.util.Set ;
+import java.util.*;
 
 import org.apache.jena.atlas.iterator.Iter ;
 import org.apache.jena.graph.Graph ;
@@ -33,7 +29,7 @@ import org.apache.jena.rdf.model.* ;
 import org.apache.jena.sparql.core.Quad ;
 import org.apache.jena.sparql.util.NotUniqueException ;
 import org.apache.jena.sparql.util.PropertyRequiredException ;
-import org.apache.jena.sparql.util.QueryExecUtils ;
+import org.apache.jena.sparql.util.QueryExecUtils;
 import org.apache.jena.sparql.util.TypeNotUniqueException ;
 import org.apache.jena.util.iterator.ExtendedIterator ;
 import org.apache.jena.vocabulary.RDF ;
@@ -181,9 +177,9 @@ public class GraphUtils {
         QuerySolutionMap qsm = new QuerySolutionMap() ;
         qsm.add("ATYPE", atype) ;
 
-        QueryExecution qExec = QueryExecutionFactory.create(q, model, qsm) ;
-        Resource r = (Resource)QueryExecUtils.getOne(qExec, "root") ;
-        return r ;
+        try(QueryExecution qExec = QueryExecutionFactory.create(q, model, qsm)) {
+            return (Resource)QueryExecUtils.getAtMostOne(qExec, "root") ;
+        }
     }
 
     public static String fmtURI(Resource r) {


Mime
View raw message