LUCENE-1978: Remove the rest of deprecated HitCollectors

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@824781 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2009-10-13 14:28:01 +00:00
parent e8cf50a651
commit c5af8f8fee
18 changed files with 64 additions and 534 deletions

View File

@ -56,9 +56,9 @@ API Changes
* LUCENE-1975: Remove deprecated SpanQuery.getTerms() and generify
Query.extractTerms(Set<Term>) (Michael Busch)
* LUCENE-1972: Remove deprecated ExtendedFieldCache, custom and auto
caches, SortField.AUTO, deprecated custom sort, deprecated sorting
HitCollectors, deprecated TopDocs HitCollectors, legacy search.
* LUCENE-1972, LUCENE-1978: Remove deprecated ExtendedFieldCache,
custom and auto caches, SortField.AUTO, deprecated custom sort,
deprecated HitCollector, legacy search setting in SortField.
Make new Sort(SortField...) and Sort.setSort(SortField...) varargs-
enabled. (Uwe Schindler)

View File

@ -42,7 +42,7 @@
<property name="Name" value="Lucene"/>
<property name="dev.version" value="3.0-dev"/>
<property name="version" value="${dev.version}"/>
<property name="compatibility.tag" value="lucene_2_9_back_compat_tests_20091013b"/>
<property name="compatibility.tag" value="lucene_2_9_back_compat_tests_20091013c"/>
<property name="spec.version" value="${version}"/>
<property name="year" value="2000-${current.year}"/>
<property name="final.name" value="lucene-${name}-${version}"/>

View File

