╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
2943 .parent_to_children = parent_to_children, 2944 .child_to_pending_parents = child_to_pending_parents, 2945 .ready_queue = ready_queue, 2946 .ready_queue_index = 0, 2947 .newest_object_id = null, 2948 .oldest_object_id = null, 2949 }; 2950 errdefer self.deinit(io, allocator); 2951 2952 try self.collect(state, io, allocator, consumed_object_ids, ref); 2953 2954 return self; 2955 } 2956 2957 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2958 self.db_file.close(io); 2959 self.repo_dir.deleteFile(io, db_name) catch {}; 2960 allocator.destroy(self.db); 2961 } 2962 2963 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2964 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2965 2966 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2967 self.ready_queue_index += 1; 2968 2969 try self.enqueueChildren(&oid); 2970 return oid; 2971 } 2972 2973 fn collect( 2974 self: *CommitIterator(repo_kind, repo_opts), 2975 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2976 io: std.Io, 2977 allocator: std.mem.Allocator, 2978 consumed_object_ids: DB.HashMap(.read_only), 2979 ref: rf.Ref, 2980 ) !void { 2981 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2982 2983 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2984 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2985 var walk_queue_index: u64 = 0; 2986 2987 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2988 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2989 2990 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2991 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2992 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2993 try walk_queue.append(.{ .bytes = &head_oid }); 2994 2995 while (walk_queue_index < try walk_queue.count()) { 2996 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2997 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2998 2999 walk_queue_index += 1; 3000 3001 // if we've seen this object id, skip it 3002 if (null != try seen_object_ids.getCursor(oid_int)) { 3003 continue; 3004 } 3005 3006 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 3007 3008 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 3009 defer commit_object.deinit(); 3010 3011 // if this object id has already been consumed, skip it 3012 if (null != try consumed_object_ids.getCursor(oid_int)) { 3013 continue; 3014 } 3015 3016 if (self.newest_object_id == null) self.newest_object_id = oid; 3017 self.oldest_object_id = oid; 3018 3019 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 3020 var pending_parent_count: u64 = 0; 3021 for (parent_oids) |*parent_oid| { 3022 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 3023 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 3024 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 3025 3026 // if this object id has already been consumed, skip it 3027 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 3028 continue; 3029 } 3030 3031 // if this object id hasn't already been seen, add it to the walk queue 3032 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 3033 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 3034 } 3035 3036 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 3037 const children = try TempDB.HashSet(.read_write).init(children_cursor); 3038 try children.put(oid_int, .{ .bytes = &oid }); 3039 pending_parent_count += 1; 3040 } 3041 3042 if (pending_parent_count == 0) { 3043 try self.ready_queue.append(.{ .bytes = &oid }); 3044 } else { 3045 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 3046 } 3047 } 3048 } 3049 3050 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 3051 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 3052 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 3053 const children = try TempDB.HashSet(.read_only).init(children_cursor); 3054 var children_iter = try children.iterator(); 3055 3056 while (try children_iter.next()) |*child_cursor| { 3057 const kv_pair = try child_cursor.readKeyValuePair(); 3058 const child_oid_int = kv_pair.hash; 3059 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 3060 const pending_parent_count = try pending_parent_cursor.readUint(); 3061 3062 if (pending_parent_count <= 1) { 3063 _ = try self.child_to_pending_parents.remove(child_oid_int); 3064 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 3065 try self.ready_queue.append(.{ .bytes = &child_oid }); 3066 } else { 3067 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 3068 } 3069 } 3070 } 3071 3072 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 3073 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 3074 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 3075 _ = try oid_cursor.readBytes(&oid); 3076 return oid; 3077 } 3078 }; 3079 } 3080