jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [07/15] jena git commit: Added some tests for close() on QueryIterSort ad QueryIterTopN.
Date Wed, 26 Apr 2017 09:09:21 GMT
Added some tests for close() on QueryIterSort ad QueryIterTopN.


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

Branch: refs/heads/master
Commit: 2fe3c281aa55605552e66cae8ac1484072c1944a
Parents: 6989f2f
Author: Chris Dollin <ehog.hedge@googlemail.com>
Authored: Thu Apr 20 14:53:01 2017 +0100
Committer: Chris Dollin <ehog.hedge@googlemail.com>
Committed: Thu Apr 20 14:53:01 2017 +0100

----------------------------------------------------------------------
 .../apache/jena/atlas/data/SortedDataBag.java   |  1 +
 .../sparql/engine/iterator/QueryIterSort.java   | 14 ++--
 .../sparql/engine/iterator/QueryIterTopN.java   | 46 +++++++----
 .../engine/iterator/TestQueryIterSort.java      | 81 +++++++++++++++++---
 4 files changed, 112 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/2fe3c281/jena-arq/src/main/java/org/apache/jena/atlas/data/SortedDataBag.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/atlas/data/SortedDataBag.java b/jena-arq/src/main/java/org/apache/jena/atlas/data/SortedDataBag.java
