╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ search.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
125 } 126 127 fn step(self: *Posting) !void { 128 const cursor = (try self.iter.next()) orelse { 129 self.done = true; 130 return; 131 }; 132 const pair = try cursor.readKeyValuePair(); 133 self.key_len = (try pair.key_cursor.readBytes(&self.key)).len; 134 } 135 }; 136 137 pub fn init(index: DB.SortedMap(.read_only), aa: std.mem.Allocator, text: []const u8) !Self { 138 var terms: std.ArrayList(Term) = .empty; 139 for (try tokenize(aa, text)) |word| { 140 var postings: std.ArrayList(Posting) = .empty; 141 var iter = try index.iteratorFrom(word); 142 while (try iter.next()) |cursor| { 143 if (postings.items.len == max_expansions) break; 144 const pair = try cursor.readKeyValuePair(); 145 var buffer: [max_word_len]u8 = undefined; 146 const indexed = try pair.key_cursor.readBytes(&buffer); 147 if (!std.mem.startsWith(u8, indexed, word)) break; 148 try postings.append(aa, .{ .set = try DB.SortedSet(.read_only).init(pair.value_cursor) }); 149 } 150 // a word nothing starts with rules out every document 151 if (postings.items.len == 0) return .{ .terms = .empty }; 152 try terms.append(aa, .{ .postings = postings.items }); 153 } 154 return .{ .terms = terms }; 155 } 156 157 // narrow the results to the doc keys in `set`. it is one more term, 158 // with that set as its only posting. a query nothing can match stays 159 // empty, since requiring a set would turn it into every doc key in it. 160 pub fn require(self: *Self, aa: std.mem.Allocator, set: DB.SortedSet(.read_only)) !void { 161 if (self.terms.items.len == 0) return; 162 const postings = try aa.alloc(Posting, 1); 163 postings[0] = .{ .set = set }; 164 try self.terms.append(aa, .{ .postings = postings }); 165 } 166 167 // start the next result at `key` rather than the first one. 168 pub fn seek(self: *Self, key: []const u8) void { 169 @memcpy(self.pos[0..key.len], key); 170 self.pos_len = key.len; 171 self.returned = false; 172 } 173 174 pub fn next(self: *Self) !?[]const u8 { 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