Skip to content

Commit 25e83a0

Browse files
committed
Check for expired deadline even within filesegment search
1 parent 7d30ba3 commit 25e83a0

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

src/FileSegment.zig

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const assert = std.debug.assert;
55
const common = @import("common.zig");
66
const SearchResults = common.SearchResults;
77
const KeepOrDelete = common.KeepOrDelete;
8+
const Deadline = @import("utils/Deadline.zig");
89

910
const Item = @import("segment.zig").Item;
1011
const SegmentInfo = @import("segment.zig").SegmentInfo;
@@ -70,7 +71,7 @@ pub fn getBlockData(self: Self, block: usize) []const u8 {
7071
return self.blocks[block * self.block_size .. (block + 1) * self.block_size];
7172
}
7273

73-
pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults) !void {
74+
pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults, deadline: Deadline) !void {
7475
var prev_block_no: usize = std.math.maxInt(usize);
7576
var prev_block_range_start: usize = 0;
7677

@@ -86,7 +87,7 @@ pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults) !
8687
// We want to find hash=7, lowerBound returns block=2 (9), but block=1 could still contain hash=6, so we go one back.
8788
// We want to find hash=10, lowerBound returns block=3 (EOF), but block=2 could still contain hash=6, so we go one back.
8889

89-
for (sorted_hashes) |hash| {
90+
for (sorted_hashes, 1..) |hash, i| {
9091
var block_no = std.sort.lowerBound(u32, hash, self.index.items[prev_block_range_start..], {}, std.sort.asc(u32)) + prev_block_range_start;
9192
if (block_no > 0) {
9293
block_no -= 1;
@@ -100,10 +101,14 @@ pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults) !
100101
try filefmt.readBlock(block_data, &block_items, self.min_doc_id);
101102
}
102103
const matches = std.sort.equalRange(Item, Item{ .hash = hash, .id = 0 }, block_items.items, {}, Item.cmpByHash);
103-
for (matches[0]..matches[1]) |i| {
104-
try results.incr(block_items.items[i].id, self.info.version);
104+
for (matches[0]..matches[1]) |j| {
105+
try results.incr(block_items.items[j].id, self.info.version);
105106
}
106107
}
108+
109+
if (i % 10 == 0) {
110+
try deadline.check();
111+
}
107112
}
108113
}
109114

src/MemorySegment.zig

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn deinit(self: *Self, delete_file: KeepOrDelete) void {
4646
self.items.deinit(self.allocator);
4747
}
4848

49-
pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults) !void {
49+
pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults, deadline: Deadline) !void {
5050
var items = self.items.items;
5151
for (sorted_hashes) |hash| {
5252
const matches = std.sort.equalRange(Item, Item{ .hash = hash, .id = 0 }, items, {}, Item.cmpByHash);
@@ -55,6 +55,7 @@ pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults) !
5555
}
5656
items = items[matches[1]..];
5757
}
58+
_ = deadline;
5859
}
5960

6061
pub fn getSize(self: Self) usize {

src/segment_list.zig

+2-4
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,8 @@ pub fn SegmentList(Segment: type) type {
149149

150150
pub fn search(self: Self, hashes: []const u32, results: *SearchResults, deadline: Deadline) !void {
151151
for (self.nodes.items) |node| {
152-
if (deadline.isExpired()) {
153-
return error.Timeout;
154-
}
155-
try node.value.search(hashes, results);
152+
try deadline.check();
153+
try node.value.search(hashes, results, deadline);
156154
}
157155
results.removeOutdatedResults(self);
158156
}

src/utils/Deadline.zig

+6
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ pub fn setTimeout(self: *Self, timeout_ms: i64) void {
1616
pub fn isExpired(self: *const Self) bool {
1717
return self.deadline_ms > 0 and time.milliTimestamp() >= self.deadline_ms;
1818
}
19+
20+
pub fn check(self: Self) !void {
21+
if (self.isExpired()) {
22+
return error.Timeout;
23+
}
24+
}

0 commit comments

Comments
 (0)