index a9d2682..ee7aee3 100644
--- a/jena-arq/src/main/java/org/apache/jena/atlas/data/SortedDataBag.java
+++ b/jena-arq/src/main/java/org/apache/jena/atlas/data/SortedDataBag.java
@@ -94,6 +94,7 @@ public class SortedDataBag<E> extends AbstractDataBag<E> {
      */
     public void cancel() {
         comparator.cancel();
+        close();
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/jena/blob/2fe3c281/jena-arq/src/main/java/org/apache/jena/sparql/engine/iterator/QueryIterSort.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/engine/iterator/QueryIterSort.java
b/jena-arq/src/main/java/org/apache/jena/sparql/engine/iterator/QueryIterSort.java
index 3e565db..4485111 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/engine/iterator/QueryIterSort.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/engine/iterator/QueryIterSort.java
@@ -44,9 +44,7 @@ import org.apache.jena.sparql.engine.binding.BindingComparator;
  */
 
 public class QueryIterSort extends QueryIterPlainWrapper {
-    private final QueryIterator embeddedIterator; // Keep a record of the
-    // underlying source for
-    // .cancel.
+    private final QueryIterator embeddedIterator;
     final SortedDataBag<Binding> db;
 
     public QueryIterSort(QueryIterator qIter, List<SortCondition> conditions, ExecutionContext
context) {
@@ -57,10 +55,8 @@ public class QueryIterSort extends QueryIterPlainWrapper {
             final ExecutionContext context) {
         super(null, context);
         this.embeddedIterator = qIter;
-
         ThresholdPolicy<Binding> policy = ThresholdPolicyFactory.policyFromContext(context.getContext());
         this.db = BagFactory.newSortedBag(policy, SerializationFactoryFinder.bindingSerializationFactory(),
comparator);
-
         this.setIterator(new SortedBindingIterator(qIter));
     }
 
@@ -71,6 +67,13 @@ public class QueryIterSort extends QueryIterPlainWrapper {
         super.requestCancel();
     }
 
+    @Override
+    protected void closeIterator() {
+        this.db.close();
+        this.embeddedIterator.close();
+        super.closeIterator();
+    }
+
     private class SortedBindingIterator extends IteratorDelayedInitialization<Binding>
implements Closeable {
         private final QueryIterator qIter;
 
@@ -89,6 +92,7 @@ public class QueryIterSort extends QueryIterPlainWrapper {
             // iterator in a try/finally block, and thus will call
             // close() themselves.
             catch (QueryCancelledException e) {
+		QueryIterSort.this.close();
                 close();
                 throw e;
             }

http://git-wip-us.apache.org/repos/asf/jena/blob/2fe3c281/jena-arq/src/main/java/org/apache/jena/sparql/engine/iterator/QueryIterTopN.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/engine/iterator/QueryIterTopN.java
b/jena-arq/src/main/java/org/apache/jena/sparql/engine/iterator/QueryIterTopN.java
index 137790d..65bc397 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/engine/iterator/QueryIterTopN.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/engine/iterator/QueryIterTopN.java
@@ -29,6 +29,7 @@ import java.util.PriorityQueue ;
 import org.apache.jena.atlas.iterator.Iter ;
 import org.apache.jena.atlas.iterator.IteratorDelayedInitialization ;
 import org.apache.jena.query.Query ;
+import org.apache.jena.query.QueryCancelledException;
 import org.apache.jena.query.QueryExecException ;
 import org.apache.jena.query.SortCondition ;
 import org.apache.jena.sparql.engine.ExecutionContext ;
@@ -83,26 +84,39 @@ public class QueryIterTopN extends QueryIterPlainWrapper
         super.requestCancel() ;
     }
 
+    @Override
+    protected void closeIterator() {
+        this.embeddedIterator.close();
+        super.closeIterator();
+    }
+
     private Iterator<Binding> sortTopN(final QueryIterator qIter, final Comparator<Binding>
comparator) {
         return new IteratorDelayedInitialization<Binding>() {
             @Override
             protected Iterator<Binding> initializeIterator() {
-                while ( qIter.hasNext() ) {
-                    Binding binding = qIter.next() ;
-                    if ( heap.size() < limit )
-                        add(binding) ;
-                    else {
-                        Binding currentMaxLeastN = heap.peek() ;
-                        if ( comparator.compare(binding, currentMaxLeastN) < 0 )
-                            add(binding) ;
-                    }
-                }
-                qIter.close() ;
-                Binding[] y = heap.toArray(new Binding[]{}) ;
-                heap = null ;
-                Arrays.sort(y, comparator) ;
-                return asList(y).iterator() ;
-            }
+		try {
+	                while ( qIter.hasNext() ) {
+	                    Binding binding = qIter.next() ;
+	                    if ( heap.size() < limit )
+	                        add(binding) ;
+	                    else {
+	                        Binding currentMaxLeastN = heap.peek() ;
+	                        if ( comparator.compare(binding, currentMaxLeastN) < 0 )
+	                            add(binding) ;
+	                    }
+	                }
+	                qIter.close() ;
+	                Binding[] y = heap.toArray(new Binding[]{}) ;
+	                heap = null ;
+	                Arrays.sort(y, comparator) ;
+	                return asList(y).iterator() ;
+	            }
+		catch (QueryCancelledException e) {
+			QueryIterTopN.this.close();
+			this.close();
+			throw e;
+		}
+	        }
         } ;
     }
 

http://git-wip-us.apache.org/repos/asf/jena/blob/2fe3c281/jena-arq/src/test/java/org/apache/jena/sparql/engine/iterator/TestQueryIterSort.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/engine/iterator/TestQueryIterSort.java
b/jena-arq/src/test/java/org/apache/jena/sparql/engine/iterator/TestQueryIterSort.java
index e7eeb68..07f8c78 100644
--- a/jena-arq/src/test/java/org/apache/jena/sparql/engine/iterator/TestQueryIterSort.java
+++ b/jena-arq/src/test/java/org/apache/jena/sparql/engine/iterator/TestQueryIterSort.java
@@ -18,9 +18,7 @@
 
 package org.apache.jena.sparql.engine.iterator;
 
-import static org.junit.Assert.assertEquals ;
-import static org.junit.Assert.assertNotNull ;
-import static org.junit.Assert.assertTrue ;
+import static org.junit.Assert.*;
 
 import java.util.ArrayList ;
 import java.util.Iterator ;
@@ -135,6 +133,37 @@ public class TestQueryIterSort {
         assertEquals(0, DataBagExaminer.countTemporaryFiles(qIter.db)) ;
     }
     
+    @Test public void testCloseClosesSourceIterator() {
+	Context context = new Context() ;
+	ExecutionContext ec = new ExecutionContext(context, (Graph) null, (DatasetGraph) null, (OpExecutorFactory)
null);
+	QueryIterSort qis = new QueryIterSort(iterator, comparator, ec);
+	qis.close();
+	assertTrue("source iterator should have been closed", iterator.isClosed());
+    }
+
+        @Test public void testExhaustionClosesSourceIterator() {
+		iterator.setCallback(new Callback()
+			{ @Override public void call() { /* do nothing */ }
+		});
+		Context context = new Context() ;
+		ExecutionContext ec = new ExecutionContext(context, (Graph) null, (DatasetGraph) null,
(OpExecutorFactory) null);
+		QueryIterSort qis = new QueryIterSort(iterator, comparator, ec);
+		while (qis.hasNext()) qis.next();
+		assertTrue("source iterator should have been closed", iterator.isClosed());
+       }
+
+        @Test public void testCancelClosesSourceIterator() {
+		Context context = new Context() ;
+		ExecutionContext ec = new ExecutionContext(context, (Graph) null, (DatasetGraph) null,
(OpExecutorFactory) null);
+		QueryIterSort qis = new QueryIterSort(iterator, comparator, ec);
+		try {
+			while (qis.hasNext()) qis.next();
+			fail("query should have been cancelled by trigger");
+		} catch (QueryCancelledException q) {
+			assertTrue("source iterator should have been closed", iterator.isClosed());
+		}
+	}
+
     @Test
     public void testCleanAfterExhaustion()
     {
@@ -248,6 +277,32 @@ public class TestQueryIterSort {
         assertEquals(0, DataBagExaminer.countTemporaryFiles(qIter.db)) ;
     }
 
+    @Test public void testTopNCloseClosesSource() {
+
+	long numItems = 3;
+	boolean distinct = false;
+	Context context = new Context() ;
+	ExecutionContext ec = new ExecutionContext(context, (Graph) null, (DatasetGraph) null, (OpExecutorFactory)
null);
+	QueryIterTopN tn = new QueryIterTopN(iterator, comparator, numItems, distinct, ec);
+	tn.close();
+	assertTrue(iterator.isClosed());
+	}
+
+       @Test public void testTopNExhaustionClosesSource() {
+
+	Callback nullCB = new Callback() { @Override public void call() {} };
+	iterator.setCallback(nullCB);
+
+	long numItems = 3;
+	boolean distinct = false;
+	Context context = new Context() ;
+	ExecutionContext ec = new ExecutionContext(context, (Graph) null, (DatasetGraph) null, (OpExecutorFactory)
null);
+	QueryIterTopN tn = new QueryIterTopN(iterator, comparator, numItems, distinct, ec);
+	while (tn.hasNext()) tn.next();
+	assertTrue(iterator.isClosed());
+    }
+
+
     private Binding randomBinding(Var[] vars)
     {
         BindingMap binding = BindingFactory.create();
@@ -279,13 +334,14 @@ public class TestQueryIterSort {
     }
     
 
-    private class CallbackIterator implements QueryIterator
+    private static class CallbackIterator implements QueryIterator
     {
         int elementsReturned = 0 ;
         Callback callback ;
         int trigger ;
         Iterator<Binding> delegate ;
         boolean canceled = false ;
+        boolean closed = false ;
         
         public CallbackIterator(Iterator<Binding> delegate, int trigger, Callback callback)
         {
@@ -302,7 +358,10 @@ public class TestQueryIterSort {
         @Override
         public boolean hasNext() 
         {
-            return delegate.hasNext() ;
+		// self-closing
+		boolean has = delegate.hasNext() ;
+		if (has == false) closed = true;
+		return has ;
         }
 
         @Override
@@ -326,6 +385,10 @@ public class TestQueryIterSort {
             return elementsReturned ;
         }
 
+        public boolean isClosed() {
+		return closed ;
+        }
+
         public boolean isCanceled() {
             return canceled ;
         }
@@ -339,10 +402,10 @@ public class TestQueryIterSort {
 
         @Override
         public void cancel() { canceled = true ; }
-        
+
         @Override
-        public void close() { throw new ARQNotImplemented() ; }
-        
+        public void close() { closed = true ; }
+
         @Override
         public void output(IndentedWriter out, SerializationContext sCxt) { throw new ARQNotImplemented()
; }
         
@@ -354,7 +417,7 @@ public class TestQueryIterSort {
 
     }
 
-    private interface Callback
+    public interface Callback
     {
         public void call() ;
     }


Mime
View raw message