You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2016/07/27 12:06:34 UTC

[05/11] jena git commit: Clarify return value from abortableSort.

Clarify return value from abortableSort.

Rather than a boolean (true => cancelled), return one of
Finish.ABORTED or Finish.COMPLETED as appropriate.


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

Branch: refs/heads/master
Commit: 205671cf292ed0213d6fc4791bd2903a79b9e383
Parents: d4c8962
Author: Chris Dollin <eh...@googlemail.com>
Authored: Tue Jul 19 14:57:18 2016 +0100
Committer: Chris Dollin <eh...@googlemail.com>
Committed: Tue Jul 19 14:57:18 2016 +0100

----------------------------------------------------------------------
 .../jena/atlas/data/AbortableComparator.java    |  20 ++--
 .../apache/jena/atlas/data/SortedDataBag.java   |  10 +-
 .../sparql/engine/iterator/QueryIterSort.java   | 116 +++++++++----------
 3 files changed, 70 insertions(+), 76 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/205671cf/jena-arq/src/main/java/org/apache/jena/atlas/data/AbortableComparator.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/atlas/data/AbortableComparator.java b/jena-arq/src/main/java/org/apache/jena/atlas/data/AbortableComparator.java
index 7be58ec..408bd0e 100644
--- a/jena-arq/src/main/java/org/apache/jena/atlas/data/AbortableComparator.java
+++ b/jena-arq/src/main/java/org/apache/jena/atlas/data/AbortableComparator.java
@@ -27,14 +27,16 @@ public final class AbortableComparator<E> implements Comparator<E>
 		this.baseComparator = comparator;
 	}
 	
-    /**
-        <code>AbandonSort</code> is the exception thrown from
-        <code>AbortableComparator</code> to abandon a sort.
-    */
+	/**
+        	<code>AbandonSort</code> is the exception thrown from
+        	<code>AbortableComparator</code> to abandon a sort.
+	 */
 	public static class AbandonSort extends RuntimeException 
-    {
+	{
 		private static final long serialVersionUID = 1L;
-    }
+	}
+	
+	public static enum Finish {COMPLETED, ABORTED}
 	
 	/**
 	    The test for whether the sort has been cancelled is
@@ -67,7 +69,7 @@ public final class AbortableComparator<E> implements Comparator<E>
 	    Sort the array <code>e</code> using this comparator
 	 	with the additional ability to abort the sort.
 	*/
-	public boolean abortableSort(E[] e) 
+	public Finish abortableSort(E[] e) 
 	{
 		try 
 		{ 
@@ -75,9 +77,9 @@ public final class AbortableComparator<E> implements Comparator<E>
 		}
 		catch (AbandonSort s) 
 		{ 
-			return true;
+			return Finish.ABORTED;
 		}
-		return false;
+		return Finish.COMPLETED;
 	}
 	
 	/**

http://git-wip-us.apache.org/repos/asf/jena/blob/205671cf/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 9ae78e2..b248006 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
@@ -32,6 +32,7 @@ import java.util.NoSuchElementException ;
 import java.util.PriorityQueue ;
 
 import org.apache.jena.atlas.AtlasException ;
+import org.apache.jena.atlas.data.AbortableComparator.Finish;
 import org.apache.jena.atlas.iterator.Iter ;
 import org.apache.jena.atlas.iterator.IteratorResourceClosing ;
 import org.apache.jena.atlas.lib.Closeable ;
@@ -149,7 +150,7 @@ public class SortedDataBag<E> extends AbstractDataBag<E>
             // never get around to using it anyway.
             
             E[] array = (E[]) memory.toArray();
-            if (!comparator.abortableSort(array)) 
+            if (comparator.abortableSort(array) == Finish.COMPLETED) 
             {
 	            Sink<E> serializer = serializationFactory.createSerializer(out);
 	            try
@@ -209,13 +210,8 @@ public class SortedDataBag<E> extends AbstractDataBag<E>
         // Constructing an iterator from this class is not thread-safe (just like all the the other methods)
         if (!finishedAdding && memSize > 1)
         {
-            // Again, some ugliness for speed
             E[] array = (E[]) memory.toArray();
-            if (comparator.abortableSort(array)) 
-            {
-            	// if we comment this back in, we lose the timeout message!
-            	// return Iter.nullIterator();
-            }
+            comparator.abortableSort(array); // don't care if we aborted or not
             memory = Arrays.asList(array);
         }
         

http://git-wip-us.apache.org/repos/asf/jena/blob/205671cf/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 7824e3f..edaaa5c 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
@@ -36,73 +36,69 @@ import org.apache.jena.sparql.engine.QueryIterator ;
 import org.apache.jena.sparql.engine.binding.Binding ;
 import org.apache.jena.sparql.engine.binding.BindingComparator ;
 
-/** 
- * Sort a query iterator.  The sort will happen in-memory unless the size of the
+/**
+ * Sort a query iterator. The sort will happen in-memory unless the size of the
  * iterator exceeds a configurable threshold. In that case, a disk sort is used.
  * 
  * @see SortedDataBag
  */
 
