You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2014/12/22 10:50:32 UTC

svn commit: r1647274 - in /lucene/dev/branches/branch_5x: ./ lucene/ lucene/core/ lucene/core/src/java/org/apache/lucene/util/ lucene/core/src/test/org/apache/lucene/util/ lucene/test-framework/ lucene/test-framework/src/java/org/apache/lucene/util/

Author: jpountz
Date: Mon Dec 22 09:50:31 2014
New Revision: 1647274

URL: http://svn.apache.org/r1647274
Log:
LUCENE-6123: Fix BitDocIdSet.Builder.andNot.

Added:
    lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/util/TestBitDocIdSetBuilder.java
      - copied unchanged from r1647270, lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestBitDocIdSetBuilder.java
Modified:
    lucene/dev/branches/branch_5x/   (props changed)
    lucene/dev/branches/branch_5x/lucene/   (props changed)
    lucene/dev/branches/branch_5x/lucene/core/   (props changed)
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/util/BitDocIdSet.java
    lucene/dev/branches/branch_5x/lucene/test-framework/   (props changed)
    lucene/dev/branches/branch_5x/lucene/test-framework/src/java/org/apache/lucene/util/BaseBitSetTestCase.java

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/util/BitDocIdSet.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/util/BitDocIdSet.java?rev=1647274&r1=1647273&r2=1647274&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/util/BitDocIdSet.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/util/BitDocIdSet.java Mon Dec 22 09:50:31 2014
@@ -107,6 +107,11 @@ public class BitDocIdSet extends DocIdSe
       this(maxDoc, false);
     }
 
+    // pkg-private for testing
+    boolean dense() {
+      return denseSet != null;
+    }
+
     /**
      * Add the content of the provided {@link DocIdSetIterator} to this builder.
      */
@@ -157,8 +162,8 @@ public class BitDocIdSet extends DocIdSe
     public void andNot(DocIdSetIterator it) throws IOException {
       if (denseSet != null) {
         denseSet.andNot(it);
-      } else if (denseSet != null) {
-        denseSet.andNot(it);
+      } else if (sparseSet != null) {
+        sparseSet.andNot(it);
       }
     }
 

Modified: lucene/dev/branches/branch_5x/lucene/test-framework/src/java/org/apache/lucene/util/BaseBitSetTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/test-framework/src/java/org/apache/lucene/util/BaseBitSetTestCase.java?rev=1647274&r1=1647273&r2=1647274&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/test-framework/src/java/org/apache/lucene/util/BaseBitSetTestCase.java (original)
+++ lucene/dev/branches/branch_5x/lucene/test-framework/src/java/org/apache/lucene/util/BaseBitSetTestCase.java Mon Dec 22 09:50:31 2014
@@ -171,45 +171,103 @@ public abstract class BaseBitSetTestCase
     }
   }
 
