Skip to content

Apache Lucene open-source search software

simplify unionSize, implement in terms of intersectionSize

Yonik Seeley9931b8799f50
diff --git a/src/java/org/apache/solr/search/BitDocSet.java b/src/java/org/apache/solr/search/BitDocSet.java
index 18b3e1de599..32075caaf23 100644
--- a/src/java/org/apache/solr/search/BitDocSet.java
+++ b/src/java/org/apache/solr/search/BitDocSet.java
@@ -156,6 +156,8 @@ public class BitDocSet extends DocSetBase {
@Override
public int unionSize(DocSet other) {
if (other instanceof BitDocSet) {
+ // if we don't know our current size, this is faster than
+ // size + other.size - intersection_size
return (int)OpenBitSet.unionCount(this.bits, ((BitDocSet)other).bits);
} else {
// they had better not call us back!
@@ -170,7 +172,6 @@ public class BitDocSet extends DocSetBase {
// size - intersection_size
return (int)OpenBitSet.andNotCount(this.bits, ((BitDocSet)other).bits);
} else {
- // use BaseDocSet's size-intersection_size
return super.andNotSize(other);
}
}
diff --git a/src/java/org/apache/solr/search/DocSet.java b/src/java/org/apache/solr/search/DocSet.java
index 852e2fc3b3c..c9260d2e7e3 100644
--- a/src/java/org/apache/solr/search/DocSet.java
+++ b/src/java/org/apache/solr/search/DocSet.java
@@ -219,9 +219,8 @@ abstract class DocSetBase implements DocSet {
return intersection(other).size();
}
- // subclasses have more efficient implementations
public int unionSize(DocSet other) {
- return union(other).size();
+ return this.size() + other.size() - this.intersectionSize(other);
}
public DocSet andNot(DocSet other) {
diff --git a/src/java/org/apache/solr/search/HashDocSet.java b/src/java/org/apache/solr/search/HashDocSet.java
index a6299e1b608..9ccdb4ec099 100644
--- a/src/java/org/apache/solr/search/HashDocSet.java
+++ b/src/java/org/apache/solr/search/HashDocSet.java
@@ -271,37 +271,10 @@ public final class HashDocSet extends DocSetBase {
}
return resultCount;
}
-
+
}
- @Override
- public int unionSize(DocSet other) {
- if (other instanceof HashDocSet) {
- // set "a" to the smallest doc set for the most efficient
- // union count.
- final HashDocSet a = size()<=other.size() ? this : (HashDocSet)other;
- final HashDocSet b = size()<=other.size() ? (HashDocSet)other : this;
-
- int resultCount=b.size();
- for (int i=0; i<a.table.length; i++) {
- int id=a.table[i];
- if (id >= 0 && !b.exists(id)) {
- resultCount++;
- }
- }
- return resultCount;
- } else {
- int resultCount=other.size();
- for (int i=0; i<table.length; i++) {
- int id=table[i];
- if (id >= 0 && !other.exists(id)) {
- resultCount++;
- }
- }
- return resultCount;
- }
- }
- // 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)
+ // don't implement andNotSize() and unionSize() on purpose... they are implemented
+ // in BaseDocSet in terms of intersectionSize().
}
diff --git a/src/test/org/apache/solr/search/TestDocSet.java b/src/test/org/apache/solr/search/TestDocSet.java
index af53df9c9fb..9fd6b23314d 100644
--- a/src/test/org/apache/solr/search/TestDocSet.java
+++ b/src/test/org/apache/solr/search/TestDocSet.java
@@ -55,6 +55,8 @@ public class TestDocSet extends TestCase {
DocSet b1 = getDocSet(a1);
DocSet b2 = getDocSet(a2);
+ // System.out.println("b1="+b1+", b2="+b2);
+
assertEquals((int)a1.cardinality(), b1.size());
assertEquals((int)a2.cardinality(), b2.size());