Skip to content

Commit 8405699

Browse files
committed
Fix DISIDocIdStream::count so that it does not try to count beyond max (apache#14522)
Fix DISIDocIdStream::count so that it does not try to count beyond max. From the perspective of DISIDocIdStream it should not be necessary to check the iterator position before calling count.
1 parent a1f0af3 commit 8405699

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

lucene/CHANGES.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ http://s.apache.org/luceneversions
77

88
Bug Fixes
99
---------------------
10-
(No changes)
10+
11+
* GITHUB#14522: Fix DISIDocIdStream::count so that it does not try to count beyond max.
12+
(Chris Hegarty}
1113

1214
======================= Lucene 10.2.0 =======================
1315

lucene/core/src/java/org/apache/lucene/search/DISIDocIdStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public void forEach(int upTo, CheckedIntConsumer<IOException> consumer) throws I
5151

5252
@Override
5353
public int count(int upTo) throws IOException {
54+
upTo = Math.min(upTo, max);
5455
if (iterator.docID() >= upTo) {
5556
return 0;
5657
}
5758
// If the collector is just interested in the count, loading in a bit set and counting bits is
5859
// often faster than incrementing a counter on every call to nextDoc().
5960
assert spare.scanIsEmpty();
60-
upTo = Math.min(upTo, max);
6161
int offset = iterator.docID();
6262
iterator.intoBitSet(upTo, spare, offset);
6363
int count = spare.cardinality(0, upTo - offset);

lucene/core/src/test/org/apache/lucene/search/TestDISIDocIdStream.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,24 @@ public void testCountUpTo() throws IOException {
111111
assertEquals(bitSet.cardinality(60, 80), stream.count(120));
112112

113113
assertFalse(stream.mayHaveRemaining());
114+
115+
assertEquals(0, stream.count(40)); // we're well past this doc
116+
assertEquals(0, stream.count(100)); // we're well past this doc/max
117+
assertEquals(0, stream.count()); // we're well past this doc/max
118+
}
119+
120+
// Tests that count when the iterator, but not the external interaction with the stream,
121+
// is past max does not fail, returns 0.
122+
public void testCountUpTo2() throws IOException {
123+
FixedBitSet bitSet = new FixedBitSet(100);
124+
bitSet.set(1);
125+
bitSet.set(85);
126+
BitSetIterator iterator = new BitSetIterator(bitSet, bitSet.approximateCardinality());
127+
DocIdStream stream = new DISIDocIdStream(iterator, 80, new FixedBitSet(100));
128+
iterator.advance(1);
129+
130+
assertEquals(1, stream.count(79));
131+
assertEquals(0, stream.count());
114132
}
115133

116134
public void testMixForEachCountUpTo() throws IOException {

0 commit comments

Comments
 (0)