╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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