╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
2522 } 2523 } 2524 2525 try map.put(key, .{ .uint = value }); 2526 }, 2527 .signed => { 2528 if (try map.getCursor(key)) |value_cursor| { 2529 if (try value_cursor.readInt() == value) { 2530 return; 2531 } 2532 } 2533 2534 try map.put(key, .{ .int = value }); 2535 }, 2536 }, 2537 .bool => { 2538 const bytes = if (value) "true" else "false"; 2539 if (try map.getCursor(key)) |value_cursor| { 2540 var buffer: [5]u8 = undefined; 2541 const existing = try value_cursor.readBytesObject(&buffer); 2542 if (existing.format_tag) |format_tag| { 2543 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 2544 return; 2545 } 2546 } 2547 } 2548 2549 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 2550 }, 2551 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 2552 .@"union" => switch (value) { 2553 inline else => |payload, tag| { 2554 if (@TypeOf(payload) == void) { 2555 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 2556 } else { 2557 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 2558 if (try map.getCursor(key)) |cursor| { 2559 switch (cursor.slot().tag) { 2560 .hash_map => { 2561 const variants = try DB.HashMap(.read_only).init(cursor); 2562 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 2563 }, 2564 else => try map.put(key, .{ .slot = null }), 2565 } 2566 } 2567 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 2568 if (@typeInfo(@TypeOf(payload)) == .optional) { 2569 // keep the variant even when its payload is null 2570 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 2571 } 2572 if (try variants.getCursor(tag_key)) |cursor| { 2573 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 2574 } 2575 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 2576 } 2577 }, 2578 }, 2579 .optional => |optional_info| { 2580 // a missing key is null 2581 if (value) |child| { 2582 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 2583 } else { 2584 _ = try map.remove(key); 2585 } 2586 }, 2587 .@"struct" => try upsert( 2588 Field, 2589 DB, 2590 hash_kind, 2591 try DB.HashMap(.read_write).init(try map.putCursor(key)), 2592 value, 2593 ), 2594 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 2595 } 2596 } 2597 2598 fn upsertBytes( 2599 comptime DB: type, 2600 comptime hash_kind: hash.HashKind, 2601 map: DB.HashMap(.read_write), 2602 key: hash.HashInt(hash_kind), 2603 value: []const u8, 2604 ) !void { 2605 var existing_cursor_maybe = try map.getCursor(key); 2606 if (existing_cursor_maybe) |*existing_cursor| { 2607 if (try bytesEqual(DB, existing_cursor, value)) { 2608 return; 2609 } 2610 } 2611 2612 try map.put(key, .{ .bytes = value }); 2613 } 2614 2615 fn bytesEqual( 2616 comptime DB: type, 2617 cursor: *DB.Cursor(.read_only), 2618 value: []const u8, 2619 ) !bool { 2620 switch (cursor.slot().tag) { 2621 .bytes, .short_bytes => {}, 2622 else => return false, 2623 } 2624 var read_buffer: [1024]u8 = undefined; 2625 var reader = try cursor.reader(&read_buffer); 2626 if (reader.size != value.len) { 2627 return false; 2628 } 2629 2630 var chunk_buffer: [1024]u8 = undefined; 2631 var offset: usize = 0; 2632 while (offset < value.len) { 2633 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2634 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2635 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2636 return false; 2637 } 2638 offset += chunk_len; 2639 } 2640 2641 return true; 2642 } 2643 2644 // 2645 // CommitIterator 2646 // 2647 // differs from xit's ObjectIterator in a few ways: 2648 // 2649 // 1. iterates parents first, rather than children first 2650 // 2. `next` only returns the object id, not the full Object 2651 // 3. stores its temporary state in a file on disk to avoid OOMs 2652 // 2653 2654 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2655 return struct { 2656 const DB = EventDB(repo_opts.hash); 2657 // a plain file is used for the scratch db because each write outside 2658 // a transaction is synced anyway, so buffering would gain nothing 2659 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2660 const db_name = "haxy-repo-events.db"; 2661 2662 repo_dir: std.Io.Dir, 2663 db_file: std.Io.File, 2664 db: *TempDB, 2665 // map of each object id to its children 2666 parent_to_children: TempDB.HashMap(.read_write), 2667 // tracking pending parents is necessary because a child can only 2668 // be returned after all of its unconsumed parents have been returned 2669 child_to_pending_parents: TempDB.HashMap(.read_write), 2670 // queue of object ids that are ready to be returned from `next` 2671 ready_queue: TempDB.ArrayList(.read_write), 2672 ready_queue_index: u64, 2673 // the object id at the tip of this branch 2674 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2675 // the object id furthest back in the history that hasn't been consumed 2676 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2677 2678 pub fn init( 2679 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2680 io: std.Io, 2681 allocator: std.mem.Allocator, 2682 consumed_object_ids: DB.HashMap(.read_only), 2683 ref: rf.Ref, 2684 ) !CommitIterator(repo_kind, repo_opts) { 2685 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2686 errdefer { 2687 db_file.close(io); 2688 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2689 } 2690 2691 // cursors point at the db, so it needs a stable address 2692 const db_ptr = try allocator.create(TempDB); 2693 errdefer allocator.destroy(db_ptr); 2694 // the db is scratch state that gets deleted after iteration, so 2695 // there is nothing worth fsyncing 2696 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2697 2698 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2699 2700 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2701 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2702 2703 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2704 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2705 2706 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2707 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2708 2709 var self = CommitIterator(repo_kind, repo_opts){ 2710 .repo_dir = state.core.repo_dir, 2711 .db_file = db_file, 2712 .db = db_ptr, 2713 .parent_to_children = parent_to_children, 2714 .child_to_pending_parents = child_to_pending_parents, 2715 .ready_queue = ready_queue, 2716 .ready_queue_index = 0, 2717 .newest_object_id = null, 2718 .oldest_object_id = null, 2719 }; 2720 errdefer self.deinit(io, allocator); 2721 2722 try self.collect(state, io, allocator, consumed_object_ids, ref); 2723 2724 return self; 2725 } 2726 2727 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2728 self.db_file.close(io); 2729 self.repo_dir.deleteFile(io, db_name) catch {}; 2730 allocator.destroy(self.db); 2731 } 2732 2733 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2734 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2735 2736 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2737 self.ready_queue_index += 1; 2738 2739 try self.enqueueChildren(&oid); 2740 return oid; 2741 } 2742 2743 fn collect( 2744 self: *CommitIterator(repo_kind, repo_opts), 2745 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2746 io: std.Io, 2747 allocator: std.mem.Allocator, 2748 consumed_object_ids: DB.HashMap(.read_only), 2749 ref: rf.Ref, 2750 ) !void { 2751 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2752 2753 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2754 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2755 var walk_queue_index: u64 = 0; 2756 2757 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2758 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2759 2760 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2761 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2762 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2763 try walk_queue.append(.{ .bytes = &head_oid }); 2764 2765 while (walk_queue_index < try walk_queue.count()) { 2766 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2767 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2768 2769 walk_queue_index += 1; 2770 2771 // if we've seen this object id, skip it 2772 if (null != try seen_object_ids.getCursor(oid_int)) { 2773 continue; 2774 } 2775 2776 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2777 2778 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2779 defer commit_object.deinit(); 2780 2781 // if this object id has already been consumed, skip it 2782 if (null != try consumed_object_ids.getCursor(oid_int)) { 2783 continue; 2784 } 2785 2786 if (self.newest_object_id == null) self.newest_object_id = oid; 2787 self.oldest_object_id = oid; 2788 2789 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2790 var pending_parent_count: u64 = 0; 2791 for (parent_oids) |*parent_oid| { 2792 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2793 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2794 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2795 2796 // if this object id has already been consumed, skip it 2797 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2798 continue; 2799 } 2800 2801 // if this object id hasn't already been seen, add it to the walk queue 2802 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2803 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2804 } 2805 2806 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2807 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2808 try children.put(oid_int, .{ .bytes = &oid }); 2809 pending_parent_count += 1; 2810 } 2811 2812 if (pending_parent_count == 0) { 2813 try self.ready_queue.append(.{ .bytes = &oid }); 2814 } else { 2815 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2816 } 2817 } 2818 } 2819 2820 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2821 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2822 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2823 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2824 var children_iter = try children.iterator(); 2825 2826 while (try children_iter.next()) |*child_cursor| { 2827 const kv_pair = try child_cursor.readKeyValuePair(); 2828 const child_oid_int = kv_pair.hash; 2829 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2830 const pending_parent_count = try pending_parent_cursor.readUint(); 2831 2832 if (pending_parent_count <= 1) { 2833 _ = try self.child_to_pending_parents.remove(child_oid_int); 2834 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2835 try self.ready_queue.append(.{ .bytes = &child_oid }); 2836 } else { 2837 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2838 } 2839 } 2840 } 2841 2842 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2843 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2844 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2845 _ = try oid_cursor.readBytes(&oid); 2846 return oid; 2847 } 2848 }; 2849 } 2850