╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2621 .oldest_object_id = null, 2622 }; 2623 errdefer self.deinit(io, allocator); 2624 2625 try self.collect(state, io, allocator, consumed_object_ids, ref); 2626 2627 return self; 2628 } 2629 2630 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2631 self.db_file.close(io); 2632 self.repo_dir.deleteFile(io, db_name) catch {}; 2633 allocator.destroy(self.db); 2634 } 2635 2636 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2637 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2638 2639 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2640 self.ready_queue_index += 1; 2641 2642 try self.enqueueChildren(&oid); 2643 return oid; 2644 } 2645 2646 fn collect( 2647 self: *CommitIterator(repo_kind, repo_opts), 2648 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2649 io: std.Io, 2650 allocator: std.mem.Allocator, 2651 consumed_object_ids: DB.HashMap(.read_only), 2652 ref: rf.Ref, 2653 ) !void { 2654 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2655 2656 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2657 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2658 var walk_queue_index: u64 = 0; 2659 2660 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2661 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2662 2663 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2664 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2665 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2666 try walk_queue.append(.{ .bytes = &head_oid }); 2667 2668 while (walk_queue_index < try walk_queue.count()) { 2669 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2670 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2671 2672 walk_queue_index += 1; 2673 2674 // if we've seen this object id, skip it 2675 if (null != try seen_object_ids.getCursor(oid_int)) { 2676 continue; 2677 } 2678 2679 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2680 2681 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2682 defer commit_object.deinit(); 2683 2684 // if this object id has already been consumed, skip it 2685 if (null != try consumed_object_ids.getCursor(oid_int)) { 2686 continue; 2687 } 2688 2689 if (self.newest_object_id == null) self.newest_object_id = oid; 2690 self.oldest_object_id = oid; 2691 2692 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2693 var pending_parent_count: u64 = 0; 2694 for (parent_oids) |*parent_oid| { 2695 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2696 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2697 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2698 2699 // if this object id has already been consumed, skip it 2700 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2701 continue; 2702 } 2703 2704 // if this object id hasn't already been seen, add it to the walk queue 2705 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2706 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2707 } 2708 2709 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2710 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2711 try children.put(oid_int, .{ .bytes = &oid }); 2712 pending_parent_count += 1; 2713 } 2714 2715 if (pending_parent_count == 0) { 2716 try self.ready_queue.append(.{ .bytes = &oid }); 2717 } else { 2718 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2719 } 2720 } 2721 } 2722 2723 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2724 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2725 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2726 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2727 var children_iter = try children.iterator(); 2728 2729 while (try children_iter.next()) |*child_cursor| { 2730 const kv_pair = try child_cursor.readKeyValuePair(); 2731 const child_oid_int = kv_pair.hash; 2732 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2733 const pending_parent_count = try pending_parent_cursor.readUint(); 2734 2735 if (pending_parent_count <= 1) { 2736 _ = try self.child_to_pending_parents.remove(child_oid_int); 2737 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2738 try self.ready_queue.append(.{ .bytes = &child_oid }); 2739 } else { 2740 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2741 } 2742 } 2743 } 2744 2745 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2746 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2747 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2748 _ = try oid_cursor.readBytes(&oid); 2749 return oid; 2750 } 2751 }; 2752 } 2753