Apache Solr open-source search software
LUCENE-4862: Added CollectionTerminatedException to allow permature termination of the collection of a single IndexReader leaf.
Adrien Grand986444de446b
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex cb27b4569f6..9ff25dfb767 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -116,6 +116,10 @@ New Features* LUCENE-4859: IndexReader now exposes Terms statistics: getDocCount,getSumDocFreq, getSumTotalTermFreq. (Shai Erera)+* LUCENE-4862: It is now possible to terminate collection of a single+ IndexReader leaf by throwing a CollectionTerminatedException in+ Collector.collect. (Adrien Grand, Shai Erera)+API Changes* LUCENE-4844: removed TaxonomyReader.getParent(), you should usediff --git a/lucene/core/src/java/org/apache/lucene/search/CollectionTerminatedException.java b/lucene/core/src/java/org/apache/lucene/search/CollectionTerminatedException.javanew file mode 100644index 00000000000..9caadfa2976--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/search/CollectionTerminatedException.java@@ -0,0 +1,34 @@+package org.apache.lucene.search;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++/** Throw this exception in {@link Collector#collect(int)} to prematurely+ * terminate collection of the current leaf.+ * <p>Note: IndexSearcher swallows this exception and never re-throws it.+ * As a consequence, you should not catch it when calling+ * {@link IndexSearcher#search} as it is unnecessary and might hide misuse+ * of this exception. */+@SuppressWarnings("serial")+public final class CollectionTerminatedException extends RuntimeException {++ /** Sole constructor. */+ public CollectionTerminatedException() {+ super();+ }++}diff --git a/lucene/core/src/java/org/apache/lucene/search/Collector.java b/lucene/core/src/java/org/apache/lucene/search/Collector.javaindex 1d4121c1c31..312f5074b58 100644--- a/lucene/core/src/java/org/apache/lucene/search/Collector.java+++ b/lucene/core/src/java/org/apache/lucene/search/Collector.java@@ -134,7 +134,10 @@ public abstract class Collector {/*** Called once for every document matching a query, with the unbased document* number.- *+ * <p>Note: The collection of the current segment can be terminated by throwing+ * a {@link CollectionTerminatedException}. In this case, the last docs of the+ * current {@link AtomicReaderContext} will be skipped and {@link IndexSearcher}+ * will swallow the exception and continue collection with the next leaf.* <p>* Note: This is called in an inner search loop. For good search performance,* implementations of this method should not call {@link IndexSearcher#doc(int)} ordiff --git a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.javaindex 0ebe25af937..d56a0ef71ad 100644--- a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java+++ b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java@@ -32,7 +32,6 @@ import java.util.concurrent.ExecutorService;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;-import org.apache.lucene.document.Document;import org.apache.lucene.index.AtomicReaderContext;import org.apache.lucene.index.DirectoryReader; // javadocsimport org.apache.lucene.index.IndexReader;@@ -595,7 +594,12 @@ public class IndexSearcher {collector.setNextReader(ctx);Scorer scorer = weight.scorer(ctx, !collector.acceptsDocsOutOfOrder(), true, ctx.reader().getLiveDocs());if (scorer != null) {- scorer.score(collector);+ try {+ scorer.score(collector);+ } catch (CollectionTerminatedException e) {+ // collection was terminated prematurely+ // continue with the following leaf+ }}}}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestEarlyTermination.java b/lucene/core/src/test/org/apache/lucene/search/TestEarlyTermination.javanew file mode 100644index 00000000000..89aa94543c9--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/search/TestEarlyTermination.java@@ -0,0 +1,93 @@+package org.apache.lucene.search;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import java.io.IOException;++import org.apache.lucene.document.Document;+import org.apache.lucene.index.AtomicReaderContext;+import org.apache.lucene.index.IndexReader;+import org.apache.lucene.index.RandomIndexWriter;+import org.apache.lucene.store.Directory;+import org.apache.lucene.util.LuceneTestCase;++public class TestEarlyTermination extends LuceneTestCase {++ Directory dir;+ RandomIndexWriter writer;++ public void setUp() throws Exception {+ super.setUp();+ dir = newDirectory();+ writer = new RandomIndexWriter(random(), dir);+ final int numDocs = atLeast(100);+ for (int i = 0; i < numDocs; i++) {+ writer.addDocument(new Document());+ if (rarely()) {+ writer.commit();+ }+ }+ }++ public void tearDown() throws Exception {+ super.tearDown();+ writer.close();+ dir.close();+ }++ public void testEarlyTermination() throws IOException {+ final int iters = atLeast(5);+ final IndexReader reader = writer.getReader();+ final IndexSearcher searcher = new IndexSearcher(reader);++ for (int i = 0; i < iters; ++i) {+ final Collector collector = new Collector() {++ final boolean outOfOrder = random().nextBoolean();+ boolean collectionTerminated = true;++ @Override+ public void setScorer(Scorer scorer) throws IOException {}++ @Override+ public void collect(int doc) throws IOException {+ assertFalse(collectionTerminated);+ if (rarely()) {+ collectionTerminated = true;+ throw new CollectionTerminatedException();+ }+ }++ @Override+ public void setNextReader(AtomicReaderContext context) throws IOException {+ collectionTerminated = false;+ }++ @Override+ public boolean acceptsDocsOutOfOrder() {+ return outOfOrder;+ }++ };++ searcher.search(new MatchAllDocsQuery(), collector);+ }+ reader.close();+ }++}