Skip to content

Apache Lucene open-source search software

Fix DISIDocIdStream::count so that it does not try to count beyond max (#14522)

Chris Hegarty4ac2d145acb1
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 0cdad826b81..309549a55b2 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -97,6 +97,14 @@ Other
* GITHUB#14474: Minor refactor of ComponentTree (Ankit Jain)
* GITHUB#14459: Minor refactoring of DocValuesConsumer to allow subclasses to reuse some of its merge logic. (Martijn van Groningen)
+======================= Lucene 10.2.1 =======================
+
+Bug Fixes
+---------------------
+
+* GITHUB#14522: Fix DISIDocIdStream::count so that it does not try to count beyond max.
+ (Chris Hegarty}
+
======================= Lucene 10.2.0 =======================
API Changes
diff --git a/lucene/core/src/java/org/apache/lucene/search/DISIDocIdStream.java b/lucene/core/src/java/org/apache/lucene/search/DISIDocIdStream.java
index 0541969d7e3..201cbfe09fa 100644
--- a/lucene/core/src/java/org/apache/lucene/search/DISIDocIdStream.java
+++ b/lucene/core/src/java/org/apache/lucene/search/DISIDocIdStream.java
@@ -51,13 +51,13 @@ final class DISIDocIdStream extends DocIdStream {
@Override
public int count(int upTo) throws IOException {
+ upTo = Math.min(upTo, max);
if (iterator.docID() >= upTo) {
return 0;
}
// If the collector is just interested in the count, loading in a bit set and counting bits is
// often faster than incrementing a counter on every call to nextDoc().
assert spare.scanIsEmpty();
- upTo = Math.min(upTo, max);
int offset = iterator.docID();
iterator.intoBitSet(upTo, spare, offset);
int count = spare.cardinality(0, upTo - offset);
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestDISIDocIdStream.java b/lucene/core/src/test/org/apache/lucene/search/TestDISIDocIdStream.java
index 9a400c0237c..0948c236884 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestDISIDocIdStream.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestDISIDocIdStream.java
@@ -111,6 +111,24 @@ public class TestDISIDocIdStream extends LuceneTestCase {
assertEquals(bitSet.cardinality(60, 80), stream.count(120));
assertFalse(stream.mayHaveRemaining());
+
+ assertEquals(0, stream.count(40)); // we're well past this doc
+ assertEquals(0, stream.count(100)); // we're well past this doc/max
+ assertEquals(0, stream.count()); // we're well past this doc/max
+ }
+
+ // Tests that count when the iterator, but not the external interaction with the stream,
+ // is past max does not fail, returns 0.
+ public void testCountUpTo2() throws IOException {
+ FixedBitSet bitSet = new FixedBitSet(100);
+ bitSet.set(1);
+ bitSet.set(85);
+ BitSetIterator iterator = new BitSetIterator(bitSet, bitSet.approximateCardinality());
+ DocIdStream stream = new DISIDocIdStream(iterator, 80, new FixedBitSet(100));
+ iterator.advance(1);
+
+ assertEquals(1, stream.count(79));
+ assertEquals(0, stream.count());
}
public void testMixForEachCountUpTo() throws IOException {