╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2221 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 2222 } 2223 }, 2224 }, 2225 .optional => |optional_info| { 2226 // a missing key is null 2227 if (value) |child| { 2228 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 2229 } else { 2230 _ = try map.remove(key); 2231 } 2232 }, 2233 .@"struct" => try upsert( 2234 Field, 2235 DB, 2236 hash_kind, 2237 try DB.HashMap(.read_write).init(try map.putCursor(key)), 2238 value, 2239 ), 2240 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 2241 } 2242 } 2243 2244 fn upsertBytes( 2245 comptime DB: type, 2246 comptime hash_kind: hash.HashKind, 2247 map: DB.HashMap(.read_write), 2248 key: hash.HashInt(hash_kind), 2249 value: []const u8, 2250 ) !void { 2251 var existing_cursor_maybe = try map.getCursor(key); 2252 if (existing_cursor_maybe) |*existing_cursor| { 2253 if (try bytesEqual(DB, existing_cursor, value)) { 2254 return; 2255 } 2256 } 2257 2258 try map.put(key, .{ .bytes = value }); 2259 } 2260 2261 fn bytesEqual( 2262 comptime DB: type, 2263 cursor: *DB.Cursor(.read_only), 2264 value: []const u8, 2265 ) !bool { 2266 switch (cursor.slot().tag) { 2267 .bytes, .short_bytes => {}, 2268 else => return false, 2269 } 2270 var read_buffer: [1024]u8 = undefined; 2271 var reader = try cursor.reader(&read_buffer); 2272 if (reader.size != value.len) { 2273 return false; 2274 } 2275 2276 var chunk_buffer: [1024]u8 = undefined; 2277 var offset: usize = 0; 2278 while (offset < value.len) { 2279 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2280 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2281 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2282 return false; 2283 } 2284 offset += chunk_len; 2285 } 2286 2287 return true; 2288 } 2289 2290 // 2291 // CommitIterator 2292 // 2293 // differs from xit's ObjectIterator in a few ways: 2294 // 2295 // 1. iterates parents first, rather than children first 2296 // 2. `next` only returns the object id, not the full Object 2297 // 3. stores its temporary state in a file on disk to avoid OOMs 2298 // 2299 2300 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2301 return struct { 2302 const DB = EventDB(repo_opts.hash); 2303 // a plain file is used for the scratch db because each write outside 2304 // a transaction is synced anyway, so buffering would gain nothing 2305 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2306 const db_name = "haxy-repo-events.db"; 2307 2308 repo_dir: std.Io.Dir, 2309 db_file: std.Io.File, 2310 db: *TempDB, 2311 // map of each object id to its children 2312 parent_to_children: TempDB.HashMap(.read_write), 2313 // tracking pending parents is necessary because a child can only 2314 // be returned after all of its unconsumed parents have been returned 2315 child_to_pending_parents: TempDB.HashMap(.read_write), 2316 // queue of object ids that are ready to be returned from `next` 2317 ready_queue: TempDB.ArrayList(.read_write), 2318 ready_queue_index: u64, 2319 // the object id at the tip of this branch 2320 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2321 // the object id furthest back in the history that hasn't been consumed 2322 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2323 2324 pub fn init( 2325 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2326 io: std.Io, 2327 allocator: std.mem.Allocator, 2328 consumed_object_ids: DB.HashMap(.read_only), 2329 ref: rf.Ref, 2330 ) !CommitIterator(repo_kind, repo_opts) { 2331 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2332 errdefer { 2333 db_file.close(io); 2334 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2335 } 2336 2337 // cursors point at the db, so it needs a stable address 2338 const db_ptr = try allocator.create(TempDB); 2339 errdefer allocator.destroy(db_ptr); 2340 // the db is scratch state that gets deleted after iteration, so 2341 // there is nothing worth fsyncing 2342 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2343 2344 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2345 2346 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2347 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2348 2349 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2350 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2351 2352 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2353 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2354 2355 var self = CommitIterator(repo_kind, repo_opts){ 2356 .repo_dir = state.core.repo_dir, 2357 .db_file = db_file, 2358 .db = db_ptr, 2359 .parent_to_children = parent_to_children, 2360 .child_to_pending_parents = child_to_pending_parents, 2361 .ready_queue = ready_queue, 2362 .ready_queue_index = 0, 2363 .newest_object_id = null, 2364 .oldest_object_id = null, 2365 }; 2366 errdefer self.deinit(io, allocator); 2367 2368 try self.collect(state, io, allocator, consumed_object_ids, ref); 2369 2370 return self; 2371 } 2372 2373 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2374 self.db_file.close(io); 2375 self.repo_dir.deleteFile(io, db_name) catch {}; 2376 allocator.destroy(self.db); 2377 } 2378 2379 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2380 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2381 2382 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2383 self.ready_queue_index += 1; 2384 2385 try self.enqueueChildren(&oid); 2386 return oid; 2387 } 2388 2389 fn collect( 2390 self: *CommitIterator(repo_kind, repo_opts), 2391 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2392 io: std.Io, 2393 allocator: std.mem.Allocator, 2394 consumed_object_ids: DB.HashMap(.read_only), 2395 ref: rf.Ref, 2396 ) !void { 2397 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2398 2399 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2400 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2401 var walk_queue_index: u64 = 0; 2402 2403 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2404 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2405 2406 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2407 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2408 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2409 try walk_queue.append(.{ .bytes = &head_oid }); 2410 2411 while (walk_queue_index < try walk_queue.count()) { 2412 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2413 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2414 2415 walk_queue_index += 1; 2416 2417 // if we've seen this object id, skip it 2418 if (null != try seen_object_ids.getCursor(oid_int)) { 2419 continue; 2420 } 2421 2422 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2423 2424 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2425 defer commit_object.deinit(); 2426 2427 // if this object id has already been consumed, skip it 2428 if (null != try consumed_object_ids.getCursor(oid_int)) { 2429 continue; 2430 } 2431 2432 if (self.newest_object_id == null) self.newest_object_id = oid; 2433 self.oldest_object_id = oid; 2434 2435 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2436 var pending_parent_count: u64 = 0; 2437 for (parent_oids) |*parent_oid| { 2438 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2439 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2440 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2441 2442 // if this object id has already been consumed, skip it 2443 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2444 continue; 2445 } 2446 2447 // if this object id hasn't already been seen, add it to the walk queue 2448 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2449 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2450 } 2451 2452 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2453 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2454 try children.put(oid_int, .{ .bytes = &oid }); 2455 pending_parent_count += 1; 2456 } 2457 2458 if (pending_parent_count == 0) { 2459 try self.ready_queue.append(.{ .bytes = &oid }); 2460 } else { 2461 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2462 } 2463 } 2464 } 2465 2466 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2467 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2468 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2469 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2470 var children_iter = try children.iterator(); 2471 2472 while (try children_iter.next()) |*child_cursor| { 2473 const kv_pair = try child_cursor.readKeyValuePair(); 2474 const child_oid_int = kv_pair.hash; 2475 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2476 const pending_parent_count = try pending_parent_cursor.readUint(); 2477 2478 if (pending_parent_count <= 1) { 2479 _ = try self.child_to_pending_parents.remove(child_oid_int); 2480 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2481 try self.ready_queue.append(.{ .bytes = &child_oid }); 2482 } else { 2483 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2484 } 2485 } 2486 } 2487 2488 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2489 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2490 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2491 _ = try oid_cursor.readBytes(&oid); 2492 return oid; 2493 } 2494 }; 2495 } 2496