-  /** Test the {@link BitSet#and}, {@link BitSet#or} and {@link BitSet#andNot} methods. */
-  public void testBulkOperations() throws IOException {
+  private void testOr(float load) throws IOException {
     final int numBits = 1 + random().nextInt(100000);
-    BitSet set1 = new JavaUtilBitSet(randomSet(numBits, 0), numBits);
+    BitSet set1 = new JavaUtilBitSet(randomSet(numBits, 0), numBits); // empty
     T set2 = copyOf(set1, numBits);
-    final int iters = TEST_NIGHTLY ? 50 + random().nextInt(50) : 10 + random().nextInt(10);
-    for (int i = 0; i < iters; ++i) {
-      // make extreme percents more likely
-      float percentSet2 = rarely() ? 0 : (float) Math.pow(random().nextDouble(), 2);
-      if (random().nextBoolean()) {
-        percentSet2 = 1 - percentSet2;
+    
+    final int iterations = atLeast(10);
+    for (int iter = 0; iter < iterations; ++iter) {
+      DocIdSet otherSet = randomCopy(new JavaUtilBitSet(randomSet(numBits, load), numBits), numBits);
+      DocIdSetIterator otherIterator = otherSet.iterator();
+      if (otherIterator != null) {
+        set1.or(otherIterator);
+        set2.or(otherSet.iterator());
+        assertEquals(set1, set2, numBits);
       }
-      BitSet bulkSet = new JavaUtilBitSet(randomSet(numBits, percentSet2), numBits);
-      // operations are sometimes specialized based on the impl, so randomize the impl
-      final DocIdSet bulkSetCopy = randomCopy(bulkSet, numBits);
-      // now randomize the operation
-      if (bulkSetCopy.iterator() == null) {
-        continue;
+    }
+  }
+
+  /** Test {@link BitSet#or(DocIdSetIterator)} on sparse sets. */
+  public void testOrSparse() throws IOException {
+    testOr(0.001f);
+  }
+
+  /** Test {@link BitSet#or(DocIdSetIterator)} on dense sets. */
+  public void testOrDense() throws IOException {
+    testOr(0.5f);
+  }
+
+  /** Test {@link BitSet#or(DocIdSetIterator)} on a random density. */
+  public void testOrRandom() throws IOException {
+    testOr(random().nextFloat());
+  }
+
+  private void testAnd(float load) throws IOException {
+    final int numBits = 1 + random().nextInt(100000);
+    BitSet set1 = new JavaUtilBitSet(randomSet(numBits, numBits), numBits); // full
+    T set2 = copyOf(set1, numBits);
+    
+    final int iterations = atLeast(10);
+    for (int iter = 0; iter < iterations; ++iter) {
+      // BitSets have specializations to merge with certain impls, so we randomize the impl...
+      DocIdSet otherSet = randomCopy(new JavaUtilBitSet(randomSet(numBits, load), numBits), numBits);
+      DocIdSetIterator otherIterator = otherSet.iterator();
+      if (otherIterator != null) {
+        set1.and(otherIterator);
+        set2.and(otherSet.iterator());
+        assertEquals(set1, set2, numBits);
       }
-      DocIdSetIterator it1 = bulkSetCopy.iterator();
-      DocIdSetIterator it2 = bulkSetCopy.iterator();
-      switch (random().nextInt(3)) {
-        case 0:
-          set1.or(it1);
-          set2.or(it2);
-          break;
-        case 1:
-          set1.and(it1);
-          set2.and(it2);
-          break;
-        default:
-          set1.andNot(it1);
-          set2.andNot(it2);
-          break;
+    }
+  }
+
+  /** Test {@link BitSet#and(DocIdSetIterator)} on sparse sets. */
+  public void testAndSparse() throws IOException {
+    testAnd(0.1f);
+  }
+
+  /** Test {@link BitSet#and(DocIdSetIterator)} on dense sets. */
+  public void testAndDense() throws IOException {
+    testAnd(0.99f);
+  }
+
+  /** Test {@link BitSet#and(DocIdSetIterator)} on a random density. */
+  public void testAndRandom() throws IOException {
+    testAnd(random().nextFloat());
+  }
+
+  private void testAndNot(float load) throws IOException {
+    final int numBits = 1 + random().nextInt(100000);
+    BitSet set1 = new JavaUtilBitSet(randomSet(numBits, numBits), numBits); // full
+    T set2 = copyOf(set1, numBits);
+    
+    final int iterations = atLeast(10);
+    for (int iter = 0; iter < iterations; ++iter) {
+      DocIdSet otherSet = randomCopy(new JavaUtilBitSet(randomSet(numBits, load), numBits), numBits);
+      DocIdSetIterator otherIterator = otherSet.iterator();
+      if (otherIterator != null) {
+        set1.andNot(otherIterator);
+        set2.andNot(otherSet.iterator());
+        assertEquals(set1, set2, numBits);
       }
-      assertEquals(set1, set2, numBits);
     }
   }
 
+  /** Test {@link BitSet#andNot(DocIdSetIterator)} on sparse sets. */
+  public void testAndNotSparse() throws IOException {
+    testAndNot(0.01f);
+  }
+  
+  /** Test {@link BitSet#andNot(DocIdSetIterator)} on dense sets. */
+  public void testAndNotDense() throws IOException {
+    testAndNot(0.9f);
+  }
+
+  /** Test {@link BitSet#andNot(DocIdSetIterator)} on a random density. */
+  public void testAndNotRandom() throws IOException {
+    testAndNot(random().nextFloat());
+  }
+
   private static class JavaUtilBitSet extends BitSet {
 
     private final java.util.BitSet bitSet;