@@ -5,6 +5,7 @@ const assert = std.debug.assert;
5
5
const common = @import ("common.zig" );
6
6
const SearchResults = common .SearchResults ;
7
7
const KeepOrDelete = common .KeepOrDelete ;
8
+ const Deadline = @import ("utils/Deadline.zig" );
8
9
9
10
const Item = @import ("segment.zig" ).Item ;
10
11
const SegmentInfo = @import ("segment.zig" ).SegmentInfo ;
@@ -70,7 +71,7 @@ pub fn getBlockData(self: Self, block: usize) []const u8 {
70
71
return self .blocks [block * self .block_size .. (block + 1 ) * self .block_size ];
71
72
}
72
73
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 {
74
75
var prev_block_no : usize = std .math .maxInt (usize );
75
76
var prev_block_range_start : usize = 0 ;
76
77
@@ -86,7 +87,7 @@ pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults) !
86
87
// We want to find hash=7, lowerBound returns block=2 (9), but block=1 could still contain hash=6, so we go one back.
87
88
// We want to find hash=10, lowerBound returns block=3 (EOF), but block=2 could still contain hash=6, so we go one back.
88
89
89
- for (sorted_hashes ) | hash | {
90
+ for (sorted_hashes , 1 .. ) | hash , i | {
90
91
var block_no = std .sort .lowerBound (u32 , hash , self .index .items [prev_block_range_start .. ], {}, std .sort .asc (u32 )) + prev_block_range_start ;
91
92
if (block_no > 0 ) {
92
93
block_no -= 1 ;
@@ -100,10 +101,14 @@ pub fn search(self: Self, sorted_hashes: []const u32, results: *SearchResults) !
100
101
try filefmt .readBlock (block_data , & block_items , self .min_doc_id );
101
102
}
102
103
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 );
105
106
}
106
107
}
108
+
109
+ if (i % 10 == 0 ) {
110
+ try deadline .check ();
111
+ }
107
112
}
108
113
}
109
114
0 commit comments