From 3534973444030d33a1c5808edb68bda32f9fcd10 Mon Sep 17 00:00:00 2001 From: ChrisHegarty Date: Fri, 18 Apr 2025 12:52:40 +0100 Subject: [PATCH 1/2] Fix DISIDocIdStream::count so that is does not try to count beyond max --- .../apache/lucene/search/DISIDocIdStream.java | 2 +- .../lucene/search/TestDISIDocIdStream.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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 0541969d7e34..201cbfe09faa 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 @@ public void forEach(int upTo, CheckedIntConsumer consumer) throws I @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 9a400c0237cf..0948c2368847 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 void testCountUpTo() throws IOException { 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 { From 4982149542dee0340683e679e14c928667ce3f54 Mon Sep 17 00:00:00 2001 From: ChrisHegarty Date: Fri, 18 Apr 2025 14:27:53 +0100 Subject: [PATCH 2/2] changelog --- lucene/CHANGES.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 6309e3ec3cae..7855cec6330d 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -96,6 +96,14 @@ Other --------------------- * GITHUB#14474: Minor refactor of ComponentTree (Ankit Jain) +======================= 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