╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
692 693 var self = CommitIterator(repo_opts){ 694 .repo_dir = state.core.repo_dir, 695 .db_file = db_file, 696 .db = db_ptr, 697 .parent_to_children = parent_to_children, 698 .child_to_pending_parents = child_to_pending_parents, 699 .ready_queue = ready_queue, 700 .ready_queue_index = 0, 701 .newest_object_id = null, 702 .oldest_object_id = null, 703 }; 704 errdefer self.deinit(io, allocator); 705 706 try self.collect(state, io, allocator, consumed_object_ids, ref); 707 708 return self; 709 } 710 711 pub fn deinit(self: *CommitIterator(repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 712 self.db_file.close(io); 713 self.db.core.memory.buffer.deinit(); 714 allocator.destroy(self.db.core.memory.buffer); 715 self.repo_dir.deleteFile(io, db_name) catch {}; 716 allocator.destroy(self.db); 717 } 718 719 pub fn next(self: *CommitIterator(repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 720 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 721 722 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 723 self.ready_queue_index += 1; 724 725 try self.enqueueChildren(&oid); 726 return oid; 727 } 728 729 fn collect( 730 self: *CommitIterator(repo_opts), 731 state: rp.Repo(.xit, repo_opts).State(.read_only), 732 io: std.Io, 733 allocator: std.mem.Allocator, 734 consumed_object_ids: DB.HashMap(.read_only), 735 ref: rf.Ref, 736 ) !void { 737 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 738 739 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 740 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 741 var walk_queue_index: u64 = 0; 742 743 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 744 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 745 746 const head_oid_hex = (try rf.readRecur(.xit, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 747 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 748 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 749 try walk_queue.append(.{ .bytes = &head_oid }); 750 751 while (walk_queue_index < try walk_queue.count()) { 752 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 753 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 754 755 walk_queue_index += 1; 756 757 // if we've seen this object id, skip it 758 if (null != try seen_object_ids.getCursor(oid_int)) { 759 continue; 760 } 761 762 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 763 764 var commit_object = try obj.Object(.xit, repo_opts, .full).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 765 defer commit_object.deinit(); 766 767 // if this object id has already been consumed, skip it 768 if (null != try consumed_object_ids.getCursor(oid_int)) { 769 continue; 770 } 771 772 if (self.newest_object_id == null) self.newest_object_id = oid; 773 self.oldest_object_id = oid; 774 775 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 776 var pending_parent_count: u64 = 0; 777 for (parent_oids) |*parent_oid| { 778 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 779 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 780 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 781 782 // if this object id has already been consumed, skip it 783 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 784 continue; 785 } 786 787 // if this object id hasn't already been seen, add it to the walk queue 788 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 789 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 790 } 791 792 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 793 const children = try DB.HashSet(.read_write).init(children_cursor); 794 try children.put(oid_int, .{ .bytes = &oid }); 795 pending_parent_count += 1; 796 } 797 798 if (pending_parent_count == 0) { 799 try self.ready_queue.append(.{ .bytes = &oid }); 800 } else { 801 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 802 } 803 } 804 } 805 806 fn enqueueChildren(self: *CommitIterator(repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 807 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 808 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 809 const children = try DB.HashSet(.read_only).init(children_cursor); 810 var children_iter = try children.iterator(); 811 812 while (try children_iter.next()) |*child_cursor| { 813 const kv_pair = try child_cursor.readKeyValuePair(); 814 const child_oid_int = kv_pair.hash; 815 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 816 const pending_parent_count = try pending_parent_cursor.readUint(); 817 818 if (pending_parent_count <= 1) { 819 _ = try self.child_to_pending_parents.remove(child_oid_int); 820 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 821 try self.ready_queue.append(.{ .bytes = &child_oid }); 822 } else { 823 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 824 } 825 } 826 } 827 828 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 829 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 830 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 831 _ = try oid_cursor.readBytes(&oid); 832 return oid; 833 } 834 }; 835 } 836