-public class QueryIterSort extends QueryIterPlainWrapper
-{
-	private final QueryIterator embeddedIterator;      // Keep a record of the underlying source for .cancel.
-	final SortedDataBag<Binding> db;
-	
-    public QueryIterSort(QueryIterator qIter, List<SortCondition> conditions, ExecutionContext context)
-    {
-        this(qIter, new BindingComparator(conditions, context), context) ;
-    }
+public class QueryIterSort extends QueryIterPlainWrapper {
+	private final QueryIterator embeddedIterator ; // Keep a record of the
+							// underlying source for
+							// .cancel.
+	final SortedDataBag<Binding> db ;
 
-    public QueryIterSort(final QueryIterator qIter, final Comparator<Binding> comparator, 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));
-    }
+	public QueryIterSort(QueryIterator qIter, List<SortCondition> conditions, ExecutionContext context) {
+		this(qIter, new BindingComparator(conditions, context), context) ;
+	}
 
-    @Override
-    public void requestCancel()
-    {
-    	this.db.cancel();
-        this.embeddedIterator.cancel() ;
-        super.requestCancel() ;
-    }
+	public QueryIterSort(final QueryIterator qIter, final Comparator<Binding> comparator,
+			final ExecutionContext context) {
+		super(null, context) ;
+		this.embeddedIterator = qIter ;
 
-    private class SortedBindingIterator extends IteratorDelayedInitialization<Binding> implements Closeable
-    {
-        private final QueryIterator qIter;
-        
-        public SortedBindingIterator(final QueryIterator qIter)
-        {
-            this.qIter = qIter;
-        }
-        
-        @Override
-        protected Iterator<Binding> initializeIterator()
-        {
-            try
-            {
-                db.addAll(qIter);
-                return db.iterator();
-            }
-            // Should we catch other exceptions too?  Theoretically the user should be using this
-            // iterator in a try/finally block, and thus will call close() themselves. 
-            catch (QueryCancelledException e)
-            {
-                close();
-                throw e;
-            }
-        }
+		ThresholdPolicy<Binding> policy = ThresholdPolicyFactory.policyFromContext(context.getContext()) ;
+		this.db = BagFactory.newSortedBag(policy, SerializationFactoryFinder.bindingSerializationFactory(),
+				comparator) ;
+
+		this.setIterator(new SortedBindingIterator(qIter)) ;
+	}
+
+	@Override
+	public void requestCancel() {
+		this.db.cancel() ;
+		this.embeddedIterator.cancel() ;
+		super.requestCancel() ;
+	}
+
+	private class SortedBindingIterator extends IteratorDelayedInitialization<Binding> implements Closeable {
+		private final QueryIterator qIter ;
+
+		public SortedBindingIterator(final QueryIterator qIter) {
+			this.qIter = qIter ;
+		}
+
+		@Override
+		protected Iterator<Binding> initializeIterator() {
+			try {
+				db.addAll(qIter) ;
+				return db.iterator() ;
+			}
+			// Should we catch other exceptions too? Theoretically
+			// the user should be using this
+			// iterator in a try/finally block, and thus will call
+			// close() themselves.
+			catch (QueryCancelledException e) {
+				close() ;
+				throw e ;
+			}
+		}
+
+		@Override
+		public void close() {
+			db.close() ;
+		}
+	}
 
-        @Override
-        public void close()
-        {
-            db.close();
-        }
-    }
-    
 }