╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2313 switch (@typeInfo(T)) { 2314 .@"struct" => |struct_info| { 2315 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2316 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)); 2317 @field(event, field_name) = try readField(field_type, DB, hash_kind, arena, cursor); 2318 } 2319 }, 2320 else => @compileError("read expects a struct"), 2321 } 2322 return event; 2323 } 2324 2325 fn readField( 2326 comptime Field: type, 2327 comptime DB: type, 2328 comptime hash_kind: hash.HashKind, 2329 arena: *std.heap.ArenaAllocator, 2330 cursor_maybe: ?DB.Cursor(.read_only), 2331 ) !Field { 2332 if (@typeInfo(Field) == .optional) { 2333 // a missing key or a null union payload is null 2334 const cursor = cursor_maybe orelse return null; 2335 if (cursor.slot().tag == .none) return null; 2336 return try readField(@typeInfo(Field).optional.child, DB, hash_kind, arena, cursor); 2337 } 2338 const cursor = cursor_maybe orelse return error.NotFound; 2339 switch (@typeInfo(Field)) { 2340 .pointer => |pointer_info| { 2341 if (pointer_info.size != .slice or pointer_info.child != u8 or !pointer_info.attrs.@"const") { 2342 @compileError("unsupported read field type: " ++ @typeName(Field)); 2343 } 2344 return try cursor.readBytesAlloc(arena.allocator(), null); 2345 }, 2346 .array => |array_info| { 2347 if (array_info.child != u8) @compileError("unsupported read field type: " ++ @typeName(Field)); 2348 var bytes: Field = undefined; 2349 if ((try cursor.readBytes(&bytes)).len != bytes.len) return error.InvalidByteArrayLength; 2350 return bytes; 2351 }, 2352 .bool => { 2353 var buffer: [5]u8 = undefined; 2354 const bytes = try cursor.readBytesObject(&buffer); 2355 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 2356 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 2357 if (std.mem.eql(u8, bytes.value, "true")) return true; 2358 if (std.mem.eql(u8, bytes.value, "false")) return false; 2359 return error.InvalidBool; 2360 }, 2361 .int => |int_info| return switch (int_info.signedness) { 2362 .unsigned => @intCast(try cursor.readUint()), 2363 .signed => @intCast(try cursor.readInt()), 2364 }, 2365 .@"enum" => return std.meta.stringToEnum(Field, try cursor.readBytesAlloc(arena.allocator(), null)) orelse error.InvalidEnumTag, 2366 .@"union" => |union_info| { 2367 if (union_info.tag_type == null) @compileError("unsupported read field type: " ++ @typeName(Field)); 2368 switch (cursor.slot().tag) { 2369 .bytes, .short_bytes => { 2370 const name = try cursor.readBytesAlloc(arena.allocator(), null); 2371 inline for (union_info.field_names, union_info.field_types) |variant_name, variant_type| { 2372 if (variant_type == void and std.mem.eql(u8, name, variant_name)) { 2373 return @unionInit(Field, variant_name, {}); 2374 } 2375 } 2376 return error.InvalidUnion; 2377 }, 2378 .hash_map => { 2379 const variants = try DB.HashMap(.read_only).init(cursor); 2380 var iter = try variants.iterator(); 2381 const entry = (try iter.next()) orelse return error.InvalidUnion; 2382 const pair = try entry.readKeyValuePair(); 2383 if (try iter.next() != null) return error.InvalidUnion; 2384 inline for (union_info.field_names, union_info.field_types) |variant_name, variant_type| { 2385 if (variant_type != void and pair.hash == hash.hashInt(hash_kind, variant_name)) { 2386 return @unionInit(Field, variant_name, try readField(variant_type, DB, hash_kind, arena, pair.value_cursor)); 2387 } 2388 } 2389 return error.InvalidUnion; 2390 }, 2391 else => return error.InvalidUnion, 2392 } 2393 }, 2394 .@"struct" => return try read(Field, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)), 2395 else => @compileError("unsupported read field type: " ++ @typeName(Field)), 2396 } 2397 } 2398 2399 // whether two events carry the same data 2400 fn fieldsEqual(comptime T: type, a: T, b: T) bool { 2401 switch (@typeInfo(T)) { 2402 .@"struct" => |struct_info| { 2403 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2404 if (!fieldEqual(field_type, @field(a, field_name), @field(b, field_name))) return false; 2405 } 2406 }, 2407 else => @compileError("fieldsEqual expects a struct"), 2408 } 2409 2410 return true; 2411 } 2412 2413 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 2414 switch (@typeInfo(Field)) { 2415 .pointer => |pointer_info| { 2416 if (pointer_info.size == .slice and pointer_info.child == u8) { 2417 return std.mem.eql(u8, a, b); 2418 } else { 2419 @compileError("unsupported field type: " ++ @typeName(Field)); 2420 } 2421 }, 2422 .array => |array_info| { 2423 if (array_info.child == u8) { 2424 return std.mem.eql(u8, &a, &b); 2425 } else { 2426 @compileError("unsupported field type: " ++ @typeName(Field)); 2427 } 2428 }, 2429 .void => return true, 2430 .bool, .int, .@"enum" => return a == b, 2431 .@"union" => { 2432 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 2433 return switch (a) { 2434 inline else => |value, tag| fieldEqual(@TypeOf(value), value, @field(b, @tagName(tag))), 2435 }; 2436 }, 2437 .@"struct" => return fieldsEqual(Field, a, b), 2438 .optional => |optional_info| { 2439 if (a) |a_value| { 2440 if (b) |b_value| return fieldEqual(optional_info.child, a_value, b_value); 2441 return false; 2442 } 2443 return b == null; 2444 }, 2445 else => @compileError("unsupported field type: " ++ @typeName(Field)), 2446 } 2447 } 2448 2449 // 2450 // writing to xitdb 2451 // 2452 2453 pub fn upsert( 2454 comptime T: type, 2455 comptime DB: type, 2456 comptime hash_kind: hash.HashKind, 2457 map: DB.HashMap(.read_write), 2458 event: T, 2459 ) !void { 2460 switch (@typeInfo(T)) { 2461 .@"struct" => |struct_info| { 2462 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2463 try upsertField(DB, hash_kind, map, field_name, field_type, @field(event, field_name)); 2464 } 2465 }, 2466 else => @compileError("upsert expects a struct"), 2467 } 2468 } 2469 2470 fn upsertField( 2471 comptime DB: type, 2472 comptime hash_kind: hash.HashKind, 2473 map: DB.HashMap(.read_write), 2474 comptime field_name: []const u8, 2475 comptime Field: type, 2476 value: Field, 2477 ) !void { 2478 const key = hash.hashInt(hash_kind, field_name); 2479 2480 switch (@typeInfo(Field)) { 2481 .pointer => |pointer_info| { 2482 if (pointer_info.size == .slice and pointer_info.child == u8) { 2483 try upsertBytes(DB, hash_kind, map, key, value); 2484 } else { 2485 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2486 } 2487 }, 2488 .array => |array_info| { 2489 if (array_info.child == u8) { 2490 try upsertBytes(DB, hash_kind, map, key, &value); 2491 } else { 2492 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2493 } 2494 }, 2495 .int => |int_info| switch (int_info.signedness) { 2496 .unsigned => { 2497 if (try map.getCursor(key)) |value_cursor| { 2498 if (try value_cursor.readUint() == value) { 2499 return; 2500 } 2501 } 2502 2503 try map.put(key, .{ .uint = value }); 2504 }, 2505 .signed => { 2506 if (try map.getCursor(key)) |value_cursor| { 2507 if (try value_cursor.readInt() == value) { 2508 return; 2509 } 2510 } 2511 2512 try map.put(key, .{ .int = value }); 2513 }, 2514 }, 2515 .bool => { 2516 const bytes = if (value) "true" else "false"; 2517 if (try map.getCursor(key)) |value_cursor| { 2518 var buffer: [5]u8 = undefined; 2519 const existing = try value_cursor.readBytesObject(&buffer); 2520 if (existing.format_tag) |format_tag| { 2521 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 2522 return; 2523 } 2524 } 2525 } 2526 2527 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 2528 }, 2529 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 2530 .@"union" => switch (value) { 2531 inline else => |payload, tag| { 2532 if (@TypeOf(payload) == void) { 2533 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 2534 } else { 2535 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 2536 if (try map.getCursor(key)) |cursor| { 2537 switch (cursor.slot().tag) { 2538 .hash_map => { 2539 const variants = try DB.HashMap(.read_only).init(cursor); 2540 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 2541 }, 2542 else => try map.put(key, .{ .slot = null }), 2543 } 2544 } 2545 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 2546 if (@typeInfo(@TypeOf(payload)) == .optional) { 2547 // keep the variant even when its payload is null 2548 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 2549 } 2550 if (try variants.getCursor(tag_key)) |cursor| { 2551 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 2552 } 2553 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 2554 } 2555 }, 2556 }, 2557 .optional => |optional_info| { 2558 // a missing key is null 2559 if (value) |child| { 2560 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 2561 } else { 2562 _ = try map.remove(key); 2563 } 2564 }, 2565 .@"struct" => try upsert( 2566 Field, 2567 DB, 2568 hash_kind, 2569 try DB.HashMap(.read_write).init(try map.putCursor(key)), 2570 value, 2571 ), 2572 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 2573 } 2574 } 2575 2576 fn upsertBytes( 2577 comptime DB: type, 2578 comptime hash_kind: hash.HashKind, 2579 map: DB.HashMap(.read_write), 2580 key: hash.HashInt(hash_kind), 2581 value: []const u8, 2582 ) !void { 2583 var existing_cursor_maybe = try map.getCursor(key); 2584 if (existing_cursor_maybe) |*existing_cursor| { 2585 if (try bytesEqual(DB, existing_cursor, value)) { 2586 return; 2587 } 2588 } 2589 2590 try map.put(key, .{ .bytes = value }); 2591 } 2592 2593 fn bytesEqual( 2594 comptime DB: type, 2595 cursor: *DB.Cursor(.read_only), 2596 value: []const u8, 2597 ) !bool { 2598 switch (cursor.slot().tag) { 2599 .bytes, .short_bytes => {}, 2600 else => return false, 2601 } 2602 var read_buffer: [1024]u8 = undefined; 2603 var reader = try cursor.reader(&read_buffer); 2604 if (reader.size != value.len) { 2605 return false; 2606 } 2607 2608 var chunk_buffer: [1024]u8 = undefined; 2609 var offset: usize = 0; 2610 while (offset < value.len) { 2611 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2612 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2613 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2614 return false; 2615 } 2616 offset += chunk_len; 2617 } 2618 2619 return true; 2620 } 2621 2622 // 2623 // CommitIterator 2624 // 2625 // differs from xit's ObjectIterator in a few ways: 2626 // 2627 // 1. iterates parents first, rather than children first 2628 // 2. `next` only returns the object id, not the full Object 2629 // 3. stores its temporary state in a file on disk to avoid OOMs 2630 // 2631 2632 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2633 return struct { 2634 const DB = EventDB(repo_opts.hash); 2635 // a plain file is used for the scratch db because each write outside 2636 // a transaction is synced anyway, so buffering would gain nothing 2637 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2638 const db_name = "haxy-repo-events.db"; 2639 2640 repo_dir: std.Io.Dir, 2641 db_file: std.Io.File, 2642 db: *TempDB, 2643 // map of each object id to its children 2644 parent_to_children: TempDB.HashMap(.read_write), 2645 // tracking pending parents is necessary because a child can only 2646 // be returned after all of its unconsumed parents have been returned 2647 child_to_pending_parents: TempDB.HashMap(.read_write), 2648 // queue of object ids that are ready to be returned from `next` 2649 ready_queue: TempDB.ArrayList(.read_write), 2650 ready_queue_index: u64, 2651 // the object id at the tip of this branch 2652 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2653 // the object id furthest back in the history that hasn't been consumed 2654 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2655 2656 pub fn init( 2657 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2658 io: std.Io, 2659 allocator: std.mem.Allocator, 2660 consumed_object_ids: DB.HashMap(.read_only), 2661 ref: rf.Ref, 2662 ) !CommitIterator(repo_kind, repo_opts) { 2663 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2664 errdefer { 2665 db_file.close(io); 2666 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2667 } 2668 2669 // cursors point at the db, so it needs a stable address 2670 const db_ptr = try allocator.create(TempDB); 2671 errdefer allocator.destroy(db_ptr); 2672 // the db is scratch state that gets deleted after iteration, so 2673 // there is nothing worth fsyncing 2674 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2675 2676 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2677 2678 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2679 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2680 2681 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2682 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2683 2684 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2685 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2686 2687 var self = CommitIterator(repo_kind, repo_opts){ 2688 .repo_dir = state.core.repo_dir, 2689 .db_file = db_file, 2690 .db = db_ptr, 2691 .parent_to_children = parent_to_children, 2692 .child_to_pending_parents = child_to_pending_parents, 2693 .ready_queue = ready_queue, 2694 .ready_queue_index = 0, 2695 .newest_object_id = null, 2696 .oldest_object_id = null, 2697 }; 2698 errdefer self.deinit(io, allocator); 2699 2700 try self.collect(state, io, allocator, consumed_object_ids, ref); 2701 2702 return self; 2703 } 2704 2705 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2706 self.db_file.close(io); 2707 self.repo_dir.deleteFile(io, db_name) catch {}; 2708 allocator.destroy(self.db); 2709 } 2710 2711 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2712 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2713 2714 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2715 self.ready_queue_index += 1; 2716 2717 try self.enqueueChildren(&oid); 2718 return oid; 2719 } 2720 2721 fn collect( 2722 self: *CommitIterator(repo_kind, repo_opts), 2723 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2724 io: std.Io, 2725 allocator: std.mem.Allocator, 2726 consumed_object_ids: DB.HashMap(.read_only), 2727 ref: rf.Ref, 2728 ) !void { 2729 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2730 2731 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2732 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2733 var walk_queue_index: u64 = 0; 2734 2735 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2736 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2737 2738 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2739 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2740 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2741 try walk_queue.append(.{ .bytes = &head_oid }); 2742 2743 while (walk_queue_index < try walk_queue.count()) { 2744 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2745 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2746 2747 walk_queue_index += 1; 2748 2749 // if we've seen this object id, skip it 2750 if (null != try seen_object_ids.getCursor(oid_int)) { 2751 continue; 2752 } 2753 2754 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2755 2756 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2757 defer commit_object.deinit(); 2758 2759 // if this object id has already been consumed, skip it 2760 if (null != try consumed_object_ids.getCursor(oid_int)) { 2761 continue; 2762 } 2763 2764 if (self.newest_object_id == null) self.newest_object_id = oid; 2765 self.oldest_object_id = oid; 2766 2767 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2768 var pending_parent_count: u64 = 0; 2769 for (parent_oids) |*parent_oid| { 2770 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2771 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2772 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2773 2774 // if this object id has already been consumed, skip it 2775 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2776 continue; 2777 } 2778 2779 // if this object id hasn't already been seen, add it to the walk queue 2780 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2781 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2782 } 2783 2784 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2785 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2786 try children.put(oid_int, .{ .bytes = &oid }); 2787 pending_parent_count += 1; 2788 } 2789 2790 if (pending_parent_count == 0) { 2791 try self.ready_queue.append(.{ .bytes = &oid }); 2792 } else { 2793 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2794 } 2795 } 2796 } 2797 2798 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2799 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2800 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2801 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2802 var children_iter = try children.iterator(); 2803 2804 while (try children_iter.next()) |*child_cursor| { 2805 const kv_pair = try child_cursor.readKeyValuePair(); 2806 const child_oid_int = kv_pair.hash; 2807 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2808 const pending_parent_count = try pending_parent_cursor.readUint(); 2809 2810 if (pending_parent_count <= 1) { 2811 _ = try self.child_to_pending_parents.remove(child_oid_int); 2812 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2813 try self.ready_queue.append(.{ .bytes = &child_oid }); 2814 } else { 2815 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2816 } 2817 } 2818 } 2819 2820 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2821 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2822 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2823 _ = try oid_cursor.readBytes(&oid); 2824 return oid; 2825 } 2826 }; 2827 } 2828