[ https://issues.apache.org/jira/browse/JENA-1212?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15382465#comment-15382465 ]
ASF GitHub Bot commented on JENA-1212: -------------------------------------- Github user ajs6f commented on a diff in the pull request: https://github.com/apache/jena/pull/157#discussion_r71170120 --- Diff: jena-arq/src/main/java/org/apache/jena/atlas/data/SortedDataBag.java --- @@ -69,19 +69,81 @@ protected final ThresholdPolicy<E> policy; protected final SerializationFactory<E> serializationFactory; - protected final Comparator<? super E> comparator; + protected final CanAbortComparator comparator; protected boolean finishedAdding = false; protected boolean spilled = false; protected boolean closed = false; + protected volatile boolean cancelled; public SortedDataBag(ThresholdPolicy<E> policy, SerializationFactory<E> serializerFactory, Comparator<? super E> comparator) { this.policy = policy; this.serializationFactory = serializerFactory; - this.comparator = comparator; + this.comparator = new CanAbortComparator(comparator); } + private final class CanAbortComparator implements Comparator<E> + { + /** + The test for whether the sort has been cancelled is + performed every <code>cancelTestFrequency</code> comparisons. + This reduces the (presumed) overhead of access to a + volatile boolean. + */ + static final int cancelTestFrequency = 10000; + + /** + Count of the number of times this comparator has been called. + */ + int count = 0; + + final Comparator<? super E> baseComparator; + + public CanAbortComparator(Comparator<? super E> comparator) + { + this.baseComparator = comparator; + } + + @Override public int compare(E o1, E o2) + { + count += 1; + if (count % cancelTestFrequency == 0) + { + if (cancelled) throw new AbandonSort(); + } + return baseComparator.compare(o1, o2); + } + + /** + Sort the array <code>e</code> using this comparator + with the additional ability to abort the sort. + */ + public boolean abortableSort(E[] e) { + try { Arrays.sort(e, this); } --- End diff -- If the concern of this PR is being able to deal with really big piles of data, maybe it's worth adding a switch to use `Arrays.parallelSort(e, this)` here? > allow ORDER BY sort to be cancelled > ----------------------------------- > > Key: JENA-1212 > URL: https://issues.apache.org/jira/browse/JENA-1212 > Project: Apache Jena > Issue Type: Improvement > Components: ARQ > Reporter: christopher james dollin > Priority: Minor > Fix For: Jena 3.1.1 > > > When a query with an ORDER BY is cancelled, the component > Arrays.sort() that sorts the chunk(s) of the result > bindings runs to completion before the cancel finishes. > [See QueryIterSort and SortedDataBag.] > For a large result set, this results in a long wait > before the cancelled request finally finishes. This > can be inconvenient. -- This message was sent by Atlassian JIRA (v6.3.4#6332)