Skip to content

Commit 83e51eb

Browse files
ChrisHegartyjpountz
authored andcommitted
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 7c9e94f commit 83e51eb

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

lucene/CHANGES.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ Other
9797
* GITHUB#14474: Minor refactor of ComponentTree (Ankit Jain)
9898
* GITHUB#14459: Minor refactoring of DocValuesConsumer to allow subclasses to reuse some of its merge logic. (Martijn van Groningen)
9999

100+
======================= Lucene 10.2.1 =======================
101+
102+
Bug Fixes
103+
---------------------
104+
105+
* GITHUB#14522: Fix DISIDocIdStream::count so that it does not try to count beyond max.
106+
(Chris Hegarty}
107+
100108
======================= Lucene 10.2.0 =======================
101109

102110
API Changes

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)