mirror of https://github.com/apache/lucene.git
LUCENE-2636: rename to MultiCollector (trunk)
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@996060 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
27387ff48f
commit
628b4ed4d2
|
@ -213,8 +213,8 @@ New features
|
||||||
to gather the hit-count per sub-clause and per document while a
|
to gather the hit-count per sub-clause and per document while a
|
||||||
search is running. (Simon Willnauer, Mike McCandless)
|
search is running. (Simon Willnauer, Mike McCandless)
|
||||||
|
|
||||||
* LUCENE-2636: Added ChainingCollector which allows chaining several Collectors
|
* LUCENE-2636: Added MultiCollector which allows running the search with several
|
||||||
together and be passed to IndexSearcher.search methods. (Shai Erera)
|
Collectors. (Shai Erera)
|
||||||
|
|
||||||
* LUCENE-2504: FieldComparator.setNextReader now returns a
|
* LUCENE-2504: FieldComparator.setNextReader now returns a
|
||||||
FieldComparator instance. You can "return this", to just reuse the
|
FieldComparator instance. You can "return this", to just reuse the
|
||||||
|
|
|
@ -24,22 +24,30 @@ import org.apache.lucene.search.Collector;
|
||||||
import org.apache.lucene.search.Scorer;
|
import org.apache.lucene.search.Scorer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link Collector} which allows chaining several {@link Collector}s in order
|
* A {@link Collector} which allows running a search with several
|
||||||
* to process the matching documents emitted to {@link #collect(int)}. This
|
* {@link Collector}s. It offers a static {@link #wrap} method which accepts a
|
||||||
* collector accepts a list of {@link Collector}s in its constructor, allowing
|
* list of collectots and wraps them with {@link MultiCollector}, while
|
||||||
* for some of them to be <code>null</code>. It optimizes away those
|
* filtering out the <code>null</code> null ones.
|
||||||
* <code>null</code> collectors, so that they are not acessed during collection
|
|
||||||
* time.
|
|
||||||
* <p>
|
|
||||||
* <b>NOTE:</b> if all the collectors passed to the constructor are null, then
|
|
||||||
* {@link IllegalArgumentException} is thrown - it is useless to run the search
|
|
||||||
* with 0 collectors.
|
|
||||||
*/
|
*/
|
||||||
public class ChainingCollector extends Collector {
|
public class MultiCollector extends Collector {
|
||||||
|
|
||||||
private final Collector[] collectors;
|
/**
|
||||||
|
* Wraps a list of {@link Collector}s with a {@link MultiCollector}. This
|
||||||
public ChainingCollector(Collector... collectors) {
|
* method works as follows:
|
||||||
|
* <ul>
|
||||||
|
* <li>Filters out the <code>null</code> collectors, so they are not used
|
||||||
|
* during search time.
|
||||||
|
* <li>If the input contains 1 real collector (i.e. non-<code>null</code> ),
|
||||||
|
* it is returned.
|
||||||
|
* <li>Otherwise the method returns a {@link MultiCollector} which wraps the
|
||||||
|
* non-<code>null</code> ones.
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @throws IllegalArgumentException
|
||||||
|
* if either 0 collectors were input, or all collectors are
|
||||||
|
* <code>null</code>.
|
||||||
|
*/
|
||||||
|
public static Collector wrap(Collector... collectors) {
|
||||||
// For the user's convenience, we allow null collectors to be passed.
|
// For the user's convenience, we allow null collectors to be passed.
|
||||||
// However, to improve performance, these null collectors are found
|
// However, to improve performance, these null collectors are found
|
||||||
// and dropped from the array we save for actual collection time.
|
// and dropped from the array we save for actual collection time.
|
||||||
|
@ -52,19 +60,35 @@ public class ChainingCollector extends Collector {
|
||||||
|
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
throw new IllegalArgumentException("At least 1 collector must not be null");
|
throw new IllegalArgumentException("At least 1 collector must not be null");
|
||||||
|
} else if (n == 1) {
|
||||||
|
// only 1 Collector - return it.
|
||||||
|
Collector col = null;
|
||||||
|
for (Collector c : collectors) {
|
||||||
|
if (c != null) {
|
||||||
|
col = c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return col;
|
||||||
} else if (n == collectors.length) {
|
} else if (n == collectors.length) {
|
||||||
// No null collectors, can use the given list as is.
|
return new MultiCollector(collectors);
|
||||||
this.collectors = collectors;
|
|
||||||
} else {
|
} else {
|
||||||
this.collectors = new Collector[n];
|
Collector[] colls = new Collector[n];
|
||||||
n = 0;
|
n = 0;
|
||||||
for (Collector c : collectors) {
|
for (Collector c : collectors) {
|
||||||
if (c != null) {
|
if (c != null) {
|
||||||
this.collectors[n++] = c;
|
colls[n++] = c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return new MultiCollector(colls);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final Collector[] collectors;
|
||||||
|
|
||||||
|
private MultiCollector(Collector... collectors) {
|
||||||
|
this.collectors = collectors;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean acceptsDocsOutOfOrder() {
|
public boolean acceptsDocsOutOfOrder() {
|
|
@ -27,7 +27,7 @@ import org.apache.lucene.search.Scorer;
|
||||||
import org.apache.lucene.util.LuceneTestCaseJ4;
|
import org.apache.lucene.util.LuceneTestCaseJ4;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class ChainingCollectorTest extends LuceneTestCaseJ4 {
|
public class MultiCollectorTest extends LuceneTestCaseJ4 {
|
||||||
|
|
||||||
private static class DummyCollector extends Collector {
|
private static class DummyCollector extends Collector {
|
||||||
|
|
||||||
|
@ -63,21 +63,30 @@ public class ChainingCollectorTest extends LuceneTestCaseJ4 {
|
||||||
public void testNullCollectors() throws Exception {
|
public void testNullCollectors() throws Exception {
|
||||||
// Tests that the collector rejects all null collectors.
|
// Tests that the collector rejects all null collectors.
|
||||||
try {
|
try {
|
||||||
new ChainingCollector(null, null);
|
MultiCollector.wrap(null, null);
|
||||||
fail("all collectors null should not be supported");
|
fail("only null collectors should not be supported");
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that the collector handles some null collectors well. If it
|
// Tests that the collector handles some null collectors well. If it
|
||||||
// doesn't, an NPE would be thrown.
|
// doesn't, an NPE would be thrown.
|
||||||
Collector c = new ChainingCollector(new DummyCollector(), null, new DummyCollector());
|
Collector c = MultiCollector.wrap(new DummyCollector(), null, new DummyCollector());
|
||||||
|
assertTrue(c instanceof MultiCollector);
|
||||||
assertTrue(c.acceptsDocsOutOfOrder());
|
assertTrue(c.acceptsDocsOutOfOrder());
|
||||||
c.collect(1);
|
c.collect(1);
|
||||||
c.setNextReader(null, 0);
|
c.setNextReader(null, 0);
|
||||||
c.setScorer(null);
|
c.setScorer(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSingleCollector() throws Exception {
|
||||||
|
// Tests that if a single Collector is input, it is returned (and not MultiCollector).
|
||||||
|
DummyCollector dc = new DummyCollector();
|
||||||
|
assertSame(dc, MultiCollector.wrap(dc));
|
||||||
|
assertSame(dc, MultiCollector.wrap(dc, null));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCollector() throws Exception {
|
public void testCollector() throws Exception {
|
||||||
// Tests that the collector delegates calls to input collectors properly.
|
// Tests that the collector delegates calls to input collectors properly.
|
||||||
|
@ -85,7 +94,7 @@ public class ChainingCollectorTest extends LuceneTestCaseJ4 {
|
||||||
// Tests that the collector handles some null collectors well. If it
|
// Tests that the collector handles some null collectors well. If it
|
||||||
// doesn't, an NPE would be thrown.
|
// doesn't, an NPE would be thrown.
|
||||||
DummyCollector[] dcs = new DummyCollector[] { new DummyCollector(), new DummyCollector() };
|
DummyCollector[] dcs = new DummyCollector[] { new DummyCollector(), new DummyCollector() };
|
||||||
Collector c = new ChainingCollector(dcs);
|
Collector c = MultiCollector.wrap(dcs);
|
||||||
assertTrue(c.acceptsDocsOutOfOrder());
|
assertTrue(c.acceptsDocsOutOfOrder());
|
||||||
c.collect(1);
|
c.collect(1);
|
||||||
c.setNextReader(null, 0);
|
c.setNextReader(null, 0);
|
Loading…
Reference in New Issue