Skip to content

Apache Lucene open-source search software

DocSet.andNot(),andNotSize()

Yonik Seeley3a810792bfe3
diff --git a/CHANGES.txt b/CHANGES.txt
index fcc89de12c7..52cc4b8d591 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -17,6 +17,7 @@ New Features
9. Added KeywordTokenizerFactory (hossman)
10. copyField accepts dynamicfield-like names as the source.
(Darren Erik Vengroff via yonik, SOLR-21)
+11. new DocSet.andNot(), DocSet.andNotSize() (yonik)
Changes in runtime behavior
1. classes reorganized into different packages, package names changed to Apache
diff --git a/src/java/org/apache/solr/search/BitDocSet.java b/src/java/org/apache/solr/search/BitDocSet.java
index 54bfa078acd..18b3e1de599 100644
--- a/src/java/org/apache/solr/search/BitDocSet.java
+++ b/src/java/org/apache/solr/search/BitDocSet.java
@@ -143,6 +143,7 @@ public class BitDocSet extends DocSetBase {
return bits.get(doc);
}
+ @Override
public int intersectionSize(DocSet other) {
if (other instanceof BitDocSet) {
return (int)OpenBitSet.intersectionCount(this.bits, ((BitDocSet)other).bits);
@@ -152,6 +153,7 @@ public class BitDocSet extends DocSetBase {
}
}
+ @Override
public int unionSize(DocSet other) {
if (other instanceof BitDocSet) {
return (int)OpenBitSet.unionCount(this.bits, ((BitDocSet)other).bits);
@@ -161,6 +163,18 @@ public class BitDocSet extends DocSetBase {
}
}
+ @Override
+ public int andNotSize(DocSet other) {
+ if (other instanceof BitDocSet) {
+ // if we don't know our current size, this is faster than
+ // size - intersection_size
+ return (int)OpenBitSet.andNotCount(this.bits, ((BitDocSet)other).bits);
+ } else {
+ // use BaseDocSet's size-intersection_size
+ return super.andNotSize(other);
+ }
+ }
+
public long memSize() {
return (bits.getBits().length << 3) + 16;
}
diff --git a/src/java/org/apache/solr/search/DocSet.java b/src/java/org/apache/solr/search/DocSet.java
index c90cb445319..852e2fc3b3c 100644
--- a/src/java/org/apache/solr/search/DocSet.java
+++ b/src/java/org/apache/solr/search/DocSet.java
@@ -124,6 +124,17 @@ public interface DocSet /* extends Collection<Integer> */ {
*/
public int unionSize(DocSet other);
+ /**
+ * Returns the documents in this set that are not in the other set. Neither set is modified - a new DocSet is
+ * created and returned.
+ * @return a DocSet representing this AND NOT other
+ */
+ public DocSet andNot(DocSet other);
+
+ /**
+ * Returns the number of documents in this set that are not in the other set.
+ */
+ public int andNotSize(DocSet other);
}
/** A base class that may be usefull for implementing DocSets */
@@ -208,11 +219,20 @@ abstract class DocSetBase implements DocSet {
return intersection(other).size();
}
- // TODO: do an efficient implementation
+ // subclasses have more efficient implementations
public int unionSize(DocSet other) {
return union(other).size();
}
+ public DocSet andNot(DocSet other) {
+ OpenBitSet newbits = (OpenBitSet)(this.getBits().clone());
+ newbits.andNot(other.getBits());
+ return new BitDocSet(newbits);
+ }
+
+ public int andNotSize(DocSet other) {
+ return this.size() - this.intersectionSize(other);
+ }
}
diff --git a/src/java/org/apache/solr/search/HashDocSet.java b/src/java/org/apache/solr/search/HashDocSet.java
index 97962bd3b37..a6299e1b608 100644
--- a/src/java/org/apache/solr/search/HashDocSet.java
+++ b/src/java/org/apache/solr/search/HashDocSet.java
@@ -278,7 +278,7 @@ public final class HashDocSet extends DocSetBase {
public int unionSize(DocSet other) {
if (other instanceof HashDocSet) {
// set "a" to the smallest doc set for the most efficient
- // intersection.
+ // union count.
final HashDocSet a = size()<=other.size() ? this : (HashDocSet)other;
final HashDocSet b = size()<=other.size() ? (HashDocSet)other : this;
@@ -302,5 +302,6 @@ public final class HashDocSet extends DocSetBase {
}
}
-
+ // don't implement andNotSize() on purpose... if one of the sets is a HashDocSet,
+ // its easier to get the intersection size and subtract (implementation in BaseDocSet)
}
diff --git a/src/test/org/apache/solr/search/DocSetPerf.java b/src/test/org/apache/solr/search/DocSetPerf.java
index 2036b1b7573..fedc9ce79de 100644
--- a/src/test/org/apache/solr/search/DocSetPerf.java
+++ b/src/test/org/apache/solr/search/DocSetPerf.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package org.apache.solr.search.test;
+package org.apache.solr.search;
import org.apache.solr.search.BitDocSet;
import org.apache.solr.search.HashDocSet;
diff --git a/src/test/org/apache/solr/search/TestDocSet.java b/src/test/org/apache/solr/search/TestDocSet.java
index c4942e7d4e3..af53df9c9fb 100644
--- a/src/test/org/apache/solr/search/TestDocSet.java
+++ b/src/test/org/apache/solr/search/TestDocSet.java
@@ -64,14 +64,15 @@ public class TestDocSet extends TestCase {
OpenBitSet a_and = (OpenBitSet)a1.clone(); a_and.and(a2);
OpenBitSet a_or = (OpenBitSet)a1.clone(); a_or.or(a2);
// OpenBitSet a_xor = (OpenBitSet)a1.clone(); a_xor.xor(a2);
- // OpenBitSet a_andn = (OpenBitSet)a1.clone(); a_andn.andNot(a2);
+ OpenBitSet a_andn = (OpenBitSet)a1.clone(); a_andn.andNot(a2);
checkEqual(a_and, b1.intersection(b2));
checkEqual(a_or, b1.union(b2));
+ checkEqual(a_andn, b1.andNot(b2));
assertEquals(a_and.cardinality(), b1.intersectionSize(b2));
assertEquals(a_or.cardinality(), b1.unionSize(b2));
-
+ assertEquals(a_andn.cardinality(), b1.andNotSize(b2));
}