╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2131 ) !Field { 2132 if (@typeInfo(Field) == .optional) { 2133 // a missing key or a null union payload is null 2134 const cursor = cursor_maybe orelse return null; 2135 if (cursor.slot().tag == .none) return null; 2136 return try readField(@typeInfo(Field).optional.child, DB, hash_kind, arena, cursor); 2137 } 2138 const cursor = cursor_maybe orelse return error.NotFound; 2139 switch (@typeInfo(Field)) { 2140 .pointer => |pointer_info| { 2141 if (pointer_info.size != .slice or pointer_info.child != u8 or !pointer_info.attrs.@"const") { 2142 @compileError("unsupported read field type: " ++ @typeName(Field)); 2143 } 2144 return try cursor.readBytesAlloc(arena.allocator(), null); 2145 }, 2146 .array => |array_info| { 2147 if (array_info.child != u8) @compileError("unsupported read field type: " ++ @typeName(Field)); 2148 var bytes: Field = undefined; 2149 if ((try cursor.readBytes(&bytes)).len != bytes.len) return error.InvalidByteArrayLength; 2150 return bytes; 2151 }, 2152 .bool => { 2153 var buffer: [5]u8 = undefined; 2154 const bytes = try cursor.readBytesObject(&buffer); 2155 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 2156 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 2157 if (std.mem.eql(u8, bytes.value, "true")) return true; 2158 if (std.mem.eql(u8, bytes.value, "false")) return false; 2159 return error.InvalidBool; 2160 }, 2161 .int => |int_info| return switch (int_info.signedness) { 2162 .unsigned => @intCast(try cursor.readUint()), 2163 .signed => @intCast(try cursor.readInt()), 2164 }, 2165 .@"enum" => return std.meta.stringToEnum(Field, try cursor.readBytesAlloc(arena.allocator(), null)) orelse error.InvalidEnumTag, 2166 .@"union" => |union_info| { 2167 if (union_info.tag_type == null) @compileError("unsupported read field type: " ++ @typeName(Field)); 2168 switch (cursor.slot().tag) { 2169 .bytes, .short_bytes => { 2170 const name = try cursor.readBytesAlloc(arena.allocator(), null); 2171 inline for (union_info.field_names, union_info.field_types) |variant_name, variant_type| { 2172 if (variant_type == void and std.mem.eql(u8, name, variant_name)) { 2173 return @unionInit(Field, variant_name, {}); 2174 } 2175 } 2176 return error.InvalidUnion; 2177 }, 2178 .hash_map => { 2179 const variants = try DB.HashMap(.read_only).init(cursor); 2180 var iter = try variants.iterator(); 2181 const entry = (try iter.next()) orelse return error.InvalidUnion; 2182 const pair = try entry.readKeyValuePair(); 2183 if (try iter.next() != null) return error.InvalidUnion; 2184 inline for (union_info.field_names, union_info.field_types) |variant_name, variant_type| { 2185 if (variant_type != void and pair.hash == hash.hashInt(hash_kind, variant_name)) { 2186 return @unionInit(Field, variant_name, try readField(variant_type, DB, hash_kind, arena, pair.value_cursor)); 2187 } 2188 } 2189 return error.InvalidUnion; 2190 }, 2191 else => return error.InvalidUnion, 2192 } 2193 }, 2194 .@"struct" => return try read(Field, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)), 2195 else => @compileError("unsupported read field type: " ++ @typeName(Field)), 2196 } 2197 } 2198 2199 // whether two events carry the same data 2200 fn fieldsEqual(comptime T: type, a: T, b: T) bool { 2201 switch (@typeInfo(T)) { 2202 .@"struct" => |struct_info| { 2203 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2204 if (!fieldEqual(field_type, @field(a, field_name), @field(b, field_name))) return false; 2205 } 2206 }, 2207 else => @compileError("fieldsEqual expects a struct"), 2208 } 2209 2210 return true; 2211 } 2212 2213 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 2214 switch (@typeInfo(Field)) { 2215 .pointer => |pointer_info| { 2216 if (pointer_info.size == .slice and pointer_info.child == u8) { 2217 return std.mem.eql(u8, a, b); 2218 } else { 2219 @compileError("unsupported field type: " ++ @typeName(Field)); 2220 } 2221 }, 2222 .array => |array_info| { 2223 if (array_info.child == u8) { 2224 return std.mem.eql(u8, &a, &b); 2225 } else { 2226 @compileError("unsupported field type: " ++ @typeName(Field)); 2227 } 2228 }, 2229 .void => return true, 2230 .bool, .int, .@"enum" => return a == b, 2231 .@"union" => { 2232 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 2233 return switch (a) { 2234 inline else => |value, tag| fieldEqual(@TypeOf(value), value, @field(b, @tagName(tag))), 2235 }; 2236 }, 2237 .@"struct" => return fieldsEqual(Field, a, b), 2238 .optional => |optional_info| { 2239 if (a) |a_value| { 2240 if (b) |b_value| return fieldEqual(optional_info.child, a_value, b_value); 2241 return false; 2242 } 2243 return b == null; 2244 }, 2245 else => @compileError("unsupported field type: " ++ @typeName(Field)), 2246 } 2247 } 2248 2249 // 2250 // writing to xitdb 2251 // 2252 2253 pub fn upsert( 2254 comptime T: type, 2255 comptime DB: type, 2256 comptime hash_kind: hash.HashKind, 2257 map: DB.HashMap(.read_write), 2258 event: T, 2259 ) !void { 2260 switch (@typeInfo(T)) { 2261 .@"struct" => |struct_info| { 2262 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2263 try upsertField(DB, hash_kind, map, field_name, field_type, @field(event, field_name)); 2264 } 2265 }, 2266 else => @compileError("upsert expects a struct"), 2267 } 2268 } 2269 2270 fn upsertField( 2271 comptime DB: type, 2272 comptime hash_kind: hash.HashKind, 2273 map: DB.HashMap(.read_write), 2274 comptime field_name: []const u8, 2275 comptime Field: type, 2276 value: Field, 2277 ) !void { 2278 const key = hash.hashInt(hash_kind, field_name); 2279 2280 switch (@typeInfo(Field)) { 2281 .pointer => |pointer_info| { 2282 if (pointer_info.size == .slice and pointer_info.child == u8) { 2283 try upsertBytes(DB, hash_kind, map, key, value); 2284 } else { 2285 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2286 } 2287 }, 2288 .array => |array_info| { 2289 if (array_info.child == u8) { 2290 try upsertBytes(DB, hash_kind, map, key, &value); 2291 } else { 2292 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2293 } 2294 }, 2295 .int => |int_info| switch (int_info.signedness) { 2296 .unsigned => { 2297 if (try map.getCursor(key)) |value_cursor| { 2298 if (try value_cursor.readUint() == value) { 2299 return; 2300 } 2301 } 2302 2303 try map.put(key, .{ .uint = value }); 2304 }, 2305 .signed => { 2306 if (try map.getCursor(key)) |value_cursor| { 2307 if (try value_cursor.readInt() == value) { 2308 return; 2309 } 2310 } 2311 2312 try map.put(key, .{ .int = value }); 2313 }, 2314 }, 2315 .bool => { 2316 const bytes = if (value) "true" else "false"; 2317 if (try map.getCursor(key)) |value_cursor| { 2318 var buffer: [5]u8 = undefined; 2319 const existing = try value_cursor.readBytesObject(&buffer); 2320 if (existing.format_tag) |format_tag| { 2321 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 2322 return; 2323 } 2324 } 2325 } 2326 2327 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 2328 }, 2329 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 2330 .@"union" => switch (value) { 2331 inline else => |payload, tag| { 2332 if (@TypeOf(payload) == void) { 2333 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 2334 } else { 2335 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 2336 if (try map.getCursor(key)) |cursor| { 2337 switch (cursor.slot().tag) { 2338 .hash_map => { 2339 const variants = try DB.HashMap(.read_only).init(cursor); 2340 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 2341 }, 2342 else => try map.put(key, .{ .slot = null }), 2343 } 2344 } 2345 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 2346 if (@typeInfo(@TypeOf(payload)) == .optional) { 2347 // keep the variant even when its payload is null 2348 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 2349 } 2350 if (try variants.getCursor(tag_key)) |cursor| { 2351 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 2352 } 2353 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 2354 } 2355 }, 2356 }, 2357 .optional => |optional_info| { 2358 // a missing key is null 2359 if (value) |child| { 2360 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 2361 } else { 2362 _ = try map.remove(key); 2363 } 2364 }, 2365 .@"struct" => try upsert( 2366 Field, 2367 DB, 2368 hash_kind, 2369 try DB.HashMap(.read_write).init(try map.putCursor(key)), 2370 value, 2371 ), 2372 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 2373 } 2374 } 2375 2376 fn upsertBytes( 2377 comptime DB: type, 2378 comptime hash_kind: hash.HashKind, 2379 map: DB.HashMap(.read_write), 2380 key: hash.HashInt(hash_kind), 2381 value: []const u8, 2382 ) !void { 2383 var existing_cursor_maybe = try map.getCursor(key); 2384 if (existing_cursor_maybe) |*existing_cursor| { 2385 if (try bytesEqual(DB, existing_cursor, value)) { 2386 return; 2387 } 2388 } 2389 2390 try map.put(key, .{ .bytes = value }); 2391 } 2392 2393 fn bytesEqual( 2394 comptime DB: type, 2395 cursor: *DB.Cursor(.read_only), 2396 value: []const u8, 2397 ) !bool { 2398 switch (cursor.slot().tag) { 2399 .bytes, .short_bytes => {}, 2400 else => return false, 2401 } 2402 var read_buffer: [1024]u8 = undefined; 2403 var reader = try cursor.reader(&read_buffer); 2404 if (reader.size != value.len) { 2405 return false; 2406 } 2407 2408 var chunk_buffer: [1024]u8 = undefined; 2409 var offset: usize = 0; 2410 while (offset < value.len) { 2411 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2412 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2413 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2414 return false; 2415 } 2416 offset += chunk_len; 2417 } 2418 2419 return true; 2420 } 2421 2422 // 2423 // CommitIterator 2424 // 2425 // differs from xit's ObjectIterator in a few ways: 2426 // 2427 // 1. iterates parents first, rather than children first 2428 // 2. `next` only returns the object id, not the full Object 2429 // 3. stores its temporary state in a file on disk to avoid OOMs 2430 // 2431 2432 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2433 return struct { 2434 const DB = EventDB(repo_opts.hash); 2435 // a plain file is used for the scratch db because each write outside 2436 // a transaction is synced anyway, so buffering would gain nothing 2437 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2438 const db_name = "haxy-repo-events.db"; 2439 2440 repo_dir: std.Io.Dir, 2441 db_file: std.Io.File, 2442 db: *TempDB, 2443 // map of each object id to its children 2444 parent_to_children: TempDB.HashMap(.read_write), 2445 // tracking pending parents is necessary because a child can only 2446 // be returned after all of its unconsumed parents have been returned 2447 child_to_pending_parents: TempDB.HashMap(.read_write), 2448 // queue of object ids that are ready to be returned from `next` 2449 ready_queue: TempDB.ArrayList(.read_write), 2450 ready_queue_index: u64, 2451 // the object id at the tip of this branch 2452 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2453 // the object id furthest back in the history that hasn't been consumed 2454 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2455 2456 pub fn init( 2457 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2458 io: std.Io, 2459 allocator: std.mem.Allocator, 2460 consumed_object_ids: DB.HashMap(.read_only), 2461 ref: rf.Ref, 2462 ) !CommitIterator(repo_kind, repo_opts) { 2463 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2464 errdefer { 2465 db_file.close(io); 2466 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2467 } 2468 2469 // cursors point at the db, so it needs a stable address 2470 const db_ptr = try allocator.create(TempDB); 2471 errdefer allocator.destroy(db_ptr); 2472 // the db is scratch state that gets deleted after iteration, so 2473 // there is nothing worth fsyncing 2474 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2475 2476 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2477 2478 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2479 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2480 2481 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2482 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2483 2484 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2485 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2486 2487 var self = CommitIterator(repo_kind, repo_opts){ 2488 .repo_dir = state.core.repo_dir, 2489 .db_file = db_file, 2490 .db = db_ptr, 2491 .parent_to_children = parent_to_children, 2492 .child_to_pending_parents = child_to_pending_parents, 2493 .ready_queue = ready_queue, 2494 .ready_queue_index = 0, 2495 .newest_object_id = null, 2496 .oldest_object_id = null, 2497 }; 2498 errdefer self.deinit(io, allocator); 2499 2500 try self.collect(state, io, allocator, consumed_object_ids, ref); 2501 2502 return self; 2503 } 2504 2505 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2506 self.db_file.close(io); 2507 self.repo_dir.deleteFile(io, db_name) catch {}; 2508 allocator.destroy(self.db); 2509 } 2510 2511 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2512 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2513 2514 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2515 self.ready_queue_index += 1; 2516 2517 try self.enqueueChildren(&oid); 2518 return oid; 2519 } 2520 2521 fn collect( 2522 self: *CommitIterator(repo_kind, repo_opts), 2523 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2524 io: std.Io, 2525 allocator: std.mem.Allocator, 2526 consumed_object_ids: DB.HashMap(.read_only), 2527 ref: rf.Ref, 2528 ) !void { 2529 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2530 2531 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2532 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2533 var walk_queue_index: u64 = 0; 2534 2535 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2536 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2537 2538 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2539 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2540 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2541 try walk_queue.append(.{ .bytes = &head_oid }); 2542 2543 while (walk_queue_index < try walk_queue.count()) { 2544 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2545 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2546 2547 walk_queue_index += 1; 2548 2549 // if we've seen this object id, skip it 2550 if (null != try seen_object_ids.getCursor(oid_int)) { 2551 continue; 2552 } 2553 2554 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2555 2556 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2557 defer commit_object.deinit(); 2558 2559 // if this object id has already been consumed, skip it 2560 if (null != try consumed_object_ids.getCursor(oid_int)) { 2561 continue; 2562 } 2563 2564 if (self.newest_object_id == null) self.newest_object_id = oid; 2565 self.oldest_object_id = oid; 2566 2567 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2568 var pending_parent_count: u64 = 0; 2569 for (parent_oids) |*parent_oid| { 2570 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2571 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2572 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2573 2574 // if this object id has already been consumed, skip it 2575 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2576 continue; 2577 } 2578 2579 // if this object id hasn't already been seen, add it to the walk queue 2580 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2581 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2582 } 2583 2584 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2585 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2586 try children.put(oid_int, .{ .bytes = &oid }); 2587 pending_parent_count += 1; 2588 } 2589 2590 if (pending_parent_count == 0) { 2591 try self.ready_queue.append(.{ .bytes = &oid }); 2592 } else { 2593 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2594 } 2595 } 2596 } 2597 2598 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2599 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2600 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2601 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2602 var children_iter = try children.iterator(); 2603 2604 while (try children_iter.next()) |*child_cursor| { 2605 const kv_pair = try child_cursor.readKeyValuePair(); 2606 const child_oid_int = kv_pair.hash; 2607 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2608 const pending_parent_count = try pending_parent_cursor.readUint(); 2609 2610 if (pending_parent_count <= 1) { 2611 _ = try self.child_to_pending_parents.remove(child_oid_int); 2612 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2613 try self.ready_queue.append(.{ .bytes = &child_oid }); 2614 } else { 2615 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2616 } 2617 } 2618 } 2619 2620 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2621 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2622 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2623 _ = try oid_cursor.readBytes(&oid); 2624 return oid; 2625 } 2626 }; 2627 } 2628