Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid reload block when seeking backward in SegmentTermsEnum. #13253

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ private boolean setEOF() {
@Override
public boolean seekExact(BytesRef target) throws IOException {

int originEndCount = -1;
vsop-479 marked this conversation as resolved.
Show resolved Hide resolved
if (fr.index == null) {
throw new IllegalStateException("terms index was not loaded");
}
Expand Down Expand Up @@ -435,7 +436,9 @@ public boolean seekExact(BytesRef target) throws IOException {
// rewind frame ord=" + lastFrame.ord);
// }
currentFrame = lastFrame;
currentFrame.rewind();
originEndCount = currentFrame.entCount;
currentFrame.rewind2();

} else {
// Target is exactly the same as current term
assert term.length() == target.length;
Expand Down Expand Up @@ -519,6 +522,9 @@ public boolean seekExact(BytesRef target) throws IOException {
currentFrame.loadBlock();

final SeekStatus result = currentFrame.scanToTerm(target, true);
if (originEndCount != -1) {
currentFrame.entCount = originEndCount;
}
if (result == SeekStatus.FOUND) {
// if (DEBUG) {
// System.out.println(" return FOUND term=" + term.utf8ToString() + " " + term);
Expand Down Expand Up @@ -574,6 +580,9 @@ public boolean seekExact(BytesRef target) throws IOException {
currentFrame.loadBlock();

final SeekStatus result = currentFrame.scanToTerm(target, true);
if (originEndCount != -1) {
currentFrame.entCount = originEndCount;
}
if (result == SeekStatus.FOUND) {
// if (DEBUG) {
// System.out.println(" return FOUND term=" + term.utf8ToString() + " " + term);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,61 @@ void rewind() {
*/
}

// Only rewind, don't force reload block.
// Reset reader position, don't read, decompress.
// Current term greater than target, reduce endCount.
void rewind2() {
vsop-479 marked this conversation as resolved.
Show resolved Hide resolved

// Force reload:
fp = fpOrig;
// TODO: Reset entCount after this seek.
entCount = nextEnt;
vsop-479 marked this conversation as resolved.
Show resolved Hide resolved
nextEnt = 0;
hasTerms = hasTermsOrig;
if (isFloor) {
floorDataReader.setPosition(rewindPos);
numFollowFloorBlocks = floorDataReader.readVInt();
assert numFollowFloorBlocks > 0;
nextFloorLabel = floorDataReader.readByte() & 0xff;
}

suffixesReader.setPosition(0);
suffixLengthsReader.setPosition(0);
/*
//System.out.println("rewind");
// Keeps the block loaded, but rewinds its state:
if (nextEnt > 0 || fp != fpOrig) {
if (DEBUG) {
System.out.println(" rewind frame ord=" + ord + " fpOrig=" + fpOrig + " fp=" + fp + " hasTerms?=" + hasTerms + " isFloor?=" + isFloor + " nextEnt=" + nextEnt + " prefixLen=" + prefix);
}
if (fp != fpOrig) {
fp = fpOrig;
nextEnt = -1;
} else {
nextEnt = 0;
}
hasTerms = hasTermsOrig;
if (isFloor) {
floorDataReader.rewind();
numFollowFloorBlocks = floorDataReader.readVInt();
nextFloorLabel = floorDataReader.readByte() & 0xff;
}
assert suffixBytes != null;
suffixesReader.rewind();
assert statBytes != null;
statsReader.rewind();
metaDataUpto = 0;
state.termBlockOrd = 0;
// TODO: skip this if !hasTerms? Then postings
// impl wouldn't have to write useless 0 byte
postingsReader.resetTermsBlock(fieldInfo, state);
lastSubFP = -1;
} else if (DEBUG) {
System.out.println(" skip rewind fp=" + fp + " fpOrig=" + fpOrig + " nextEnt=" + nextEnt + " ord=" + ord);
}
*/
}

// Decodes next entry; returns true if it's a sub-block
public boolean next() throws IOException {
if (isLeafBlock) {
Expand Down
Loading