lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From uschind...@apache.org
Subject svn commit: r882888 - in /lucene/java/trunk/src: java/org/apache/lucene/search/NumericRangeQuery.java test/org/apache/lucene/search/TestNumericRangeQuery32.java
Date Sat, 21 Nov 2009 12:01:30 GMT
Author: uschindler
Date: Sat Nov 21 12:01:29 2009
New Revision: 882888

URL: http://svn.apache.org/viewvc?rev=882888&view=rev
Log:
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.

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/search/NumericRangeQuery.java
    lucene/java/trunk/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/NumericRangeQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/NumericRangeQuery.java?rev=882888&r1=882887&r2=882888&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/NumericRangeQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/NumericRangeQuery.java Sat Nov 21
12:01:29 2009
@@ -389,6 +389,7 @@
 
     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 @@
     /** 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 @@
       // 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 @@
       // 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 @@
       // 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);
     }
 

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java?rev=882888&r1=882887&r2=882888&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java Sat Nov
21 12:01:29 2009
@@ -25,6 +25,7 @@
 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 @@
     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);
+  }
+  
 }



Mime
View raw message