Apache Solr open-source search software
Load freqs lazily in Postings (#595)
Mayya Sharipova13ef5032fe14
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 5567f6a7df0..c726b0b43fe 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -168,6 +168,9 @@ Optimizations* LUCENE-8896: Override default implementation of IntersectVisitor#visit(DocIDSetBuilder, byte[])for several queries. (Ignacio Vera)+* LUCENE-8901: Load frequencies lazily only when needed in BlockDocsEnum and+ BlockImpactsEverythingEnum (Mayya Sharipova).+Test Framework* LUCENE-8825: CheckHits now display the shard index in case of mismatchdiff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50PostingsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50PostingsReader.javaindex ede8bdd3d62..d009d9d93b8 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50PostingsReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50PostingsReader.java@@ -270,7 +270,6 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {private int docUpto; // how many docs we've readprivate int doc; // doc we last readprivate int accum; // accumulator for doc deltas- private int freq; // freq we last read// Where this term's postings start in the .doc file:private long docTermStartFP;@@ -285,6 +284,9 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {private int nextSkipDoc;private boolean needsFreq; // true if the caller actually needs frequencies+ // as we read freqs lazily, isFreqsRead shows if freqs are read for the current block+ // always true when we don't have freqs (indexHasFreq=false) or don't need freqs (needsFreq=false)+ private boolean isFreqsRead;private int singletonDocID; // docid when there is a single pulsed posting, otherwise -1public BlockDocsEnum(FieldInfo fieldInfo) throws IOException {@@ -320,6 +322,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {doc = -1;this.needsFreq = PostingsEnum.featureRequested(flags, PostingsEnum.FREQS);+ this.isFreqsRead = true;if (indexHasFreq == false || needsFreq == false) {Arrays.fill(freqBuffer, 1);}@@ -330,10 +333,14 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {skipped = false;return this;}-+@Overridepublic int freq() throws IOException {- return freq;+ if (isFreqsRead == false) {+ forUtil.readBlock(docIn, encoded, freqBuffer); // read freqs for this block+ isFreqsRead = true;+ }+ return freqBuffer[docBufferUpto-1];}@Override@@ -362,6 +369,12 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {}private void refillDocs() throws IOException {+ // Check if we skipped reading the previous block of freqs, and if yes, position docIn after it+ if (isFreqsRead == false) {+ forUtil.skipBlock(docIn);+ isFreqsRead = true;+ }+final int left = docFreq - docUpto;assert left > 0;@@ -370,9 +383,9 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {if (indexHasFreq) {if (needsFreq) {- forUtil.readBlock(docIn, encoded, freqBuffer);+ isFreqsRead = false;} else {- forUtil.skipBlock(docIn); // skip over freqs+ forUtil.skipBlock(docIn); // skip over freqs if we don't need them at all}}} else if (docFreq == 1) {@@ -391,22 +404,19 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {return doc = NO_MORE_DOCS;}if (docBufferUpto == BLOCK_SIZE) {- refillDocs();+ refillDocs(); // we don't need to load freqs for now (will be loaded later if necessary)}accum += docDeltaBuffer[docBufferUpto];docUpto++;doc = accum;- freq = freqBuffer[docBufferUpto];docBufferUpto++;return doc;}@Overridepublic int advance(int target) throws IOException {- // TODO: make frq block load lazy/skippable-// current skip docID < docIDs generated from current buffer <= next skip docID// we don't need to skip if target is buffered alreadyif (docFreq > BLOCK_SIZE && target > nextSkipDoc) {@@ -442,6 +452,10 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {docBufferUpto = BLOCK_SIZE;accum = skipper.getDoc(); // actually, this is just lastSkipEntrydocIn.seek(skipper.getDocPointer()); // now point to the block we want to search+ // even if freqs were not read from the previous block, we will mark them as read,+ // as we don't need to skip the previous block freqs in refillDocs,+ // as we have already positioned docIn where in needs to be.+ isFreqsRead = true;}// next time we call advance, this is used to// foresee whether skipper is necessary.@@ -469,7 +483,6 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {}}- freq = freqBuffer[docBufferUpto];docBufferUpto++;return doc = accum;}@@ -482,7 +495,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {final class BlockPostingsEnum extends PostingsEnum {-+private final byte[] encoded;private final int[] docDeltaBuffer = new int[MAX_DATA_SIZE];@@ -542,7 +555,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {private int nextSkipDoc;private int singletonDocID; // docid when there is a single pulsed posting, otherwise -1-+public BlockPostingsEnum(FieldInfo fieldInfo) throws IOException {this.startDocIn = Lucene50PostingsReader.this.docIn;this.docIn = null;@@ -557,7 +570,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {indexHasOffsets == (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0) &&indexHasPayloads == fieldInfo.hasPayloads();}-+public PostingsEnum reset(IntBlockTermState termState) throws IOException {docFreq = termState.docFreq;docTermStartFP = termState.docStartFP;@@ -595,7 +608,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {skipped = false;return this;}-+@Overridepublic int freq() throws IOException {return freq;@@ -622,7 +635,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {}docBufferUpto = 0;}-+private void refillPositions() throws IOException {if (posIn.getFilePointer() == lastPosBlockFP) {final int count = (int) (totalTermFreq % BLOCK_SIZE);@@ -671,7 +684,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {position = 0;return doc;}-+@Overridepublic int advance(int target) throws IOException {// TODO: make frq block load lazy/skippable@@ -695,7 +708,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {skipped = true;}- final int newDocUpto = skipper.skipTo(target) + 1;+ final int newDocUpto = skipper.skipTo(target) + 1;if (newDocUpto > docUpto) {// Skipper moved@@ -796,17 +809,17 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {public int startOffset() {return -1;}-+@Overridepublic int endOffset() {return -1;}-+@Overridepublic BytesRef getPayload() {return null;}-+@Overridepublic long cost() {return docFreq;@@ -815,7 +828,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {// Also handles payloads + offsetsfinal class EverythingEnum extends PostingsEnum {-+private final byte[] encoded;private final int[] docDeltaBuffer = new int[MAX_DATA_SIZE];@@ -891,11 +904,11 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {private long skipOffset;private int nextSkipDoc;-+private boolean needsOffsets; // true if we actually need offsetsprivate boolean needsPayloads; // true if we actually need payloadsprivate int singletonDocID; // docid when there is a single pulsed posting, otherwise -1-+public EverythingEnum(FieldInfo fieldInfo) throws IOException {this.startDocIn = Lucene50PostingsReader.this.docIn;this.docIn = null;@@ -930,7 +943,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {indexHasOffsets == (fieldInfo.getIndexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0) &&indexHasPayloads == fieldInfo.hasPayloads();}-+public EverythingEnum reset(IntBlockTermState termState, int flags) throws IOException {docFreq = termState.docFreq;docTermStartFP = termState.docStartFP;@@ -972,7 +985,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {skipped = false;return this;}-+@Overridepublic int freq() throws IOException {return freq;@@ -998,7 +1011,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {}docBufferUpto = 0;}-+private void refillPositions() throws IOException {if (posIn.getFilePointer() == lastPosBlockFP) {final int count = (int) (totalTermFreq % BLOCK_SIZE);@@ -1088,7 +1101,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {lastStartOffset = 0;return doc;}-+@Overridepublic int advance(int target) throws IOException {// TODO: make frq block load lazy/skippable@@ -1112,7 +1125,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {skipped = true;}- final int newDocUpto = skipper.skipTo(target) + 1;+ final int newDocUpto = skipper.skipTo(target) + 1;if (newDocUpto > docUpto) {// Skipper moved@@ -1218,7 +1231,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {@Overridepublic int nextPosition() throws IOException {assert posPendingCount > 0;-+if (posPendingFP != -1) {posIn.seek(posPendingFP);posPendingFP = -1;@@ -1266,12 +1279,12 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {public int startOffset() {return startOffset;}-+@Overridepublic int endOffset() {return endOffset;}-+@Overridepublic BytesRef getPayload() {if (payloadLength == 0) {@@ -1280,13 +1293,14 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {return payload;}}-+@Overridepublic long cost() {return docFreq;}}+final class BlockImpactsEverythingEnum extends ImpactsEnum {private final byte[] encoded;@@ -1325,9 +1339,9 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {private int docFreq; // number of docs in this posting listprivate long totalTermFreq; // number of positions in this posting listprivate int docUpto; // how many docs we've read+ private int posDocUpTo; // for how many docs we've read positions, offsets, and payloadsprivate int doc; // doc we last readprivate int accum; // accumulator for doc deltas- private int freq; // freq we last readprivate int position; // current position// how many positions "behind" we are; nextPosition must@@ -1362,6 +1376,8 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {private final boolean needsPositions;private final boolean needsOffsets; // true if we actually need offsetsprivate final boolean needsPayloads; // true if we actually need payloads++ private boolean isFreqsRead; // shows if freqs for the current doc block are read into freqBufferprivate long seekTo = -1;@@ -1431,6 +1447,8 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {doc = -1;accum = 0;docUpto = 0;+ posDocUpTo = 0;+ isFreqsRead = true;docBufferUpto = BLOCK_SIZE;skipper = new Lucene50ScoreSkipReader(version,@@ -1448,7 +1466,11 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {@Overridepublic int freq() throws IOException {- return freq;+ if (indexHasFreq && (isFreqsRead == false)) {+ forUtil.readBlock(docIn, encoded, freqBuffer); // read freqs for this block+ isFreqsRead = true;+ }+ return freqBuffer[docBufferUpto-1];}@Override@@ -1457,13 +1479,31 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {}private void refillDocs() throws IOException {+ if (indexHasFreq) {+ if (isFreqsRead == false) { // previous freq block was not read+ // check if we need to load the previous freq block to catch up on positions or we can skip it+ if (indexHasPos && needsPositions && (posDocUpTo < docUpto)) {+ forUtil.readBlock(docIn, encoded, freqBuffer); // load the previous freq block+ } else {+ forUtil.skipBlock(docIn); // skip it+ }+ isFreqsRead = true;+ }+ if (indexHasPos && needsPositions) {+ while (posDocUpTo < docUpto) { // catch on positions, bring posPendingCount upto the current doc+ posPendingCount += freqBuffer[docBufferUpto - (docUpto - posDocUpTo)];+ posDocUpTo++;+ }+ }+ }+final int left = docFreq - docUpto;assert left > 0;if (left >= BLOCK_SIZE) {forUtil.readBlock(docIn, encoded, docDeltaBuffer);if (indexHasFreq) {- forUtil.readBlock(docIn, encoded, freqBuffer);+ isFreqsRead = false; // freq block will be loaded lazily when necessary, we don't load it here}} else {readVIntBlock(docIn, docDeltaBuffer, freqBuffer, left, indexHasFreq);@@ -1551,6 +1591,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {// Skipper movedassert newDocUpto % BLOCK_SIZE == 0 : "got " + newDocUpto;docUpto = newDocUpto;+ posDocUpTo = docUpto;// Force to read next blockdocBufferUpto = BLOCK_SIZE;@@ -1592,6 +1633,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {if (seekTo >= 0) {docIn.seek(seekTo);seekTo = -1;+ isFreqsRead = true; // reset isFreqsRead}refillDocs();}@@ -1599,8 +1641,6 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {// Now scan:while (true) {accum += docDeltaBuffer[docBufferUpto];- freq = freqBuffer[docBufferUpto];- posPendingCount += freq;docBufferUpto++;docUpto++;@@ -1623,7 +1663,7 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {// blocks only to sum up how many positions to skipprivate void skipPositions() throws IOException {// Skip positions now:- int toSkip = posPendingCount - freq;+ int toSkip = posPendingCount - freqBuffer[docBufferUpto-1];// if (DEBUG) {// System.out.println(" FPR.skipPositions: toSkip=" + toSkip);// }@@ -1678,6 +1718,16 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {if (indexHasPos == false || needsPositions == false) {return -1;}++ if (isFreqsRead == false) {+ forUtil.readBlock(docIn, encoded, freqBuffer); // read freqs for this docs block+ isFreqsRead = true;+ }+ while (posDocUpTo < docUpto) { // bring posPendingCount upto the current doc+ posPendingCount += freqBuffer[docBufferUpto - (docUpto - posDocUpTo)];+ posDocUpTo++;+ }+assert posPendingCount > 0;if (posPendingFP != -1) {@@ -1693,9 +1743,9 @@ public final class Lucene50PostingsReader extends PostingsReaderBase {posBufferUpto = BLOCK_SIZE;}- if (posPendingCount > freq) {+ if (posPendingCount > freqBuffer[docBufferUpto-1]) {skipPositions();- posPendingCount = freq;+ posPendingCount = freqBuffer[docBufferUpto-1];}if (posBufferUpto == BLOCK_SIZE) {