You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2011/09/21 18:26:26 UTC

svn commit: r1173745 - in /lucene/dev/branches/branch_3x/lucene: ./ src/java/org/apache/lucene/search/ src/test/org/apache/lucene/search/

Author: uschindler
Date: Wed Sep 21 16:26:25 2011
New Revision: 1173745

URL: http://svn.apache.org/viewvc?rev=1173745&view=rev
Log:
ILUCENE-3390: Invert to FieldCache.getDocsWithField

Modified:
    lucene/dev/branches/branch_3x/lucene/CHANGES.txt
    lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldCache.java
    lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldCacheImpl.java
    lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldComparator.java
    lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestFieldCache.java

Modified: lucene/dev/branches/branch_3x/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/CHANGES.txt?rev=1173745&r1=1173744&r2=1173745&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/branch_3x/lucene/CHANGES.txt Wed Sep 21 16:26:25 2011
@@ -12,7 +12,8 @@ Changes in backwards compatibility polic
   be populated directly into the FieldCache arrays during sorting,
   leading to concurrency issues. To fix this behaviour, the method
   signatures had to be changed:
-  - FieldCache.getUnValuedDocs() returns the interface Bits instead DocIdSet
+  - FieldCache.getUnValuedDocs() was renamed to FieldCache.getDocsWithField()
+    returning a Bits interface (backported from Lucene 4.0).
   - FieldComparator.setMissingValue() was removed and added to
     constructor
   As this is expert API, most code will not be affected.

Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldCache.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldCache.java?rev=1173745&r1=1173744&r2=1173745&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldCache.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldCache.java Wed Sep 21 16:26:25 2011
@@ -314,9 +314,9 @@ public interface FieldCache {
   /** Checks the internal cache for an appropriate entry, and if none is found,
    * reads the terms in <code>field</code> and returns a bit set at the size of
    * <code>reader.maxDoc()</code>, with turned on bits for each docid that 
-   * does not have a value for this field.
+   * does have a value for this field.
    */
-  public Bits getUnValuedDocs (IndexReader reader, String field) 
+  public Bits getDocsWithField(IndexReader reader, String field) 
   throws IOException;
   
   /** Checks the internal cache for an appropriate entry, and if none is

Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldCacheImpl.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldCacheImpl.java?rev=1173745&r1=1173744&r2=1173745&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldCacheImpl.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldCacheImpl.java Wed Sep 21 16:26:25 2011
@@ -59,7 +59,7 @@ class FieldCacheImpl implements FieldCac
     caches.put(Double.TYPE, new DoubleCache(this));
     caches.put(String.class, new StringCache(this));
     caches.put(StringIndex.class, new StringIndexCache(this));
-    caches.put(UnValuedDocsCache.class, new UnValuedDocsCache(this));
+    caches.put(DocsWithFieldCache.class, new DocsWithFieldCache(this));
   }
 
   public synchronized void purgeAllCaches() {
@@ -413,13 +413,13 @@ class FieldCacheImpl implements FieldCac
     }
   }
   
-  public Bits getUnValuedDocs(IndexReader reader, String field)
+  public Bits getDocsWithField(IndexReader reader, String field)
       throws IOException {
-    return (Bits) caches.get(UnValuedDocsCache.class).get(reader, new Entry(field, null));
+    return (Bits) caches.get(DocsWithFieldCache.class).get(reader, new Entry(field, null));
   }
 
-  static final class UnValuedDocsCache extends Cache {
-    UnValuedDocsCache(FieldCache wrapper) {
+  static final class DocsWithFieldCache extends Cache {
+    DocsWithFieldCache(FieldCache wrapper) {
       super(wrapper);
     }
     
@@ -428,13 +428,15 @@ class FieldCacheImpl implements FieldCac
     throws IOException {
       final Entry entry = entryKey;
       final String field = entry.field;      
-      final FixedBitSet res = new FixedBitSet(reader.maxDoc());
+      FixedBitSet res = null;
       final TermDocs termDocs = reader.termDocs();
       final TermEnum termEnum = reader.terms(new Term(field));
       try {
         do {
           final Term term = termEnum.term();
           if (term == null || term.field() != field) break;
+          if (res == null) // late init
+            res = new FixedBitSet(reader.maxDoc());
           termDocs.seek(termEnum);
           while (termDocs.next()) {
             res.set(termDocs.doc());
@@ -444,14 +446,15 @@ class FieldCacheImpl implements FieldCac
         termDocs.close();
         termEnum.close();
       }
+      if (res == null)
+        return new Bits.MatchNoBits(reader.maxDoc());
       final int numSet = res.cardinality();
       if (numSet >= reader.numDocs()) {
         // The cardinality of the BitSet is numDocs if all documents have a value.
         // As deleted docs are not in TermDocs, this is always true
         assert numSet == reader.numDocs();
-        return new Bits.MatchNoBits(reader.maxDoc());
+        return new Bits.MatchAllBits(reader.maxDoc());
       }
-      res.flip(0, reader.maxDoc());
       return res;
     }
   }

Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldComparator.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldComparator.java?rev=1173745&r1=1173744&r2=1173745&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldComparator.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/FieldComparator.java Wed Sep 21 16:26:25 2011
@@ -187,7 +187,7 @@ public abstract class FieldComparator<T>
     protected final T missingValue;
     protected final String field;
 
-    protected Bits unvaluedDocs = null;
+    protected Bits docsWithField = null;
     
     public NumericComparator(String field, T missingValue) {
       this.field = field;
@@ -197,12 +197,12 @@ public abstract class FieldComparator<T>
     @Override
     public void setNextReader(IndexReader reader, int docBase) throws IOException {
       if (missingValue != null) {
-        unvaluedDocs = FieldCache.DEFAULT.getUnValuedDocs(reader, field);
+        docsWithField = FieldCache.DEFAULT.getDocsWithField(reader, field);
         // optimization to remove unneeded checks on the bit interface:
-        if (unvaluedDocs instanceof Bits.MatchNoBits)
-          unvaluedDocs = null;
+        if (docsWithField instanceof Bits.MatchAllBits)
+          docsWithField = null;
       } else {
-        unvaluedDocs = null;
+        docsWithField = null;
       }
     }
     
@@ -234,7 +234,7 @@ public abstract class FieldComparator<T>
     @Override
     public int compareBottom(int doc) {
       byte v2 = currentReaderValues[doc];
-      if (unvaluedDocs != null && v2 == 0 && unvaluedDocs.get(doc))
+      if (docsWithField != null && v2 == 0 && !docsWithField.get(doc))
         v2 = missingValue;
       return bottom - v2;
     }
@@ -242,7 +242,7 @@ public abstract class FieldComparator<T>
     @Override
     public void copy(int slot, int doc) {
       byte v2 = currentReaderValues[doc];
-      if (unvaluedDocs != null && v2 == 0 && unvaluedDocs.get(doc))
+      if (docsWithField != null && v2 == 0 && !docsWithField.get(doc))
         v2 = missingValue;
       values[slot] = v2;
     }
@@ -344,7 +344,7 @@ public abstract class FieldComparator<T>
     @Override
     public int compareBottom(int doc) {
       double v2 = currentReaderValues[doc];
-      if (unvaluedDocs != null && v2 == 0 && unvaluedDocs.get(doc))
+      if (docsWithField != null && v2 == 0 && !docsWithField.get(doc))
         v2 = missingValue;
       if (bottom > v2) {
         return 1;
@@ -358,7 +358,7 @@ public abstract class FieldComparator<T>
     @Override
     public void copy(int slot, int doc) {
       double v2 = currentReaderValues[doc];
-      if (unvaluedDocs != null && v2 == 0 && unvaluedDocs.get(doc))
+      if (docsWithField != null && v2 == 0 && !docsWithField.get(doc))
         v2 = missingValue;
       values[slot] = v2;
     }
@@ -418,7 +418,7 @@ public abstract class FieldComparator<T>
       // TODO: are there sneaky non-branch ways to compute
       // sign of float?
       float v2 = currentReaderValues[doc];
-      if (unvaluedDocs != null && v2 == 0 && unvaluedDocs.get(doc))
+      if (docsWithField != null && v2 == 0 && !docsWithField.get(doc))
         v2 = missingValue;
       if (bottom > v2) {
         return 1;
@@ -432,7 +432,7 @@ public abstract class FieldComparator<T>
     @Override
     public void copy(int slot, int doc) {
       float v2 = currentReaderValues[doc];
-      if (unvaluedDocs != null && v2 == 0 && unvaluedDocs.get(doc))
+      if (docsWithField != null && v2 == 0 && !docsWithField.get(doc))
         v2 = missingValue;
       values[slot] = v2;
     }
@@ -496,7 +496,7 @@ public abstract class FieldComparator<T>
       // Cannot return bottom - values[slot2] because that
       // may overflow
       int v2 = currentReaderValues[doc];
-      if (unvaluedDocs != null && v2 == 0 && unvaluedDocs.get(doc))
+      if (docsWithField != null && v2 == 0 && !docsWithField.get(doc))
         v2 = missingValue;
       if (bottom > v2) {
         return 1;
@@ -510,7 +510,7 @@ public abstract class FieldComparator<T>
     @Override
     public void copy(int slot, int doc) {
       int v2 = currentReaderValues[doc];
-      if (unvaluedDocs != null && v2 == 0 && unvaluedDocs.get(doc))
+      if (docsWithField != null && v2 == 0 && !docsWithField.get(doc))
         v2 = missingValue;
       values[slot] = v2;
     }
@@ -570,7 +570,7 @@ public abstract class FieldComparator<T>
       // TODO: there are sneaky non-branch ways to compute
       // -1/+1/0 sign
       long v2 = currentReaderValues[doc];
-      if (unvaluedDocs != null && v2 == 0 && unvaluedDocs.get(doc))
+      if (docsWithField != null && v2 == 0 && !docsWithField.get(doc))
         v2 = missingValue;
       if (bottom > v2) {
         return 1;
@@ -584,7 +584,7 @@ public abstract class FieldComparator<T>
     @Override
     public void copy(int slot, int doc) {
       long v2 = currentReaderValues[doc];
-      if (unvaluedDocs != null && v2 == 0 && unvaluedDocs.get(doc))
+      if (docsWithField != null && v2 == 0 && !docsWithField.get(doc))
         v2 = missingValue;
       values[slot] = v2;
     }
@@ -700,7 +700,7 @@ public abstract class FieldComparator<T>
     @Override
     public int compareBottom(int doc) {
       short v2 = currentReaderValues[doc];
-      if (unvaluedDocs != null && v2 == 0 && unvaluedDocs.get(doc))
+      if (docsWithField != null && v2 == 0 && !docsWithField.get(doc))
         v2 = missingValue;
       return bottom - v2;
     }
@@ -708,7 +708,7 @@ public abstract class FieldComparator<T>
     @Override
     public void copy(int slot, int doc) {
       short v2 = currentReaderValues[doc];
-      if (unvaluedDocs != null && v2 == 0 && unvaluedDocs.get(doc))
+      if (docsWithField != null && v2 == 0 && !docsWithField.get(doc))
         v2 = missingValue;
       values[slot] = v2;
     }

Modified: lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestFieldCache.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestFieldCache.java?rev=1173745&r1=1173744&r2=1173745&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestFieldCache.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestFieldCache.java Wed Sep 21 16:26:25 2011
@@ -138,20 +138,20 @@ public class TestFieldCache extends Luce
 
     }
     
-    Bits unvalued = cache.getUnValuedDocs(reader, "theLong");
-    assertSame("Second request to cache return same array", unvalued, cache.getUnValuedDocs(reader, "theLong"));
-    assertTrue("unvalued(theLong) must be class Bits.MatchNoBits", unvalued instanceof Bits.MatchNoBits);
-    assertTrue("unvalued(theLong) Size: " + unvalued.length() + " is not: " + NUM_DOCS, unvalued.length() == NUM_DOCS);
-    for (int i = 0; i < unvalued.length(); i++) {
-      assertFalse(unvalued.get(i));
+    Bits docsWithField = cache.getDocsWithField(reader, "theLong");
+    assertSame("Second request to cache return same array", docsWithField, cache.getDocsWithField(reader, "theLong"));
+    assertTrue("docsWithField(theLong) must be class Bits.MatchAllBits", docsWithField instanceof Bits.MatchAllBits);
+    assertTrue("docsWithField(theLong) Size: " + docsWithField.length() + " is not: " + NUM_DOCS, docsWithField.length() == NUM_DOCS);
+    for (int i = 0; i < docsWithField.length(); i++) {
+      assertTrue(docsWithField.get(i));
     }
     
-    unvalued = cache.getUnValuedDocs(reader, "sparse");
-    assertSame("Second request to cache return same array", unvalued, cache.getUnValuedDocs(reader, "sparse"));
-    assertFalse("unvalued(sparse) must not be class Bits.MatchNoBits", unvalued instanceof Bits.MatchNoBits);
-    assertTrue("unvalued(sparse) Size: " + unvalued.length() + " is not: " + NUM_DOCS, unvalued.length() == NUM_DOCS);
-    for (int i = 0; i < unvalued.length(); i++) {
-      assertEquals(i%2 != 0, unvalued.get(i));
+    docsWithField = cache.getDocsWithField(reader, "sparse");
+    assertSame("Second request to cache return same array", docsWithField, cache.getDocsWithField(reader, "sparse"));
+    assertFalse("docsWithField(sparse) must not be class Bits.MatchAllBits", docsWithField instanceof Bits.MatchAllBits);
+    assertTrue("docsWithField(sparse) Size: " + docsWithField.length() + " is not: " + NUM_DOCS, docsWithField.length() == NUM_DOCS);
+    for (int i = 0; i < docsWithField.length(); i++) {
+      assertEquals(i%2 == 0, docsWithField.get(i));
     }
   }
 }