╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
2670 // queue of object ids that are ready to be returned from `next` 2671 ready_queue: TempDB.ArrayList(.read_write), 2672 ready_queue_index: u64, 2673 // the object id at the tip of this branch 2674 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2675 // the object id furthest back in the history that hasn't been consumed 2676 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2677 2678 pub fn init( 2679 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2680 io: std.Io, 2681 allocator: std.mem.Allocator, 2682 consumed_object_ids: DB.HashMap(.read_only), 2683 ref: rf.Ref, 2684 ) !CommitIterator(repo_kind, repo_opts) { 2685 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2686 errdefer { 2687 db_file.close(io); 2688 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2689 } 2690 2691 // cursors point at the db, so it needs a stable address 2692 const db_ptr = try allocator.create(TempDB); 2693 errdefer allocator.destroy(db_ptr); 2694 // the db is scratch state that gets deleted after iteration, so 2695 // there is nothing worth fsyncing 2696 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2697 2698 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2699 2700 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2701 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2702 2703 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2704 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2705 2706 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2707 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2708 2709 var self = CommitIterator(repo_kind, repo_opts){ 2710 .repo_dir = state.core.repo_dir, 2711 .db_file = db_file, 2712 .db = db_ptr, 2713 .parent_to_children = parent_to_children, 2714 .child_to_pending_parents = child_to_pending_parents, 2715 .ready_queue = ready_queue, 2716 .ready_queue_index = 0, 2717 .newest_object_id = null, 2718 .oldest_object_id = null, 2719 }; 2720 errdefer self.deinit(io, allocator); 2721 2722 try self.collect(state, io, allocator, consumed_object_ids, ref); 2723 2724 return self; 2725 } 2726 2727 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2728 self.db_file.close(io); 2729 self.repo_dir.deleteFile(io, db_name) catch {}; 2730 allocator.destroy(self.db); 2731 } 2732 2733 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2734 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2735 2736 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2737 self.ready_queue_index += 1; 2738 2739 try self.enqueueChildren(&oid); 2740 return oid; 2741 } 2742 2743 fn collect( 2744 self: *CommitIterator(repo_kind, repo_opts), 2745 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2746 io: std.Io, 2747 allocator: std.mem.Allocator, 2748 consumed_object_ids: DB.HashMap(.read_only), 2749 ref: rf.Ref, 2750 ) !void { 2751 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2752 2753 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2754 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2755 var walk_queue_index: u64 = 0; 2756 2757 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2758 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2759 2760 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2761 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2762 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2763 try walk_queue.append(.{ .bytes = &head_oid }); 2764 2765 while (walk_queue_index < try walk_queue.count()) { 2766 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2767 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2768 2769 walk_queue_index += 1; 2770 2771 // if we've seen this object id, skip it 2772 if (null != try seen_object_ids.getCursor(oid_int)) { 2773 continue; 2774 } 2775 2776 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2777 2778 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2779 defer commit_object.deinit(); 2780 2781 // if this object id has already been consumed, skip it 2782 if (null != try consumed_object_ids.getCursor(oid_int)) { 2783 continue; 2784 } 2785 2786 if (self.newest_object_id == null) self.newest_object_id = oid; 2787 self.oldest_object_id = oid; 2788 2789 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2790 var pending_parent_count: u64 = 0; 2791 for (parent_oids) |*parent_oid| { 2792 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2793 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2794 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2795 2796 // if this object id has already been consumed, skip it 2797 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2798 continue; 2799 } 2800 2801 // if this object id hasn't already been seen, add it to the walk queue 2802 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2803 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2804 } 2805 2806 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2807 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2808 try children.put(oid_int, .{ .bytes = &oid }); 2809 pending_parent_count += 1; 2810 } 2811 2812 if (pending_parent_count == 0) { 2813 try self.ready_queue.append(.{ .bytes = &oid }); 2814 } else { 2815 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2816 } 2817 } 2818 } 2819 2820 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2821 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2822 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2823 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2824 var children_iter = try children.iterator(); 2825 2826 while (try children_iter.next()) |*child_cursor| { 2827 const kv_pair = try child_cursor.readKeyValuePair(); 2828 const child_oid_int = kv_pair.hash; 2829 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2830 const pending_parent_count = try pending_parent_cursor.readUint(); 2831 2832 if (pending_parent_count <= 1) { 2833 _ = try self.child_to_pending_parents.remove(child_oid_int); 2834 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2835 try self.ready_queue.append(.{ .bytes = &child_oid }); 2836 } else { 2837 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2838 } 2839 } 2840 } 2841 2842 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2843 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2844 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2845 _ = try oid_cursor.readBytes(&oid); 2846 return oid; 2847 } 2848 }; 2849 } 2850