╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1977 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 1978 1979 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 1980 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 1981 1982 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 1983 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 1984 1985 var self = CommitIterator(repo_kind, repo_opts){ 1986 .repo_dir = state.core.repo_dir, 1987 .db_file = db_file, 1988 .db = db_ptr, 1989 .parent_to_children = parent_to_children, 1990 .child_to_pending_parents = child_to_pending_parents, 1991 .ready_queue = ready_queue, 1992 .ready_queue_index = 0, 1993 .newest_object_id = null, 1994 .oldest_object_id = null, 1995 }; 1996 errdefer self.deinit(io, allocator); 1997 1998 try self.collect(state, io, allocator, consumed_object_ids, ref); 1999 2000 return self; 2001 } 2002 2003 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2004 self.db_file.close(io); 2005 self.db.core.memory.buffer.deinit(); 2006 allocator.destroy(self.db.core.memory.buffer); 2007 self.repo_dir.deleteFile(io, db_name) catch {}; 2008 allocator.destroy(self.db); 2009 } 2010 2011 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2012 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2013 2014 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2015 self.ready_queue_index += 1; 2016 2017 try self.enqueueChildren(&oid); 2018 return oid; 2019 } 2020 2021 fn collect( 2022 self: *CommitIterator(repo_kind, repo_opts), 2023 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2024 io: std.Io, 2025 allocator: std.mem.Allocator, 2026 consumed_object_ids: DB.HashMap(.read_only), 2027 ref: rf.Ref, 2028 ) !void { 2029 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 2030 2031 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2032 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 2033 var walk_queue_index: u64 = 0; 2034 2035 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2036 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 2037 2038 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2039 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2040 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2041 try walk_queue.append(.{ .bytes = &head_oid }); 2042 2043 while (walk_queue_index < try walk_queue.count()) { 2044 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2045 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2046 2047 walk_queue_index += 1; 2048 2049 // if we've seen this object id, skip it 2050 if (null != try seen_object_ids.getCursor(oid_int)) { 2051 continue; 2052 } 2053 2054 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2055 2056 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2057 defer commit_object.deinit(); 2058 2059 // if this object id has already been consumed, skip it 2060 if (null != try consumed_object_ids.getCursor(oid_int)) { 2061 continue; 2062 } 2063 2064 if (self.newest_object_id == null) self.newest_object_id = oid; 2065 self.oldest_object_id = oid; 2066 2067 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2068 var pending_parent_count: u64 = 0; 2069 for (parent_oids) |*parent_oid| { 2070 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2071 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2072 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2073 2074 // if this object id has already been consumed, skip it 2075 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2076 continue; 2077 } 2078 2079 // if this object id hasn't already been seen, add it to the walk queue 2080 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2081 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2082 } 2083 2084 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2085 const children = try DB.HashSet(.read_write).init(children_cursor); 2086 try children.put(oid_int, .{ .bytes = &oid }); 2087 pending_parent_count += 1; 2088 } 2089 2090 if (pending_parent_count == 0) { 2091 try self.ready_queue.append(.{ .bytes = &oid }); 2092 } else { 2093 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2094 } 2095 } 2096 } 2097 2098 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2099 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2100 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2101 const children = try DB.HashSet(.read_only).init(children_cursor); 2102 var children_iter = try children.iterator(); 2103 2104 while (try children_iter.next()) |*child_cursor| { 2105 const kv_pair = try child_cursor.readKeyValuePair(); 2106 const child_oid_int = kv_pair.hash; 2107 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2108 const pending_parent_count = try pending_parent_cursor.readUint(); 2109 2110 if (pending_parent_count <= 1) { 2111 _ = try self.child_to_pending_parents.remove(child_oid_int); 2112 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2113 try self.ready_queue.append(.{ .bytes = &child_oid }); 2114 } else { 2115 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2116 } 2117 } 2118 } 2119 2120 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2121 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2122 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2123 _ = try oid_cursor.readBytes(&oid); 2124 return oid; 2125 } 2126 }; 2127 } 2128