╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ search.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
103 }; 104 105 // one indexed word's doc keys, positioned at the current one. 106 const Posting = struct { 107 set: DB.SortedSet(.read_only), 108 iter: DB.Cursor(.read_only).Iter = undefined, 109 key: [max_doc_key_len]u8 = undefined, 110 key_len: usize = 0, 111 done: bool = false, 112 started: bool = false, 113 114 // move to the first key at or after `key`. one step reaches it in 115 // a dense posting, and a seek skips the rest of a sparse one. 116 fn advance(self: *Posting, key: []const u8) !void { 117 if (self.done) return; 118 if (self.started) { 119 if (std.mem.order(u8, self.key[0..self.key_len], key) != .lt) return; 120 try self.step(); 121 if (self.done or std.mem.order(u8, self.key[0..self.key_len], key) != .lt) return; 122 } 123 self.iter = try self.set.iteratorFrom(key); 124 self.started = true; 125 try self.step(); 126 } 127 128 fn step(self: *Posting) !void { 129 const cursor = (try self.iter.next()) orelse { 130 self.done = true; 131 return; 132 }; 133 const pair = try cursor.readKeyValuePair(); 134 self.key_len = (try pair.key_cursor.readBytes(&self.key)).len; 135 } 136 }; 137 138 pub fn init(index: DB.SortedMap(.read_only), aa: std.mem.Allocator, text: []const u8) !Self { 139 var terms: std.ArrayList(Term) = .empty; 140 for (try tokenize(aa, text)) |word| { 141 var postings: std.ArrayList(Posting) = .empty; 142 var iter = try index.iteratorFrom(word); 143 while (try iter.next()) |cursor| { 144 if (postings.items.len == max_expansions) break; 145 const pair = try cursor.readKeyValuePair(); 146 var buffer: [max_word_len]u8 = undefined; 147 const indexed = try pair.key_cursor.readBytes(&buffer); 148 if (!std.mem.startsWith(u8, indexed, word)) break; 149 try postings.append(aa, .{ .set = try DB.SortedSet(.read_only).init(pair.value_cursor) }); 150 } 151 // a word nothing starts with leaves an empty term, which rules 152 // out every document 153 try terms.append(aa, .{ .postings = postings.items }); 154 } 155 return .{ .terms = terms }; 156 } 157 158 // narrow the results to the doc keys in `set`. it is one more term, 159 // with that set as its only posting. 160 pub fn require(self: *Self, aa: std.mem.Allocator, set: DB.SortedSet(.read_only)) !void { 161 const postings = try aa.alloc(Posting, 1); 162 postings[0] = .{ .set = set }; 163 try self.terms.append(aa, .{ .postings = postings }); 164 } 165 166 // start the next result at `key` rather than the first one. 167 pub fn seek(self: *Self, key: []const u8) void { 168 @memcpy(self.pos[0..key.len], key); 169 self.pos_len = key.len; 170 self.returned = false; 171 } 172 173 pub fn next(self: *Self) !?[]const u8 { 174 // with no terms at all nothing narrows the results, so none are listed 175 if (self.terms.items.len == 0) return null; 176 if (self.returned) { 177 // doc keys are at most max_doc_key_len, so appending a byte 178 // lands strictly after the one just returned 179 self.pos[self.pos_len] = 0; 180 self.pos_len += 1; 181 self.returned = false; 182 } 183 // intersect by seeking: a term that lands past the candidate makes 184 // its key the new candidate and the others seek up to it. 185 var i: usize = 0; 186 while (i < self.terms.items.len) { 187 const key = (try self.terms.items[i].seek(self.pos[0..self.pos_len])) orelse return null; 188 if (std.mem.order(u8, key, self.pos[0..self.pos_len]) == .gt) { 189 @memcpy(self.pos[0..key.len], key); 190 self.pos_len = key.len; 191 i = 0; 192 continue; 193 } 194 i += 1; 195 } 196 self.returned = true; 197 return self.pos[0..self.pos_len]; 198 } 199 }; 200 } 201 202 // the unique lowercased words of `text`, in the order they first appear. 203 fn tokenize(aa: std.mem.Allocator, text: []const u8) ![]const []const u8 { 204 var words: std.StringArrayHashMapUnmanaged(void) = .empty; 205 const limit = @min(text.len, max_indexed_bytes); 206 var i: usize = 0; 207 while (i < limit) { 208 if (!isWordByte(text[i])) { 209 i += 1; 210 continue; 211 } 212 const start = i; 213 while (i < limit and isWordByte(text[i])) i += 1; 214 const word = text[start..@min(i, start + max_word_len)]; 215 if (word.len < min_word_len) continue; 216 const lowered = try aa.alloc(u8, word.len); 217 _ = std.ascii.lowerString(lowered, word); 218 try words.put(aa, lowered, {}); 219 if (words.count() == max_words_per_doc) break; 220 } 221 return words.keys(); 222 } 223 224 // a word is a run of ascii alphanumerics and non-ascii bytes. 225 fn isWordByte(byte: u8) bool { 226 return std.ascii.isAlphanumeric(byte) or byte >= 0x80; 227 } 228