mirror of https://github.com/apache/lucene.git
Improvement in NumericRangeTermEnum to reuse the term and not intern the field name everytime. It also throws UnsupportedOperationException in endEnum() method, which is not used, and should not be used by client code/subclasses (undefined behaviour).
Also adds a test for the TermEnum. git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@882888 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
52d8057021
commit
254e3e9293
|
@ -389,6 +389,7 @@ public final class NumericRangeQuery<T extends Number> extends MultiTermQuery {
|
|||
|
||||
private final IndexReader reader;
|
||||
private final LinkedList<String> rangeBounds = new LinkedList<String>();
|
||||
private final Term termTemplate = new Term(field);
|
||||
private String currentUpperBound = null;
|
||||
|
||||
NumericRangeTermEnum(final IndexReader reader) throws IOException {
|
||||
|
@ -482,8 +483,7 @@ public final class NumericRangeQuery<T extends Number> extends MultiTermQuery {
|
|||
/** this is a dummy, it is not used by this class. */
|
||||
@Override
|
||||
protected boolean endEnum() {
|
||||
assert false; // should never be called
|
||||
return (currentTerm == null);
|
||||
throw new UnsupportedOperationException("not implemented");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -504,7 +504,7 @@ public final class NumericRangeQuery<T extends Number> extends MultiTermQuery {
|
|||
// if a current term exists, the actual enum is initialized:
|
||||
// try change to next term, if no such term exists, fall-through
|
||||
if (currentTerm != null) {
|
||||
assert actualEnum!=null;
|
||||
assert actualEnum != null;
|
||||
if (actualEnum.next()) {
|
||||
currentTerm = actualEnum.term();
|
||||
if (termCompare(currentTerm)) return true;
|
||||
|
@ -513,7 +513,10 @@ public final class NumericRangeQuery<T extends Number> extends MultiTermQuery {
|
|||
// if all above fails, we go forward to the next enum,
|
||||
// if one is available
|
||||
currentTerm = null;
|
||||
if (rangeBounds.size() < 2) return false;
|
||||
if (rangeBounds.size() < 2) {
|
||||
assert rangeBounds.size() == 0;
|
||||
return false;
|
||||
}
|
||||
// close the current enum and read next bounds
|
||||
if (actualEnum != null) {
|
||||
actualEnum.close();
|
||||
|
@ -525,7 +528,7 @@ public final class NumericRangeQuery<T extends Number> extends MultiTermQuery {
|
|||
// next enum found.
|
||||
// if this behavior is changed/modified in the superclass,
|
||||
// this enum will not work anymore!
|
||||
setEnum(reader.terms(new Term(field, lowerBound)));
|
||||
setEnum(reader.terms(termTemplate.createTerm(lowerBound)));
|
||||
return (currentTerm != null);
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@ import org.apache.lucene.document.Field;
|
|||
import org.apache.lucene.document.NumericField;
|
||||
import org.apache.lucene.index.IndexWriter;
|
||||
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.store.RAMDirectory;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
import org.apache.lucene.util.NumericUtils;
|
||||
|
@ -437,4 +438,37 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
|
|||
assertFalse(q2.equals(q1));
|
||||
}
|
||||
|
||||
private void testEnum(int lower, int upper) throws Exception {
|
||||
NumericRangeQuery<Integer> q = NumericRangeQuery.newIntRange("field4", 4, lower, upper, true, true);
|
||||
FilteredTermEnum termEnum = q.getEnum(searcher.getIndexReader());
|
||||
try {
|
||||
int count = 0;
|
||||
do {
|
||||
final Term t = termEnum.term();
|
||||
if (t != null) {
|
||||
final int val = NumericUtils.prefixCodedToInt(t.text());
|
||||
assertTrue("value not in bounds", val >= lower && val <= upper);
|
||||
count++;
|
||||
} else break;
|
||||
} while (termEnum.next());
|
||||
assertFalse(termEnum.next());
|
||||
System.out.println("TermEnum on 'field4' for range [" + lower + "," + upper + "] contained " + count + " terms.");
|
||||
} finally {
|
||||
termEnum.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void testEnum() throws Exception {
|
||||
int count=3000;
|
||||
int lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3);
|
||||
// test enum with values
|
||||
testEnum(lower, upper);
|
||||
// test empty enum
|
||||
testEnum(upper, lower);
|
||||
// test empty enum outside of bounds
|
||||
lower = distance*noDocs+startOffset;
|
||||
upper = 2 * lower;
|
||||
testEnum(lower, upper);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue