╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
679 680 var chunk_buffer: [1024]u8 = undefined; 681 var offset: usize = 0; 682 while (offset < value.len) { 683 const chunk_len = @min(chunk_buffer.len, value.len - offset); 684 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 685 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 686 return false; 687 } 688 offset += chunk_len; 689 } 690 691 return true; 692 } 693 694 // 695 // CommitIterator 696 // 697 // differs from xit's ObjectIterator in a few ways: 698 // 699 // 1. iterates parents first, rather than children first 700 // 2. `next` only returns the object id, not the full Object 701 // 3. stores its temporary state in a file on disk to avoid OOMs 702 // 703 704 pub fn CommitIterator(comptime repo_opts: rp.RepoOpts(.xit)) type { 705 return struct { 706 const DB = rp.Repo(.xit, repo_opts).DB; 707 const db_name = "haxy-repo-events.db"; 708 709 repo_dir: std.Io.Dir, 710 db_file: std.Io.File, 711 db: *DB, 712 // map of each object id to its children 713 parent_to_children: DB.HashMap(.read_write), 714 // tracking pending parents is necessary because a child can only 715 // be returned after all of its unconsumed parents have been returned 716 child_to_pending_parents: DB.HashMap(.read_write), 717 // queue of object ids that are ready to be returned from `next` 718 ready_queue: DB.ArrayList(.read_write), 719 ready_queue_index: u64, 720 // the object id at the tip of this branch 721 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 722 // the object id furthest back in the history that hasn't been consumed 723 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 724 725 pub fn init( 726 state: rp.Repo(.xit, repo_opts).State(.read_only), 727 io: std.Io, 728 allocator: std.mem.Allocator, 729 consumed_object_ids: DB.HashMap(.read_only), 730 ref: rf.Ref, 731 ) !CommitIterator(repo_opts) { 732 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 733 errdefer { 734 db_file.close(io); 735 state.core.repo_dir.deleteFile(io, db_name) catch {}; 736 } 737 738 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 739 errdefer allocator.destroy(buffer_ptr); 740 741 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 742 errdefer buffer_ptr.deinit(); 743 744 const db_ptr = try allocator.create(DB); 745 errdefer allocator.destroy(db_ptr); 746 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr }); 747 748 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 749 750 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 751 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 752 753 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 754 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 755 756 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 757 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 758 759 var self = CommitIterator(repo_opts){ 760 .repo_dir = state.core.repo_dir, 761 .db_file = db_file, 762 .db = db_ptr, 763 .parent_to_children = parent_to_children, 764 .child_to_pending_parents = child_to_pending_parents, 765 .ready_queue = ready_queue, 766 .ready_queue_index = 0, 767 .newest_object_id = null, 768 .oldest_object_id = null, 769 }; 770 errdefer self.deinit(io, allocator); 771 772 try self.collect(state, io, allocator, consumed_object_ids, ref); 773 774 return self; 775 } 776 777 pub fn deinit(self: *CommitIterator(repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 778 self.db_file.close(io); 779 self.db.core.memory.buffer.deinit(); 780 allocator.destroy(self.db.core.memory.buffer); 781 self.repo_dir.deleteFile(io, db_name) catch {}; 782 allocator.destroy(self.db); 783 } 784 785 pub fn next(self: *CommitIterator(repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 786 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 787 788 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 789 self.ready_queue_index += 1; 790 791 try self.enqueueChildren(&oid); 792 return oid; 793 } 794 795 fn collect( 796 self: *CommitIterator(repo_opts), 797 state: rp.Repo(.xit, repo_opts).State(.read_only), 798 io: std.Io, 799 allocator: std.mem.Allocator, 800 consumed_object_ids: DB.HashMap(.read_only), 801 ref: rf.Ref, 802 ) !void { 803 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 804 805 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 806 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 807 var walk_queue_index: u64 = 0; 808 809 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 810 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 811 812 const head_oid_hex = (try rf.readRecur(.xit, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 813 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 814 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 815 try walk_queue.append(.{ .bytes = &head_oid }); 816 817 while (walk_queue_index < try walk_queue.count()) { 818 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 819 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 820 821 walk_queue_index += 1; 822 823 // if we've seen this object id, skip it 824 if (null != try seen_object_ids.getCursor(oid_int)) { 825 continue; 826 } 827 828 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 829 830 var commit_object = try obj.Object(.xit, repo_opts, .full).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 831 defer commit_object.deinit(); 832 833 // if this object id has already been consumed, skip it 834 if (null != try consumed_object_ids.getCursor(oid_int)) { 835 continue; 836 } 837 838 if (self.newest_object_id == null) self.newest_object_id = oid; 839 self.oldest_object_id = oid; 840 841 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 842 var pending_parent_count: u64 = 0; 843 for (parent_oids) |*parent_oid| { 844 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 845 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 846 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 847 848 // if this object id has already been consumed, skip it 849 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 850 continue; 851 } 852 853 // if this object id hasn't already been seen, add it to the walk queue 854 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 855 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 856 } 857 858 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 859 const children = try DB.HashSet(.read_write).init(children_cursor); 860 try children.put(oid_int, .{ .bytes = &oid }); 861 pending_parent_count += 1; 862 } 863 864 if (pending_parent_count == 0) { 865 try self.ready_queue.append(.{ .bytes = &oid }); 866 } else { 867 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 868 } 869 } 870 } 871 872 fn enqueueChildren(self: *CommitIterator(repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 873 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 874 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 875 const children = try DB.HashSet(.read_only).init(children_cursor); 876 var children_iter = try children.iterator(); 877 878 while (try children_iter.next()) |*child_cursor| { 879 const kv_pair = try child_cursor.readKeyValuePair(); 880 const child_oid_int = kv_pair.hash; 881 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 882 const pending_parent_count = try pending_parent_cursor.readUint(); 883 884 if (pending_parent_count <= 1) { 885 _ = try self.child_to_pending_parents.remove(child_oid_int); 886 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 887 try self.ready_queue.append(.{ .bytes = &child_oid }); 888 } else { 889 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 890 } 891 } 892 } 893 894 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 895 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 896 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 897 _ = try oid_cursor.readBytes(&oid); 898 return oid; 899 } 900 }; 901 } 902