Author: ivol37 at gmail.com
Date: Tue Dec 28 16:50:28 2010
New Revision: 551

Log:
[AMDATU-183] Small improvement: close the iterator nicely

Modified:
   
trunk/amdatu-semanticweb/sparqlendpoint/src/main/java/org/amdatu/semanticweb/sparqlendpoint/service/SPARQLEndpointServiceImpl.java
   
trunk/integration-tests/src/test/java/org/amdatu/test/integration/tests/RDF2GOTest.java

Modified: 
trunk/amdatu-semanticweb/sparqlendpoint/src/main/java/org/amdatu/semanticweb/sparqlendpoint/service/SPARQLEndpointServiceImpl.java
==============================================================================
--- 
trunk/amdatu-semanticweb/sparqlendpoint/src/main/java/org/amdatu/semanticweb/sparqlendpoint/service/SPARQLEndpointServiceImpl.java
  (original)
+++ 
trunk/amdatu-semanticweb/sparqlendpoint/src/main/java/org/amdatu/semanticweb/sparqlendpoint/service/SPARQLEndpointServiceImpl.java
  Tue Dec 28 16:50:28 2010
@@ -147,25 +147,29 @@
             int resultRow = 1;
             int returnRowCount = 0;
             final int endIndex = endIndexParam > 0 ? endIndexParam : 
MAX_ROW_COUNT;
-            while (iterator.hasNext()) {
-                if (startIndex <= resultRow && resultRow <= endIndex && 
returnRowCount < MAX_ROW_COUNT) {
-                    JSONObject row = new JSONObject();
-                    QueryRow queryRow = iterator.next();
-                    for (String name : names) {
-                        String value;
-                        try {
-                            value = queryRow.getLiteralValue(name);
-                        } catch (ModelRuntimeException e) {
-                            value = queryRow.getValue(name).toString();
+            try {
+                while (iterator.hasNext()) {
+                    if (startIndex <= resultRow && resultRow <= endIndex && 
returnRowCount < MAX_ROW_COUNT) {
+                        JSONObject row = new JSONObject();
+                        QueryRow queryRow = iterator.next();
+                        for (String name : names) {
+                            String value;
+                            try {
+                                value = queryRow.getLiteralValue(name);
+                            } catch (ModelRuntimeException e) {
+                                value = queryRow.getValue(name).toString();
+                            }
+                            row.put(name, value);
+                            returnRowCount++;
+                            jsonObject.append("row", row);
                         }
-                        row.put(name, value);
-                        returnRowCount++;
-                        jsonObject.append("row", row);
+                    } else {
+                        iterator.next();
                     }
-                } else {
-                    iterator.next();
+                    resultRow++;
                 }
-                resultRow++;
+            } finally {
+                iterator.close();
             }
 
             jsonObject.put("startIndex", resultRow - 1 > 0 ? startIndex : 0);

Modified: 
trunk/integration-tests/src/test/java/org/amdatu/test/integration/tests/RDF2GOTest.java
==============================================================================
--- 
trunk/integration-tests/src/test/java/org/amdatu/test/integration/tests/RDF2GOTest.java
     (original)
+++ 
trunk/integration-tests/src/test/java/org/amdatu/test/integration/tests/RDF2GOTest.java
     Tue Dec 28 16:50:28 2010
@@ -106,13 +106,17 @@
         String sparqQuery = "SELECT ?subject ?predicate ?object WHERE 
{?subject ?predicate ?object}";
         QueryResultTable resultTable = model.sparqlSelect(sparqQuery);
         ClosableIterator<QueryRow> iter = resultTable.iterator();
-        while (iter.hasNext()) {
-            QueryRow row = iter.next();
-            Node subject = row.getValue("subject");
-            Node predicate = row.getValue("predicate");
-            Node object = row.getValue("object");
-            m_logService.log(LogService.LOG_DEBUG, "subject='" + 
subject.toString() + "', predicate='"
-                + predicate.toString() + "', object='" + object + "'");
+        try {
+            while (iter.hasNext()) {
+                QueryRow row = iter.next();
+                Node subject = row.getValue("subject");
+                Node predicate = row.getValue("predicate");
+                Node object = row.getValue("object");
+                m_logService.log(LogService.LOG_DEBUG, "subject='" + 
subject.toString() + "', predicate='"
+                    + predicate.toString() + "', object='" + object + "'");
+            }
+        } finally {
+            iter.close();
         }
 
         model.close();

Reply via email to