You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2015/09/04 14:17:03 UTC

svn commit: r1701233 - in /lucene/dev/branches/branch_5x: ./ lucene/ lucene/CHANGES.txt lucene/core/ lucene/core/src/java/org/apache/lucene/search/MultiCollector.java lucene/core/src/test/org/apache/lucene/search/TestMultiCollector.java

Author: jpountz
Date: Fri Sep  4 12:17:03 2015
New Revision: 1701233

URL: http://svn.apache.org/r1701233
Log:
LUCENE-6772: MultiCollector now handles CollectionTerminatedException.

Added:
    lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/search/TestMultiCollector.java
      - copied unchanged from r1701231, lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestMultiCollector.java
Modified:
    lucene/dev/branches/branch_5x/   (props changed)
    lucene/dev/branches/branch_5x/lucene/   (props changed)
    lucene/dev/branches/branch_5x/lucene/CHANGES.txt   (contents, props changed)
    lucene/dev/branches/branch_5x/lucene/core/   (props changed)
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/MultiCollector.java

Modified: lucene/dev/branches/branch_5x/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/CHANGES.txt?rev=1701233&r1=1701232&r2=1701233&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/branch_5x/lucene/CHANGES.txt Fri Sep  4 12:17:03 2015
@@ -108,6 +108,12 @@ Changes in Backwards Compatibility Polic
   If you have indexed text using those stemmers you may need to reindex.
   (Uwe Schindler, Robert Muir)
 
+Changes in Runtime Behavior
+
+* LUCENE-6772: MultiCollector now catches CollectionTerminatedException and
+  removes the collector that threw this exception from the list of sub
+  collectors to collect. (Adrien Grand)
+
 ======================= Lucene 5.3.0 =======================
 
 New Features

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/MultiCollector.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/MultiCollector.java?rev=1701233&r1=1701232&r2=1701233&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/MultiCollector.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/MultiCollector.java Fri Sep  4 12:17:03 2015
@@ -18,7 +18,9 @@ package org.apache.lucene.search;
  */
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
 
 import org.apache.lucene.index.LeafReaderContext;
 
@@ -112,21 +114,37 @@ public class MultiCollector implements C
 
   @Override
   public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
-    final LeafCollector[] leafCollectors = new LeafCollector[collectors.length];
-    for (int i = 0; i < collectors.length; ++i) {
-      leafCollectors[i] = collectors[i].getLeafCollector(context);
+    final List<LeafCollector> leafCollectors = new ArrayList<>();
+    for (Collector collector : collectors) {
+      final LeafCollector leafCollector;
+      try {
+        leafCollector = collector.getLeafCollector(context);
+      } catch (CollectionTerminatedException e) {
+        // this leaf collector does not need this segment
+        continue;
+      }
+      leafCollectors.add(leafCollector);
+    }
+    switch (leafCollectors.size()) {
+      case 0:
+        throw new CollectionTerminatedException();
+      case 1:
+        return leafCollectors.get(0);
+      default:
+        return new MultiLeafCollector(leafCollectors, cacheScores);
     }
-    return new MultiLeafCollector(leafCollectors, cacheScores);
   }
 
   private static class MultiLeafCollector implements LeafCollector {
 
     private final boolean cacheScores;
     private final LeafCollector[] collectors;
+    private int numCollectors;
 
-    private MultiLeafCollector(LeafCollector[] collectors, boolean cacheScores) {
-      this.collectors = collectors;
+    private MultiLeafCollector(List<LeafCollector> collectors, boolean cacheScores) {
+      this.collectors = collectors.toArray(new LeafCollector[collectors.size()]);
       this.cacheScores = cacheScores;
+      this.numCollectors = this.collectors.length;
     }
 
     @Override
@@ -139,10 +157,28 @@ public class MultiCollector implements C
       }
     }
 
+    private void removeCollector(int i) {
+      System.arraycopy(collectors, i + 1, collectors, i, numCollectors - i - 1);
+      --numCollectors;
+      collectors[numCollectors] = null;
+    }
+
     @Override
     public void collect(int doc) throws IOException {
-      for (LeafCollector c : collectors) {
-        c.collect(doc);
+      final LeafCollector[] collectors = this.collectors;
+      int numCollectors = this.numCollectors;
+      for (int i = 0; i < numCollectors; ) {
+        final LeafCollector collector = collectors[i];
+        try {
+          collector.collect(doc);
+          ++i;
+        } catch (CollectionTerminatedException e) {
+          removeCollector(i);
+          numCollectors = this.numCollectors;
+          if (numCollectors == 0) {
+            throw new CollectionTerminatedException();
+          }
+        }
       }
     }