@ -43,10 +43,11 @@ import org.apache.lucene.index.TermPositionVector;
import org.apache.lucene.index.TermPositions;
import org.apache.lucene.index.TermVectorMapper;
import org.apache.lucene.index.FieldInvertState;
import org.apache.lucene.search.HitCollector;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Similarity;
/**
@ -414,10 +415,22 @@ public class MemoryIndex implements Serializable {
Searcher searcher = createSearcher();
try {
final float[] scores = new float[1]; // inits to 0.0f (no match)
searcher.search(query, new HitCollector() {
public void collect(int doc, float score) {
scores[0] = score;
searcher.search(query, new Collector() {
private Scorer scorer;
public void collect(int doc) throws IOException {
scores[0] = scorer.score();
}
public void setScorer(Scorer scorer) throws IOException {
this.scorer = scorer;
}
public boolean acceptsDocsOutOfOrder() {
return true;
}
public void setNextReader(IndexReader reader, int docBase) { }
});
float score = scores[0];
return score;

View File

@ -42,11 +42,13 @@ import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.HitCollector;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
@ -424,11 +426,23 @@ public class MemoryIndexTest extends BaseTokenStreamTestCase {
else
searcher = ((MemoryIndex) index).createSearcher();
final float[] scores = new float[1]; // inits to 0.0f
searcher.search(query, new HitCollector() {
public void collect(int doc, float score) {
scores[0] = score;
final float[] scores = new float[1]; // inits to 0.0f (no match)
searcher.search(query, new Collector() {
private Scorer scorer;
public void collect(int doc) throws IOException {
scores[0] = scorer.score();
}
public void setScorer(Scorer scorer) throws IOException {
this.scorer = scorer;
}
public boolean acceptsDocsOutOfOrder() {
return true;
}
public void setNextReader(IndexReader reader, int docBase) { }
});
float score = scores[0];
// Hits hits = searcher.search(query);

View File

@ -44,13 +44,6 @@ public class RemoteSearchable
super();
this.local = local;
}
/** @deprecated use {@link #search(Weight, Filter, Collector)} instead. */
public void search(Weight weight, Filter filter, HitCollector results)
throws IOException {
local.search(weight, filter, results);
}
public void search(Weight weight, Filter filter, Collector results)
throws IOException {
local.search(weight, filter, results);

View File

@ -17,10 +17,14 @@ package org.apache.lucene.queryParser.surround.query;
* limitations under the License.
*/
import java.io.IOException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.HitCollector;
import org.apache.lucene.queryParser.surround.parser.QueryParser;
@ -52,16 +56,32 @@ public class BooleanQueryTst {
public void setVerbose(boolean verbose) {this.verbose = verbose;}
class TestCollector extends HitCollector { // FIXME: use check hits from Lucene tests
class TestCollector extends Collector { // FIXME: use check hits from Lucene tests
int totalMatched;
boolean[] encountered;
private Scorer scorer = null;
private int docBase = 0;
TestCollector() {
totalMatched = 0;
encountered = new boolean[expectedDocNrs.length];
}
public void collect(int docNr, float score) {
public void setScorer(Scorer scorer) throws IOException {
this.scorer = scorer;
}
public boolean acceptsDocsOutOfOrder() {
return true;
}
public void setNextReader(IndexReader reader, int docBase) throws IOException {
this.docBase = docBase;
}
public void collect(int docNr) throws IOException {
float score = scorer.score();
docNr += docBase;
/* System.out.println(docNr + " '" + dBase.getDocs()[docNr] + "': " + score); */
TestCase.assertTrue(queryText + ": positive score", score > 0.0);
TestCase.assertTrue(queryText + ": too many hits", totalMatched < expectedDocNrs.length);

View File

@ -270,11 +270,6 @@ final class BooleanScorer extends Scorer {
return false;
}
/** @deprecated use {@link #score(Collector, int, int)} instead. */
protected boolean score(HitCollector hc, int max) throws IOException {
return score(new HitCollectorWrapper(hc), max, docID());
}
public int advance(int target) throws IOException {
throw new UnsupportedOperationException();
@ -328,11 +323,6 @@ final class BooleanScorer extends Scorer {
public void score(Collector collector) throws IOException {
score(collector, Integer.MAX_VALUE, nextDoc());
}
/** @deprecated use {@link #score(Collector)} instead. */
public void score(HitCollector hc) throws IOException {
score(new HitCollectorWrapper(hc));
}
public String toString() {
StringBuilder buffer = new StringBuilder();

View File

@ -260,16 +260,6 @@ class BooleanScorer2 extends Scorer {
: new DisjunctionSumScorer(prohibitedScorers)));
}
/** Scores and collects all matching documents.
* @param hc The collector to which all matching documents are passed through
* {@link HitCollector#collect(int, float)}.
* <br>When this method is used the {@link #explain(int)} method should not be used.
* @deprecated use {@link #score(Collector)} instead.
*/
public void score(HitCollector hc) throws IOException {
score(new HitCollectorWrapper(hc));
}
/** Scores and collects all matching documents.
* @param collector The collector to which all matching documents are passed through.
* <br>When this method is used the {@link #explain(int)} method should not be used.
@ -280,19 +270,6 @@ class BooleanScorer2 extends Scorer {
collector.collect(doc);
}
}
/** Expert: Collects matching documents in a range.
* <br>Note that {@link #next()} must be called once before this method is
* called for the first time.
* @param hc The collector to which all matching documents are passed through
* {@link HitCollector#collect(int, float)}.
* @param max Do not score documents past this.
* @return true if more matching documents may remain.
* @deprecated use {@link #score(Collector, int, int)} instead.
*/
protected boolean score(HitCollector hc, int max) throws IOException {
return score(new HitCollectorWrapper(hc), max, docID());
}
protected boolean score(Collector collector, int max, int firstDocID) throws IOException {
doc = firstDocID;

View File

@ -26,13 +26,6 @@ import org.apache.lucene.index.IndexReader;
* gather raw results from a search, and implement sorting
* or custom result filtering, collation, etc. </p>
*
* <p>As of 2.9, this class replaces the deprecated
* HitCollector, and offers an API for efficient collection
* of hits across sequential {@link IndexReader}s. {@link
* IndexSearcher} advances the collector through each of the
* sub readers, in an arbitrary order. This results in a
* higher performance means of collection.</p>
*
* <p>Lucene's core collectors are derived from Collector.
* Likely your application can use one of these classes, or
* subclass {@link TopDocsCollector}, instead of
@ -60,8 +53,7 @@ import org.apache.lucene.index.IndexReader;
*
* <li>{@link TimeLimitingCollector}, which wraps any other
* Collector and aborts the search if it's taken too much
* time, will subclass Collector in 3.0 (presently it
* subclasses the deprecated HitCollector).</li>
* time.</li>
*
* <li>{@link PositiveScoresOnlyCollector} wraps any other
* Collector and prevents collection of hits whose score

View File

@ -25,7 +25,6 @@ import org.apache.lucene.util.ScorerDocQueue;
/** A Scorer for OR like queries, counterpart of <code>ConjunctionScorer</code>.
* This Scorer implements {@link Scorer#skipTo(int)} and uses skipTo() on the given Scorers.
* TODO: Implement score(HitCollector, int).
*/
class DisjunctionSumScorer extends Scorer {
/** The number of subscorers. */
@ -108,16 +107,6 @@ class DisjunctionSumScorer extends Scorer {
}
}
/** Scores and collects all matching documents.
* @param hc The collector to which all matching documents are passed through
* {@link HitCollector#collect(int, float)}.
* <br>When this method is used the {@link #explain(int)} method should not be used.
* @deprecated use {@link #score(Collector)} instead.
*/
public void score(HitCollector hc) throws IOException {
score(new HitCollectorWrapper(hc));
}
/** Scores and collects all matching documents.
* @param collector The collector to which all matching documents are passed through.
* <br>When this method is used the {@link #explain(int)} method should not be used.
@ -129,19 +118,6 @@ class DisjunctionSumScorer extends Scorer {
}
}
/** Expert: Collects matching documents in a range. Hook for optimization.
* Note that {@link #next()} must be called once before this method is called
* for the first time.
* @param hc The collector to which all matching documents are passed through
* {@link HitCollector#collect(int, float)}.
* @param max Do not score documents past this.
* @return true if more matching documents may remain.
* @deprecated use {@link #score(Collector, int, int)} instead.
*/
protected boolean score(HitCollector hc, int max) throws IOException {
return score(new HitCollectorWrapper(hc), max, docID());
}
/** Expert: Collects matching documents in a range. Hook for optimization.
* Note that {@link #next()} must be called once before this method is called
* for the first time.

View File

@ -1,55 +0,0 @@
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.
*/
/**
* Lower-level search API. <br>
* HitCollectors are primarily meant to be used to implement queries, sorting
* and filtering. See {@link Collector} for a lower level and higher performance
* (on a multi-segment index) API.
*
* @see Searcher#search(Query,HitCollector)
* @deprecated Please use {@link Collector} instead.
*/
public abstract class HitCollector {
/** Called once for every document matching a query, with the document
* number and its raw score.
*
* <P>If, for example, an application wished to collect all of the hits for a
* query in a BitSet, then it might:<pre>
* Searcher searcher = new IndexSearcher(indexReader);
* final BitSet bits = new BitSet(indexReader.maxDoc());
* searcher.search(query, new HitCollector() {
* public void collect(int doc, float score) {
* bits.set(doc);
* }
* });
* </pre>
*
* <p>Note: This is called in an inner search loop. For good search
* performance, implementations of this method should not call
* {@link Searcher#doc(int)} or
* {@link org.apache.lucene.index.IndexReader#document(int)} on every
* document number encountered. Doing so can slow searches by an order
* of magnitude or more.
* <p>Note: The <code>score</code> passed to this method is a raw score.
* In other words, the score will not necessarily be a float whose value is
* between 0 and 1.
*/
public abstract void collect(int doc, float score);
}

View File

@ -1,57 +0,0 @@
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.index.IndexReader;
/**
* Wrapper for ({@link HitCollector}) implementations, which simply re-bases the
* incoming docID before calling {@link HitCollector#collect}.
*
* @deprecated Please migrate custom HitCollectors to the new {@link Collector}
* class. This class will be removed when {@link HitCollector} is
* removed.
*/
public class HitCollectorWrapper extends Collector {
private HitCollector collector;
private int base = 0;
private Scorer scorer = null;
public HitCollectorWrapper(HitCollector collector) {
this.collector = collector;
}
public void setNextReader(IndexReader reader, int docBase) {
base = docBase;
}
public void collect(int doc) throws IOException {
collector.collect(doc + base, scorer.score());
}
public void setScorer(Scorer scorer) throws IOException {
this.scorer = scorer;
}
public boolean acceptsDocsOutOfOrder() {
return false;
}
}

View File

@ -54,16 +54,6 @@ public abstract class Scorer extends DocIdSetIterator {
return this.similarity;
}
/** Scores and collects all matching documents.
* @param hc The collector to which all matching documents are passed through
* {@link HitCollector#collect(int, float)}.
* <br>When this method is used the {@link #explain(int)} method should not be used.
* @deprecated use {@link #score(Collector)} instead.
*/
public void score(HitCollector hc) throws IOException {
score(new HitCollectorWrapper(hc));
}
/** Scores and collects all matching documents.
* @param collector The collector to which all matching documents are passed.
* <br>When this method is used the {@link #explain(int)} method should not be used.
@ -76,19 +66,6 @@ public abstract class Scorer extends DocIdSetIterator {
}
}
/** Expert: Collects matching documents in a range. Hook for optimization.
* Note that {@link #next()} must be called once before this method is called
* for the first time.
* @param hc The collector to which all matching documents are passed through
* {@link HitCollector#collect(int, float)}.
* @param max Do not score documents past this.
* @return true if more matching documents may remain.
* @deprecated use {@link #score(Collector, int, int)} instead.
*/
protected boolean score(HitCollector hc, int max) throws IOException {
return score(new HitCollectorWrapper(hc), max, docID());
}
/**
* Expert: Collects matching documents in a range. Hook for optimization.
* Note, <code>firstDocID</code> is added to ensure that {@link #nextDoc()}

View File

@ -43,26 +43,6 @@ import org.apache.lucene.index.Term;
*/
public interface Searchable {
/** Lower-level search API.
*
* <p>{@link HitCollector#collect(int,float)} is called for every non-zero
* scoring document.
* <br>HitCollector-based access to remote indexes is discouraged.
*
* <p>Applications should only use this if they need <i>all</i> of the
* matching documents. The high-level search API ({@link
* Searcher#search(Query)}) is usually more efficient, as it skips
* non-high-scoring hits.
*
* @param weight to match documents
* @param filter if non-null, used to permit documents to be collected.
* @param results to receive hits
* @throws BooleanQuery.TooManyClauses
* @deprecated use {@link #search(Weight, Filter, Collector)} instead.
*/
void search(Weight weight, Filter filter, HitCollector results)
throws IOException;
/**
* Lower-level search API.
*
@ -115,7 +95,6 @@ public interface Searchable {
* <p>Applications should usually call {@link Searcher#search(Query)} or
* {@link Searcher#search(Query,Filter)} instead.
* @throws BooleanQuery.TooManyClauses
* @deprecated use {@link #search(Weight, Filter, int)} instead.
*/
TopDocs search(Weight weight, Filter filter, int n) throws IOException;

View File

@ -50,26 +50,6 @@ public abstract class Searcher implements Searchable {
return search(createWeight(query), filter, n, sort);
}
/** Lower-level search API.
*
* <p>{@link HitCollector#collect(int,float)} is called for every matching
* document.
*
* <p>Applications should only use this if they need <i>all</i> of the
* matching documents. The high-level search API ({@link
* Searcher#search(Query)}) is usually more efficient, as it skips
* non-high-scoring hits.
* <p>Note: The <code>score</code> passed to this method is a raw score.
* In other words, the score will not necessarily be a float whose value is
* between 0 and 1.
* @throws BooleanQuery.TooManyClauses
* @deprecated use {@link #search(Query, Collector)} instead.
*/
public void search(Query query, HitCollector results)
throws IOException {
search(createWeight(query), null, new HitCollectorWrapper(results));
}
/** Lower-level search API.
*
* <p>{@link Collector#collect(int)} is called for every matching document.
@ -88,28 +68,6 @@ public abstract class Searcher implements Searchable {
search(createWeight(query), null, results);
}
/** Lower-level search API.
*
* <p>{@link HitCollector#collect(int,float)} is called for every matching
* document.
* <br>HitCollector-based access to remote indexes is discouraged.
*
* <p>Applications should only use this if they need <i>all</i> of the
* matching documents. The high-level search API ({@link
* Searcher#search(Query, Filter, int)}) is usually more efficient, as it skips
* non-high-scoring hits.
*
* @param query to match documents
* @param filter if non-null, used to permit documents to be collected.
* @param results to receive hits
* @throws BooleanQuery.TooManyClauses
* @deprecated use {@link #search(Query, Filter, Collector)} instead.
*/
public void search(Query query, Filter filter, HitCollector results)
throws IOException {
search(createWeight(query), filter, new HitCollectorWrapper(results));
}
/** Lower-level search API.
*
* <p>{@link Collector#collect(int)} is called for every matching
@ -199,15 +157,6 @@ public abstract class Searcher implements Searchable {
return result;
}
/* The following abstract methods were added as a workaround for GCJ bug #15411.
* http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15411
*/
/**
* @deprecated use {@link #search(Weight, Filter, Collector)} instead.
*/
public void search(Weight weight, Filter filter, HitCollector results) throws IOException {
search(weight, filter, new HitCollectorWrapper(results));
}
abstract public void search(Weight weight, Filter filter, Collector results) throws IOException;
abstract public void close() throws IOException;
abstract public int docFreq(Term term) throws IOException;

View File

@ -65,20 +65,10 @@ final class TermScorer extends Scorer {
scoreCache[i] = getSimilarity().tf(i) * weightValue;
}
/** @deprecated use {@link #score(Collector)} instead. */
public void score(HitCollector hc) throws IOException {
score(new HitCollectorWrapper(hc));
}
public void score(Collector c) throws IOException {
score(c, Integer.MAX_VALUE, nextDoc());
}
/** @deprecated use {@link #score(Collector, int, int)} instead. */
protected boolean score(HitCollector c, int end) throws IOException {
return score(new HitCollectorWrapper(c), end, doc);
}
// firstDocID is ignored since nextDoc() sets 'doc'
protected boolean score(Collector c, int end, int firstDocID) throws IOException {
c.setScorer(this);
@ -166,8 +156,6 @@ final class TermScorer extends Scorer {
}
/** Returns an explanation of the score for a document.
* <br>When this method is used, the {@link #next()} method
* and the {@link #score(HitCollector)} method should not be used.
* @param doc The document number for the explanation.
*/
public Explanation explain(int doc) throws IOException {

View File

@ -1,217 +0,0 @@
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.
*/
/**
* <p>
* The TimeLimitedCollector is used to timeout search requests that take longer
* than the maximum allowed search time limit. After this time is exceeded, the
* search thread is stopped by throwing a TimeExceeded Exception.
* </p>
*
* @deprecated Use {@link TimeLimitingCollector} instead, which extends the new
* {@link Collector}. This class will be removed in 3.0.
*/
public class TimeLimitedCollector extends HitCollector {
/**
* Default timer resolution.
* @see #setResolution(long)
*/
public static final int DEFAULT_RESOLUTION = 20;
/**
* Default for {@link #isGreedy()}.
* @see #isGreedy()
*/
public boolean DEFAULT_GREEDY = false;
private static long resolution = DEFAULT_RESOLUTION;
private boolean greedy = DEFAULT_GREEDY ;
private static class TimerThread extends Thread {
// NOTE: we can avoid explicit synchronization here for several reasons:
// * updates to volatile long variables are atomic
// * only single thread modifies this value
// * use of volatile keyword ensures that it does not reside in
// a register, but in main memory (so that changes are visible to
// other threads).
// * visibility of changes does not need to be instantaneous, we can
// afford losing a tick or two.
//
// See section 17 of the Java Language Specification for details.
private volatile long time = 0;
/**
* TimerThread provides a pseudo-clock service to all searching
* threads, so that they can count elapsed time with less overhead
* than repeatedly calling System.currentTimeMillis. A single
* thread should be created to be used for all searches.
*/
private TimerThread() {
super("TimeLimitedCollector timer thread");
this.setDaemon( true );
}
public void run() {
while( true ) {
// TODO: Use System.nanoTime() when Lucene moves to Java SE 5.
time += resolution;
try {
Thread.sleep( resolution );
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException(ie);
}
}
}
/**
* Get the timer value in milliseconds.
*/
public long getMilliseconds() {
return time;
}
}
/**
* Thrown when elapsed search time exceeds allowed search time.
*/
public static class TimeExceededException extends RuntimeException {
private long timeAllowed;
private long timeElapsed;
private int lastDocCollected;
private TimeExceededException(long timeAllowed, long timeElapsed, int lastDocCollected) {
super("Elapsed time: " + timeElapsed + "Exceeded allowed search time: " + timeAllowed + " ms.");
this.timeAllowed = timeAllowed;
this.timeElapsed = timeElapsed;
this.lastDocCollected = lastDocCollected;
}
/**
* Returns allowed time (milliseconds).
*/
public long getTimeAllowed() {
return timeAllowed;
}
/**
* Returns elapsed time (milliseconds).
*/
public long getTimeElapsed() {
return timeElapsed;
}
/**
* Returns last doc that was collected when the search time exceeded.
*/
public int getLastDocCollected() {
return lastDocCollected;
}
}
// Declare and initialize a single static timer thread to be used by
// all TimeLimitedCollector instances. The JVM assures that
// this only happens once.
private final static TimerThread TIMER_THREAD = new TimerThread();
static {
TIMER_THREAD.start();
}
private final long t0;
private final long timeout;
private final HitCollector hc;
/**
* Create a TimeLimitedCollector wrapper over another HitCollector with a specified timeout.
* @param hc the wrapped HitCollector
* @param timeAllowed max time allowed for collecting hits after which {@link TimeExceededException} is thrown
*/
public TimeLimitedCollector(final HitCollector hc, final long timeAllowed) {
this.hc = hc;
t0 = TIMER_THREAD.getMilliseconds();
this.timeout = t0 + timeAllowed;
}
/**
* Calls collect() on the decorated HitCollector.
*
* @throws TimeExceededException if the time allowed has been exceeded.
*/
public void collect( final int doc, final float score ) {
long time = TIMER_THREAD.getMilliseconds();
if( timeout < time) {
if (greedy) {
//System.out.println(this+" greedy: before failing, collecting doc: "+doc+" "+(time-t0));
hc.collect( doc, score );
}
//System.out.println(this+" failing on: "+doc+" "+(time-t0));
throw new TimeExceededException( timeout-t0, time-t0, doc );
}
//System.out.println(this+" collecting: "+doc+" "+(time-t0));
hc.collect( doc, score );
}
/**
* Return the timer resolution.
* @see #setResolution(long)
*/
public static long getResolution() {
return resolution;
}
/**
* Set the timer resolution.
* The default timer resolution is 20 milliseconds.
* This means that a search required to take no longer than
* 800 milliseconds may be stopped after 780 to 820 milliseconds.
* <br>Note that:
* <ul>
* <li>Finer (smaller) resolution is more accurate but less efficient.</li>
* <li>Setting resolution to less than 5 milliseconds will be silently modified to 5 milliseconds.</li>
* <li>Setting resolution smaller than current resolution might take effect only after current
* resolution. (Assume current resolution of 20 milliseconds is modified to 5 milliseconds,
* then it can take up to 20 milliseconds for the change to have effect.</li>
* </ul>
*/
public static void setResolution(long newResolution) {
resolution = Math.max(newResolution,5); // 5 milliseconds is about the minimum reasonable time for a Object.wait(long) call.
}
/**
* Checks if this time limited collector is greedy in collecting the last hit.
* A non greedy collector, upon a timeout, would throw a {@link TimeExceededException}
* without allowing the wrapped collector to collect current doc. A greedy one would
* first allow the wrapped hit collector to collect current doc and only then
* throw a {@link TimeExceededException}.
* @see #setGreedy(boolean)
*/
public boolean isGreedy() {
return greedy;
}
/**
* Sets whether this time limited collector is greedy.
* @param greedy true to make this time limited greedy
* @see #isGreedy()
*/
public void setGreedy(boolean greedy) {
this.greedy = greedy;
}
}

View File

@ -420,19 +420,10 @@ public class CheckHits {
checkExplanations(query);
return super.search(query,filter,n,sort);
}
/** @deprecated use {@link #search(Query, Collector)} instead. */
public void search(Query query, HitCollector results) throws IOException {
search(query, new HitCollectorWrapper(results));
}
public void search(Query query, Collector results) throws IOException {
checkExplanations(query);
super.search(query, results);
}
/** @deprecated use {@link #search(Query, Filter, Collector)} instead. */
public void search(Query query, Filter filter,
HitCollector results) throws IOException {
search(query, filter, new HitCollectorWrapper(results));
}
public void search(Query query, Filter filter, Collector results) throws IOException {
checkExplanations(query);
super.search(query, filter, results);