Apache Lucene open-source search software
LUCENE-7462: Give doc values APIs an `advanceExact` method.
Adrien Grand9aca4c9d5608
diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/codecs/lucene53/Lucene53NormsProducer.java b/lucene/backward-codecs/src/java/org/apache/lucene/codecs/lucene53/Lucene53NormsProducer.javaindex a97cb5a8ea3..718fcd651b9 100644--- a/lucene/backward-codecs/src/java/org/apache/lucene/codecs/lucene53/Lucene53NormsProducer.java+++ b/lucene/backward-codecs/src/java/org/apache/lucene/codecs/lucene53/Lucene53NormsProducer.java@@ -221,6 +221,12 @@ class Lucene53NormsProducer extends NormsProducer {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return true;+ }+@Overridepublic long cost() {// TODOdiff --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.javaindex 1f785feae64..f1c169c7ff8 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@@ -476,6 +476,12 @@ final class Lucene54DocValuesProducer extends DocValuesProducer implements Closereturn docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return true;+ }+@Overridepublic long cost() {// TODO@@ -523,6 +529,13 @@ final class Lucene54DocValuesProducer extends DocValuesProducer implements Closereturn doc = NO_MORE_DOCS;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ doc = target;+ value = values.get(doc);+ return value != 0 || docsWithField.get(doc);+ }+@Overridepublic long cost() {return maxDoc;@@ -695,6 +708,16 @@ final class Lucene54DocValuesProducer extends DocValuesProducer implements Closereturn doc = hiDoc;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ if (advance(target) == target) {+ return true;+ }+ --index;+ doc = target;+ return false;+ }+@Overridepublic long longValue() {assert index >= 0;@@ -890,6 +913,11 @@ final class Lucene54DocValuesProducer extends DocValuesProducer implements Closereturn sparseValues.advance(target);}+ @Override+ public boolean advanceExact(int target) throws IOException {+ return sparseValues.advanceExact(target);+ }+@Overridepublic long cost() {return sparseValues.cost();@@ -933,7 +961,14 @@ final class Lucene54DocValuesProducer extends DocValuesProducer implements Closereturn nextDoc();}}-++ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ ord = (int) ordinals.get(target);+ return ord != -1;+ }+@Overridepublic int ordValue() {return ord;@@ -1016,6 +1051,11 @@ final class Lucene54DocValuesProducer extends DocValuesProducer implements Closereturn sparseValues.advance(target);}+ @Override+ public boolean advanceExact(int target) throws IOException {+ return sparseValues.advanceExact(target);+ }+@Overridepublic long cost() {return sparseValues.cost();@@ -1060,6 +1100,12 @@ final class Lucene54DocValuesProducer extends DocValuesProducer implements Close}}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return docsWithField.get(docID);+ }+@Overridepublic long cost() {// TODO@@ -1121,6 +1167,14 @@ final class Lucene54DocValuesProducer extends DocValuesProducer implements Close}}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ startOffset = ordIndex.get(docID);+ endOffset = ordIndex.get(docID+1L);+ return endOffset > startOffset;+ }+@Overridepublic long cost() {// TODO@@ -1184,6 +1238,15 @@ final class Lucene54DocValuesProducer extends DocValuesProducer implements Close}}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ int ord = (int) ordinals.get(docID);+ startOffset = offsets[ord];+ endOffset = offsets[ord+1];+ return endOffset > startOffset;+ }+@Overridepublic long cost() {// TODOdiff --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.javaindex c6ca201e3fb..b231716098f 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@@ -106,7 +106,7 @@ public class TestLucene54DocValuesFormat extends BaseCompressingDocValuesFormatTpublic void testSortedVariableLengthBigVsStoredFields() throws Exception {int numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {- doTestSortedVsStoredFields(atLeast(300), 1, 32766);+ doTestSortedVsStoredFields(atLeast(300), 1d, 1, 32766);}}@@ -114,7 +114,7 @@ public class TestLucene54DocValuesFormat extends BaseCompressingDocValuesFormatTpublic void testSortedVariableLengthManyVsStoredFields() throws Exception {int numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {- doTestSortedVsStoredFields(TestUtil.nextInt(random(), 1024, 2049), 1, 500);+ doTestSortedVsStoredFields(TestUtil.nextInt(random(), 1024, 2049), 1d, 1, 500);}}@@ -201,6 +201,7 @@ public class TestLucene54DocValuesFormat extends BaseCompressingDocValuesFormatT}final IndexReader indexReader = writer.getReader();+ TestUtil.checkReader(indexReader);writer.close();for (LeafReaderContext context : indexReader.leaves()) {diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesReader.javaindex adf5e427aa9..09f97aba802 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesReader.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesReader.java@@ -144,7 +144,7 @@ class SimpleTextDocValuesReader extends DocValuesProducer {if (values == null) {return null;} else {- DocIdSetIterator docsWithField = getNumericDocsWithField(fieldInfo);+ DocValuesIterator docsWithField = getNumericDocsWithField(fieldInfo);return new NumericDocValues() {@Override@@ -167,6 +167,11 @@ class SimpleTextDocValuesReader extends DocValuesProducer {return docsWithField.advance(target);}+ @Override+ public boolean advanceExact(int target) throws IOException {+ return docsWithField.advanceExact(target);+ }+@Overridepublic long longValue() throws IOException {return values.apply(docsWithField.docID());@@ -214,12 +219,16 @@ class SimpleTextDocValuesReader extends DocValuesProducer {}};}-- private DocIdSetIterator getNumericDocsWithField(FieldInfo fieldInfo) throws IOException {++ private static abstract class DocValuesIterator extends DocIdSetIterator {+ abstract boolean advanceExact(int target) throws IOException;+ }++ private DocValuesIterator getNumericDocsWithField(FieldInfo fieldInfo) throws IOException {final OneField field = fields.get(fieldInfo.name);final IndexInput in = data.clone();final BytesRefBuilder scratch = new BytesRefBuilder();- return new DocIdSetIterator() {+ return new DocValuesIterator() {int doc = -1;@@ -250,6 +259,15 @@ class SimpleTextDocValuesReader extends DocValuesProducer {}return doc = NO_MORE_DOCS;}++ @Override+ boolean advanceExact(int target) throws IOException {+ this.doc = target;+ in.seek(field.dataStartFilePointer + (1+field.pattern.length()+2)*target);+ SimpleTextUtil.readLine(in, scratch); // data+ SimpleTextUtil.readLine(in, scratch); // 'T' or 'F'+ return scratch.byteAt(0) == (byte) 'T';+ }};}@@ -265,7 +283,7 @@ class SimpleTextDocValuesReader extends DocValuesProducer {final BytesRefBuilder scratch = new BytesRefBuilder();final DecimalFormat decoder = new DecimalFormat(field.pattern, new DecimalFormatSymbols(Locale.ROOT));- DocIdSetIterator docsWithField = getBinaryDocsWithField(fieldInfo);+ DocValuesIterator docsWithField = getBinaryDocsWithField(fieldInfo);IntFunction<BytesRef> values = new IntFunction<BytesRef>() {final BytesRefBuilder term = new BytesRefBuilder();@@ -316,6 +334,11 @@ class SimpleTextDocValuesReader extends DocValuesProducer {return docsWithField.advance(target);}+ @Override+ public boolean advanceExact(int target) throws IOException {+ return docsWithField.advanceExact(target);+ }+@Overridepublic BytesRef binaryValue() throws IOException {return values.apply(docsWithField.docID());@@ -323,13 +346,13 @@ class SimpleTextDocValuesReader extends DocValuesProducer {};}- private DocIdSetIterator getBinaryDocsWithField(FieldInfo fieldInfo) throws IOException {+ private DocValuesIterator getBinaryDocsWithField(FieldInfo fieldInfo) throws IOException {final OneField field = fields.get(fieldInfo.name);final IndexInput in = data.clone();final BytesRefBuilder scratch = new BytesRefBuilder();final DecimalFormat decoder = new DecimalFormat(field.pattern, new DecimalFormatSymbols(Locale.ROOT));- return new DocIdSetIterator() {+ return new DocValuesIterator() {int doc = -1;@@ -371,6 +394,26 @@ class SimpleTextDocValuesReader extends DocValuesProducer {}return doc = NO_MORE_DOCS;}++ @Override+ boolean advanceExact(int target) throws IOException {+ this.doc = target;+ in.seek(field.dataStartFilePointer + (9+field.pattern.length() + field.maxLength+2)*target);+ SimpleTextUtil.readLine(in, scratch);+ assert StringHelper.startsWith(scratch.get(), LENGTH);+ int len;+ try {+ len = decoder.parse(new String(scratch.bytes(), LENGTH.length, scratch.length() - LENGTH.length, StandardCharsets.UTF_8)).intValue();+ } catch (ParseException pe) {+ throw new CorruptIndexException("failed to parse int length", in, pe);+ }+ // skip past bytes+ byte bytes[] = new byte[len];+ in.readBytes(bytes, 0, len);+ SimpleTextUtil.readLine(in, scratch); // newline+ SimpleTextUtil.readLine(in, scratch); // 'T' or 'F'+ return scratch.byteAt(0) == (byte) 'T';+ }};}@@ -424,7 +467,20 @@ class SimpleTextDocValuesReader extends DocValuesProducer {}return doc = NO_MORE_DOCS;}-++ @Override+ public boolean advanceExact(int target) throws IOException {+ this.doc = target;+ in.seek(field.dataStartFilePointer + field.numValues * (9 + field.pattern.length() + field.maxLength) + target * (1 + field.ordPattern.length()));+ SimpleTextUtil.readLine(in, scratch);+ try {+ ord = (int) ordDecoder.parse(scratch.get().utf8ToString()).longValue()-1;+ } catch (ParseException pe) {+ throw new CorruptIndexException("failed to parse ord", in, pe);+ }+ return ord >= 0;+ }+@Overridepublic int ordValue() {return ord;@@ -488,6 +544,15 @@ class SimpleTextDocValuesReader extends DocValuesProducer {return doc;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ if (binary.advanceExact(target)) {+ setCurrentDoc();+ return true;+ }+ return false;+ }+long values[];int index;@@ -569,6 +634,20 @@ class SimpleTextDocValuesReader extends DocValuesProducer {return doc = NO_MORE_DOCS;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ in.seek(field.dataStartFilePointer + field.numValues * (9 + field.pattern.length() + field.maxLength) + target * (1 + field.ordPattern.length()));+ SimpleTextUtil.readLine(in, scratch);+ String ordList = scratch.get().utf8ToString().trim();+ doc = target;+ if (ordList.isEmpty() == false) {+ currentOrds = ordList.split(",");+ currentIndex = 0;+ return true;+ }+ return false;+ }+@Overridepublic long nextOrd() throws IOException {if (currentIndex == currentOrds.length) {diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesWriter.javaindex 2649ae6b4a9..8c6bdde30b3 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesWriter.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesWriter.java@@ -342,6 +342,15 @@ class SimpleTextDocValuesWriter extends DocValuesConsumer {return doc;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ if (values.advanceExact(target)) {+ setCurrentDoc();+ return true;+ }+ return false;+ }+final StringBuilder builder = new StringBuilder();BytesRef binaryValue;diff --git a/lucene/core/src/java/org/apache/lucene/codecs/DocValuesConsumer.java b/lucene/core/src/java/org/apache/lucene/codecs/DocValuesConsumer.javaindex e04d5b9962f..e61724fbab1 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/DocValuesConsumer.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/DocValuesConsumer.java@@ -227,6 +227,11 @@ public abstract class DocValuesConsumer implements Closeable {throw new UnsupportedOperationException();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ throw new UnsupportedOperationException();+ }+@Overridepublic long cost() {return finalCost;@@ -319,6 +324,11 @@ public abstract class DocValuesConsumer implements Closeable {throw new UnsupportedOperationException();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ throw new UnsupportedOperationException();+ }+@Overridepublic long cost() {return finalCost;@@ -416,6 +426,11 @@ public abstract class DocValuesConsumer implements Closeable {throw new UnsupportedOperationException();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ throw new UnsupportedOperationException();+ }+@Overridepublic int docValueCount() {return currentSub.values.docValueCount();@@ -574,6 +589,11 @@ public abstract class DocValuesConsumer implements Closeable {throw new UnsupportedOperationException();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ throw new UnsupportedOperationException();+ }+@Overridepublic long cost() {return finalCost;@@ -731,6 +751,11 @@ public abstract class DocValuesConsumer implements Closeable {throw new UnsupportedOperationException();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ throw new UnsupportedOperationException();+ }+@Overridepublic long nextOrd() throws IOException {long subOrd = currentSub.values.nextOrd();diff --git a/lucene/core/src/java/org/apache/lucene/codecs/NormsConsumer.java b/lucene/core/src/java/org/apache/lucene/codecs/NormsConsumer.javaindex 3a6ce2274d5..51abb69b91e 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/NormsConsumer.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/NormsConsumer.java@@ -157,6 +157,11 @@ public abstract class NormsConsumer implements Closeable {throw new UnsupportedOperationException();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ throw new UnsupportedOperationException();+ }+@Overridepublic long cost() {return 0;diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene70/IndexedDISI.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene70/IndexedDISI.javaindex 3ea3141ea17..24eaf7a9092 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene70/IndexedDISI.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene70/IndexedDISI.java@@ -112,6 +112,9 @@ final class IndexedDISI extends DocIdSetIterator {private int doc = -1;private int index = -1;+ // SPARSE variables+ boolean exists;+// DENSE variablesprivate long word;private int wordIndex = -1;@@ -129,7 +132,7 @@ final class IndexedDISI extends DocIdSetIterator {@Overridepublic int advance(int target) throws IOException {final int targetBlock = target & 0xFFFF0000;- if (block != targetBlock) {+ if (block < targetBlock) {advanceBlock(targetBlock);}if (block == targetBlock) {@@ -138,7 +141,19 @@ final class IndexedDISI extends DocIdSetIterator {}readBlockHeader();}- return doc = method.readFirstDoc(this);+ boolean found = method.advanceWithinBlock(this, block);+ assert found;+ return doc;+ }++ public boolean advanceExact(int target) throws IOException {+ final int targetBlock = target & 0xFFFF0000;+ if (block < targetBlock) {+ advanceBlock(targetBlock);+ }+ boolean found = block == targetBlock && method.advanceExactWithinBlock(this, target);+ this.doc = target;+ return found;}private void advanceBlock(int targetBlock) throws IOException {@@ -185,11 +200,6 @@ final class IndexedDISI extends DocIdSetIterator {enum Method {SPARSE {- @Override- int readFirstDoc(IndexedDISI disi) throws IOException {- disi.index++;- return disi.block | Short.toUnsignedInt(disi.slice.readShort());- }@Overrideboolean advanceWithinBlock(IndexedDISI disi, int target) throws IOException {final int targetInBlock = target & 0xFFFF;@@ -199,23 +209,37 @@ final class IndexedDISI extends DocIdSetIterator {disi.index++;if (doc >= targetInBlock) {disi.doc = disi.block | doc;+ disi.exists = true;return true;}}return false;}- },- DENSE {@Override- int readFirstDoc(IndexedDISI disi) throws IOException {- do {- disi.word = disi.slice.readLong();- disi.wordIndex++;- } while (disi.word == 0L);- disi.index = disi.numberOfOnes;- disi.numberOfOnes += Long.bitCount(disi.word);- return disi.block | (disi.wordIndex << 6) | Long.numberOfTrailingZeros(disi.word);+ boolean advanceExactWithinBlock(IndexedDISI disi, int target) throws IOException {+ final int targetInBlock = target & 0xFFFF;+ // TODO: binary search+ if (target == disi.doc) {+ return disi.exists;+ }+ for (; disi.index < disi.nextBlockIndex;) {+ int doc = Short.toUnsignedInt(disi.slice.readShort());+ disi.index++;+ if (doc >= targetInBlock) {+ if (doc != targetInBlock) {+ disi.index--;+ disi.slice.seek(disi.slice.getFilePointer() - Short.BYTES);+ break;+ }+ disi.exists = true;+ return true;+ }+ }+ disi.exists = false;+ return false;}+ },+ DENSE {@Overrideboolean advanceWithinBlock(IndexedDISI disi, int target) throws IOException {final int targetInBlock = target & 0xFFFF;@@ -244,26 +268,42 @@ final class IndexedDISI extends DocIdSetIterator {}return false;}- },- ALL {@Override- int readFirstDoc(IndexedDISI disi) {- return disi.block;+ boolean advanceExactWithinBlock(IndexedDISI disi, int target) throws IOException {+ final int targetInBlock = target & 0xFFFF;+ final int targetWordIndex = targetInBlock >>> 6;+ for (int i = disi.wordIndex + 1; i <= targetWordIndex; ++i) {+ disi.word = disi.slice.readLong();+ disi.numberOfOnes += Long.bitCount(disi.word);+ }+ disi.wordIndex = targetWordIndex;++ long leftBits = disi.word >>> target;+ disi.index = disi.numberOfOnes - Long.bitCount(leftBits);+ return (leftBits & 1L) != 0;}+ },+ ALL {@Overrideboolean advanceWithinBlock(IndexedDISI disi, int target) throws IOException {disi.doc = target;disi.index = target - disi.gap;return true;}+ @Override+ boolean advanceExactWithinBlock(IndexedDISI disi, int target) throws IOException {+ disi.index = target - disi.gap;+ return true;+ }};- /** Read the first document of the current block. */- abstract int readFirstDoc(IndexedDISI disi) throws IOException;-/** Advance to the first doc from the block that is equal to or greater than {@code target}.* Return true if there is such a doc and false otherwise. */abstract boolean advanceWithinBlock(IndexedDISI disi, int target) throws IOException;++ /** Advance the iterator exactly to the position corresponding to the given {@code target}+ * and return whether this document exists. */+ abstract boolean advanceExactWithinBlock(IndexedDISI disi, int target) throws IOException;}}diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene70/Lucene70DocValuesProducer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene70/Lucene70DocValuesProducer.javaindex 637c8eef11f..19815bab218 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene70/Lucene70DocValuesProducer.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene70/Lucene70DocValuesProducer.java@@ -374,6 +374,12 @@ final class Lucene70DocValuesProducer extends DocValuesProducer implements Closereturn doc = target;}+ @Override+ public boolean advanceExact(int target) {+ doc = target;+ return true;+ }+@Overridepublic long cost() {return maxDoc;@@ -391,6 +397,11 @@ final class Lucene70DocValuesProducer extends DocValuesProducer implements Closereturn disi.advance(target);}+ @Override+ public boolean advanceExact(int target) throws IOException {+ return disi.advanceExact(target);+ }+@Overridepublic int nextDoc() throws IOException {return disi.nextDoc();@@ -520,6 +531,12 @@ final class Lucene70DocValuesProducer extends DocValuesProducer implements Closereturn doc = target;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ doc = target;+ return true;+ }+@Overridepublic BytesRef binaryValue() throws IOException {return bytesRefs.get(doc);@@ -550,6 +567,11 @@ final class Lucene70DocValuesProducer extends DocValuesProducer implements Closereturn disi.advance(target);}+ @Override+ public boolean advanceExact(int target) throws IOException {+ return disi.advanceExact(target);+ }+@Overridepublic BytesRef binaryValue() throws IOException {return bytesRefs.get(disi.index());@@ -615,6 +637,12 @@ final class Lucene70DocValuesProducer extends DocValuesProducer implements Closereturn doc = target;}+ @Override+ public boolean advanceExact(int target) {+ doc = target;+ return true;+ }+@Overridepublic int ordValue() {return (int) ords.get(doc);@@ -645,6 +673,11 @@ final class Lucene70DocValuesProducer extends DocValuesProducer implements Closereturn disi.advance(target);}+ @Override+ public boolean advanceExact(int target) throws IOException {+ return disi.advanceExact(target);+ }+@Overridepublic int ordValue() {return (int) ords.get(disi.index());@@ -959,6 +992,15 @@ final class Lucene70DocValuesProducer extends DocValuesProducer implements Closereturn doc = target;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ start = addresses.get(target);+ end = addresses.get(target + 1L);+ count = (int) (end - start);+ doc = target;+ return true;+ }+@Overridepublic long nextValue() throws IOException {return values.get(start++);@@ -1000,6 +1042,12 @@ final class Lucene70DocValuesProducer extends DocValuesProducer implements Closereturn disi.advance(target);}+ @Override+ public boolean advanceExact(int target) throws IOException {+ set = false;+ return disi.advanceExact(target);+ }+@Overridepublic long nextValue() throws IOException {set();@@ -1072,6 +1120,14 @@ final class Lucene70DocValuesProducer extends DocValuesProducer implements Closereturn doc = target;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ start = addresses.get(target);+ end = addresses.get(target + 1L);+ doc = target;+ return true;+ }+@Overridepublic long nextOrd() throws IOException {if (start == end) {@@ -1112,6 +1168,12 @@ final class Lucene70DocValuesProducer extends DocValuesProducer implements Closereturn disi.advance(target);}+ @Override+ public boolean advanceExact(int target) throws IOException {+ set = false;+ return disi.advanceExact(target);+ }+@Overridepublic long nextOrd() throws IOException {if (set == false) {diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene70/Lucene70NormsProducer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene70/Lucene70NormsProducer.javaindex e3f6f79552d..c97f1c389ba 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene70/Lucene70NormsProducer.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene70/Lucene70NormsProducer.java@@ -159,6 +159,12 @@ final class Lucene70NormsProducer extends NormsProducer {return doc = target;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ this.doc = target;+ return true;+ }+@Overridepublic long cost() {return maxDoc;@@ -176,6 +182,11 @@ final class Lucene70NormsProducer extends NormsProducer {return disi.advance(target);}+ @Override+ public boolean advanceExact(int target) throws IOException {+ return disi.advanceExact(target);+ }+@Overridepublic int nextDoc() throws IOException {return disi.nextDoc();diff --git a/lucene/core/src/java/org/apache/lucene/index/BinaryDocValues.java b/lucene/core/src/java/org/apache/lucene/index/BinaryDocValues.javaindex 66397e4475f..6d23cf15ddb 100644--- a/lucene/core/src/java/org/apache/lucene/index/BinaryDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/BinaryDocValues.java@@ -19,13 +19,12 @@ package org.apache.lucene.index;import java.io.IOException;-import org.apache.lucene.search.DocIdSetIterator;import org.apache.lucene.util.BytesRef;/*** A per-document numeric value.*/-public abstract class BinaryDocValues extends DocIdSetIterator {+public abstract class BinaryDocValues extends DocValuesIterator {/** Sole constructor. (For invocation by subclass* constructors, typically implicit.) */@@ -33,6 +32,8 @@ public abstract class BinaryDocValues extends DocIdSetIterator {/*** Returns the binary value for the current document ID.+ * It is illegal to call this method after {@link #advanceExact(int)}+ * returned {@code false}.* @return binary value*/public abstract BytesRef binaryValue() throws IOException;diff --git a/lucene/core/src/java/org/apache/lucene/index/BinaryDocValuesWriter.java b/lucene/core/src/java/org/apache/lucene/index/BinaryDocValuesWriter.javaindex ff2e67c1a38..9611a03c265 100644--- a/lucene/core/src/java/org/apache/lucene/index/BinaryDocValuesWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/BinaryDocValuesWriter.java@@ -153,6 +153,11 @@ class BinaryDocValuesWriter extends DocValuesWriter {throw new UnsupportedOperationException();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ throw new UnsupportedOperationException();+ }+@Overridepublic long cost() {return docsWithField.cost();diff --git a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.javaindex aec75375d7b..f3d35625b86 100644--- a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java+++ b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java@@ -2062,13 +2062,83 @@ public final class CheckIndex implements Closeable {return status;}+ @FunctionalInterface+ private static interface DocValuesIteratorSupplier {+ DocValuesIterator get(FieldInfo fi) throws IOException;+ }++ private static void checkDVIterator(FieldInfo fi, int maxDoc, DocValuesIteratorSupplier producer) throws IOException {+ String field = fi.name;++ // Check advance+ DocValuesIterator it1 = producer.get(fi);+ DocValuesIterator it2 = producer.get(fi);+ int i = 0;+ for (int doc = it1.nextDoc(); ; doc = it1.nextDoc()) {++ if (i++ % 10 == 1) {+ int doc2 = it2.advance(doc - 1);+ if (doc2 < doc - 1) {+ throw new RuntimeException("dv iterator field=" + field + ": doc=" + (doc-1) + " went backwords (got: " + doc2 + ")");+ }+ if (doc2 == doc - 1) {+ doc2 = it2.nextDoc();+ }+ if (doc2 != doc) {+ throw new RuntimeException("dv iterator field=" + field + ": doc=" + doc + " was not found through advance() (got: " + doc2 + ")");+ }+ if (it2.docID() != doc) {+ throw new RuntimeException("dv iterator field=" + field + ": doc=" + doc + " reports wrong doc ID (got: " + it2.docID() + ")");+ }+ }++ if (doc == NO_MORE_DOCS) {+ break;+ }+ }++ // Check advanceExact+ it1 = producer.get(fi);+ it2 = producer.get(fi);+ i = 0;+ int lastDoc = -1;+ for (int doc = it1.nextDoc(); doc != NO_MORE_DOCS ; doc = it1.nextDoc()) {++ if (i++ % 13 == 1) {+ boolean found = it2.advanceExact(doc - 1);+ if ((doc - 1 == lastDoc) != found) {+ throw new RuntimeException("dv iterator field=" + field + ": doc=" + (doc-1) + " disagrees about whether document exists (got: " + found + ")");+ }+ if (it2.docID() != doc - 1) {+ throw new RuntimeException("dv iterator field=" + field + ": doc=" + (doc-1) + " reports wrong doc ID (got: " + it2.docID() + ")");+ }++ boolean found2 = it2.advanceExact(doc - 1);+ if (found != found2) {+ throw new RuntimeException("dv iterator field=" + field + ": doc=" + (doc-1) + " has unstable advanceExact");+ }++ if (i % 1 == 0) {+ int doc2 = it2.nextDoc();+ if (doc != doc2) {+ throw new RuntimeException("dv iterator field=" + field + ": doc=" + doc + " was not found through advance() (got: " + doc2 + ")");+ }+ if (it2.docID() != doc) {+ throw new RuntimeException("dv iterator field=" + field + ": doc=" + doc + " reports wrong doc ID (got: " + it2.docID() + ")");+ }+ }+ }++ lastDoc = doc;+ }+ }+private static void checkBinaryDocValues(String fieldName, int maxDoc, BinaryDocValues bdv) throws IOException {int doc;if (bdv.docID() != -1) {throw new RuntimeException("binary dv iterator for field: " + fieldName + " should start at docID=-1, but got " + bdv.docID());}// TODO: we could add stats to DVs, e.g. total doc count w/ a value for this field- // TODO: check advance toowhile ((doc = bdv.nextDoc()) != NO_MORE_DOCS) {BytesRef value = bdv.binaryValue();value.isValid();@@ -2083,7 +2153,6 @@ public final class CheckIndex implements Closeable {FixedBitSet seenOrds = new FixedBitSet(dv.getValueCount());int maxOrd2 = -1;int docID;- // TODO: check advance toowhile ((docID = dv.nextDoc()) != NO_MORE_DOCS) {int ord = dv.ordValue();if (ord == -1) {@@ -2119,7 +2188,6 @@ public final class CheckIndex implements Closeable {LongBitSet seenOrds = new LongBitSet(dv.getValueCount());long maxOrd2 = -1;int docID;- // TODO: check advance toowhile ((docID = dv.nextDoc()) != NO_MORE_DOCS) {long lastOrd = -1;long ord;@@ -2164,7 +2232,6 @@ public final class CheckIndex implements Closeable {if (ndv.docID() != -1) {throw new RuntimeException("dv iterator for field: " + fieldName + " should start at docID=-1, but got " + ndv.docID());}- // TODO: check advance toowhile (true) {int docID = ndv.nextDoc();if (docID == NO_MORE_DOCS) {@@ -2191,7 +2258,6 @@ public final class CheckIndex implements Closeable {throw new RuntimeException("dv iterator for field: " + fieldName + " should start at docID=-1, but got " + ndv.docID());}// TODO: we could add stats to DVs, e.g. total doc count w/ a value for this field- // TODO: check advance toowhile ((doc = ndv.nextDoc()) != NO_MORE_DOCS) {ndv.longValue();}@@ -2201,23 +2267,28 @@ public final class CheckIndex implements Closeable {switch(fi.getDocValuesType()) {case SORTED:status.totalSortedFields++;+ checkDVIterator(fi, maxDoc, dvReader::getSorted);checkBinaryDocValues(fi.name, maxDoc, dvReader.getSorted(fi));checkSortedDocValues(fi.name, maxDoc, dvReader.getSorted(fi));break;case SORTED_NUMERIC:status.totalSortedNumericFields++;+ checkDVIterator(fi, maxDoc, dvReader::getSortedNumeric);checkSortedNumericDocValues(fi.name, maxDoc, dvReader.getSortedNumeric(fi));break;case SORTED_SET:status.totalSortedSetFields++;+ checkDVIterator(fi, maxDoc, dvReader::getSortedSet);checkSortedSetDocValues(fi.name, maxDoc, dvReader.getSortedSet(fi));break;case BINARY:status.totalBinaryFields++;+ checkDVIterator(fi, maxDoc, dvReader::getBinary);checkBinaryDocValues(fi.name, maxDoc, dvReader.getBinary(fi));break;case NUMERIC:status.totalNumericFields++;+ checkDVIterator(fi, maxDoc, dvReader::getNumeric);checkNumericDocValues(fi.name, dvReader.getNumeric(fi));break;default:diff --git a/lucene/core/src/java/org/apache/lucene/index/DocValues.java b/lucene/core/src/java/org/apache/lucene/index/DocValues.javaindex 3377f45f0fb..b25d484b9b6 100644--- a/lucene/core/src/java/org/apache/lucene/index/DocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/DocValues.java@@ -35,26 +35,27 @@ public final class DocValues {*/public static final BinaryDocValues emptyBinary() {return new BinaryDocValues() {- private boolean exhausted = false;+ private int doc = -1;@Overridepublic int advance(int target) {- assert exhausted == false;- assert target >= 0;- exhausted = true;- return NO_MORE_DOCS;+ return doc = NO_MORE_DOCS;+ }++ @Override+ public boolean advanceExact(int target) throws IOException {+ doc = target;+ return true;}@Overridepublic int docID() {- return exhausted ? NO_MORE_DOCS : -1;+ return doc;}@Overridepublic int nextDoc() {- assert exhausted == false;- exhausted = true;- return NO_MORE_DOCS;+ return doc = NO_MORE_DOCS;}@Override@@ -75,26 +76,27 @@ public final class DocValues {*/public static final NumericDocValues emptyNumeric() {return new NumericDocValues() {- private boolean exhausted = false;+ private int doc = -1;@Overridepublic int advance(int target) {- assert exhausted == false;- assert target >= 0;- exhausted = true;- return NO_MORE_DOCS;+ return doc = NO_MORE_DOCS;+ }++ @Override+ public boolean advanceExact(int target) throws IOException {+ doc = target;+ return false;}@Overridepublic int docID() {- return exhausted ? NO_MORE_DOCS : -1;+ return doc;}@Overridepublic int nextDoc() {- assert exhausted == false;- exhausted = true;- return NO_MORE_DOCS;+ return doc = NO_MORE_DOCS;}@Override@@ -140,26 +142,27 @@ public final class DocValues {final BytesRef empty = new BytesRef();return new SortedDocValues() {- private boolean exhausted = false;+ private int doc = -1;@Overridepublic int advance(int target) {- assert exhausted == false;- assert target >= 0;- exhausted = true;- return NO_MORE_DOCS;+ return doc = NO_MORE_DOCS;+ }++ @Override+ public boolean advanceExact(int target) throws IOException {+ doc = target;+ return false;}@Overridepublic int docID() {- return exhausted ? NO_MORE_DOCS : -1;+ return doc;}@Overridepublic int nextDoc() {- assert exhausted == false;- exhausted = true;- return NO_MORE_DOCS;+ return doc = NO_MORE_DOCS;}@Override@@ -191,26 +194,27 @@ public final class DocValues {public static final SortedNumericDocValues emptySortedNumeric(int maxDoc) {return new SortedNumericDocValues() {- private boolean exhausted = false;+ private int doc = -1;@Overridepublic int advance(int target) {- assert exhausted == false;- assert target >= 0;- exhausted = true;- return NO_MORE_DOCS;+ return doc = NO_MORE_DOCS;+ }++ @Override+ public boolean advanceExact(int target) throws IOException {+ doc = target;+ return false;}@Overridepublic int docID() {- return exhausted ? NO_MORE_DOCS : -1;+ return doc;}@Overridepublic int nextDoc() {- assert exhausted == false;- exhausted = true;- return NO_MORE_DOCS;+ return doc = NO_MORE_DOCS;}@Override@@ -237,26 +241,27 @@ public final class DocValues {final BytesRef empty = new BytesRef();return new SortedSetDocValues() {- private boolean exhausted = false;+ private int doc = -1;@Overridepublic int advance(int target) {- assert exhausted == false;- assert target >= 0;- exhausted = true;- return NO_MORE_DOCS;+ return doc = NO_MORE_DOCS;+ }++ @Override+ public boolean advanceExact(int target) throws IOException {+ doc = target;+ return false;}@Overridepublic int docID() {- return exhausted ? NO_MORE_DOCS : -1;+ return doc;}@Overridepublic int nextDoc() {- assert exhausted == false;- exhausted = true;- return NO_MORE_DOCS;+ return doc = NO_MORE_DOCS;}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/index/DocValuesIterator.java b/lucene/core/src/java/org/apache/lucene/index/DocValuesIterator.javanew file mode 100644index 00000000000..d53e26a3e4b--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/index/DocValuesIterator.java@@ -0,0 +1,33 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.index;++import java.io.IOException;++import org.apache.lucene.search.DocIdSetIterator;++abstract class DocValuesIterator extends DocIdSetIterator {++ /** Advance the iterator to exactly {@code target} and return whether+ * {@code target} has a value.+ * {@code target} must be greater than or equal to the current+ * {@link #docID() doc ID} and must be a valid doc ID, ie. ≥ 0 and+ * < {@code maxDoc}.+ * After this method returns, {@link #docID()} retuns {@code target}. */+ public abstract boolean advanceExact(int target) throws IOException;++}diff --git a/lucene/core/src/java/org/apache/lucene/index/FilterBinaryDocValues.java b/lucene/core/src/java/org/apache/lucene/index/FilterBinaryDocValues.javaindex 66c43234858..650ad0406f5 100644--- a/lucene/core/src/java/org/apache/lucene/index/FilterBinaryDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/FilterBinaryDocValues.java@@ -49,6 +49,11 @@ public abstract class FilterBinaryDocValues extends BinaryDocValues {return in.advance(target);}+ @Override+ public boolean advanceExact(int target) throws IOException {+ return in.advanceExact(target);+ }+@Overridepublic long cost() {return in.cost();diff --git a/lucene/core/src/java/org/apache/lucene/index/FilterNumericDocValues.java b/lucene/core/src/java/org/apache/lucene/index/FilterNumericDocValues.javaindex 0058fa6dce3..bd00cf2b030 100644--- a/lucene/core/src/java/org/apache/lucene/index/FilterNumericDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/FilterNumericDocValues.java@@ -47,6 +47,11 @@ public abstract class FilterNumericDocValues extends NumericDocValues {return in.advance(target);}+ @Override+ public boolean advanceExact(int target) throws IOException {+ return in.advanceExact(target);+ }+@Overridepublic long cost() {return in.cost();diff --git a/lucene/core/src/java/org/apache/lucene/index/LegacyBinaryDocValuesWrapper.java b/lucene/core/src/java/org/apache/lucene/index/LegacyBinaryDocValuesWrapper.javaindex 13bc2075b61..919b1ff9cf8 100644--- a/lucene/core/src/java/org/apache/lucene/index/LegacyBinaryDocValuesWrapper.java+++ b/lucene/core/src/java/org/apache/lucene/index/LegacyBinaryDocValuesWrapper.java@@ -17,6 +17,8 @@package org.apache.lucene.index;+import java.io.IOException;+import org.apache.lucene.util.Bits;import org.apache.lucene.util.BytesRef;@@ -70,6 +72,12 @@ public final class LegacyBinaryDocValuesWrapper extends BinaryDocValues {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return docsWithField.get(target);+ }+@Overridepublic long cost() {return 0;diff --git a/lucene/core/src/java/org/apache/lucene/index/LegacyNumericDocValuesWrapper.java b/lucene/core/src/java/org/apache/lucene/index/LegacyNumericDocValuesWrapper.javaindex a72efe8848b..aaccc05c59d 100644--- a/lucene/core/src/java/org/apache/lucene/index/LegacyNumericDocValuesWrapper.java+++ b/lucene/core/src/java/org/apache/lucene/index/LegacyNumericDocValuesWrapper.java@@ -17,6 +17,8 @@package org.apache.lucene.index;+import java.io.IOException;+import org.apache.lucene.util.Bits;/**@@ -69,6 +71,13 @@ public final class LegacyNumericDocValuesWrapper extends NumericDocValues {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ value = values.get(docID);+ return value != 0 || docsWithField.get(docID);+ }+@Overridepublic long cost() {// TODOdiff --git a/lucene/core/src/java/org/apache/lucene/index/LegacySortedDocValuesWrapper.java b/lucene/core/src/java/org/apache/lucene/index/LegacySortedDocValuesWrapper.javaindex d8ef2f42495..ecc114b1f4a 100644--- a/lucene/core/src/java/org/apache/lucene/index/LegacySortedDocValuesWrapper.java+++ b/lucene/core/src/java/org/apache/lucene/index/LegacySortedDocValuesWrapper.java@@ -17,6 +17,8 @@package org.apache.lucene.index;+import java.io.IOException;+import org.apache.lucene.util.BytesRef;/**@@ -70,6 +72,13 @@ public final class LegacySortedDocValuesWrapper extends SortedDocValues {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ ord = values.getOrd(docID);+ return ord != -1;+ }+@Overridepublic long cost() {return 0;diff --git a/lucene/core/src/java/org/apache/lucene/index/LegacySortedNumericDocValuesWrapper.java b/lucene/core/src/java/org/apache/lucene/index/LegacySortedNumericDocValuesWrapper.javaindex bf3c6cd6ac0..cfb61e3c2d1 100644--- a/lucene/core/src/java/org/apache/lucene/index/LegacySortedNumericDocValuesWrapper.java+++ b/lucene/core/src/java/org/apache/lucene/index/LegacySortedNumericDocValuesWrapper.java@@ -17,6 +17,8 @@package org.apache.lucene.index;+import java.io.IOException;+/*** Wraps a {@link LegacySortedNumericDocValues} into a {@link SortedNumericDocValues}.*@@ -71,6 +73,13 @@ public final class LegacySortedNumericDocValuesWrapper extends SortedNumericDocVreturn docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ values.setDocument(docID);+ return values.count() != 0;+ }+@Overridepublic long cost() {return 0;diff --git a/lucene/core/src/java/org/apache/lucene/index/LegacySortedSetDocValuesWrapper.java b/lucene/core/src/java/org/apache/lucene/index/LegacySortedSetDocValuesWrapper.javaindex 45d12d22988..0e96e020949 100644--- a/lucene/core/src/java/org/apache/lucene/index/LegacySortedSetDocValuesWrapper.java+++ b/lucene/core/src/java/org/apache/lucene/index/LegacySortedSetDocValuesWrapper.java@@ -17,6 +17,8 @@package org.apache.lucene.index;+import java.io.IOException;+import org.apache.lucene.util.BytesRef;/**@@ -71,6 +73,14 @@ public final class LegacySortedSetDocValuesWrapper extends SortedSetDocValues {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ values.setDocument(docID);+ ord = values.nextOrd();+ return ord != NO_MORE_ORDS;+ }+@Overridepublic long cost() {return 0;diff --git a/lucene/core/src/java/org/apache/lucene/index/MultiDocValues.java b/lucene/core/src/java/org/apache/lucene/index/MultiDocValues.javaindex aeb49c57491..51d684d3a27 100644--- a/lucene/core/src/java/org/apache/lucene/index/MultiDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/MultiDocValues.java@@ -138,6 +138,27 @@ public class MultiDocValues {}}+ @Override+ public boolean advanceExact(int targetDocID) throws IOException {+ if (targetDocID <= docID) {+ throw new IllegalArgumentException("can only advance beyond current document: on docID=" + docID + " but targetDocID=" + targetDocID);+ }+ int readerIndex = ReaderUtil.subIndex(targetDocID, leaves);+ if (readerIndex >= nextLeaf) {+ if (readerIndex == leaves.size()) {+ throw new IllegalArgumentException("Out of range: " + targetDocID);+ }+ currentLeaf = leaves.get(readerIndex);+ currentValues = currentLeaf.reader().getNormValues(field);+ nextLeaf = readerIndex+1;+ }+ docID = targetDocID;+ if (currentValues == null) {+ return false;+ }+ return currentValues.advanceExact(targetDocID - currentLeaf.docBase);+ }+@Overridepublic long longValue() throws IOException {return currentValues.longValue();@@ -243,6 +264,26 @@ public class MultiDocValues {}}+ @Override+ public boolean advanceExact(int targetDocID) throws IOException {+ if (targetDocID <= docID) {+ throw new IllegalArgumentException("can only advance beyond current document: on docID=" + docID + " but targetDocID=" + targetDocID);+ }+ int readerIndex = ReaderUtil.subIndex(targetDocID, leaves);+ if (readerIndex >= nextLeaf) {+ if (readerIndex == leaves.size()) {+ throw new IllegalArgumentException("Out of range: " + targetDocID);+ }+ currentLeaf = leaves.get(readerIndex);+ currentValues = currentLeaf.reader().getNumericDocValues(field);+ nextLeaf = readerIndex+1;+ }+ docID = targetDocID;+ if (currentValues == null) {+ return false;+ }+ return currentValues.advanceExact(targetDocID - currentLeaf.docBase);+ }@Overridepublic long longValue() throws IOException {return currentValues.longValue();@@ -347,6 +388,27 @@ public class MultiDocValues {}}+ @Override+ public boolean advanceExact(int targetDocID) throws IOException {+ if (targetDocID <= docID) {+ throw new IllegalArgumentException("can only advance beyond current document: on docID=" + docID + " but targetDocID=" + targetDocID);+ }+ int readerIndex = ReaderUtil.subIndex(targetDocID, leaves);+ if (readerIndex >= nextLeaf) {+ if (readerIndex == leaves.size()) {+ throw new IllegalArgumentException("Out of range: " + targetDocID);+ }+ currentLeaf = leaves.get(readerIndex);+ currentValues = currentLeaf.reader().getBinaryDocValues(field);+ nextLeaf = readerIndex+1;+ }+ docID = targetDocID;+ if (currentValues == null) {+ return false;+ }+ return currentValues.advanceExact(targetDocID - currentLeaf.docBase);+ }+@Overridepublic BytesRef binaryValue() throws IOException {return currentValues.binaryValue();@@ -461,6 +523,27 @@ public class MultiDocValues {}}+ @Override+ public boolean advanceExact(int targetDocID) throws IOException {+ if (targetDocID <= docID) {+ throw new IllegalArgumentException("can only advance beyond current document: on docID=" + docID + " but targetDocID=" + targetDocID);+ }+ int readerIndex = ReaderUtil.subIndex(targetDocID, leaves);+ if (readerIndex >= nextLeaf) {+ if (readerIndex == leaves.size()) {+ throw new IllegalArgumentException("Out of range: " + targetDocID);+ }+ currentLeaf = leaves.get(readerIndex);+ currentValues = values[readerIndex];+ nextLeaf = readerIndex+1;+ }+ docID = targetDocID;+ if (currentValues == null) {+ return false;+ }+ return currentValues.advanceExact(targetDocID - currentLeaf.docBase);+ }+@Overridepublic long cost() {return finalTotalCost;@@ -922,6 +1005,27 @@ public class MultiDocValues {}}+ @Override+ public boolean advanceExact(int targetDocID) throws IOException {+ if (targetDocID <= docID) {+ throw new IllegalArgumentException("can only advance beyond current document: on docID=" + docID + " but targetDocID=" + targetDocID);+ }+ int readerIndex = ReaderUtil.subIndex(targetDocID, docStarts);+ if (readerIndex >= nextLeaf) {+ if (readerIndex == values.length) {+ throw new IllegalArgumentException("Out of range: " + targetDocID);+ }+ currentDocStart = docStarts[readerIndex];+ currentValues = values[readerIndex];+ nextLeaf = readerIndex+1;+ }+ docID = targetDocID;+ if (currentValues == null) {+ return false;+ }+ return currentValues.advanceExact(targetDocID - currentDocStart);+ }+@Overridepublic int ordValue() {return (int) mapping.getGlobalOrds(nextLeaf-1).get(currentValues.ordValue());@@ -1028,6 +1132,27 @@ public class MultiDocValues {}}+ @Override+ public boolean advanceExact(int targetDocID) throws IOException {+ if (targetDocID < docID) {+ throw new IllegalArgumentException("can only advance beyond current document: on docID=" + docID + " but targetDocID=" + targetDocID);+ }+ int readerIndex = ReaderUtil.subIndex(targetDocID, docStarts);+ if (readerIndex >= nextLeaf) {+ if (readerIndex == values.length) {+ throw new IllegalArgumentException("Out of range: " + targetDocID);+ }+ currentDocStart = docStarts[readerIndex];+ currentValues = values[readerIndex];+ nextLeaf = readerIndex+1;+ }+ docID = targetDocID;+ if (currentValues == null) {+ return false;+ }+ return currentValues.advanceExact(targetDocID - currentDocStart);+ }+@Overridepublic long nextOrd() throws IOException {long segmentOrd = currentValues.nextOrd();diff --git a/lucene/core/src/java/org/apache/lucene/index/NormValuesWriter.java b/lucene/core/src/java/org/apache/lucene/index/NormValuesWriter.javaindex 46b8c1ceb15..b0d05e4859f 100644--- a/lucene/core/src/java/org/apache/lucene/index/NormValuesWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/NormValuesWriter.java@@ -132,6 +132,11 @@ class NormValuesWriter {throw new UnsupportedOperationException();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ throw new UnsupportedOperationException();+ }+@Overridepublic long cost() {return docsWithField.cost();diff --git a/lucene/core/src/java/org/apache/lucene/index/NumericDocValues.java b/lucene/core/src/java/org/apache/lucene/index/NumericDocValues.javaindex 5ae2e476fa5..29b99186386 100644--- a/lucene/core/src/java/org/apache/lucene/index/NumericDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/NumericDocValues.java@@ -19,12 +19,10 @@ package org.apache.lucene.index;import java.io.IOException;-import org.apache.lucene.search.DocIdSetIterator;-/*** A per-document numeric value.*/-public abstract class NumericDocValues extends DocIdSetIterator {+public abstract class NumericDocValues extends DocValuesIterator {/** Sole constructor. (For invocation by subclass* constructors, typically implicit.) */@@ -32,7 +30,10 @@ public abstract class NumericDocValues extends DocIdSetIterator {/*** Returns the numeric value for the current document ID.+ * It is illegal to call this method after {@link #advanceExact(int)}+ * returned {@code false}.* @return numeric value*/public abstract long longValue() throws IOException;+}diff --git a/lucene/core/src/java/org/apache/lucene/index/NumericDocValuesWriter.java b/lucene/core/src/java/org/apache/lucene/index/NumericDocValuesWriter.javaindex adfa70678c9..24a701064cb 100644--- a/lucene/core/src/java/org/apache/lucene/index/NumericDocValuesWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/NumericDocValuesWriter.java@@ -118,6 +118,11 @@ class NumericDocValuesWriter extends DocValuesWriter {throw new UnsupportedOperationException();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ throw new UnsupportedOperationException();+ }+@Overridepublic long cost() {return docsWithField.cost();diff --git a/lucene/core/src/java/org/apache/lucene/index/ReadersAndUpdates.java b/lucene/core/src/java/org/apache/lucene/index/ReadersAndUpdates.javaindex 894c81a3d4e..3cd465c9d41 100644--- a/lucene/core/src/java/org/apache/lucene/index/ReadersAndUpdates.java+++ b/lucene/core/src/java/org/apache/lucene/index/ReadersAndUpdates.java@@ -359,6 +359,11 @@ class ReadersAndUpdates {throw new UnsupportedOperationException();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ throw new UnsupportedOperationException();+ }+@Overridepublic long cost() {// TODO@@ -461,6 +466,11 @@ class ReadersAndUpdates {throw new UnsupportedOperationException();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ throw new UnsupportedOperationException();+ }+@Overridepublic long cost() {return currentValues.cost();diff --git a/lucene/core/src/java/org/apache/lucene/index/SingletonSortedNumericDocValues.java b/lucene/core/src/java/org/apache/lucene/index/SingletonSortedNumericDocValues.javaindex d95f0c0349b..5dbdec8f306 100644--- a/lucene/core/src/java/org/apache/lucene/index/SingletonSortedNumericDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/SingletonSortedNumericDocValues.java@@ -27,7 +27,6 @@ import java.io.IOException;*/final class SingletonSortedNumericDocValues extends SortedNumericDocValues {private final NumericDocValues in;- private long value;public SingletonSortedNumericDocValues(NumericDocValues in) {if (in.docID() != -1) {@@ -51,30 +50,27 @@ final class SingletonSortedNumericDocValues extends SortedNumericDocValues {@Overridepublic int nextDoc() throws IOException {- int docID = in.nextDoc();- if (docID != NO_MORE_DOCS) {- value = in.longValue();- }- return docID;+ return in.nextDoc();}@Overridepublic int advance(int target) throws IOException {- int docID = in.advance(target);- if (docID != NO_MORE_DOCS) {- value = in.longValue();- }- return docID;+ return in.advance(target);+ }++ @Override+ public boolean advanceExact(int target) throws IOException {+ return in.advanceExact(target);}-+@Overridepublic long cost() {return in.cost();}@Override- public long nextValue() {- return value;+ public long nextValue() throws IOException {+ return in.longValue();}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/index/SingletonSortedSetDocValues.java b/lucene/core/src/java/org/apache/lucene/index/SingletonSortedSetDocValues.javaindex cc7360e1d17..f16cdf1743c 100644--- a/lucene/core/src/java/org/apache/lucene/index/SingletonSortedSetDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/SingletonSortedSetDocValues.java@@ -29,7 +29,6 @@ import org.apache.lucene.util.BytesRef;*/final class SingletonSortedSetDocValues extends SortedSetDocValues {private final SortedDocValues in;- private long currentOrd;private long ord;/** Creates a multi-valued view over the provided SortedDocValues */@@ -55,8 +54,8 @@ final class SingletonSortedSetDocValues extends SortedSetDocValues {@Overridepublic long nextOrd() {- long v = currentOrd;- currentOrd = NO_MORE_ORDS;+ long v = ord;+ ord = NO_MORE_ORDS;return v;}@@ -64,7 +63,7 @@ final class SingletonSortedSetDocValues extends SortedSetDocValues {public int nextDoc() throws IOException {int docID = in.nextDoc();if (docID != NO_MORE_DOCS) {- currentOrd = ord = in.ordValue();+ ord = in.ordValue();}return docID;}@@ -73,11 +72,20 @@ final class SingletonSortedSetDocValues extends SortedSetDocValues {public int advance(int target) throws IOException {int docID = in.advance(target);if (docID != NO_MORE_DOCS) {- currentOrd = ord = in.ordValue();+ ord = in.ordValue();}return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ if (in.advanceExact(target)) {+ ord = in.ordValue();+ return true;+ }+ return false;+ }+@Overridepublic BytesRef lookupOrd(long ord) throws IOException {// cast is ok: single-valued cannot exceed Integer.MAX_VALUEdiff --git a/lucene/core/src/java/org/apache/lucene/index/SortedDocValues.java b/lucene/core/src/java/org/apache/lucene/index/SortedDocValues.javaindex 7ff084f2cb7..e2d7dfd4692 100644--- a/lucene/core/src/java/org/apache/lucene/index/SortedDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/SortedDocValues.java@@ -40,6 +40,8 @@ public abstract class SortedDocValues extends BinaryDocValues {/*** Returns the ordinal for the current docID.+ * It is illegal to call this method after {@link #advanceExact(int)}+ * returned {@code false}.* @return ordinal for the document: this is dense, starts at 0, then* increments by 1 for the next value in sorted order.*/@@ -107,4 +109,5 @@ public abstract class SortedDocValues extends BinaryDocValues {public TermsEnum termsEnum() throws IOException {return new SortedDocValuesTermsEnum(this);}+}diff --git a/lucene/core/src/java/org/apache/lucene/index/SortedDocValuesWriter.java b/lucene/core/src/java/org/apache/lucene/index/SortedDocValuesWriter.javaindex 885ee89bb03..7e43e497f7b 100644--- a/lucene/core/src/java/org/apache/lucene/index/SortedDocValuesWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/SortedDocValuesWriter.java@@ -165,6 +165,11 @@ class SortedDocValuesWriter extends DocValuesWriter {throw new UnsupportedOperationException();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ throw new UnsupportedOperationException();+ }+@Overridepublic long cost() {return docsWithField.cost();diff --git a/lucene/core/src/java/org/apache/lucene/index/SortedNumericDocValues.java b/lucene/core/src/java/org/apache/lucene/index/SortedNumericDocValues.javaindex 8c114957956..a76b46d4045 100644--- a/lucene/core/src/java/org/apache/lucene/index/SortedNumericDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/SortedNumericDocValues.java@@ -18,14 +18,12 @@ package org.apache.lucene.index;import java.io.IOException;-import org.apache.lucene.search.DocIdSetIterator;-/*** A list of per-document numeric values, sorted* according to {@link Long#compare(long, long)}.*/-public abstract class SortedNumericDocValues extends DocIdSetIterator {+public abstract class SortedNumericDocValues extends DocValuesIterator {/** Sole constructor. (For invocation by subclass* constructors, typically implicit.) */@@ -40,6 +38,8 @@ public abstract class SortedNumericDocValues extends DocIdSetIterator {/*** Retrieves the number of values for the current document. This must always* be greater than zero.+ * It is illegal to call this method after {@link #advanceExact(int)}+ * returned {@code false}.*/public abstract int docValueCount();}diff --git a/lucene/core/src/java/org/apache/lucene/index/SortedNumericDocValuesWriter.java b/lucene/core/src/java/org/apache/lucene/index/SortedNumericDocValuesWriter.javaindex e154547d758..3f50623d4ea 100644--- a/lucene/core/src/java/org/apache/lucene/index/SortedNumericDocValuesWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/SortedNumericDocValuesWriter.java@@ -154,6 +154,11 @@ class SortedNumericDocValuesWriter extends DocValuesWriter {throw new UnsupportedOperationException();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ throw new UnsupportedOperationException();+ }+@Overridepublic int docValueCount() {return valueCount;diff --git a/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValues.java b/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValues.javaindex 439843b6f3f..6d02c250863 100644--- a/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValues.java@@ -19,7 +19,6 @@ package org.apache.lucene.index;import java.io.IOException;-import org.apache.lucene.search.DocIdSetIterator;import org.apache.lucene.util.BytesRef;/**@@ -30,7 +29,7 @@ import org.apache.lucene.util.BytesRef;* dictionary value (ordinal) can be retrieved for each document. Ordinals* are dense and in increasing sorted order.*/-public abstract class SortedSetDocValues extends DocIdSetIterator {+public abstract class SortedSetDocValues extends DocValuesIterator {/** Sole constructor. (For invocation by subclass* constructors, typically implicit.) */@@ -43,6 +42,8 @@ public abstract class SortedSetDocValues extends DocIdSetIterator {/*** Returns the next ordinal for the current document.+ * It is illegal to call this method after {@link #advanceExact(int)}+ * returned {@code false}.* @return next ordinal for the document, or {@link #NO_MORE_ORDS}.* ordinals are dense, start at 0, then increment by 1 for* the next value in sorted order.diff --git a/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValuesWriter.java b/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValuesWriter.javaindex e7d915fc0c4..35157d49c16 100644--- a/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValuesWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValuesWriter.java@@ -225,6 +225,11 @@ class SortedSetDocValuesWriter extends DocValuesWriter {throw new UnsupportedOperationException();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ throw new UnsupportedOperationException();+ }+@Overridepublic long getValueCount() {return ordMap.length;diff --git a/lucene/core/src/java/org/apache/lucene/index/SortingLeafReader.java b/lucene/core/src/java/org/apache/lucene/index/SortingLeafReader.javaindex 8139ed19c1b..4fb5027a392 100644--- a/lucene/core/src/java/org/apache/lucene/index/SortingLeafReader.java+++ b/lucene/core/src/java/org/apache/lucene/index/SortingLeafReader.java@@ -185,6 +185,12 @@ class SortingLeafReader extends FilterLeafReader {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return dvs.docsWithField.get(target);+ }+@Overridepublic BytesRef binaryValue() {return dvs.values[docID];@@ -254,6 +260,12 @@ class SortingLeafReader extends FilterLeafReader {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return dvs.docsWithField.get(target);+ }+@Overridepublic long longValue() {return dvs.values[docID];@@ -394,6 +406,12 @@ class SortingLeafReader extends FilterLeafReader {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return ords[target] != -1;+ }+@Overridepublic int ordValue() {return ords[docID];@@ -467,6 +485,13 @@ class SortingLeafReader extends FilterLeafReader {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ ordUpto = 0;+ return ords[docID] != null;+ }+@Overridepublic long nextOrd() {if (ordUpto == ords[docID].length) {@@ -538,6 +563,13 @@ class SortingLeafReader extends FilterLeafReader {}}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ upto = 0;+ return values[docID] != null;+ }+@Overridepublic long nextValue() {if (upto == values[docID].length) {diff --git a/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java b/lucene/core/src/java/org/apache/lucene/search/FieldComparator.javaindex b6c17c0817c..82162015d0f 100644--- a/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java+++ b/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java@@ -178,11 +178,7 @@ public abstract class FieldComparator<T> {}private double getValueForDoc(int doc) throws IOException {- int curDocID = currentReaderValues.docID();- if (doc > curDocID) {- curDocID = currentReaderValues.advance(doc);- }- if (doc == curDocID) {+ if (currentReaderValues.advanceExact(doc)) {return Double.longBitsToDouble(currentReaderValues.longValue());} else {return missingValue;@@ -242,11 +238,7 @@ public abstract class FieldComparator<T> {}private float getValueForDoc(int doc) throws IOException {- int curDocID = currentReaderValues.docID();- if (doc > curDocID) {- curDocID = currentReaderValues.advance(doc);- }- if (doc == curDocID) {+ if (currentReaderValues.advanceExact(doc)) {return Float.intBitsToFloat((int) currentReaderValues.longValue());} else {return missingValue;@@ -308,11 +300,7 @@ public abstract class FieldComparator<T> {}private int getValueForDoc(int doc) throws IOException {- int curDocID = currentReaderValues.docID();- if (doc > curDocID) {- curDocID = currentReaderValues.advance(doc);- }- if (doc == curDocID) {+ if (currentReaderValues.advanceExact(doc)) {return (int) currentReaderValues.longValue();} else {return missingValue;@@ -372,11 +360,7 @@ public abstract class FieldComparator<T> {}private long getValueForDoc(int doc) throws IOException {- int curDocID = currentReaderValues.docID();- if (doc > curDocID) {- curDocID = currentReaderValues.advance(doc);- }- if (doc == curDocID) {+ if (currentReaderValues.advanceExact(doc)) {return currentReaderValues.longValue();} else {return missingValue;@@ -656,15 +640,11 @@ public abstract class FieldComparator<T> {}private int getOrdForDoc(int doc) throws IOException {- int curDocID = termsIndex.docID();- if (doc > curDocID) {- if (termsIndex.advance(doc) == doc) {- return termsIndex.ordValue();- }- } else if (doc == curDocID) {+ if (termsIndex.advanceExact(doc)) {return termsIndex.ordValue();+ } else {+ return -1;}- return -1;}@Override@@ -864,11 +844,7 @@ public abstract class FieldComparator<T> {}private BytesRef getValueForDoc(int doc) throws IOException {- int curDocID = docTerms.docID();- if (doc > curDocID) {- curDocID = docTerms.advance(doc);- }- if (doc == curDocID) {+ if (docTerms.advanceExact(doc)) {return docTerms.binaryValue();} else {return null;diff --git a/lucene/core/src/java/org/apache/lucene/search/SortedNumericSelector.java b/lucene/core/src/java/org/apache/lucene/search/SortedNumericSelector.javaindex 43e97e706d9..705454eb67b 100644--- a/lucene/core/src/java/org/apache/lucene/search/SortedNumericSelector.java+++ b/lucene/core/src/java/org/apache/lucene/search/SortedNumericSelector.java@@ -131,6 +131,15 @@ public class SortedNumericSelector {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ if (in.advanceExact(target)) {+ value = in.nextValue();+ return true;+ }+ return false;+ }+@Overridepublic long cost() {return in.cost();@@ -181,6 +190,15 @@ public class SortedNumericSelector {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ if (in.advanceExact(target)) {+ setValue();+ return true;+ }+ return false;+ }+@Overridepublic long cost() {return in.cost();diff --git a/lucene/core/src/java/org/apache/lucene/search/SortedSetSelector.java b/lucene/core/src/java/org/apache/lucene/search/SortedSetSelector.javaindex f10dbf727e3..2d6c35102ba 100644--- a/lucene/core/src/java/org/apache/lucene/search/SortedSetSelector.java+++ b/lucene/core/src/java/org/apache/lucene/search/SortedSetSelector.java@@ -117,6 +117,15 @@ public class SortedSetSelector {return docID();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ if (in.advanceExact(target)) {+ setOrd();+ return true;+ }+ return false;+ }+@Overridepublic long cost() {return in.cost();@@ -179,6 +188,15 @@ public class SortedSetSelector {return docID();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ if (in.advanceExact(target)) {+ setOrd();+ return true;+ }+ return false;+ }+@Overridepublic long cost() {return in.cost();@@ -248,6 +266,15 @@ public class SortedSetSelector {return docID();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ if (in.advanceExact(target)) {+ setOrd();+ return true;+ }+ return false;+ }+@Overridepublic long cost() {return in.cost();@@ -329,6 +356,15 @@ public class SortedSetSelector {return docID();}+ @Override+ public boolean advanceExact(int target) throws IOException {+ if (in.advanceExact(target)) {+ setOrd();+ return true;+ }+ return false;+ }+@Overridepublic long cost() {return in.cost();diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java b/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.javaindex 0cb86dbee7f..ff390b32f4d 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/BM25Similarity.java@@ -244,11 +244,7 @@ public class BM25Similarity extends Similarity {if (norms == null) {norm = k1;} else {- int normsDocID = norms.docID();- if (normsDocID < doc) {- normsDocID = norms.advance(doc);- }- if (normsDocID == doc) {+ if (norms.advanceExact(doc)) {norm = cache[(byte)norms.longValue() & 0xFF];} else {norm = cache[0];@@ -310,7 +306,7 @@ public class BM25Similarity extends Similarity {"tfNorm, computed from:", subs);} else {byte norm;- if (norms.advance(doc) == doc) {+ if (norms.advanceExact(doc)) {norm = (byte) norms.longValue();} else {norm = 0;diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/SimilarityBase.java b/lucene/core/src/java/org/apache/lucene/search/similarities/SimilarityBase.javaindex ed837c4d128..925dc5973da 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/SimilarityBase.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/SimilarityBase.java@@ -279,11 +279,7 @@ public abstract class SimilarityBase extends Similarity {if (norms == null) {return 1F;}- int normsDocID = norms.docID();- if (normsDocID < doc) {- normsDocID = norms.advance(doc);- }- if (normsDocID == doc) {+ if (norms.advanceExact(doc)) {return decodeNormValue((byte) norms.longValue());} else {return decodeNormValue((byte) 0);diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/TFIDFSimilarity.java b/lucene/core/src/java/org/apache/lucene/search/similarities/TFIDFSimilarity.javaindex 6cd87b56562..cd8acd693a8 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/TFIDFSimilarity.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/TFIDFSimilarity.java@@ -599,11 +599,7 @@ public abstract class TFIDFSimilarity extends Similarity {return raw;} else {long normValue;- int normsDocID = norms.docID();- if (normsDocID < doc) {- normsDocID = norms.advance(doc);- }- if (normsDocID == doc) {+ if (norms.advanceExact(doc)) {normValue = norms.longValue();} else {normValue = 0;@@ -649,7 +645,7 @@ public abstract class TFIDFSimilarity extends Similarity {private Explanation explainField(int doc, Explanation freq, IDFStats stats, NumericDocValues norms) throws IOException {Explanation tfExplanation = Explanation.match(tf(freq.getValue()), "tf(freq="+freq.getValue()+"), with freq of:", freq);float norm;- if (norms != null && norms.advance(doc) == doc) {+ if (norms != null && norms.advanceExact(doc)) {norm = decodeNormValue(norms.longValue());} else {norm = 1f;diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene70/TestIndexedDISI.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene70/TestIndexedDISI.javaindex 18b4590badd..64bfbd5fc96 100644--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene70/TestIndexedDISI.java+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene70/TestIndexedDISI.java@@ -153,7 +153,7 @@ public class TestIndexedDISI extends LuceneTestCase {public void testRandom() throws IOException {try (Directory dir = newDirectory()) {- for (int i = 0; i < 100; ++i) {+ for (int i = 0; i < 10; ++i) {doTestRandom(dir);}}@@ -217,6 +217,32 @@ public class TestIndexedDISI extends LuceneTestCase {}}+ for (int step : new int[] {10, 100, 1000, 10000, 100000}) {+ try (IndexInput in = dir.openInput("foo", IOContext.DEFAULT)) {+ IndexedDISI disi = new IndexedDISI(in, 0L, length, cardinality);+ BitSetIterator disi2 = new BitSetIterator(set, cardinality);+ int index = -1;+ for (int target = 0; target < set.length(); ) {+ target += TestUtil.nextInt(random(), 0, step);+ int doc = disi2.docID();+ while (doc < target) {+ doc = disi2.nextDoc();+ index++;+ }++ boolean exists = disi.advanceExact(target);+ assertEquals(doc == target, exists);+ if (exists) {+ assertEquals(index, disi.index());+ } else if (random().nextBoolean()) {+ assertEquals(doc, disi.nextDoc());+ assertEquals(index, disi.index());+ target = doc;+ }+ }+ }+ }+dir.deleteFile("foo");}diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene70/TestLucene70DocValuesFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene70/TestLucene70DocValuesFormat.javaindex 5ad701ee6e0..8661298b51d 100644--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene70/TestLucene70DocValuesFormat.java+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene70/TestLucene70DocValuesFormat.java@@ -104,7 +104,7 @@ public class TestLucene70DocValuesFormat extends BaseCompressingDocValuesFormatTpublic void testSortedVariableLengthBigVsStoredFields() throws Exception {int numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {- doTestSortedVsStoredFields(atLeast(300), 1, 32766);+ doTestSortedVsStoredFields(atLeast(300), 1d, 1, 32766);}}@@ -112,7 +112,7 @@ public class TestLucene70DocValuesFormat extends BaseCompressingDocValuesFormatTpublic void testSortedVariableLengthManyVsStoredFields() throws Exception {int numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {- doTestSortedVsStoredFields(TestUtil.nextInt(random(), 1024, 2049), 1, 500);+ doTestSortedVsStoredFields(TestUtil.nextInt(random(), 1024, 2049), 1d, 1, 500);}}diff --git a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.javaindex 12194948db5..4fff6a637b8 100644--- a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.java+++ b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.java@@ -199,10 +199,7 @@ public class SortedSetDocValuesFacetCounts extends Facets {int doc;while ((doc = docs.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {//System.out.println(" doc=" + doc);- if (doc > segValues.docID()) {- segValues.advance(doc);- }- if (doc == segValues.docID()) {+ if (segValues.advanceExact(doc)) {int term = (int) segValues.nextOrd();while (term != SortedSetDocValues.NO_MORE_ORDS) {//System.out.println(" segOrd=" + segOrd + " ord=" + term + " globalOrd=" + ordinalMap.getGlobalOrd(segOrd, term));@@ -219,10 +216,7 @@ public class SortedSetDocValuesFacetCounts extends Facets {int doc;while ((doc = docs.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {//System.out.println(" doc=" + doc);- if (doc > segValues.docID()) {- segValues.advance(doc);- }- if (doc == segValues.docID()) {+ if (segValues.advanceExact(doc)) {int term = (int) segValues.nextOrd();while (term != SortedSetDocValues.NO_MORE_ORDS) {//System.out.println(" ord=" + term);@@ -246,10 +240,7 @@ public class SortedSetDocValuesFacetCounts extends Facets {// just aggregate directly into counts:int doc;while ((doc = docs.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- if (doc > segValues.docID()) {- segValues.advance(doc);- }- if (doc == segValues.docID()) {+ if (segValues.advanceExact(doc)) {int term = (int) segValues.nextOrd();while (term != SortedSetDocValues.NO_MORE_ORDS) {counts[term]++;diff --git a/lucene/join/src/java/org/apache/lucene/search/join/BlockJoinSelector.java b/lucene/join/src/java/org/apache/lucene/search/join/BlockJoinSelector.javaindex a2e0c5557ca..359b3cb22eb 100644--- a/lucene/join/src/java/org/apache/lucene/search/join/BlockJoinSelector.java+++ b/lucene/join/src/java/org/apache/lucene/search/join/BlockJoinSelector.java@@ -102,7 +102,7 @@ public class BlockJoinSelector {}return new SortedDocValues() {- private int ord;+ private int ord = -1;private int docID = -1;@Override@@ -168,6 +168,60 @@ public class BlockJoinSelector {return nextDoc();}+ @Override+ public boolean advanceExact(int targetParentDocID) throws IOException {+ if (targetParentDocID < docID) {+ throw new IllegalArgumentException("target must be after the current document: current=" + docID + " target=" + targetParentDocID);+ }+ int previousDocId = docID;+ docID = targetParentDocID;+ if (targetParentDocID == previousDocId) {+ return ord != -1;+ }+ docID = targetParentDocID;+ ord = -1;+ if (parents.get(targetParentDocID) == false) {+ return false;+ }+ int prevParentDocId = docID == 0 ? -1 : parents.prevSetBit(docID - 1);+ int childDoc = values.docID();+ if (childDoc <= prevParentDocId) {+ childDoc = values.advance(prevParentDocId + 1);+ }+ if (childDoc >= docID) {+ return false;+ }++ boolean hasValue = false;+ for (int doc = values.docID(); doc < docID; doc = values.nextDoc()) {+ if (children.get(doc)) {+ ord = values.ordValue();+ hasValue = true;+ values.nextDoc();+ break;+ }+ }+ if (hasValue == false) {+ return false;+ }++ for (int doc = values.docID(); doc < docID; doc = values.nextDoc()) {+ if (children.get(doc)) {+ switch (selection) {+ case MIN:+ ord = Math.min(ord, values.ordValue());+ break;+ case MAX:+ ord = Math.max(ord, values.ordValue());+ break;+ default:+ throw new AssertionError();+ }+ }+ }+ return true;+ }+@Overridepublic int ordValue() {return ord;@@ -287,6 +341,54 @@ public class BlockJoinSelector {}}+ @Override+ public boolean advanceExact(int targetParentDocID) throws IOException {+ if (targetParentDocID <= parentDocID) {+ throw new IllegalArgumentException("target must be after the current document: current=" + parentDocID + " target=" + targetParentDocID);+ }+ parentDocID = targetParentDocID;+ if (parents.get(targetParentDocID) == false) {+ return false;+ }+ int prevParentDocId = parentDocID == 0 ? -1 : parents.prevSetBit(parentDocID - 1);+ int childDoc = values.docID();+ if (childDoc <= prevParentDocId) {+ childDoc = values.advance(prevParentDocId + 1);+ }+ if (childDoc >= parentDocID) {+ return false;+ }++ boolean hasValue = false;+ for (int doc = values.docID(); doc < parentDocID; doc = values.nextDoc()) {+ if (children.get(doc)) {+ value = values.longValue();+ hasValue = true;+ values.nextDoc();+ break;+ }+ }+ if (hasValue == false) {+ return false;+ }++ for (int doc = values.docID(); doc < parentDocID; doc = values.nextDoc()) {+ if (children.get(doc)) {+ switch (selection) {+ case MIN:+ value = Math.min(value, values.longValue());+ break;+ case MAX:+ value = Math.max(value, values.longValue());+ break;+ default:+ throw new AssertionError();+ }+ }+ }+ return true;+ }+@Overridepublic long longValue() {return value;diff --git a/lucene/join/src/java/org/apache/lucene/search/join/GenericTermsCollector.java b/lucene/join/src/java/org/apache/lucene/search/join/GenericTermsCollector.javaindex 3ad0fe39d0f..47b1b62b305 100644--- a/lucene/join/src/java/org/apache/lucene/search/join/GenericTermsCollector.java+++ b/lucene/join/src/java/org/apache/lucene/search/join/GenericTermsCollector.java@@ -73,6 +73,13 @@ interface GenericTermsCollector extends Collector {return docID;}+ @Override+ public boolean advanceExact(int dest) throws IOException {+ boolean exists = target.advanceExact(dest);+ out.println("\nadvanceExact(" + dest + ") -> exists# "+exists);+ return exists;+ }+@Overridepublic long cost() {return target.cost();diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinSelector.java b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinSelector.javaindex 41f994c6782..04cb771422d 100644--- a/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinSelector.java+++ b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinSelector.java@@ -149,6 +149,12 @@ public class TestBlockJoinSelector extends LuceneTestCase {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return ords[docID] != -1;+ }+@Overridepublic int ordValue() {assert ords[docID] != -1;@@ -256,6 +262,12 @@ public class TestBlockJoinSelector extends LuceneTestCase {}}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return docsWithValue.get(docID);+ }+@Overridepublic long longValue() {return values[docID];diff --git a/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java b/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.javaindex ccbbf243d76..218d26c87c1 100644--- a/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java+++ b/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java@@ -969,6 +969,12 @@ public class MemoryIndex {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return docID == 0;+ }+@Overridepublic long cost() {return 1;diff --git a/lucene/misc/src/test/org/apache/lucene/search/TestDiversifiedTopDocsCollector.java b/lucene/misc/src/test/org/apache/lucene/search/TestDiversifiedTopDocsCollector.javaindex b64afc5cb32..043141ac72a 100644--- a/lucene/misc/src/test/org/apache/lucene/search/TestDiversifiedTopDocsCollector.java+++ b/lucene/misc/src/test/org/apache/lucene/search/TestDiversifiedTopDocsCollector.java@@ -143,6 +143,11 @@ public class TestDiversifiedTopDocsCollector extends LuceneTestCase {return sdv.advance(target + context.docBase);}+ @Override+ public boolean advanceExact(int target) throws IOException {+ return sdv.advanceExact(target + context.docBase);+ }+@Overridepublic long cost() {return 0;@@ -187,6 +192,10 @@ public class TestDiversifiedTopDocsCollector extends LuceneTestCase {return vals.advance(target);}@Override+ public boolean advanceExact(int target) throws IOException {+ return vals.advanceExact(target);+ }+ @Overridepublic long cost() {return vals.cost();}diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/AssertingLeafReader.java b/lucene/test-framework/src/java/org/apache/lucene/index/AssertingLeafReader.javaindex 6686ec4778e..37c549ec723 100644--- a/lucene/test-framework/src/java/org/apache/lucene/index/AssertingLeafReader.java+++ b/lucene/test-framework/src/java/org/apache/lucene/index/AssertingLeafReader.java@@ -398,6 +398,7 @@ public class AssertingLeafReader extends FilterLeafReader {private final NumericDocValues in;private final int maxDoc;private int lastDocID = -1;+ private boolean exists;public AssertingNumericDocValues(NumericDocValues in, int maxDoc) {this.in = in;@@ -420,6 +421,7 @@ public class AssertingLeafReader extends FilterLeafReader {assert docID == NO_MORE_DOCS || docID < maxDoc;assert docID == in.docID();lastDocID = docID;+ exists = docID != NO_MORE_DOCS;return docID;}@@ -432,9 +434,22 @@ public class AssertingLeafReader extends FilterLeafReader {assert docID >= target;assert docID == NO_MORE_DOCS || docID < maxDoc;lastDocID = docID;+ exists = docID != NO_MORE_DOCS;return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ assertThread("Numeric doc values", creationThread);+ assert target >= 0;+ assert target >= in.docID();+ assert target < maxDoc;+ exists = in.advanceExact(target);+ assert in.docID() == target;+ lastDocID = target;+ return exists;+ }+@Overridepublic long cost() {assertThread("Numeric doc values", creationThread);@@ -446,8 +461,7 @@ public class AssertingLeafReader extends FilterLeafReader {@Overridepublic long longValue() throws IOException {assertThread("Numeric doc values", creationThread);- assert in.docID() != -1;- assert in.docID() != NO_MORE_DOCS;+ assert exists;return in.longValue();}@@ -463,6 +477,7 @@ public class AssertingLeafReader extends FilterLeafReader {private final BinaryDocValues in;private final int maxDoc;private int lastDocID = -1;+ private boolean exists;public AssertingBinaryDocValues(BinaryDocValues in, int maxDoc) {this.in = in;@@ -485,6 +500,7 @@ public class AssertingLeafReader extends FilterLeafReader {assert docID == NO_MORE_DOCS || docID < maxDoc;assert docID == in.docID();lastDocID = docID;+ exists = docID != NO_MORE_DOCS;return docID;}@@ -497,9 +513,22 @@ public class AssertingLeafReader extends FilterLeafReader {assert docID >= target;assert docID == NO_MORE_DOCS || docID < maxDoc;lastDocID = docID;+ exists = docID != NO_MORE_DOCS;return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ assertThread("Numeric doc values", creationThread);+ assert target >= 0;+ assert target >= in.docID();+ assert target < maxDoc;+ exists = in.advanceExact(target);+ assert in.docID() == target;+ lastDocID = target;+ return exists;+ }+@Overridepublic long cost() {assertThread("Binary doc values", creationThread);@@ -511,8 +540,7 @@ public class AssertingLeafReader extends FilterLeafReader {@Overridepublic BytesRef binaryValue() throws IOException {assertThread("Binary doc values", creationThread);- assert in.docID() != -1;- assert in.docID() != NO_MORE_DOCS;+ assert exists;return in.binaryValue();}@@ -529,6 +557,7 @@ public class AssertingLeafReader extends FilterLeafReader {private final int maxDoc;private final int valueCount;private int lastDocID = -1;+ private boolean exists;public AssertingSortedDocValues(SortedDocValues in, int maxDoc) {this.in = in;@@ -551,6 +580,7 @@ public class AssertingLeafReader extends FilterLeafReader {assert docID == NO_MORE_DOCS || docID < maxDoc;assert docID == in.docID();lastDocID = docID;+ exists = docID != NO_MORE_DOCS;return docID;}@@ -563,9 +593,22 @@ public class AssertingLeafReader extends FilterLeafReader {assert docID >= target;assert docID == NO_MORE_DOCS || docID < maxDoc;lastDocID = docID;+ exists = docID != NO_MORE_DOCS;return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ assertThread("Numeric doc values", creationThread);+ assert target >= 0;+ assert target >= in.docID();+ assert target < maxDoc;+ exists = in.advanceExact(target);+ assert in.docID() == target;+ lastDocID = target;+ return exists;+ }+@Overridepublic long cost() {assertThread("Sorted doc values", creationThread);@@ -577,6 +620,7 @@ public class AssertingLeafReader extends FilterLeafReader {@Overridepublic int ordValue() {assertThread("Sorted doc values", creationThread);+ assert exists;int ord = in.ordValue();assert ord >= -1 && ord < valueCount;return ord;@@ -625,6 +669,7 @@ public class AssertingLeafReader extends FilterLeafReader {private final int maxDoc;private int lastDocID = -1;private int valueUpto;+ private boolean exists;public AssertingSortedNumericDocValues(SortedNumericDocValues in, int maxDoc) {this.in = in;@@ -645,6 +690,7 @@ public class AssertingLeafReader extends FilterLeafReader {assert docID == in.docID();lastDocID = docID;valueUpto = 0;+ exists = docID != NO_MORE_DOCS;return docID;}@@ -659,9 +705,23 @@ public class AssertingLeafReader extends FilterLeafReader {assert docID == NO_MORE_DOCS || docID < maxDoc;lastDocID = docID;valueUpto = 0;+ exists = docID != NO_MORE_DOCS;return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ assertThread("Numeric doc values", creationThread);+ assert target >= 0;+ assert target >= in.docID();+ assert target < maxDoc;+ exists = in.advanceExact(target);+ assert in.docID() == target;+ lastDocID = target;+ valueUpto = 0;+ return exists;+ }+@Overridepublic long cost() {assertThread("Sorted numeric doc values", creationThread);@@ -673,6 +733,7 @@ public class AssertingLeafReader extends FilterLeafReader {@Overridepublic long nextValue() throws IOException {assertThread("Sorted numeric doc values", creationThread);+ assert exists;assert valueUpto < in.docValueCount(): "valueUpto=" + valueUpto + " in.docValueCount()=" + in.docValueCount();valueUpto++;return in.nextValue();@@ -681,6 +742,7 @@ public class AssertingLeafReader extends FilterLeafReader {@Overridepublic int docValueCount() {assertThread("Sorted numeric doc values", creationThread);+ assert exists;assert in.docValueCount() > 0;return in.docValueCount();}@@ -693,7 +755,8 @@ public class AssertingLeafReader extends FilterLeafReader {private final int maxDoc;private final long valueCount;private int lastDocID = -1;- long lastOrd = NO_MORE_ORDS;+ private long lastOrd = NO_MORE_ORDS;+ private boolean exists;public AssertingSortedSetDocValues(SortedSetDocValues in, int maxDoc) {this.in = in;@@ -717,6 +780,7 @@ public class AssertingLeafReader extends FilterLeafReader {assert docID == in.docID();lastDocID = docID;lastOrd = -2;+ exists = docID != NO_MORE_DOCS;return docID;}@@ -731,9 +795,23 @@ public class AssertingLeafReader extends FilterLeafReader {assert docID == NO_MORE_DOCS || docID < maxDoc;lastDocID = docID;lastOrd = -2;+ exists = docID != NO_MORE_DOCS;return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ assertThread("Numeric doc values", creationThread);+ assert target >= 0;+ assert target >= in.docID();+ assert target < maxDoc;+ exists = in.advanceExact(target);+ assert in.docID() == target;+ lastDocID = target;+ lastOrd = -2;+ return exists;+ }+@Overridepublic long cost() {assertThread("Sorted set doc values", creationThread);@@ -746,6 +824,7 @@ public class AssertingLeafReader extends FilterLeafReader {public long nextOrd() throws IOException {assertThread("Sorted set doc values", creationThread);assert lastOrd != NO_MORE_ORDS;+ assert exists;long ord = in.nextOrd();assert ord < valueCount;assert ord == NO_MORE_ORDS || ord > lastOrd;diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BaseDocValuesFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BaseDocValuesFormatTestCase.javaindex b9bf7451546..d55f212b160 100644--- a/lucene/test-framework/src/java/org/apache/lucene/index/BaseDocValuesFormatTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/index/BaseDocValuesFormatTestCase.java@@ -30,6 +30,8 @@ import java.util.Map;import java.util.Set;import java.util.TreeSet;import java.util.concurrent.CountDownLatch;+import java.util.function.LongSupplier;+import java.util.function.Supplier;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.MockAnalyzer;@@ -556,7 +558,6 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTesIndexReader ireader = DirectoryReader.open(directory); // read-only=trueassert ireader.leaves().size() == 1;BinaryDocValues dv = ireader.leaves().get(0).reader().getBinaryDocValues("dv");- BytesRef scratch = new BytesRef();for(int i=0;i<2;i++) {Document doc2 = ireader.leaves().get(0).reader().document(i);String expected;@@ -1185,20 +1186,7 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTesdir.close();}- static abstract class LongProducer {- abstract long next();- }-- private void doTestNumericsVsStoredFields(final long minValue, final long maxValue) throws Exception {- doTestNumericsVsStoredFields(new LongProducer() {- @Override- long next() {- return TestUtil.nextLong(random(), minValue, maxValue);- }- });- }-- private void doTestNumericsVsStoredFields(LongProducer longs) throws Exception {+ private void doTestNumericsVsStoredFields(double density, LongSupplier longs) throws Exception {Directory dir = newDirectory();IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));RandomIndexWriter writer = new RandomIndexWriter(random(), dir, conf);@@ -1216,8 +1204,12 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTes// for numbers of values <= 256, all storage layouts are testedassert numDocs > 256;for (int i = 0; i < numDocs; i++) {+ if (random().nextDouble() > density) {+ writer.addDocument(new Document());+ continue;+ }idField.setStringValue(Integer.toString(i));- long value = longs.next();+ long value = longs.getAsLong();storedField.setStringValue(Long.toString(value));dvField.setLongValue(value);writer.addDocument(doc);@@ -1241,20 +1233,28 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTes// compareDirectoryReader ir = DirectoryReader.open(dir);+ TestUtil.checkReader(ir);for (LeafReaderContext context : ir.leaves()) {LeafReader r = context.reader();- NumericDocValues docValues = r.getNumericDocValues("dv");+ NumericDocValues docValues = DocValues.getNumeric(r, "dv");+ docValues.nextDoc();for (int i = 0; i < r.maxDoc(); i++) {- long storedValue = Long.parseLong(r.document(i).get("stored"));- assertEquals(i, docValues.nextDoc());- assertEquals(storedValue, docValues.longValue());+ String storedValue = r.document(i).get("stored");+ if (storedValue == null) {+ assertTrue(docValues.docID() > i);+ } else {+ assertEquals(i, docValues.docID());+ assertEquals(Long.parseLong(storedValue), docValues.longValue());+ docValues.nextDoc();+ }}+ assertEquals(DocIdSetIterator.NO_MORE_DOCS, docValues.docID());}ir.close();dir.close();}- private void doTestSortedNumericsVsStoredFields(LongProducer counts, LongProducer values) throws Exception {+ private void doTestSortedNumericsVsStoredFields(LongSupplier counts, LongSupplier values) throws Exception {Directory dir = newDirectory();IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));RandomIndexWriter writer = new RandomIndexWriter(random(), dir, conf);@@ -1268,10 +1268,10 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTesDocument doc = new Document();doc.add(new StringField("id", Integer.toString(i), Field.Store.NO));- int valueCount = (int) counts.next();+ int valueCount = (int) counts.getAsLong();long valueArray[] = new long[valueCount];for (int j = 0; j < valueCount; j++) {- long value = values.next();+ long value = values.getAsLong();valueArray[j] = value;doc.add(new SortedNumericDocValuesField("dv", value));}@@ -1300,6 +1300,7 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTes// compareDirectoryReader ir = DirectoryReader.open(dir);+ TestUtil.checkReader(ir);for (LeafReaderContext context : ir.leaves()) {LeafReader r = context.reader();SortedNumericDocValues docValues = DocValues.getSortedNumeric(r, "dv");@@ -1326,39 +1327,74 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTespublic void testBooleanNumericsVsStoredFields() throws Exception {int numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {- doTestNumericsVsStoredFields(0, 1);+ doTestNumericsVsStoredFields(1, () -> random().nextInt(2));}}-++ public void testSparseBooleanNumericsVsStoredFields() throws Exception {+ int numIterations = atLeast(1);+ for (int i = 0; i < numIterations; i++) {+ doTestNumericsVsStoredFields(random().nextDouble(), () -> random().nextInt(2));+ }+ }+public void testByteNumericsVsStoredFields() throws Exception {int numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {- doTestNumericsVsStoredFields(Byte.MIN_VALUE, Byte.MAX_VALUE);+ doTestNumericsVsStoredFields(1, () -> TestUtil.nextInt(random(), Byte.MIN_VALUE, Byte.MAX_VALUE));}}-++ public void testSparseByteNumericsVsStoredFields() throws Exception {+ int numIterations = atLeast(1);+ for (int i = 0; i < numIterations; i++) {+ doTestNumericsVsStoredFields(random().nextDouble(), () -> TestUtil.nextInt(random(), Byte.MIN_VALUE, Byte.MAX_VALUE));+ }+ }+public void testShortNumericsVsStoredFields() throws Exception {int numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {- doTestNumericsVsStoredFields(Short.MIN_VALUE, Short.MAX_VALUE);+ doTestNumericsVsStoredFields(1, () -> TestUtil.nextInt(random(), Short.MIN_VALUE, Short.MAX_VALUE));}}-++ public void testSparseShortNumericsVsStoredFields() throws Exception {+ int numIterations = atLeast(1);+ for (int i = 0; i < numIterations; i++) {+ doTestNumericsVsStoredFields(random().nextDouble(), () -> TestUtil.nextInt(random(), Short.MIN_VALUE, Short.MAX_VALUE));+ }+ }+public void testIntNumericsVsStoredFields() throws Exception {int numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {- doTestNumericsVsStoredFields(Integer.MIN_VALUE, Integer.MAX_VALUE);+ doTestNumericsVsStoredFields(1, random()::nextInt);+ }+ }++ public void testSparseIntNumericsVsStoredFields() throws Exception {+ int numIterations = atLeast(1);+ for (int i = 0; i < numIterations; i++) {+ doTestNumericsVsStoredFields(random().nextDouble(), random()::nextInt);}}public void testLongNumericsVsStoredFields() throws Exception {int numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {- doTestNumericsVsStoredFields(Long.MIN_VALUE, Long.MAX_VALUE);+ doTestNumericsVsStoredFields(1, random()::nextLong);}}- private void doTestBinaryVsStoredFields(int minLength, int maxLength) throws Exception {+ public void testSparseLongNumericsVsStoredFields() throws Exception {+ int numIterations = atLeast(1);+ for (int i = 0; i < numIterations; i++) {+ doTestNumericsVsStoredFields(random().nextDouble(), random()::nextLong);+ }+ }++ private void doTestBinaryVsStoredFields(double density, Supplier<byte[]> bytes) throws Exception {Directory dir = newDirectory();IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));RandomIndexWriter writer = new RandomIndexWriter(random(), dir, conf);@@ -1373,15 +1409,12 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTes// index some docsint numDocs = atLeast(300);for (int i = 0; i < numDocs; i++) {- idField.setStringValue(Integer.toString(i));- final int length;- if (minLength == maxLength) {- length = minLength; // fixed length- } else {- length = TestUtil.nextInt(random(), minLength, maxLength);+ if (random().nextDouble() > density) {+ writer.addDocument(new Document());+ continue;}- byte buffer[] = new byte[length];- random().nextBytes(buffer);+ idField.setStringValue(Integer.toString(i));+ byte[] buffer = bytes.get();storedField.setBytesValue(buffer);dvField.setBytesValue(buffer);writer.addDocument(doc);@@ -1399,28 +1432,44 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTes// compareDirectoryReader ir = writer.getReader();+ TestUtil.checkReader(ir);for (LeafReaderContext context : ir.leaves()) {LeafReader r = context.reader();- BinaryDocValues docValues = r.getBinaryDocValues("dv");+ BinaryDocValues docValues = DocValues.getBinary(r, "dv");+ docValues.nextDoc();for (int i = 0; i < r.maxDoc(); i++) {BytesRef binaryValue = r.document(i).getBinaryValue("stored");- assertEquals(i, docValues.nextDoc());- assertEquals(binaryValue, docValues.binaryValue());+ if (binaryValue == null) {+ assertTrue(docValues.docID() > i);+ } else {+ assertEquals(i, docValues.docID());+ assertEquals(binaryValue, docValues.binaryValue());+ docValues.nextDoc();+ }}+ assertEquals(DocIdSetIterator.NO_MORE_DOCS, docValues.docID());}ir.close();// compare againwriter.forceMerge(1);ir = writer.getReader();+ TestUtil.checkReader(ir);for (LeafReaderContext context : ir.leaves()) {LeafReader r = context.reader();- BinaryDocValues docValues = r.getBinaryDocValues("dv");+ BinaryDocValues docValues = DocValues.getBinary(r, "dv");+ docValues.nextDoc();for (int i = 0; i < r.maxDoc(); i++) {BytesRef binaryValue = r.document(i).getBinaryValue("stored");- assertEquals(i, docValues.nextDoc());- assertEquals(binaryValue, docValues.binaryValue());+ if (binaryValue == null) {+ assertTrue(docValues.docID() > i);+ } else {+ assertEquals(i, docValues.docID());+ assertEquals(binaryValue, docValues.binaryValue());+ docValues.nextDoc();+ }}+ assertEquals(DocIdSetIterator.NO_MORE_DOCS, docValues.docID());}ir.close();writer.close();@@ -1428,21 +1477,46 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTes}public void testBinaryFixedLengthVsStoredFields() throws Exception {+ doTestBinaryFixedLengthVsStoredFields(1);+ }++ public void testSparseBinaryFixedLengthVsStoredFields() throws Exception {+ doTestBinaryFixedLengthVsStoredFields(random().nextDouble());+ }++ private void doTestBinaryFixedLengthVsStoredFields(double density) throws Exception {int numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {int fixedLength = TestUtil.nextInt(random(), 0, 10);- doTestBinaryVsStoredFields(fixedLength, fixedLength);+ doTestBinaryVsStoredFields(density, () -> {+ byte buffer[] = new byte[fixedLength];+ random().nextBytes(buffer);+ return buffer;+ });}}-+public void testBinaryVariableLengthVsStoredFields() throws Exception {+ doTestBinaryVariableLengthVsStoredFields(1);+ }++ public void testSparseBinaryVariableLengthVsStoredFields() throws Exception {+ doTestBinaryVariableLengthVsStoredFields(random().nextDouble());+ }++ public void doTestBinaryVariableLengthVsStoredFields(double density) throws Exception {int numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {- doTestBinaryVsStoredFields(0, 10);+ doTestBinaryVsStoredFields(density, () -> {+ final int length = random().nextInt(10);+ byte buffer[] = new byte[length];+ random().nextBytes(buffer);+ return buffer;+ });}}- protected void doTestSortedVsStoredFields(int numDocs, int minLength, int maxLength) throws Exception {+ protected void doTestSortedVsStoredFields(int numDocs, double density, Supplier<byte[]> bytes) throws Exception {Directory dir = newFSDirectory(createTempDir("dvduel"));IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));RandomIndexWriter writer = new RandomIndexWriter(random(), dir, conf);@@ -1456,15 +1530,12 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTes// index some docsfor (int i = 0; i < numDocs; i++) {- idField.setStringValue(Integer.toString(i));- final int length;- if (minLength == maxLength) {- length = minLength; // fixed length- } else {- length = TestUtil.nextInt(random(), minLength, maxLength);+ if (random().nextDouble() > density) {+ writer.addDocument(new Document());+ continue;}- byte buffer[] = new byte[length];- random().nextBytes(buffer);+ idField.setStringValue(Integer.toString(i));+ byte[] buffer = bytes.get();storedField.setBytesValue(buffer);dvField.setBytesValue(buffer);writer.addDocument(doc);@@ -1482,28 +1553,44 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTes// compareDirectoryReader ir = writer.getReader();+ TestUtil.checkReader(ir);for (LeafReaderContext context : ir.leaves()) {LeafReader r = context.reader();BinaryDocValues docValues = DocValues.getBinary(r, "dv");+ docValues.nextDoc();for (int i = 0; i < r.maxDoc(); i++) {BytesRef binaryValue = r.document(i).getBinaryValue("stored");- assertEquals(i, docValues.nextDoc());- assertEquals(binaryValue, docValues.binaryValue());+ if (binaryValue == null) {+ assertTrue(docValues.docID() > i);+ } else {+ assertEquals(i, docValues.docID());+ assertEquals(binaryValue, docValues.binaryValue());+ docValues.nextDoc();+ }}+ assertEquals(DocIdSetIterator.NO_MORE_DOCS, docValues.docID());}ir.close();writer.forceMerge(1);// compare againir = writer.getReader();+ TestUtil.checkReader(ir);for (LeafReaderContext context : ir.leaves()) {LeafReader r = context.reader();BinaryDocValues docValues = DocValues.getBinary(r, "dv");+ docValues.nextDoc();for (int i = 0; i < r.maxDoc(); i++) {BytesRef binaryValue = r.document(i).getBinaryValue("stored");- assertEquals(i, docValues.nextDoc());- assertEquals(binaryValue, docValues.binaryValue());+ if (binaryValue == null) {+ assertTrue(docValues.docID() > i);+ } else {+ assertEquals(i, docValues.docID());+ assertEquals(binaryValue, docValues.binaryValue());+ docValues.nextDoc();+ }}+ assertEquals(DocIdSetIterator.NO_MORE_DOCS, docValues.docID());}ir.close();writer.close();@@ -1514,17 +1601,41 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTesint numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {int fixedLength = TestUtil.nextInt(random(), 1, 10);- doTestSortedVsStoredFields(atLeast(300), fixedLength, fixedLength);+ doTestSortedVsStoredFields(atLeast(300), 1, fixedLength, fixedLength);}}- public void testSortedVariableLengthVsStoredFields() throws Exception {+ public void testSparseSortedFixedLengthVsStoredFields() throws Exception {int numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {- doTestSortedVsStoredFields(atLeast(300), 1, 10);+ int fixedLength = TestUtil.nextInt(random(), 1, 10);+ doTestSortedVsStoredFields(atLeast(300), random().nextDouble(), fixedLength, fixedLength);}}+ public void testSortedVariableLengthVsStoredFields() throws Exception {+ int numIterations = atLeast(1);+ for (int i = 0; i < numIterations; i++) {+ doTestSortedVsStoredFields(atLeast(300), 1, 1, 10);+ }+ }++ public void testSparseSortedVariableLengthVsStoredFields() throws Exception {+ int numIterations = atLeast(1);+ for (int i = 0; i < numIterations; i++) {+ doTestSortedVsStoredFields(atLeast(300), random().nextDouble(), 1, 10);+ }+ }++ protected void doTestSortedVsStoredFields(int numDocs, double density, int minLength, int maxLength) throws Exception {+ doTestSortedVsStoredFields(numDocs, density, () -> {+ int length = TestUtil.nextInt(random(), minLength, maxLength);+ byte[] buffer = new byte[length];+ random().nextBytes(buffer);+ return buffer;+ });+ }+public void testSortedSetOneValue() throws IOException {Directory directory = newDirectory();RandomIndexWriter iwriter = new RandomIndexWriter(random(), directory);@@ -2001,6 +2112,7 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTes// compareDirectoryReader ir = writer.getReader();+ TestUtil.checkReader(ir);for (LeafReaderContext context : ir.leaves()) {LeafReader r = context.reader();SortedSetDocValues docValues = r.getSortedSetDocValues("dv");@@ -2029,6 +2141,7 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTes// compare againir = writer.getReader();+ TestUtil.checkReader(ir);for (LeafReaderContext context : ir.leaves()) {LeafReader r = context.reader();SortedSetDocValues docValues = r.getSortedSetDocValues("dv");@@ -2067,18 +2180,8 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTesint numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {doTestSortedNumericsVsStoredFields(- new LongProducer() {- @Override- long next() {- return 1;- }- },- new LongProducer() {- @Override- long next() {- return TestUtil.nextLong(random(), Long.MIN_VALUE, Long.MAX_VALUE);- }- }+ () -> 1,+ random()::nextLong);}}@@ -2087,18 +2190,8 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTesint numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {doTestSortedNumericsVsStoredFields(- new LongProducer() {- @Override- long next() {- return random().nextBoolean() ? 0 : 1;- }- },- new LongProducer() {- @Override- long next() {- return TestUtil.nextLong(random(), Long.MIN_VALUE, Long.MAX_VALUE);- }- }+ () -> random().nextBoolean() ? 0 : 1,+ random()::nextLong);}}@@ -2107,18 +2200,8 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTesint numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {doTestSortedNumericsVsStoredFields(- new LongProducer() {- @Override- long next() {- return TestUtil.nextLong(random(), 0, 50);- }- },- new LongProducer() {- @Override- long next() {- return TestUtil.nextLong(random(), Long.MIN_VALUE, Long.MAX_VALUE);- }- }+ () -> TestUtil.nextLong(random(), 0, 50),+ random()::nextLong);}}@@ -2131,18 +2214,8 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTesint numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {doTestSortedNumericsVsStoredFields(- new LongProducer() {- @Override- long next() {- return TestUtil.nextLong(random(), 0, 6);- }- },- new LongProducer() {- @Override- long next() {- return values[random().nextInt(values.length)];- }- }+ () -> TestUtil.nextLong(random(), 0, 6),+ () -> values[random().nextInt(values.length)]);}}@@ -2198,22 +2271,31 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTes}public void testGCDCompression() throws Exception {+ doTestGCDCompression(1);+ }++ public void testSparseGCDCompression() throws Exception {+ doTestGCDCompression(random().nextDouble());+ }++ private void doTestGCDCompression(double density) throws Exception {int numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {final long min = - (((long) random().nextInt(1 << 30)) << 32);final long mul = random().nextInt() & 0xFFFFFFFFL;- final LongProducer longs = new LongProducer() {- @Override- long next() {- return min + mul * random().nextInt(1 << 20);- }+ final LongSupplier longs = () -> {+ return min + mul * random().nextInt(1 << 20);};- doTestNumericsVsStoredFields(longs);+ doTestNumericsVsStoredFields(density, longs);}}public void testZeros() throws Exception {- doTestNumericsVsStoredFields(0, 0);+ doTestNumericsVsStoredFields(1, () -> 0);+ }++ public void testSparseZeros() throws Exception {+ doTestNumericsVsStoredFields(random().nextDouble(), () -> 0);}public void testZeroOrMin() throws Exception {@@ -2221,13 +2303,10 @@ public abstract class BaseDocValuesFormatTestCase extends BaseIndexFileFormatTes// the GCD of 0 and MIN_VALUE is negativeint numIterations = atLeast(1);for (int i = 0; i < numIterations; i++) {- final LongProducer longs = new LongProducer() {- @Override- long next() {- return random().nextBoolean() ? 0 : Long.MIN_VALUE;- }+ final LongSupplier longs = () -> {+ return random().nextBoolean() ? 0 : Long.MIN_VALUE;};- doTestNumericsVsStoredFields(longs);+ doTestNumericsVsStoredFields(1, longs);}}diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BaseIndexFileFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BaseIndexFileFormatTestCase.javaindex d56e6cb32b7..7a7abc091c7 100644--- a/lucene/test-framework/src/java/org/apache/lucene/index/BaseIndexFileFormatTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/index/BaseIndexFileFormatTestCase.java@@ -361,6 +361,12 @@ abstract class BaseIndexFileFormatTestCase extends LuceneTestCase {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return target == 0;+ }+@Overridepublic long cost() {return 1;@@ -414,6 +420,12 @@ abstract class BaseIndexFileFormatTestCase extends LuceneTestCase {return docID;}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return target == 0;+ }+@Overridepublic long cost() {return 1;diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BaseNormsFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BaseNormsFormatTestCase.javaindex 64e99da53f7..cd62218056a 100644--- a/lucene/test-framework/src/java/org/apache/lucene/index/BaseNormsFormatTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/index/BaseNormsFormatTestCase.java@@ -21,6 +21,7 @@ import java.util.ArrayList;import java.util.List;import java.util.Random;import java.util.concurrent.CountDownLatch;+import java.util.function.LongSupplier;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.MockAnalyzer;@@ -59,9 +60,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasint iterations = atLeast(1);final Random r = random();for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(1, new LongProducer() {+ doTestNormsVersusDocValues(1, new LongSupplier() {@Override- long next() {+ public long getAsLong() {return TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE);}});@@ -73,9 +74,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasint iterations = atLeast(1);final Random r = random();for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(random().nextDouble(), new LongProducer() {+ doTestNormsVersusDocValues(random().nextDouble(), new LongSupplier() {@Override- long next() {+ public long getAsLong() {return TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE);}});@@ -86,9 +87,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasint iterations = atLeast(1);final Random r = random();for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(1, new LongProducer() {+ doTestNormsVersusDocValues(1, new LongSupplier() {@Override- long next() {+ public long getAsLong() {return TestUtil.nextLong(r, Short.MIN_VALUE, Short.MAX_VALUE);}});@@ -100,9 +101,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasint iterations = atLeast(1);final Random r = random();for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(random().nextDouble(), new LongProducer() {+ doTestNormsVersusDocValues(random().nextDouble(), new LongSupplier() {@Override- long next() {+ public long getAsLong() {return TestUtil.nextLong(r, Short.MIN_VALUE, Short.MAX_VALUE);}});@@ -113,9 +114,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasint iterations = atLeast(1);final Random r = random();for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(1, new LongProducer() {+ doTestNormsVersusDocValues(1, new LongSupplier() {@Override- long next() {+ public long getAsLong() {return TestUtil.nextLong(r, Long.MIN_VALUE, Long.MAX_VALUE);}});@@ -127,9 +128,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasint iterations = atLeast(1);final Random r = random();for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(random().nextDouble(), new LongProducer() {+ doTestNormsVersusDocValues(random().nextDouble(), new LongSupplier() {@Override- long next() {+ public long getAsLong() {return TestUtil.nextLong(r, Long.MIN_VALUE, Long.MAX_VALUE);}});@@ -140,9 +141,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasint iterations = atLeast(1);final Random r = random();for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(1, new LongProducer() {+ doTestNormsVersusDocValues(1, new LongSupplier() {@Override- long next() {+ public long getAsLong() {int thingToDo = r.nextInt(3);switch (thingToDo) {case 0: return Long.MIN_VALUE;@@ -159,9 +160,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasint iterations = atLeast(1);final Random r = random();for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(random().nextDouble(), new LongProducer() {+ doTestNormsVersusDocValues(random().nextDouble(), new LongSupplier() {@Override- long next() {+ public long getAsLong() {int thingToDo = r.nextInt(3);switch (thingToDo) {case 0: return Long.MIN_VALUE;@@ -177,9 +178,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasint iterations = atLeast(1);final Random r = random();for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(1, new LongProducer() {+ doTestNormsVersusDocValues(1, new LongSupplier() {@Override- long next() {+ public long getAsLong() {return r.nextBoolean() ? 20 : 3;}});@@ -191,9 +192,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasint iterations = atLeast(1);final Random r = random();for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(random().nextDouble(), new LongProducer() {+ doTestNormsVersusDocValues(random().nextDouble(), new LongSupplier() {@Override- long next() {+ public long getAsLong() {return r.nextBoolean() ? 20 : 3;}});@@ -204,9 +205,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasint iterations = atLeast(1);final Random r = random();for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(1, new LongProducer() {+ doTestNormsVersusDocValues(1, new LongSupplier() {@Override- long next() {+ public long getAsLong() {return r.nextBoolean() ? 1000000L : -5000;}});@@ -218,9 +219,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasint iterations = atLeast(1);final Random r = random();for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(random().nextDouble(), new LongProducer() {+ doTestNormsVersusDocValues(random().nextDouble(), new LongSupplier() {@Override- long next() {+ public long getAsLong() {return r.nextBoolean() ? 1000000L : -5000;}});@@ -230,9 +231,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCaspublic void testAllZeros() throws Exception {int iterations = atLeast(1);for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(1, new LongProducer() {+ doTestNormsVersusDocValues(1, new LongSupplier() {@Override- long next() {+ public long getAsLong() {return 0;}});@@ -243,9 +244,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasassumeTrue("Requires sparse norms support", codecSupportsSparsity());int iterations = atLeast(1);for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(random().nextDouble(), new LongProducer() {+ doTestNormsVersusDocValues(random().nextDouble(), new LongSupplier() {@Override- long next() {+ public long getAsLong() {return 0;}});@@ -256,9 +257,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasint iterations = atLeast(1);final Random r = random();for (int i = 0; i < iterations; i++) {- doTestNormsVersusDocValues(1, new LongProducer() {+ doTestNormsVersusDocValues(1, new LongSupplier() {@Override- long next() {+ public long getAsLong() {return r.nextInt(100) == 0 ? TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE) : 0;}});@@ -270,9 +271,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasfinal Random r = random();for (int i = 0; i < iterations; i++) {final long commonValue = TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE);- doTestNormsVersusDocValues(1, new LongProducer() {+ doTestNormsVersusDocValues(1, new LongSupplier() {@Override- long next() {+ public long getAsLong() {return r.nextInt(100) == 0 ? TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE) : commonValue;}});@@ -285,9 +286,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasfinal Random r = random();for (int i = 0; i < iterations; i++) {final long commonValue = TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE);- doTestNormsVersusDocValues(random().nextDouble(), new LongProducer() {+ doTestNormsVersusDocValues(random().nextDouble(), new LongSupplier() {@Override- long next() {+ public long getAsLong() {return r.nextInt(100) == 0 ? TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE) : commonValue;}});@@ -300,9 +301,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasfor (int i = 0; i < iterations; i++) {final long commonValue = TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE);final long uncommonValue = TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE);- doTestNormsVersusDocValues(1, new LongProducer() {+ doTestNormsVersusDocValues(1, new LongSupplier() {@Override- long next() {+ public long getAsLong() {return r.nextInt(100) == 0 ? uncommonValue : commonValue;}});@@ -316,9 +317,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasfor (int i = 0; i < iterations; i++) {final long commonValue = TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE);final long uncommonValue = TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE);- doTestNormsVersusDocValues(random().nextDouble(), new LongProducer() {+ doTestNormsVersusDocValues(random().nextDouble(), new LongSupplier() {@Override- long next() {+ public long getAsLong() {return r.nextInt(100) == 0 ? uncommonValue : commonValue;}});@@ -337,9 +338,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasfor (int j = 0; j < numOtherValues; ++j) {otherValues[j] = TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE);}- doTestNormsVersusDocValues(1, new LongProducer() {+ doTestNormsVersusDocValues(1, new LongSupplier() {@Override- long next() {+ public long getAsLong() {return r.nextInt(100) == 0 ? otherValues[r.nextInt(numOtherValues - 1)] : commonValues[r.nextInt(N - 1)];}});@@ -358,9 +359,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasfor (int j = 0; j < numOtherValues; ++j) {otherValues[j] = TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE);}- doTestNormsVersusDocValues(random().nextDouble(), new LongProducer() {+ doTestNormsVersusDocValues(random().nextDouble(), new LongSupplier() {@Override- long next() {+ public long getAsLong() {return r.nextInt(100) == 0 ? otherValues[r.nextInt(numOtherValues - 1)] : commonValues[r.nextInt(N - 1)];}});@@ -386,9 +387,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasfor (int j = 0; j < numOtherValues; ++j) {otherValues[j] = TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE);}- doTestNormsVersusDocValues(1, new LongProducer() {+ doTestNormsVersusDocValues(1, new LongSupplier() {@Override- long next() {+ public long getAsLong() {return r.nextInt(100) == 0 ? otherValues[r.nextInt(numOtherValues - 1)] : commonValues[r.nextInt(N - 1)];}});@@ -417,9 +418,9 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCasfor (int j = 0; j < numOtherValues; ++j) {otherValues[j] = TestUtil.nextLong(r, Byte.MIN_VALUE, Byte.MAX_VALUE);}- doTestNormsVersusDocValues(random().nextDouble(), new LongProducer() {+ doTestNormsVersusDocValues(random().nextDouble(), new LongSupplier() {@Override- long next() {+ public long getAsLong() {return r.nextInt(100) == 0 ? otherValues[r.nextInt(numOtherValues - 1)] : commonValues[r.nextInt(N - 1)];}});@@ -427,7 +428,7 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCas}}- private void doTestNormsVersusDocValues(double density, LongProducer longs) throws Exception {+ private void doTestNormsVersusDocValues(double density, LongSupplier longs) throws Exception {int numDocs = atLeast(500);final FixedBitSet docsWithField = new FixedBitSet(numDocs);final int numDocsWithField = Math.max(1, (int) (density * numDocs));@@ -445,7 +446,7 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCas}long norms[] = new long[numDocsWithField];for (int i = 0; i < numDocsWithField; i++) {- norms[i] = longs.next();+ norms[i] = longs.getAsLong();}Directory dir = newDirectory();@@ -519,10 +520,6 @@ public abstract class BaseNormsFormatTestCase extends BaseIndexFileFormatTestCas}}- static abstract class LongProducer {- abstract long next();- }-static class CannedNormSimilarity extends Similarity {final long norms[];int index = 0;diff --git a/solr/core/src/java/org/apache/solr/request/DocValuesFacets.java b/solr/core/src/java/org/apache/solr/request/DocValuesFacets.javaindex 09ad8368bca..0dab34bf506 100644--- a/solr/core/src/java/org/apache/solr/request/DocValuesFacets.java+++ b/solr/core/src/java/org/apache/solr/request/DocValuesFacets.java@@ -272,11 +272,8 @@ public class DocValuesFacets {final LongValues ordmap = map == null ? null : map.getGlobalOrds(subIndex);int doc;while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- if (doc > si.docID()) {- si.advance(doc);- }int term;- if (doc == si.docID()) {+ if (si.advanceExact(doc)) {term = si.ordValue();} else {term = -1;@@ -301,10 +298,7 @@ public class DocValuesFacets {int doc;while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- if (doc > si.docID()) {- si.advance(doc);- }- if (doc == si.docID()) {+ if (si.advanceExact(doc)) {segCounts[1+si.ordValue()]++;} else {segCounts[0]++;@@ -334,10 +328,7 @@ public class DocValuesFacets {final LongValues ordMap = map == null ? null : map.getGlobalOrds(subIndex);int doc;while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- if (doc > si.docID()) {- si.advance(doc);- }- if (doc == si.docID()) {+ if (si.advanceExact(doc)) {// strange do-while to collect the missing count (first ord is NO_MORE_ORDS)int term = (int) si.nextOrd();do {@@ -365,10 +356,7 @@ public class DocValuesFacets {int doc;while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- if (doc > si.docID()) {- si.advance(doc);- }- if (doc == si.docID()) {+ if (si.advanceExact(doc)) {int term = (int) si.nextOrd();do {segCounts[1+term]++;diff --git a/solr/core/src/java/org/apache/solr/request/PerSegmentSingleValuedFaceting.java b/solr/core/src/java/org/apache/solr/request/PerSegmentSingleValuedFaceting.javaindex 0c88b80bfb7..3db7b4ccb93 100644--- a/solr/core/src/java/org/apache/solr/request/PerSegmentSingleValuedFaceting.java+++ b/solr/core/src/java/org/apache/solr/request/PerSegmentSingleValuedFaceting.java@@ -293,11 +293,8 @@ class PerSegmentSingleValuedFaceting {// specialized version when collecting counts for all termsint doc;while ((doc = iter.nextDoc()) < DocIdSetIterator.NO_MORE_DOCS) {- if (doc > si.docID()) {- si.advance(doc);- }int t;- if (doc == si.docID()) {+ if (si.advanceExact(doc)) {t = 1+si.ordValue();} else {t = 0;@@ -309,11 +306,8 @@ class PerSegmentSingleValuedFaceting {// version that adjusts term numbers because we aren't collecting the full rangeint doc;while ((doc = iter.nextDoc()) < DocIdSetIterator.NO_MORE_DOCS) {- if (doc > si.docID()) {- si.advance(doc);- }int term;- if (doc == si.docID()) {+ if (si.advanceExact(doc)) {term = si.ordValue();} else {term = -1;diff --git a/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java b/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.javaindex 933477b1ac1..5ac19753a2b 100644--- a/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java+++ b/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java@@ -795,7 +795,7 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrIcontinue;}Long val;- if (ndv.advance(localId) == localId) {+ if (ndv.advanceExact(localId)) {val = ndv.longValue();} else {continue;@@ -820,7 +820,7 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrIcontinue;}BytesRef value;- if (bdv.advance(localId) == localId) {+ if (bdv.advanceExact(localId)) {value = BytesRef.deepCopyOf(bdv.binaryValue());} else {continue;@@ -832,7 +832,7 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrIif (sdv == null) {continue;}- if (sdv.advance(localId) == localId) {+ if (sdv.advanceExact(localId)) {final BytesRef bRef = sdv.binaryValue();// Special handling for Boolean fields since they're stored as 'T' and 'F'.if (schemaField.getType() instanceof BoolField) {diff --git a/solr/core/src/java/org/apache/solr/search/facet/FacetFieldProcessorByArrayDV.java b/solr/core/src/java/org/apache/solr/search/facet/FacetFieldProcessorByArrayDV.javaindex fb609450320..88adf6751d6 100644--- a/solr/core/src/java/org/apache/solr/search/facet/FacetFieldProcessorByArrayDV.java+++ b/solr/core/src/java/org/apache/solr/search/facet/FacetFieldProcessorByArrayDV.java@@ -186,10 +186,7 @@ class FacetFieldProcessorByArrayDV extends FacetFieldProcessorByArray {int doc;while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- if (doc > singleDv.docID()) {- singleDv.advance(doc);- }- if (doc == singleDv.docID()) {+ if (singleDv.advanceExact(doc)) {counts[ singleDv.ordValue() + 1 ]++;} else {counts[ 0 ]++;@@ -211,10 +208,7 @@ class FacetFieldProcessorByArrayDV extends FacetFieldProcessorByArray {int doc;while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- if (doc > multiDv.docID()) {- multiDv.advance(doc);- }- if (doc == multiDv.docID()) {+ if (multiDv.advanceExact(doc)) {for(;;) {int segOrd = (int)multiDv.nextOrd();if (segOrd < 0) break;@@ -247,10 +241,7 @@ class FacetFieldProcessorByArrayDV extends FacetFieldProcessorByArray {private void collectDocs(SortedDocValues singleDv, DocIdSetIterator disi, LongValues toGlobal) throws IOException {int doc;while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- if (doc > singleDv.docID()) {- singleDv.advance(doc);- }- if (doc == singleDv.docID()) {+ if (singleDv.advanceExact(doc)) {int segOrd = singleDv.ordValue();collect(doc, segOrd, toGlobal);}@@ -260,10 +251,7 @@ class FacetFieldProcessorByArrayDV extends FacetFieldProcessorByArray {private void collectCounts(SortedDocValues singleDv, DocIdSetIterator disi, LongValues toGlobal) throws IOException {int doc;while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- if (doc > singleDv.docID()) {- singleDv.advance(doc);- }- if (doc == singleDv.docID()) {+ if (singleDv.advanceExact(doc)) {int segOrd = singleDv.ordValue();int ord = (int)toGlobal.get(segOrd);countAcc.incrementCount(ord, 1);@@ -274,10 +262,7 @@ class FacetFieldProcessorByArrayDV extends FacetFieldProcessorByArray {private void collectDocs(SortedSetDocValues multiDv, DocIdSetIterator disi, LongValues toGlobal) throws IOException {int doc;while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- if (doc > multiDv.docID()) {- multiDv.advance(doc);- }- if (doc == multiDv.docID()) {+ if (multiDv.advanceExact(doc)) {for(;;) {int segOrd = (int)multiDv.nextOrd();if (segOrd < 0) break;@@ -290,10 +275,7 @@ class FacetFieldProcessorByArrayDV extends FacetFieldProcessorByArray {private void collectCounts(SortedSetDocValues multiDv, DocIdSetIterator disi, LongValues toGlobal) throws IOException {int doc;while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- if (doc > multiDv.docID()) {- multiDv.advance(doc);- }- if (doc == multiDv.docID()) {+ if (multiDv.advanceExact(doc)) {for(;;) {int segOrd = (int)multiDv.nextOrd();if (segOrd < 0) break;diff --git a/solr/core/src/java/org/apache/solr/uninverting/FieldCacheImpl.java b/solr/core/src/java/org/apache/solr/uninverting/FieldCacheImpl.javaindex b63e5e93365..2224010a85f 100644--- a/solr/core/src/java/org/apache/solr/uninverting/FieldCacheImpl.java+++ b/solr/core/src/java/org/apache/solr/uninverting/FieldCacheImpl.java@@ -691,6 +691,12 @@ class FieldCacheImpl implements FieldCache {}}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return docsWithField.get(docID);+ }+@Overridepublic long cost() {return values.size();@@ -820,6 +826,12 @@ class FieldCacheImpl implements FieldCache {}}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return docToTermOrd.get(docID) != 0;+ }+@Overridepublic long cost() {return 0;@@ -1021,6 +1033,12 @@ class FieldCacheImpl implements FieldCache {}}+ @Override+ public boolean advanceExact(int target) throws IOException {+ docID = target;+ return docsWithField.get(docID);+ }+@Overridepublic long cost() {return 0;