╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2221 if (cursor.slot().tag == .none) return null; 2222 return try readField(@typeInfo(Field).optional.child, DB, hash_kind, arena, cursor); 2223 } 2224 const cursor = cursor_maybe orelse return error.NotFound; 2225 switch (@typeInfo(Field)) { 2226 .pointer => |pointer_info| { 2227 if (pointer_info.size != .slice or pointer_info.child != u8 or !pointer_info.attrs.@"const") { 2228 @compileError("unsupported read field type: " ++ @typeName(Field)); 2229 } 2230 return try cursor.readBytesAlloc(arena.allocator(), null); 2231 }, 2232 .array => |array_info| { 2233 if (array_info.child != u8) @compileError("unsupported read field type: " ++ @typeName(Field)); 2234 var bytes: Field = undefined; 2235 if ((try cursor.readBytes(&bytes)).len != bytes.len) return error.InvalidByteArrayLength; 2236 return bytes; 2237 }, 2238 .bool => { 2239 var buffer: [5]u8 = undefined; 2240 const bytes = try cursor.readBytesObject(&buffer); 2241 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 2242 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 2243 if (std.mem.eql(u8, bytes.value, "true")) return true; 2244 if (std.mem.eql(u8, bytes.value, "false")) return false; 2245 return error.InvalidBool; 2246 }, 2247 .int => |int_info| return switch (int_info.signedness) { 2248 .unsigned => @intCast(try cursor.readUint()), 2249 .signed => @intCast(try cursor.readInt()), 2250 }, 2251 .@"enum" => return std.meta.stringToEnum(Field, try cursor.readBytesAlloc(arena.allocator(), null)) orelse error.InvalidEnumTag, 2252 .@"union" => |union_info| { 2253 if (union_info.tag_type == null) @compileError("unsupported read field type: " ++ @typeName(Field)); 2254 switch (cursor.slot().tag) { 2255 .bytes, .short_bytes => { 2256 const name = try cursor.readBytesAlloc(arena.allocator(), null); 2257 inline for (union_info.field_names, union_info.field_types) |variant_name, variant_type| { 2258 if (variant_type == void and std.mem.eql(u8, name, variant_name)) { 2259 return @unionInit(Field, variant_name, {}); 2260 } 2261 } 2262 return error.InvalidUnion; 2263 }, 2264 .hash_map => { 2265 const variants = try DB.HashMap(.read_only).init(cursor); 2266 var iter = try variants.iterator(); 2267 const entry = (try iter.next()) orelse return error.InvalidUnion; 2268 const pair = try entry.readKeyValuePair(); 2269 if (try iter.next() != null) return error.InvalidUnion; 2270 inline for (union_info.field_names, union_info.field_types) |variant_name, variant_type| { 2271 if (variant_type != void and pair.hash == hash.hashInt(hash_kind, variant_name)) { 2272 return @unionInit(Field, variant_name, try readField(variant_type, DB, hash_kind, arena, pair.value_cursor)); 2273 } 2274 } 2275 return error.InvalidUnion; 2276 }, 2277 else => return error.InvalidUnion, 2278 } 2279 }, 2280 .@"struct" => return try read(Field, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)), 2281 else => @compileError("unsupported read field type: " ++ @typeName(Field)), 2282 } 2283 } 2284 2285 // whether two events carry the same data 2286 fn fieldsEqual(comptime T: type, a: T, b: T) bool { 2287 switch (@typeInfo(T)) { 2288 .@"struct" => |struct_info| { 2289 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2290 if (!fieldEqual(field_type, @field(a, field_name), @field(b, field_name))) return false; 2291 } 2292 }, 2293 else => @compileError("fieldsEqual expects a struct"), 2294 } 2295 2296 return true; 2297 } 2298 2299 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 2300 switch (@typeInfo(Field)) { 2301 .pointer => |pointer_info| { 2302 if (pointer_info.size == .slice and pointer_info.child == u8) { 2303 return std.mem.eql(u8, a, b); 2304 } else { 2305 @compileError("unsupported field type: " ++ @typeName(Field)); 2306 } 2307 }, 2308 .array => |array_info| { 2309 if (array_info.child == u8) { 2310 return std.mem.eql(u8, &a, &b); 2311 } else { 2312 @compileError("unsupported field type: " ++ @typeName(Field)); 2313 } 2314 }, 2315 .void => return true, 2316 .bool, .int, .@"enum" => return a == b, 2317 .@"union" => { 2318 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 2319 return switch (a) { 2320 inline else => |value, tag| fieldEqual(@TypeOf(value), value, @field(b, @tagName(tag))), 2321 }; 2322 }, 2323 .@"struct" => return fieldsEqual(Field, a, b), 2324 .optional => |optional_info| { 2325 if (a) |a_value| { 2326 if (b) |b_value| return fieldEqual(optional_info.child, a_value, b_value); 2327 return false; 2328 } 2329 return b == null; 2330 }, 2331 else => @compileError("unsupported field type: " ++ @typeName(Field)), 2332 } 2333 } 2334 2335 // 2336 // writing to xitdb 2337 // 2338 2339 pub fn upsert( 2340 comptime T: type, 2341 comptime DB: type, 2342 comptime hash_kind: hash.HashKind, 2343 map: DB.HashMap(.read_write), 2344 event: T, 2345 ) !void { 2346 switch (@typeInfo(T)) { 2347 .@"struct" => |struct_info| { 2348 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2349 try upsertField(DB, hash_kind, map, field_name, field_type, @field(event, field_name)); 2350 } 2351 }, 2352 else => @compileError("upsert expects a struct"), 2353 } 2354 } 2355 2356 fn upsertField( 2357 comptime DB: type, 2358 comptime hash_kind: hash.HashKind, 2359 map: DB.HashMap(.read_write), 2360 comptime field_name: []const u8, 2361 comptime Field: type, 2362 value: Field, 2363 ) !void { 2364 const key = hash.hashInt(hash_kind, field_name); 2365 2366 switch (@typeInfo(Field)) { 2367 .pointer => |pointer_info| { 2368 if (pointer_info.size == .slice and pointer_info.child == u8) { 2369 try upsertBytes(DB, hash_kind, map, key, value); 2370 } else { 2371 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2372 } 2373 }, 2374 .array => |array_info| { 2375 if (array_info.child == u8) { 2376 try upsertBytes(DB, hash_kind, map, key, &value); 2377 } else { 2378 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2379 } 2380 }, 2381 .int => |int_info| switch (int_info.signedness) { 2382 .unsigned => { 2383 if (try map.getCursor(key)) |value_cursor| { 2384 if (try value_cursor.readUint() == value) { 2385 return; 2386 } 2387 } 2388 2389 try map.put(key, .{ .uint = value }); 2390 }, 2391 .signed => { 2392 if (try map.getCursor(key)) |value_cursor| { 2393 if (try value_cursor.readInt() == value) { 2394 return; 2395 } 2396 } 2397 2398 try map.put(key, .{ .int = value }); 2399 }, 2400 }, 2401 .bool => { 2402 const bytes = if (value) "true" else "false"; 2403 if (try map.getCursor(key)) |value_cursor| { 2404 var buffer: [5]u8 = undefined; 2405 const existing = try value_cursor.readBytesObject(&buffer); 2406 if (existing.format_tag) |format_tag| { 2407 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 2408 return; 2409 } 2410 } 2411 } 2412 2413 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 2414 }, 2415 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 2416 .@"union" => switch (value) { 2417 inline else => |payload, tag| { 2418 if (@TypeOf(payload) == void) { 2419 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 2420 } else { 2421 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 2422 if (try map.getCursor(key)) |cursor| { 2423 switch (cursor.slot().tag) { 2424 .hash_map => { 2425 const variants = try DB.HashMap(.read_only).init(cursor); 2426 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 2427 }, 2428 else => try map.put(key, .{ .slot = null }), 2429 } 2430 } 2431 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 2432 if (@typeInfo(@TypeOf(payload)) == .optional) { 2433 // keep the variant even when its payload is null 2434 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 2435 } 2436 if (try variants.getCursor(tag_key)) |cursor| { 2437 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 2438 } 2439 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 2440 } 2441 }, 2442 }, 2443 .optional => |optional_info| { 2444 // a missing key is null 2445 if (value) |child| { 2446 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 2447 } else { 2448 _ = try map.remove(key); 2449 } 2450 }, 2451 .@"struct" => try upsert( 2452 Field, 2453 DB, 2454 hash_kind, 2455 try DB.HashMap(.read_write).init(try map.putCursor(key)), 2456 value, 2457 ), 2458 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 2459 } 2460 } 2461 2462 fn upsertBytes( 2463 comptime DB: type, 2464 comptime hash_kind: hash.HashKind, 2465 map: DB.HashMap(.read_write), 2466 key: hash.HashInt(hash_kind), 2467 value: []const u8, 2468 ) !void { 2469 var existing_cursor_maybe = try map.getCursor(key); 2470 if (existing_cursor_maybe) |*existing_cursor| { 2471 if (try bytesEqual(DB, existing_cursor, value)) { 2472 return; 2473 } 2474 } 2475 2476 try map.put(key, .{ .bytes = value }); 2477 } 2478 2479 fn bytesEqual( 2480 comptime DB: type, 2481 cursor: *DB.Cursor(.read_only), 2482 value: []const u8, 2483 ) !bool { 2484 switch (cursor.slot().tag) { 2485 .bytes, .short_bytes => {}, 2486 else => return false, 2487 } 2488 var read_buffer: [1024]u8 = undefined; 2489 var reader = try cursor.reader(&read_buffer); 2490 if (reader.size != value.len) { 2491 return false; 2492 } 2493 2494 var chunk_buffer: [1024]u8 = undefined; 2495 var offset: usize = 0; 2496 while (offset < value.len) { 2497 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2498 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2499 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2500 return false; 2501 } 2502 offset += chunk_len; 2503 } 2504 2505 return true; 2506 } 2507 2508 // 2509 // CommitIterator 2510 // 2511 // differs from xit's ObjectIterator in a few ways: 2512 // 2513 // 1. iterates parents first, rather than children first 2514 // 2. `next` only returns the object id, not the full Object 2515 // 3. stores its temporary state in a file on disk to avoid OOMs 2516 // 2517 2518 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2519 return struct { 2520 const DB = EventDB(repo_opts.hash); 2521 // a plain file is used for the scratch db because each write outside 2522 // a transaction is synced anyway, so buffering would gain nothing 2523 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2524 const db_name = "haxy-repo-events.db"; 2525 2526 repo_dir: std.Io.Dir, 2527 db_file: std.Io.File, 2528 db: *TempDB, 2529 // map of each object id to its children 2530 parent_to_children: TempDB.HashMap(.read_write), 2531 // tracking pending parents is necessary because a child can only 2532 // be returned after all of its unconsumed parents have been returned 2533 child_to_pending_parents: TempDB.HashMap(.read_write), 2534 // queue of object ids that are ready to be returned from `next` 2535 ready_queue: TempDB.ArrayList(.read_write), 2536 ready_queue_index: u64, 2537 // the object id at the tip of this branch 2538 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2539 // the object id furthest back in the history that hasn't been consumed 2540 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2541 2542 pub fn init( 2543 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2544 io: std.Io, 2545 allocator: std.mem.Allocator, 2546 consumed_object_ids: DB.HashMap(.read_only), 2547 ref: rf.Ref, 2548 ) !CommitIterator(repo_kind, repo_opts) { 2549 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2550 errdefer { 2551 db_file.close(io); 2552 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2553 } 2554 2555 // cursors point at the db, so it needs a stable address 2556 const db_ptr = try allocator.create(TempDB); 2557 errdefer allocator.destroy(db_ptr); 2558 // the db is scratch state that gets deleted after iteration, so 2559 // there is nothing worth fsyncing 2560 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2561 2562 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2563 2564 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2565 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2566 2567 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2568 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2569 2570 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2571 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2572 2573 var self = CommitIterator(repo_kind, repo_opts){ 2574 .repo_dir = state.core.repo_dir, 2575 .db_file = db_file, 2576 .db = db_ptr, 2577 .parent_to_children = parent_to_children, 2578 .child_to_pending_parents = child_to_pending_parents, 2579 .ready_queue = ready_queue, 2580 .ready_queue_index = 0, 2581 .newest_object_id = null, 2582 .oldest_object_id = null, 2583 }; 2584 errdefer self.deinit(io, allocator); 2585 2586 try self.collect(state, io, allocator, consumed_object_ids, ref); 2587 2588 return self; 2589 } 2590 2591 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2592 self.db_file.close(io); 2593 self.repo_dir.deleteFile(io, db_name) catch {}; 2594 allocator.destroy(self.db); 2595 } 2596 2597 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2598 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2599 2600 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2601 self.ready_queue_index += 1; 2602 2603 try self.enqueueChildren(&oid); 2604 return oid; 2605 } 2606 2607 fn collect( 2608 self: *CommitIterator(repo_kind, repo_opts), 2609 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2610 io: std.Io, 2611 allocator: std.mem.Allocator, 2612 consumed_object_ids: DB.HashMap(.read_only), 2613 ref: rf.Ref, 2614 ) !void { 2615 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2616 2617 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2618 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2619 var walk_queue_index: u64 = 0; 2620 2621 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2622 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2623 2624 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2625 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2626 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2627 try walk_queue.append(.{ .bytes = &head_oid }); 2628 2629 while (walk_queue_index < try walk_queue.count()) { 2630 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2631 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2632 2633 walk_queue_index += 1; 2634 2635 // if we've seen this object id, skip it 2636 if (null != try seen_object_ids.getCursor(oid_int)) { 2637 continue; 2638 } 2639 2640 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2641 2642 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2643 defer commit_object.deinit(); 2644 2645 // if this object id has already been consumed, skip it 2646 if (null != try consumed_object_ids.getCursor(oid_int)) { 2647 continue; 2648 } 2649 2650 if (self.newest_object_id == null) self.newest_object_id = oid; 2651 self.oldest_object_id = oid; 2652 2653 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2654 var pending_parent_count: u64 = 0; 2655 for (parent_oids) |*parent_oid| { 2656 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2657 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2658 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2659 2660 // if this object id has already been consumed, skip it 2661 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2662 continue; 2663 } 2664 2665 // if this object id hasn't already been seen, add it to the walk queue 2666 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2667 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2668 } 2669 2670 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2671 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2672 try children.put(oid_int, .{ .bytes = &oid }); 2673 pending_parent_count += 1; 2674 } 2675 2676 if (pending_parent_count == 0) { 2677 try self.ready_queue.append(.{ .bytes = &oid }); 2678 } else { 2679 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2680 } 2681 } 2682 } 2683 2684 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2685 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2686 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2687 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2688 var children_iter = try children.iterator(); 2689 2690 while (try children_iter.next()) |*child_cursor| { 2691 const kv_pair = try child_cursor.readKeyValuePair(); 2692 const child_oid_int = kv_pair.hash; 2693 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2694 const pending_parent_count = try pending_parent_cursor.readUint(); 2695 2696 if (pending_parent_count <= 1) { 2697 _ = try self.child_to_pending_parents.remove(child_oid_int); 2698 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2699 try self.ready_queue.append(.{ .bytes = &child_oid }); 2700 } else { 2701 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2702 } 2703 } 2704 } 2705 2706 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2707 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2708 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2709 _ = try oid_cursor.readBytes(&oid); 2710 return oid; 2711 } 2712 }; 2713 } 2714