Skip to content

Commit

Permalink
Optimize loading frame for seekCeil
Browse files Browse the repository at this point in the history
  • Loading branch information
dnhatn committed Jan 25, 2024
1 parent 10bed51 commit 7d69cf9
Showing 1 changed file with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,17 @@ private long findBlockIndex(BytesRef target) throws IOException {

@Override
public SeekStatus seekCeil(BytesRef target) throws IOException {
state.blockIndex = Math.toIntExact(findBlockIndex(target));
if (state.blockIndex == meta.numBlocks) {
final int blockIndex = Math.toIntExact(findBlockIndex(target));
if (blockIndex == meta.numBlocks) {
return SeekStatus.END;
}
index.seek(blockStartAddress + blockAddresses.get(state.blockIndex));
loadFrame();

state.blockIndex = blockIndex;
if (state.blockIndex == loadedFrameIndex) {
resetFrame();
} else {
index.seek(blockStartAddress + blockAddresses.get(state.blockIndex));
loadFrame();
}
// TODO: should we support binary search here?
int cmp;
do {
Expand Down Expand Up @@ -432,14 +436,6 @@ public ImpactsEnum impacts(int flags) throws IOException {

@Override
public BytesRef next() throws IOException {
if (state.blockIndex != loadedFrameIndex) {
long termIndexInBlock = state.termIndexInBlock;
index.seek(blockStartAddress + blockAddresses.get(state.blockIndex));
loadFrame();
while (termIndexInBlock-- > 0) {
scanNextTermInCurrentFrame();
}
}
if (state.termIndexInBlock >= state.numTermsInBlock) {
if (++state.blockIndex >= meta.numBlocks) {
return null; // exhausted
Expand All @@ -448,6 +444,13 @@ public BytesRef next() throws IOException {
index.seek(state.postingsFP + state.postingsBytes);
}
loadFrame();
} else if (state.blockIndex != loadedFrameIndex) {
long termIndexInBlock = state.termIndexInBlock;
index.seek(blockStartAddress + blockAddresses.get(state.blockIndex));
loadFrame();
while (termIndexInBlock-- > 0) {
scanNextTermInCurrentFrame();
}
}
scanNextTermInCurrentFrame();
return term.get();
Expand All @@ -465,11 +468,6 @@ private void decompressTerms(int compressedBytes, int originalBytes) throws IOEx

private void loadFrame() throws IOException {
state.termIndexInBlock = 0;
if (loadedFrameIndex == state.blockIndex) {
termsReader.setPosition(0);
index.seek(state.postingsFP);
return;
}
state.numTermsInBlock = index.readVInt();
final long originalTermsBytes = index.readVLong();
final long termBytes = index.readVLong();
Expand All @@ -480,6 +478,13 @@ private void loadFrame() throws IOException {
loadedFrameIndex = state.blockIndex;
}

private void resetFrame() throws IOException {
assert loadedFrameIndex == state.blockIndex : loadedFrameIndex + " != " + state.blockIndex;
state.termIndexInBlock = 0;
termsReader.setPosition(0);
index.seek(state.postingsFP);
}

private void scanNextTermInCurrentFrame() throws IOException {
assert loadedFrameIndex == state.blockIndex : loadedFrameIndex + " != " + state.blockIndex;
term.setLength(termsReader.readVInt());
Expand Down

0 comments on commit 7d69cf9

Please sign in to comment.