Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-6123: Fix BitDocIdSet.Builder.andNot.

Adrien Grand15665486cda3
diff --git a/lucene/core/src/java/org/apache/lucene/util/BitDocIdSet.java b/lucene/core/src/java/org/apache/lucene/util/BitDocIdSet.java
index b92deba089e..791830d0d0e 100644
--- a/lucene/core/src/java/org/apache/lucene/util/BitDocIdSet.java
+++ b/lucene/core/src/java/org/apache/lucene/util/BitDocIdSet.java
@@ -107,6 +107,11 @@ public class BitDocIdSet extends DocIdSet {
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 DocIdSet {
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);
}
}
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestBitDocIdSetBuilder.java b/lucene/core/src/test/org/apache/lucene/util/TestBitDocIdSetBuilder.java
new file mode 100644
index 00000000000..9a8f04ae4e6
--- /dev/null
+++ b/lucene/core/src/test/org/apache/lucene/util/TestBitDocIdSetBuilder.java
@@ -0,0 +1,135 @@
+package org.apache.lucene.util;
+
+
+/*
+ * 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.search.DocIdSet;
+import org.apache.lucene.search.DocIdSetIterator;
+
+public class TestBitDocIdSetBuilder extends LuceneTestCase {
+
+ private static DocIdSet randomSet(int maxDoc, int numDocs) {
+ FixedBitSet set = new FixedBitSet(maxDoc);
+ for (int i = 0; i < numDocs; ++i) {
+ while (true) {
+ final int docID = random().nextInt(maxDoc);
+ if (set.get(docID) == false) {
+ set.set(docID);
+ break;
+ }
+ }
+ }
+ return new BitDocIdSet(set);
+ }
+
+ private void assertEquals(DocIdSet set1, DocIdSet set2) throws IOException {
+ DocIdSetIterator it1 = set1.iterator();
+ DocIdSetIterator it2 = set2.iterator();
+ for (int doc = it1.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = it1.nextDoc()) {
+ assertEquals(doc, it2.nextDoc());
+ }
+ assertEquals(DocIdSetIterator.NO_MORE_DOCS, it2.nextDoc());
+ }
+
+ public void testOrDense() throws IOException {
+ final int maxDoc = TestUtil.nextInt(random(), 10000, 100000);
+ BitDocIdSet.Builder builder = new BitDocIdSet.Builder(maxDoc);
+ FixedBitSet set = new FixedBitSet(maxDoc);
+ DocIdSet other = randomSet(maxDoc, maxDoc / 2);
+ builder.or(other.iterator());
+ set.or(other.iterator());
+ assertTrue(builder.dense());
+ other = randomSet(maxDoc, maxDoc / 2);
+ builder.or(other.iterator());
+ set.or(other.iterator());
+ assertEquals(new BitDocIdSet(set), builder.build());
+ }
+
+ public void testOrSparse() throws IOException {
+ final int maxDoc = TestUtil.nextInt(random(), 10000, 100000);
+ BitDocIdSet.Builder builder = new BitDocIdSet.Builder(maxDoc);
+ FixedBitSet set = new FixedBitSet(maxDoc);
+ DocIdSet other = randomSet(maxDoc, maxDoc / 5000);
+ builder.or(other.iterator());
+ set.or(other.iterator());
+ assertFalse(builder.dense());
+ other = randomSet(maxDoc, maxDoc / 5000);
+ builder.or(other.iterator());
+ set.or(other.iterator());
+ assertEquals(new BitDocIdSet(set), builder.build());
+ }
+
+ public void testAndDense() throws IOException {
+ final int maxDoc = TestUtil.nextInt(random(), 10000, 100000);
+ BitDocIdSet.Builder builder = new BitDocIdSet.Builder(maxDoc);
+ FixedBitSet set = new FixedBitSet(maxDoc);
+ DocIdSet other = randomSet(maxDoc, maxDoc / 2);
+ builder.or(other.iterator());
+ set.or(other.iterator());
+ assertTrue(builder.dense());
+ other = randomSet(maxDoc, maxDoc / 2);
+ builder.and(other.iterator());
+ set.and(other.iterator());
+ assertEquals(new BitDocIdSet(set), builder.build());
+ }
+
+ public void testAndSparse() throws IOException {
+ final int maxDoc = TestUtil.nextInt(random(), 10000, 100000);
+ BitDocIdSet.Builder builder = new BitDocIdSet.Builder(maxDoc);
+ FixedBitSet set = new FixedBitSet(maxDoc);
+ DocIdSet other = randomSet(maxDoc, maxDoc / 2000);
+ builder.or(other.iterator());
+ set.or(other.iterator());
+ assertFalse(builder.dense());
+ other = randomSet(maxDoc, maxDoc / 2);
+ builder.and(other.iterator());
+ set.and(other.iterator());
+ assertEquals(new BitDocIdSet(set), builder.build());
+ }
+
+ public void testAndNotDense() throws IOException {
+ final int maxDoc = TestUtil.nextInt(random(), 10000, 100000);
+ BitDocIdSet.Builder builder = new BitDocIdSet.Builder(maxDoc);
+ FixedBitSet set = new FixedBitSet(maxDoc);
+ DocIdSet other = randomSet(maxDoc, maxDoc / 2);
+ builder.or(other.iterator());
+ set.or(other.iterator());
+ assertTrue(builder.dense());
+ other = randomSet(maxDoc, maxDoc / 2);
+ builder.andNot(other.iterator());
+ set.andNot(other.iterator());
+ assertEquals(new BitDocIdSet(set), builder.build());
+ }
+
+ public void testAndNotSparse() throws IOException {
+ final int maxDoc = TestUtil.nextInt(random(), 10000, 100000);
+ BitDocIdSet.Builder builder = new BitDocIdSet.Builder(maxDoc);
+ FixedBitSet set = new FixedBitSet(maxDoc);
+ DocIdSet other = randomSet(maxDoc, maxDoc / 2000);
+ builder.or(other.iterator());
+ set.or(other.iterator());
+ assertFalse(builder.dense());
+ other = randomSet(maxDoc, maxDoc / 2);
+ builder.andNot(other.iterator());
+ set.andNot(other.iterator());
+ assertEquals(new BitDocIdSet(set), builder.build());
+ }
+
+}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/BaseBitSetTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/BaseBitSetTestCase.java
index 0a262505af8..d6951baa740 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/util/BaseBitSetTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/util/BaseBitSetTestCase.java
@@ -171,45 +171,103 @@ public abstract class BaseBitSetTestCase<T extends BitSet> extends LuceneTestCas
}
}
- /** 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;