╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1905 return try read(Fields, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 1906 } 1907 1908 fn assertFieldSubset(comptime Sub: type, comptime Full: type) void { 1909 for (@typeInfo(Sub).@"struct".fields) |field| { 1910 if (!@hasField(Full, field.name)) @compileError(@typeName(Full) ++ " has no field " ++ field.name); 1911 const FullField = @FieldType(Full, field.name); 1912 if (@typeInfo(field.type) == .@"struct") { 1913 assertFieldSubset(field.type, FullField); 1914 } else if (field.type != FullField) { 1915 @compileError(@typeName(Full) ++ "." ++ field.name ++ " is a " ++ @typeName(FullField)); 1916 } 1917 } 1918 } 1919 1920 pub fn read( 1921 comptime T: type, 1922 comptime DB: type, 1923 comptime hash_kind: hash.HashKind, 1924 arena: *std.heap.ArenaAllocator, 1925 map: DB.HashMap(.read_only), 1926 ) !T { 1927 var event: T = undefined; 1928 switch (@typeInfo(T)) { 1929 .@"struct" => |struct_info| { 1930 inline for (struct_info.fields) |field| { 1931 const cursor = try map.getCursor(hash.hashInt(hash_kind, field.name)); 1932 @field(event, field.name) = try readField(field.type, DB, hash_kind, arena, cursor); 1933 } 1934 }, 1935 else => @compileError("read expects a struct"), 1936 } 1937 return event; 1938 } 1939 1940 fn readField( 1941 comptime Field: type, 1942 comptime DB: type, 1943 comptime hash_kind: hash.HashKind, 1944 arena: *std.heap.ArenaAllocator, 1945 cursor_maybe: ?DB.Cursor(.read_only), 1946 ) !Field { 1947 if (@typeInfo(Field) == .optional) { 1948 // a missing key or a null union payload is null 1949 const cursor = cursor_maybe orelse return null; 1950 if (cursor.slot().tag == .none) return null; 1951 return try readField(@typeInfo(Field).optional.child, DB, hash_kind, arena, cursor); 1952 } 1953 const cursor = cursor_maybe orelse return error.NotFound; 1954 switch (@typeInfo(Field)) { 1955 .pointer => |pointer_info| { 1956 if (pointer_info.size != .slice or pointer_info.child != u8 or !pointer_info.is_const) { 1957 @compileError("unsupported read field type: " ++ @typeName(Field)); 1958 } 1959 return try cursor.readBytesAlloc(arena.allocator(), null); 1960 }, 1961 .array => |array_info| { 1962 if (array_info.child != u8) @compileError("unsupported read field type: " ++ @typeName(Field)); 1963 var bytes: Field = undefined; 1964 if ((try cursor.readBytes(&bytes)).len != bytes.len) return error.InvalidByteArrayLength; 1965 return bytes; 1966 }, 1967 .bool => { 1968 var buffer: [5]u8 = undefined; 1969 const bytes = try cursor.readBytesObject(&buffer); 1970 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 1971 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 1972 if (std.mem.eql(u8, bytes.value, "true")) return true; 1973 if (std.mem.eql(u8, bytes.value, "false")) return false; 1974 return error.InvalidBool; 1975 }, 1976 .int => |int_info| return switch (int_info.signedness) { 1977 .unsigned => @intCast(try cursor.readUint()), 1978 .signed => @intCast(try cursor.readInt()), 1979 }, 1980 .@"enum" => return std.meta.stringToEnum(Field, try cursor.readBytesAlloc(arena.allocator(), null)) orelse error.InvalidEnumTag, 1981 .@"union" => |union_info| { 1982 if (union_info.tag_type == null) @compileError("unsupported read field type: " ++ @typeName(Field)); 1983 switch (cursor.slot().tag) { 1984 .bytes, .short_bytes => { 1985 const name = try cursor.readBytesAlloc(arena.allocator(), null); 1986 inline for (union_info.fields) |variant| { 1987 if (variant.type == void and std.mem.eql(u8, name, variant.name)) { 1988 return @unionInit(Field, variant.name, {}); 1989 } 1990 } 1991 return error.InvalidUnion; 1992 }, 1993 .hash_map => { 1994 const variants = try DB.HashMap(.read_only).init(cursor); 1995 var iter = try variants.iterator(); 1996 const entry = (try iter.next()) orelse return error.InvalidUnion; 1997 const pair = try entry.readKeyValuePair(); 1998 if (try iter.next() != null) return error.InvalidUnion; 1999 inline for (union_info.fields) |variant| { 2000 if (variant.type != void and pair.hash == hash.hashInt(hash_kind, variant.name)) { 2001 return @unionInit(Field, variant.name, try readField(variant.type, DB, hash_kind, arena, pair.value_cursor)); 2002 } 2003 } 2004 return error.InvalidUnion; 2005 }, 2006 else => return error.InvalidUnion, 2007 } 2008 }, 2009 .@"struct" => return try read(Field, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)), 2010 else => @compileError("unsupported read field type: " ++ @typeName(Field)), 2011 } 2012 } 2013 2014 // whether two events carry the same data 2015 fn fieldsEqual(comptime T: type, a: T, b: T) bool { 2016 switch (@typeInfo(T)) { 2017 .@"struct" => |struct_info| { 2018 inline for (struct_info.fields) |field| { 2019 if (!fieldEqual(field.type, @field(a, field.name), @field(b, field.name))) return false; 2020 } 2021 }, 2022 else => @compileError("fieldsEqual expects a struct"), 2023 } 2024 2025 return true; 2026 } 2027 2028 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 2029 switch (@typeInfo(Field)) { 2030 .pointer => |pointer_info| { 2031 if (pointer_info.size == .slice and pointer_info.child == u8) { 2032 return std.mem.eql(u8, a, b); 2033 } else { 2034 @compileError("unsupported field type: " ++ @typeName(Field)); 2035 } 2036 }, 2037 .array => |array_info| { 2038 if (array_info.child == u8) { 2039 return std.mem.eql(u8, &a, &b); 2040 } else { 2041 @compileError("unsupported field type: " ++ @typeName(Field)); 2042 } 2043 }, 2044 .void => return true, 2045 .bool, .int, .@"enum" => return a == b, 2046 .@"union" => { 2047 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 2048 return switch (a) { 2049 inline else => |value, tag| fieldEqual(@TypeOf(value), value, @field(b, @tagName(tag))), 2050 }; 2051 }, 2052 .@"struct" => return fieldsEqual(Field, a, b), 2053 .optional => |optional_info| { 2054 if (a) |a_value| { 2055 if (b) |b_value| return fieldEqual(optional_info.child, a_value, b_value); 2056 return false; 2057 } 2058 return b == null; 2059 }, 2060 else => @compileError("unsupported field type: " ++ @typeName(Field)), 2061 } 2062 } 2063 2064 // 2065 // writing to xitdb 2066 // 2067 2068 pub fn upsert( 2069 comptime T: type, 2070 comptime DB: type, 2071 comptime hash_kind: hash.HashKind, 2072 map: DB.HashMap(.read_write), 2073 event: T, 2074 ) !void { 2075 switch (@typeInfo(T)) { 2076 .@"struct" => |struct_info| { 2077 inline for (struct_info.fields) |field| { 2078 try upsertField(DB, hash_kind, map, field.name, field.type, @field(event, field.name)); 2079 } 2080 }, 2081 else => @compileError("upsert expects a struct"), 2082 } 2083 } 2084 2085 fn upsertField( 2086 comptime DB: type, 2087 comptime hash_kind: hash.HashKind, 2088 map: DB.HashMap(.read_write), 2089 comptime field_name: []const u8, 2090 comptime Field: type, 2091 value: Field, 2092 ) !void { 2093 const key = hash.hashInt(hash_kind, field_name); 2094 2095 switch (@typeInfo(Field)) { 2096 .pointer => |pointer_info| { 2097 if (pointer_info.size == .slice and pointer_info.child == u8) { 2098 try upsertBytes(DB, hash_kind, map, key, value); 2099 } else { 2100 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2101 } 2102 }, 2103 .array => |array_info| { 2104 if (array_info.child == u8) { 2105 try upsertBytes(DB, hash_kind, map, key, &value); 2106 } else { 2107 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2108 } 2109 }, 2110 .int => |int_info| switch (int_info.signedness) { 2111 .unsigned => { 2112 if (try map.getCursor(key)) |value_cursor| { 2113 if (try value_cursor.readUint() == value) { 2114 return; 2115 } 2116 } 2117 2118 try map.put(key, .{ .uint = value }); 2119 }, 2120 .signed => { 2121 if (try map.getCursor(key)) |value_cursor| { 2122 if (try value_cursor.readInt() == value) { 2123 return; 2124 } 2125 } 2126 2127 try map.put(key, .{ .int = value }); 2128 }, 2129 }, 2130 .bool => { 2131 const bytes = if (value) "true" else "false"; 2132 if (try map.getCursor(key)) |value_cursor| { 2133 var buffer: [5]u8 = undefined; 2134 const existing = try value_cursor.readBytesObject(&buffer); 2135 if (existing.format_tag) |format_tag| { 2136 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 2137 return; 2138 } 2139 } 2140 } 2141 2142 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 2143 }, 2144 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 2145 .@"union" => switch (value) { 2146 inline else => |payload, tag| { 2147 if (@TypeOf(payload) == void) { 2148 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 2149 } else { 2150 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 2151 if (try map.getCursor(key)) |cursor| { 2152 switch (cursor.slot().tag) { 2153 .hash_map => { 2154 const variants = try DB.HashMap(.read_only).init(cursor); 2155 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 2156 }, 2157 else => try map.put(key, .{ .slot = null }), 2158 } 2159 } 2160 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 2161 if (@typeInfo(@TypeOf(payload)) == .optional) { 2162 // keep the variant even when its payload is null 2163 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 2164 } 2165 if (try variants.getCursor(tag_key)) |cursor| { 2166 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 2167 } 2168 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 2169 } 2170 }, 2171 }, 2172 .optional => |optional_info| { 2173 // a missing key is null 2174 if (value) |child| { 2175 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 2176 } else { 2177 _ = try map.remove(key); 2178 } 2179 }, 2180 .@"struct" => try upsert( 2181 Field, 2182 DB, 2183 hash_kind, 2184 try DB.HashMap(.read_write).init(try map.putCursor(key)), 2185 value, 2186 ), 2187 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 2188 } 2189 } 2190 2191 fn upsertBytes( 2192 comptime DB: type, 2193 comptime hash_kind: hash.HashKind, 2194 map: DB.HashMap(.read_write), 2195 key: hash.HashInt(hash_kind), 2196 value: []const u8, 2197 ) !void { 2198 var existing_cursor_maybe = try map.getCursor(key); 2199 if (existing_cursor_maybe) |*existing_cursor| { 2200 if (try bytesEqual(DB, existing_cursor, value)) { 2201 return; 2202 } 2203 } 2204 2205 try map.put(key, .{ .bytes = value }); 2206 } 2207 2208 fn bytesEqual( 2209 comptime DB: type, 2210 cursor: *DB.Cursor(.read_only), 2211 value: []const u8, 2212 ) !bool { 2213 switch (cursor.slot().tag) { 2214 .bytes, .short_bytes => {}, 2215 else => return false, 2216 } 2217 var read_buffer: [1024]u8 = undefined; 2218 var reader = try cursor.reader(&read_buffer); 2219 if (reader.size != value.len) { 2220 return false; 2221 } 2222 2223 var chunk_buffer: [1024]u8 = undefined; 2224 var offset: usize = 0; 2225 while (offset < value.len) { 2226 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2227 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2228 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2229 return false; 2230 } 2231 offset += chunk_len; 2232 } 2233 2234 return true; 2235 } 2236 2237 // 2238 // CommitIterator 2239 // 2240 // differs from xit's ObjectIterator in a few ways: 2241 // 2242 // 1. iterates parents first, rather than children first 2243 // 2. `next` only returns the object id, not the full Object 2244 // 3. stores its temporary state in a file on disk to avoid OOMs 2245 // 2246 2247 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2248 return struct { 2249 const DB = EventDB(repo_opts.hash); 2250 // a plain file is used for the scratch db because each write outside 2251 // a transaction is synced anyway, so buffering would gain nothing 2252 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2253 const db_name = "haxy-repo-events.db"; 2254 2255 repo_dir: std.Io.Dir, 2256 db_file: std.Io.File, 2257 db: *TempDB, 2258 // map of each object id to its children 2259 parent_to_children: TempDB.HashMap(.read_write), 2260 // tracking pending parents is necessary because a child can only 2261 // be returned after all of its unconsumed parents have been returned 2262 child_to_pending_parents: TempDB.HashMap(.read_write), 2263 // queue of object ids that are ready to be returned from `next` 2264 ready_queue: TempDB.ArrayList(.read_write), 2265 ready_queue_index: u64, 2266 // the object id at the tip of this branch 2267 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2268 // the object id furthest back in the history that hasn't been consumed 2269 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2270 2271 pub fn init( 2272 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2273 io: std.Io, 2274 allocator: std.mem.Allocator, 2275 consumed_object_ids: DB.HashMap(.read_only), 2276 ref: rf.Ref, 2277 ) !CommitIterator(repo_kind, repo_opts) { 2278 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2279 errdefer { 2280 db_file.close(io); 2281 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2282 } 2283 2284 // cursors point at the db, so it needs a stable address 2285 const db_ptr = try allocator.create(TempDB); 2286 errdefer allocator.destroy(db_ptr); 2287 // the db is scratch state that gets deleted after iteration, so 2288 // there is nothing worth fsyncing 2289 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2290 2291 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2292 2293 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2294 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2295 2296 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2297 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2298 2299 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2300 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2301 2302 var self = CommitIterator(repo_kind, repo_opts){ 2303 .repo_dir = state.core.repo_dir, 2304 .db_file = db_file, 2305 .db = db_ptr, 2306 .parent_to_children = parent_to_children, 2307 .child_to_pending_parents = child_to_pending_parents, 2308 .ready_queue = ready_queue, 2309 .ready_queue_index = 0, 2310 .newest_object_id = null, 2311 .oldest_object_id = null, 2312 }; 2313 errdefer self.deinit(io, allocator); 2314 2315 try self.collect(state, io, allocator, consumed_object_ids, ref); 2316 2317 return self; 2318 } 2319 2320 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2321 self.db_file.close(io); 2322 self.repo_dir.deleteFile(io, db_name) catch {}; 2323 allocator.destroy(self.db); 2324 } 2325 2326 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2327 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2328 2329 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2330 self.ready_queue_index += 1; 2331 2332 try self.enqueueChildren(&oid); 2333 return oid; 2334 } 2335 2336 fn collect( 2337 self: *CommitIterator(repo_kind, repo_opts), 2338 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2339 io: std.Io, 2340 allocator: std.mem.Allocator, 2341 consumed_object_ids: DB.HashMap(.read_only), 2342 ref: rf.Ref, 2343 ) !void { 2344 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2345 2346 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2347 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2348 var walk_queue_index: u64 = 0; 2349 2350 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2351 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2352 2353 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2354 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2355 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2356 try walk_queue.append(.{ .bytes = &head_oid }); 2357 2358 while (walk_queue_index < try walk_queue.count()) { 2359 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2360 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2361 2362 walk_queue_index += 1; 2363 2364 // if we've seen this object id, skip it 2365 if (null != try seen_object_ids.getCursor(oid_int)) { 2366 continue; 2367 } 2368 2369 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2370 2371 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2372 defer commit_object.deinit(); 2373 2374 // if this object id has already been consumed, skip it 2375 if (null != try consumed_object_ids.getCursor(oid_int)) { 2376 continue; 2377 } 2378 2379 if (self.newest_object_id == null) self.newest_object_id = oid; 2380 self.oldest_object_id = oid; 2381 2382 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2383 var pending_parent_count: u64 = 0; 2384 for (parent_oids) |*parent_oid| { 2385 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2386 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2387 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2388 2389 // if this object id has already been consumed, skip it 2390 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2391 continue; 2392 } 2393 2394 // if this object id hasn't already been seen, add it to the walk queue 2395 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2396 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2397 } 2398 2399 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2400 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2401 try children.put(oid_int, .{ .bytes = &oid }); 2402 pending_parent_count += 1; 2403 } 2404 2405 if (pending_parent_count == 0) { 2406 try self.ready_queue.append(.{ .bytes = &oid }); 2407 } else { 2408 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2409 } 2410 } 2411 } 2412 2413 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2414 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2415 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2416 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2417 var children_iter = try children.iterator(); 2418 2419 while (try children_iter.next()) |*child_cursor| { 2420 const kv_pair = try child_cursor.readKeyValuePair(); 2421 const child_oid_int = kv_pair.hash; 2422 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2423 const pending_parent_count = try pending_parent_cursor.readUint(); 2424 2425 if (pending_parent_count <= 1) { 2426 _ = try self.child_to_pending_parents.remove(child_oid_int); 2427 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2428 try self.ready_queue.append(.{ .bytes = &child_oid }); 2429 } else { 2430 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2431 } 2432 } 2433 } 2434 2435 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2436 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2437 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2438 _ = try oid_cursor.readBytes(&oid); 2439 return oid; 2440 } 2441 }; 2442 } 2443