╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2161 switch (@typeInfo(T)) { 2162 .@"struct" => |struct_info| { 2163 inline for (struct_info.fields) |field| { 2164 if (!fieldEqual(field.type, @field(a, field.name), @field(b, field.name))) return false; 2165 } 2166 }, 2167 else => @compileError("fieldsEqual expects a struct"), 2168 } 2169 2170 return true; 2171 } 2172 2173 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 2174 switch (@typeInfo(Field)) { 2175 .pointer => |pointer_info| { 2176 if (pointer_info.size == .slice and pointer_info.child == u8) { 2177 return std.mem.eql(u8, a, b); 2178 } else { 2179 @compileError("unsupported field type: " ++ @typeName(Field)); 2180 } 2181 }, 2182 .array => |array_info| { 2183 if (array_info.child == u8) { 2184 return std.mem.eql(u8, &a, &b); 2185 } else { 2186 @compileError("unsupported field type: " ++ @typeName(Field)); 2187 } 2188 }, 2189 .void => return true, 2190 .bool, .int, .@"enum" => return a == b, 2191 .@"union" => { 2192 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 2193 return switch (a) { 2194 inline else => |value, tag| fieldEqual(@TypeOf(value), value, @field(b, @tagName(tag))), 2195 }; 2196 }, 2197 .@"struct" => return fieldsEqual(Field, a, b), 2198 .optional => |optional_info| { 2199 if (a) |a_value| { 2200 if (b) |b_value| return fieldEqual(optional_info.child, a_value, b_value); 2201 return false; 2202 } 2203 return b == null; 2204 }, 2205 else => @compileError("unsupported field type: " ++ @typeName(Field)), 2206 } 2207 } 2208 2209 // 2210 // writing to xitdb 2211 // 2212 2213 pub fn upsert( 2214 comptime T: type, 2215 comptime DB: type, 2216 comptime hash_kind: hash.HashKind, 2217 map: DB.HashMap(.read_write), 2218 event: T, 2219 ) !void { 2220 switch (@typeInfo(T)) { 2221 .@"struct" => |struct_info| { 2222 inline for (struct_info.fields) |field| { 2223 try upsertField(DB, hash_kind, map, field.name, field.type, @field(event, field.name)); 2224 } 2225 }, 2226 else => @compileError("upsert expects a struct"), 2227 } 2228 } 2229 2230 fn upsertField( 2231 comptime DB: type, 2232 comptime hash_kind: hash.HashKind, 2233 map: DB.HashMap(.read_write), 2234 comptime field_name: []const u8, 2235 comptime Field: type, 2236 value: Field, 2237 ) !void { 2238 const key = hash.hashInt(hash_kind, field_name); 2239 2240 switch (@typeInfo(Field)) { 2241 .pointer => |pointer_info| { 2242 if (pointer_info.size == .slice and pointer_info.child == u8) { 2243 try upsertBytes(DB, hash_kind, map, key, value); 2244 } else { 2245 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2246 } 2247 }, 2248 .array => |array_info| { 2249 if (array_info.child == u8) { 2250 try upsertBytes(DB, hash_kind, map, key, &value); 2251 } else { 2252 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2253 } 2254 }, 2255 .int => |int_info| switch (int_info.signedness) { 2256 .unsigned => { 2257 if (try map.getCursor(key)) |value_cursor| { 2258 if (try value_cursor.readUint() == value) { 2259 return; 2260 } 2261 } 2262 2263 try map.put(key, .{ .uint = value }); 2264 }, 2265 .signed => { 2266 if (try map.getCursor(key)) |value_cursor| { 2267 if (try value_cursor.readInt() == value) { 2268 return; 2269 } 2270 } 2271 2272 try map.put(key, .{ .int = value }); 2273 }, 2274 }, 2275 .bool => { 2276 const bytes = if (value) "true" else "false"; 2277 if (try map.getCursor(key)) |value_cursor| { 2278 var buffer: [5]u8 = undefined; 2279 const existing = try value_cursor.readBytesObject(&buffer); 2280 if (existing.format_tag) |format_tag| { 2281 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 2282 return; 2283 } 2284 } 2285 } 2286 2287 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 2288 }, 2289 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 2290 .@"union" => switch (value) { 2291 inline else => |payload, tag| { 2292 if (@TypeOf(payload) == void) { 2293 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 2294 } else { 2295 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 2296 if (try map.getCursor(key)) |cursor| { 2297 switch (cursor.slot().tag) { 2298 .hash_map => { 2299 const variants = try DB.HashMap(.read_only).init(cursor); 2300 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 2301 }, 2302 else => try map.put(key, .{ .slot = null }), 2303 } 2304 } 2305 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 2306 if (@typeInfo(@TypeOf(payload)) == .optional) { 2307 // keep the variant even when its payload is null 2308 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 2309 } 2310 if (try variants.getCursor(tag_key)) |cursor| { 2311 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 2312 } 2313 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 2314 } 2315 }, 2316 }, 2317 .optional => |optional_info| { 2318 // a missing key is null 2319 if (value) |child| { 2320 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 2321 } else { 2322 _ = try map.remove(key); 2323 } 2324 }, 2325 .@"struct" => try upsert( 2326 Field, 2327 DB, 2328 hash_kind, 2329 try DB.HashMap(.read_write).init(try map.putCursor(key)), 2330 value, 2331 ), 2332 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 2333 } 2334 } 2335 2336 fn upsertBytes( 2337 comptime DB: type, 2338 comptime hash_kind: hash.HashKind, 2339 map: DB.HashMap(.read_write), 2340 key: hash.HashInt(hash_kind), 2341 value: []const u8, 2342 ) !void { 2343 var existing_cursor_maybe = try map.getCursor(key); 2344 if (existing_cursor_maybe) |*existing_cursor| { 2345 if (try bytesEqual(DB, existing_cursor, value)) { 2346 return; 2347 } 2348 } 2349 2350 try map.put(key, .{ .bytes = value }); 2351 } 2352 2353 fn bytesEqual( 2354 comptime DB: type, 2355 cursor: *DB.Cursor(.read_only), 2356 value: []const u8, 2357 ) !bool { 2358 switch (cursor.slot().tag) { 2359 .bytes, .short_bytes => {}, 2360 else => return false, 2361 } 2362 var read_buffer: [1024]u8 = undefined; 2363 var reader = try cursor.reader(&read_buffer); 2364 if (reader.size != value.len) { 2365 return false; 2366 } 2367 2368 var chunk_buffer: [1024]u8 = undefined; 2369 var offset: usize = 0; 2370 while (offset < value.len) { 2371 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2372 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2373 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2374 return false; 2375 } 2376 offset += chunk_len; 2377 } 2378 2379 return true; 2380 } 2381 2382 // 2383 // CommitIterator 2384 // 2385 // differs from xit's ObjectIterator in a few ways: 2386 // 2387 // 1. iterates parents first, rather than children first 2388 // 2. `next` only returns the object id, not the full Object 2389 // 3. stores its temporary state in a file on disk to avoid OOMs 2390 // 2391 2392 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2393 return struct { 2394 const DB = EventDB(repo_opts.hash); 2395 // a plain file is used for the scratch db because each write outside 2396 // a transaction is synced anyway, so buffering would gain nothing 2397 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2398 const db_name = "haxy-repo-events.db"; 2399 2400 repo_dir: std.Io.Dir, 2401 db_file: std.Io.File, 2402 db: *TempDB, 2403 // map of each object id to its children 2404 parent_to_children: TempDB.HashMap(.read_write), 2405 // tracking pending parents is necessary because a child can only 2406 // be returned after all of its unconsumed parents have been returned 2407 child_to_pending_parents: TempDB.HashMap(.read_write), 2408 // queue of object ids that are ready to be returned from `next` 2409 ready_queue: TempDB.ArrayList(.read_write), 2410 ready_queue_index: u64, 2411 // the object id at the tip of this branch 2412 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2413 // the object id furthest back in the history that hasn't been consumed 2414 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2415 2416 pub fn init( 2417 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2418 io: std.Io, 2419 allocator: std.mem.Allocator, 2420 consumed_object_ids: DB.HashMap(.read_only), 2421 ref: rf.Ref, 2422 ) !CommitIterator(repo_kind, repo_opts) { 2423 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2424 errdefer { 2425 db_file.close(io); 2426 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2427 } 2428 2429 // cursors point at the db, so it needs a stable address 2430 const db_ptr = try allocator.create(TempDB); 2431 errdefer allocator.destroy(db_ptr); 2432 // the db is scratch state that gets deleted after iteration, so 2433 // there is nothing worth fsyncing 2434 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2435 2436 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2437 2438 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2439 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2440 2441 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2442 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2443 2444 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2445 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2446 2447 var self = CommitIterator(repo_kind, repo_opts){ 2448 .repo_dir = state.core.repo_dir, 2449 .db_file = db_file, 2450 .db = db_ptr, 2451 .parent_to_children = parent_to_children, 2452 .child_to_pending_parents = child_to_pending_parents, 2453 .ready_queue = ready_queue, 2454 .ready_queue_index = 0, 2455 .newest_object_id = null, 2456 .oldest_object_id = null, 2457 }; 2458 errdefer self.deinit(io, allocator); 2459 2460 try self.collect(state, io, allocator, consumed_object_ids, ref); 2461 2462 return self; 2463 } 2464 2465 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2466 self.db_file.close(io); 2467 self.repo_dir.deleteFile(io, db_name) catch {}; 2468 allocator.destroy(self.db); 2469 } 2470 2471 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2472 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2473 2474 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2475 self.ready_queue_index += 1; 2476 2477 try self.enqueueChildren(&oid); 2478 return oid; 2479 } 2480 2481 fn collect( 2482 self: *CommitIterator(repo_kind, repo_opts), 2483 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2484 io: std.Io, 2485 allocator: std.mem.Allocator, 2486 consumed_object_ids: DB.HashMap(.read_only), 2487 ref: rf.Ref, 2488 ) !void { 2489 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2490 2491 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2492 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2493 var walk_queue_index: u64 = 0; 2494 2495 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2496 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2497 2498 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2499 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2500 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2501 try walk_queue.append(.{ .bytes = &head_oid }); 2502 2503 while (walk_queue_index < try walk_queue.count()) { 2504 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2505 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2506 2507 walk_queue_index += 1; 2508 2509 // if we've seen this object id, skip it 2510 if (null != try seen_object_ids.getCursor(oid_int)) { 2511 continue; 2512 } 2513 2514 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2515 2516 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2517 defer commit_object.deinit(); 2518 2519 // if this object id has already been consumed, skip it 2520 if (null != try consumed_object_ids.getCursor(oid_int)) { 2521 continue; 2522 } 2523 2524 if (self.newest_object_id == null) self.newest_object_id = oid; 2525 self.oldest_object_id = oid; 2526 2527 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2528 var pending_parent_count: u64 = 0; 2529 for (parent_oids) |*parent_oid| { 2530 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2531 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2532 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2533 2534 // if this object id has already been consumed, skip it 2535 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2536 continue; 2537 } 2538 2539 // if this object id hasn't already been seen, add it to the walk queue 2540 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2541 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2542 } 2543 2544 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2545 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2546 try children.put(oid_int, .{ .bytes = &oid }); 2547 pending_parent_count += 1; 2548 } 2549 2550 if (pending_parent_count == 0) { 2551 try self.ready_queue.append(.{ .bytes = &oid }); 2552 } else { 2553 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2554 } 2555 } 2556 } 2557 2558 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2559 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2560 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2561 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2562 var children_iter = try children.iterator(); 2563 2564 while (try children_iter.next()) |*child_cursor| { 2565 const kv_pair = try child_cursor.readKeyValuePair(); 2566 const child_oid_int = kv_pair.hash; 2567 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2568 const pending_parent_count = try pending_parent_cursor.readUint(); 2569 2570 if (pending_parent_count <= 1) { 2571 _ = try self.child_to_pending_parents.remove(child_oid_int); 2572 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2573 try self.ready_queue.append(.{ .bytes = &child_oid }); 2574 } else { 2575 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2576 } 2577 } 2578 } 2579 2580 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2581 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2582 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2583 _ = try oid_cursor.readBytes(&oid); 2584 return oid; 2585 } 2586 }; 2587 } 2588