From 66c90a9683407eee52e85120cb1e80fd9dd905f5 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Mon, 31 Oct 2016 10:36:26 +0100 Subject: [PATCH] LUCENE-7528: Fix Lucene54's advanceExact impl in the sparse case. --- .../lucene54/Lucene54DocValuesProducer.java | 2 +- .../lucene54/TestLucene54DocValuesFormat.java | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/codecs/lucene54/Lucene54DocValuesProducer.java b/lucene/backward-codecs/src/java/org/apache/lucene/codecs/lucene54/Lucene54DocValuesProducer.java index f1c169c7ff8..a35f503cb98 100644 --- a/lucene/backward-codecs/src/java/org/apache/lucene/codecs/lucene54/Lucene54DocValuesProducer.java +++ b/lucene/backward-codecs/src/java/org/apache/lucene/codecs/lucene54/Lucene54DocValuesProducer.java @@ -715,7 +715,7 @@ final class Lucene54DocValuesProducer extends DocValuesProducer implements Close } --index; doc = target; - return false; + return index >= 0 && docIds.get(index) == target; } @Override diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/codecs/lucene54/TestLucene54DocValuesFormat.java b/lucene/backward-codecs/src/test/org/apache/lucene/codecs/lucene54/TestLucene54DocValuesFormat.java index b231716098f..a761dfcec15 100644 --- a/lucene/backward-codecs/src/test/org/apache/lucene/codecs/lucene54/TestLucene54DocValuesFormat.java +++ b/lucene/backward-codecs/src/test/org/apache/lucene/codecs/lucene54/TestLucene54DocValuesFormat.java @@ -488,6 +488,33 @@ public class TestLucene54DocValuesFormat extends BaseCompressingDocValuesFormatT } } + // advanceExact + for (int i = 0; i < 2000; ++i) { + sparseValues.reset(); + if (random().nextBoolean() && docIds.length > 0) { + sparseValues.advance(docIds[TestUtil.nextInt(random(), 0, docIds.length - 1)]); + } + + final int target = TestUtil.nextInt(random(), Math.max(0, sparseValues.docID()), maxDoc - 1); + final boolean exists = sparseValues.advanceExact(target); + + final int index = Arrays.binarySearch(docIds, target); + assertEquals(index >= 0, exists); + assertEquals(target, sparseValues.docID()); + + final boolean exists2 = sparseValues.advanceExact(target); + assertEquals(index >= 0, exists2); + assertEquals(target, sparseValues.docID()); + + final int nextIndex = index >= 0 ? index + 1 : -1 - index; + if (nextIndex >= docIds.length) { + assertEquals(DocIdSetIterator.NO_MORE_DOCS, sparseValues.nextDoc()); + } else { + assertEquals(docIds[nextIndex], sparseValues.nextDoc()); + } + } + + final SparseNumericDocValuesRandomAccessWrapper raWrapper = new SparseNumericDocValuesRandomAccessWrapper(sparseValues, missingValue); // random-access