╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2521 // a plain file is used for the scratch db because each write outside 2522 // a transaction is synced anyway, so buffering would gain nothing 2523 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2524 const db_name = "haxy-repo-events.db"; 2525 2526 repo_dir: std.Io.Dir, 2527 db_file: std.Io.File, 2528 db: *TempDB, 2529 // map of each object id to its children 2530 parent_to_children: TempDB.HashMap(.read_write), 2531 // tracking pending parents is necessary because a child can only 2532 // be returned after all of its unconsumed parents have been returned 2533 child_to_pending_parents: TempDB.HashMap(.read_write), 2534 // queue of object ids that are ready to be returned from `next` 2535 ready_queue: TempDB.ArrayList(.read_write), 2536 ready_queue_index: u64, 2537 // the object id at the tip of this branch 2538 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2539 // the object id furthest back in the history that hasn't been consumed 2540 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2541 2542 pub fn init( 2543 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2544 io: std.Io, 2545 allocator: std.mem.Allocator, 2546 consumed_object_ids: DB.HashMap(.read_only), 2547 ref: rf.Ref, 2548 ) !CommitIterator(repo_kind, repo_opts) { 2549 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2550 errdefer { 2551 db_file.close(io); 2552 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2553 } 2554 2555 // cursors point at the db, so it needs a stable address 2556 const db_ptr = try allocator.create(TempDB); 2557 errdefer allocator.destroy(db_ptr); 2558 // the db is scratch state that gets deleted after iteration, so 2559 // there is nothing worth fsyncing 2560 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2561 2562 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2563 2564 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2565 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2566 2567 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2568 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2569 2570 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2571 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2572 2573 var self = CommitIterator(repo_kind, repo_opts){ 2574 .repo_dir = state.core.repo_dir, 2575 .db_file = db_file, 2576 .db = db_ptr, 2577 .parent_to_children = parent_to_children, 2578 .child_to_pending_parents = child_to_pending_parents, 2579 .ready_queue = ready_queue, 2580 .ready_queue_index = 0, 2581 .newest_object_id = null, 2582 .oldest_object_id = null, 2583 }; 2584 errdefer self.deinit(io, allocator); 2585 2586 try self.collect(state, io, allocator, consumed_object_ids, ref); 2587 2588 return self; 2589 } 2590 2591 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2592 self.db_file.close(io); 2593 self.repo_dir.deleteFile(io, db_name) catch {}; 2594 allocator.destroy(self.db); 2595 } 2596 2597 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2598 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2599 2600 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2601 self.ready_queue_index += 1; 2602 2603 try self.enqueueChildren(&oid); 2604 return oid; 2605 } 2606 2607 fn collect( 2608 self: *CommitIterator(repo_kind, repo_opts), 2609 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2610 io: std.Io, 2611 allocator: std.mem.Allocator, 2612 consumed_object_ids: DB.HashMap(.read_only), 2613 ref: rf.Ref, 2614 ) !void { 2615 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2616 2617 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2618 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2619 var walk_queue_index: u64 = 0; 2620 2621 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2622 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2623 2624 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2625 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2626 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2627 try walk_queue.append(.{ .bytes = &head_oid }); 2628 2629 while (walk_queue_index < try walk_queue.count()) { 2630 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2631 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2632 2633 walk_queue_index += 1; 2634 2635 // if we've seen this object id, skip it 2636 if (null != try seen_object_ids.getCursor(oid_int)) { 2637 continue; 2638 } 2639 2640 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2641 2642 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2643 defer commit_object.deinit(); 2644 2645 // if this object id has already been consumed, skip it 2646 if (null != try consumed_object_ids.getCursor(oid_int)) { 2647 continue; 2648 } 2649 2650 if (self.newest_object_id == null) self.newest_object_id = oid; 2651 self.oldest_object_id = oid; 2652 2653 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2654 var pending_parent_count: u64 = 0; 2655 for (parent_oids) |*parent_oid| { 2656 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2657 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2658 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2659 2660 // if this object id has already been consumed, skip it 2661 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2662 continue; 2663 } 2664 2665 // if this object id hasn't already been seen, add it to the walk queue 2666 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2667 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2668 } 2669 2670 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2671 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2672 try children.put(oid_int, .{ .bytes = &oid }); 2673 pending_parent_count += 1; 2674 } 2675 2676 if (pending_parent_count == 0) { 2677 try self.ready_queue.append(.{ .bytes = &oid }); 2678 } else { 2679 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2680 } 2681 } 2682 } 2683 2684 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2685 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2686 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2687 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2688 var children_iter = try children.iterator(); 2689 2690 while (try children_iter.next()) |*child_cursor| { 2691 const kv_pair = try child_cursor.readKeyValuePair(); 2692 const child_oid_int = kv_pair.hash; 2693 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2694 const pending_parent_count = try pending_parent_cursor.readUint(); 2695 2696 if (pending_parent_count <= 1) { 2697 _ = try self.child_to_pending_parents.remove(child_oid_int); 2698 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2699 try self.ready_queue.append(.{ .bytes = &child_oid }); 2700 } else { 2701 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2702 } 2703 } 2704 } 2705 2706 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2707 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2708 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2709 _ = try oid_cursor.readBytes(&oid); 2710 return oid; 2711 } 2712 }; 2713 } 2714