Skip to content

Commit

Permalink
[core] Use binary search to optimize SnapshotManager#earlierThanTimeM…
Browse files Browse the repository at this point in the history
…ills
  • Loading branch information
tsreaper committed Jan 29, 2024
1 parent 6acc434 commit b829541
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,24 @@ public boolean snapshotExists(long snapshotId) {
public @Nullable Long earlierThanTimeMills(long timestampMills) {
Long earliest = earliestSnapshotId();
Long latest = latestSnapshotId();

if (earliest == null || latest == null) {
return null;
}

for (long i = latest; i >= earliest; i--) {
long commitTime = snapshot(i).timeMillis();
if (commitTime < timestampMills) {
return i;
if (snapshot(earliest).timeMillis() >= timestampMills) {
return earliest - 1;
}

while (earliest < latest) {
long mid = (earliest + latest + 1) / 2;
if (snapshot(mid).timeMillis() < timestampMills) {
earliest = mid;
} else {
latest = mid - 1;
}
}
return earliest - 1;
return earliest;
}

/**
Expand Down

0 comments on commit b829541

Please sign in to comment.