Skip to content

Commit c350e29

Browse files
committed
Fix FileSegment search that could get off by one block
1 parent 11b06fe commit c350e29

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/FileSegment.zig

+11-3
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,19 @@ pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults) !
7575
var block_items = std.ArrayList(Item).init(results.results.allocator);
7676
defer block_items.deinit();
7777

78+
// Let's say we have blocks like this:
79+
//
80+
// |4.......|6.......|9.......|
81+
//
82+
// We want to find hash=2, lowerBound returns block=0 (4), so we start at that block.
83+
// We want to find hash=6, lowerBound returns block=1 (6), but block=0 could still contain hash=6, so we go one back.
84+
// We want to find hash=7, lowerBound returns block=2 (9), but block=1 could still contain hash=6, so we go one back.
85+
// We want to find hash=10, lowerBound returns block=3 (EOF), but block=2 could still contain hash=6, so we go one back.
86+
7887
for (sorted_hashes) |hash| {
7988
var block_no = std.sort.lowerBound(u32, hash, self.index.items[prev_block_range_start..], {}, std.sort.asc(u32)) + prev_block_range_start;
80-
if (block_no == self.index.items.len) {
81-
// TODO this can be handled more efficiently
82-
block_no = prev_block_range_start;
89+
if (block_no > 0) {
90+
block_no -= 1;
8391
}
8492
prev_block_range_start = block_no;
8593

0 commit comments

Comments
 (0)