Apache Lucene open-source search software
Implement #docIDRunEnd() on PostingsEnum. (#14390)
Adrien Granddb00348ef18e
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene101/Lucene101PostingsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene101/Lucene101PostingsReader.javaindex 930ac0251f2..052e281ca56 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene101/Lucene101PostingsReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene101/Lucene101PostingsReader.java@@ -1059,6 +1059,32 @@ public final class Lucene101PostingsReader extends PostingsReaderBase {}}+ @Override+ public int docIDRunEnd() throws IOException {+ // Note: this assumes that BLOCK_SIZE == 128, this bit of the code would need to be changed if+ // the block size was changed.+ // Hack to avoid compiler warning that both sides of the equal sign are identical.+ long blockSize = BLOCK_SIZE;+ assert blockSize == 2 * Long.SIZE;+ boolean level0IsDense =+ encoding == DeltaEncoding.UNARY+ && docBitSet.getBits()[0] == -1L+ && docBitSet.getBits()[1] == -1L;+ if (level0IsDense) {++ int level0DocCountUpto = docFreq - docCountLeft;+ boolean level1IsDense =+ level1LastDocID - level0LastDocID == level1DocCountUpto - level0DocCountUpto;+ if (level1IsDense) {+ return level1LastDocID + 1;+ }++ return level0LastDocID + 1;+ }++ return super.docIDRunEnd();+ }+private void skipPositions(int freq) throws IOException {// Skip positions now:int toSkip = posPendingCount - freq;diff --git a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.javaindex 0cad7d72dc1..c3369cfec39 100644--- a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java+++ b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java@@ -2022,11 +2022,22 @@ public final class CheckIndex implements Closeable {}}- // Checking score blocks is heavy, we only do it on long postings lists, on every 1024th- // term or if slow checks are enabled.+ // Checking score blocks and doc ID runs is heavy, we only do it on long postings lists, on+ // every 1024th term or if slow checks are enabled.if (level >= Level.MIN_LEVEL_FOR_SLOW_CHECKS|| docFreq > 1024|| (status.termCount + status.delTermCount) % 1024 == 0) {+ postings = termsEnum.postings(postings, PostingsEnum.NONE);+ checkDocIDRuns(postings);+ if (hasFreqs) {+ postings = termsEnum.postings(postings, PostingsEnum.FREQS);+ checkDocIDRuns(postings);+ }+ if (hasPositions) {+ postings = termsEnum.postings(postings, PostingsEnum.POSITIONS);+ checkDocIDRuns(postings);+ }+// First check max scores and block uptos// But only if slow checks are enabled since we visit all docsif (level >= Level.MIN_LEVEL_FOR_SLOW_CHECKS) {@@ -2458,6 +2469,31 @@ public final class CheckIndex implements Closeable {}}+ private static void checkDocIDRuns(DocIdSetIterator iterator) throws IOException {+ int prevDoc = -1;+ int runEnd = 0;+ for (int doc = iterator.nextDoc();+ doc != DocIdSetIterator.NO_MORE_DOCS;+ doc = iterator.nextDoc()) {+ if (prevDoc + 1 < runEnd && doc != prevDoc + 1) {+ throw new CheckIndexException(+ "Run end is " + runEnd + " but next doc after " + prevDoc + " is " + doc);+ }+ int newRunEnd = iterator.docIDRunEnd();+ if (newRunEnd <= doc) {+ throw new CheckIndexException("Run end " + newRunEnd + " is <= doc ID " + doc);+ }+ if (newRunEnd > runEnd) {+ runEnd = newRunEnd;+ }+ prevDoc = doc;+ }++ if (runEnd != prevDoc + 1) {+ throw new CheckIndexException("Run end is " + runEnd + " but last doc is " + prevDoc);+ }+ }+/*** For use in tests only.*diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BasePostingsFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BasePostingsFormatTestCase.javaindex 8e0292b3f8d..d485251d202 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BasePostingsFormatTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BasePostingsFormatTestCase.java@@ -49,6 +49,7 @@ import org.apache.lucene.index.IndexOptions;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;+import org.apache.lucene.index.IndexWriterConfig.OpenMode;import org.apache.lucene.index.IndexableField;import org.apache.lucene.index.LeafReader;import org.apache.lucene.index.MultiTerms;@@ -1768,4 +1769,35 @@ public abstract class BasePostingsFormatTestCase extends BaseIndexFileFormatTestIOUtils.close(reader, w2, dir1, dir2);}++ public void testDocIDRunEnd() throws Exception {+ Directory dir = newDirectory();+ for (int iter = 0; iter < 100; ++iter) {+ IndexWriterConfig iwc = newIndexWriterConfig(null).setOpenMode(OpenMode.CREATE);+ iwc.setCodec(getCodec());+ // Prevent randomization from slowing down indexing too much+ if (iwc.getMaxBufferedDocs() < 1_000) {+ iwc.setMaxBufferedDocs(1_000);+ }+ IndexWriter iw = new IndexWriter(dir, iwc);+ Document emptyDoc = new Document();+ Document doc = new Document();+ doc.add(new StringField("", "something", Field.Store.NO));+ int numEmptyDocs = TestUtil.nextInt(random(), 0, 5_000);+ int numDocs = TestUtil.nextInt(random(), 4096, 20_000);+ for (int i = 0; i < numEmptyDocs; ++i) {+ iw.addDocument(emptyDoc);+ }+ for (int i = 0; i < numDocs; ++i) {+ iw.addDocument(doc);+ }+ iw.forceMerge(1);+ DirectoryReader ir = DirectoryReader.open(iw);+ LeafReader ar = getOnlyLeafReader(ir);+ TestUtil.checkReader(ar);+ ir.close();+ iw.close();+ }+ dir.close();+ }}