╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2421 // 2422 // differs from xit's ObjectIterator in a few ways: 2423 // 2424 // 1. iterates parents first, rather than children first 2425 // 2. `next` only returns the object id, not the full Object 2426 // 3. stores its temporary state in a file on disk to avoid OOMs 2427 // 2428 2429 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2430 return struct { 2431 const DB = EventDB(repo_opts.hash); 2432 // a plain file is used for the scratch db because each write outside 2433 // a transaction is synced anyway, so buffering would gain nothing 2434 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2435 const db_name = "haxy-repo-events.db"; 2436 2437 repo_dir: std.Io.Dir, 2438 db_file: std.Io.File, 2439 db: *TempDB, 2440 // map of each object id to its children 2441 parent_to_children: TempDB.HashMap(.read_write), 2442 // tracking pending parents is necessary because a child can only 2443 // be returned after all of its unconsumed parents have been returned 2444 child_to_pending_parents: TempDB.HashMap(.read_write), 2445 // queue of object ids that are ready to be returned from `next` 2446 ready_queue: TempDB.ArrayList(.read_write), 2447 ready_queue_index: u64, 2448 // the object id at the tip of this branch 2449 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2450 // the object id furthest back in the history that hasn't been consumed 2451 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2452 2453 pub fn init( 2454 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2455 io: std.Io, 2456 allocator: std.mem.Allocator, 2457 consumed_object_ids: DB.HashMap(.read_only), 2458 ref: rf.Ref, 2459 ) !CommitIterator(repo_kind, repo_opts) { 2460 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2461 errdefer { 2462 db_file.close(io); 2463 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2464 } 2465 2466 // cursors point at the db, so it needs a stable address 2467 const db_ptr = try allocator.create(TempDB); 2468 errdefer allocator.destroy(db_ptr); 2469 // the db is scratch state that gets deleted after iteration, so 2470 // there is nothing worth fsyncing 2471 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2472 2473 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2474 2475 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2476 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2477 2478 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2479 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2480 2481 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2482 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2483 2484 var self = CommitIterator(repo_kind, repo_opts){ 2485 .repo_dir = state.core.repo_dir, 2486 .db_file = db_file, 2487 .db = db_ptr, 2488 .parent_to_children = parent_to_children, 2489 .child_to_pending_parents = child_to_pending_parents, 2490 .ready_queue = ready_queue, 2491 .ready_queue_index = 0, 2492 .newest_object_id = null, 2493 .oldest_object_id = null, 2494 }; 2495 errdefer self.deinit(io, allocator); 2496 2497 try self.collect(state, io, allocator, consumed_object_ids, ref); 2498 2499 return self; 2500 } 2501 2502 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2503 self.db_file.close(io); 2504 self.repo_dir.deleteFile(io, db_name) catch {}; 2505 allocator.destroy(self.db); 2506 } 2507 2508 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2509 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2510 2511 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2512 self.ready_queue_index += 1; 2513 2514 try self.enqueueChildren(&oid); 2515 return oid; 2516 } 2517 2518 fn collect( 2519 self: *CommitIterator(repo_kind, repo_opts), 2520 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2521 io: std.Io, 2522 allocator: std.mem.Allocator, 2523 consumed_object_ids: DB.HashMap(.read_only), 2524 ref: rf.Ref, 2525 ) !void { 2526 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2527 2528 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2529 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2530 var walk_queue_index: u64 = 0; 2531 2532 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2533 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2534 2535 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2536 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2537 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2538 try walk_queue.append(.{ .bytes = &head_oid }); 2539 2540 while (walk_queue_index < try walk_queue.count()) { 2541 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2542 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2543 2544 walk_queue_index += 1; 2545 2546 // if we've seen this object id, skip it 2547 if (null != try seen_object_ids.getCursor(oid_int)) { 2548 continue; 2549 } 2550 2551 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2552 2553 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2554 defer commit_object.deinit(); 2555 2556 // if this object id has already been consumed, skip it 2557 if (null != try consumed_object_ids.getCursor(oid_int)) { 2558 continue; 2559 } 2560 2561 if (self.newest_object_id == null) self.newest_object_id = oid; 2562 self.oldest_object_id = oid; 2563 2564 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2565 var pending_parent_count: u64 = 0; 2566 for (parent_oids) |*parent_oid| { 2567 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2568 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2569 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2570 2571 // if this object id has already been consumed, skip it 2572 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2573 continue; 2574 } 2575 2576 // if this object id hasn't already been seen, add it to the walk queue 2577 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2578 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2579 } 2580 2581 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2582 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2583 try children.put(oid_int, .{ .bytes = &oid }); 2584 pending_parent_count += 1; 2585 } 2586 2587 if (pending_parent_count == 0) { 2588 try self.ready_queue.append(.{ .bytes = &oid }); 2589 } else { 2590 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2591 } 2592 } 2593 } 2594 2595 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2596 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2597 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2598 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2599 var children_iter = try children.iterator(); 2600 2601 while (try children_iter.next()) |*child_cursor| { 2602 const kv_pair = try child_cursor.readKeyValuePair(); 2603 const child_oid_int = kv_pair.hash; 2604 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2605 const pending_parent_count = try pending_parent_cursor.readUint(); 2606 2607 if (pending_parent_count <= 1) { 2608 _ = try self.child_to_pending_parents.remove(child_oid_int); 2609 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2610 try self.ready_queue.append(.{ .bytes = &child_oid }); 2611 } else { 2612 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2613 } 2614 } 2615 } 2616 2617 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2618 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2619 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2620 _ = try oid_cursor.readBytes(&oid); 2621 return oid; 2622 } 2623 }; 2624 } 2625