╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2637 value: Field, 2638 ) !void { 2639 const key = hash.hashInt(hash_kind, field_name); 2640 2641 switch (@typeInfo(Field)) { 2642 .pointer => |pointer_info| { 2643 if (pointer_info.size == .slice and pointer_info.child == u8) { 2644 try upsertBytes(DB, hash_kind, map, key, value); 2645 } else { 2646 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2647 } 2648 }, 2649 .array => |array_info| { 2650 if (array_info.child == u8) { 2651 try upsertBytes(DB, hash_kind, map, key, &value); 2652 } else { 2653 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2654 } 2655 }, 2656 .int => |int_info| switch (int_info.signedness) { 2657 .unsigned => { 2658 if (try map.getCursor(key)) |value_cursor| { 2659 if (try value_cursor.readUint() == value) { 2660 return; 2661 } 2662 } 2663 2664 try map.put(key, .{ .uint = value }); 2665 }, 2666 .signed => { 2667 if (try map.getCursor(key)) |value_cursor| { 2668 if (try value_cursor.readInt() == value) { 2669 return; 2670 } 2671 } 2672 2673 try map.put(key, .{ .int = value }); 2674 }, 2675 }, 2676 .bool => { 2677 const bytes = if (value) "true" else "false"; 2678 if (try map.getCursor(key)) |value_cursor| { 2679 var buffer: [5]u8 = undefined; 2680 const existing = try value_cursor.readBytesObject(&buffer); 2681 if (existing.format_tag) |format_tag| { 2682 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 2683 return; 2684 } 2685 } 2686 } 2687 2688 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 2689 }, 2690 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 2691 .@"union" => switch (value) { 2692 inline else => |payload, tag| { 2693 if (@TypeOf(payload) == void) { 2694 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 2695 } else { 2696 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 2697 if (try map.getCursor(key)) |cursor| { 2698 switch (cursor.slot().tag) { 2699 .hash_map => { 2700 const variants = try DB.HashMap(.read_only).init(cursor); 2701 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 2702 }, 2703 else => try map.put(key, .{ .slot = null }), 2704 } 2705 } 2706 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 2707 if (@typeInfo(@TypeOf(payload)) == .optional) { 2708 // keep the variant even when its payload is null 2709 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 2710 } 2711 if (try variants.getCursor(tag_key)) |cursor| { 2712 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 2713 } 2714 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 2715 } 2716 }, 2717 }, 2718 .optional => |optional_info| { 2719 // a missing key is null 2720 if (value) |child| { 2721 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 2722 } else { 2723 _ = try map.remove(key); 2724 } 2725 }, 2726 .@"struct" => try upsert( 2727 Field, 2728 DB, 2729 hash_kind, 2730 try DB.HashMap(.read_write).init(try map.putCursor(key)), 2731 value, 2732 ), 2733 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 2734 } 2735 } 2736 2737 fn upsertBytes( 2738 comptime DB: type, 2739 comptime hash_kind: hash.HashKind, 2740 map: DB.HashMap(.read_write), 2741 key: hash.HashInt(hash_kind), 2742 value: []const u8, 2743 ) !void { 2744 var existing_cursor_maybe = try map.getCursor(key); 2745 if (existing_cursor_maybe) |*existing_cursor| { 2746 if (try bytesEqual(DB, existing_cursor, value)) { 2747 return; 2748 } 2749 } 2750 2751 try map.put(key, .{ .bytes = value }); 2752 } 2753 2754 fn bytesEqual( 2755 comptime DB: type, 2756 cursor: *DB.Cursor(.read_only), 2757 value: []const u8, 2758 ) !bool { 2759 switch (cursor.slot().tag) { 2760 .bytes, .short_bytes => {}, 2761 else => return false, 2762 } 2763 var read_buffer: [1024]u8 = undefined; 2764 var reader = try cursor.reader(&read_buffer); 2765 if (reader.size != value.len) { 2766 return false; 2767 } 2768 2769 var chunk_buffer: [1024]u8 = undefined; 2770 var offset: usize = 0; 2771 while (offset < value.len) { 2772 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2773 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2774 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2775 return false; 2776 } 2777 offset += chunk_len; 2778 } 2779 2780 return true; 2781 } 2782 2783 // 2784 // CommitIterator 2785 // 2786 // differs from xit's ObjectIterator in a few ways: 2787 // 2788 // 1. iterates parents first, rather than children first 2789 // 2. `next` only returns the object id, not the full Object 2790 // 3. stores its temporary state in a file on disk to avoid OOMs 2791 // 2792 2793 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2794 return struct { 2795 const DB = EventDB(repo_opts.hash); 2796 // a plain file is used for the scratch db because each write outside 2797 // a transaction is synced anyway, so buffering would gain nothing 2798 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2799 const db_name = "haxy-repo-events.db"; 2800 2801 repo_dir: std.Io.Dir, 2802 db_file: std.Io.File, 2803 db: *TempDB, 2804 // map of each object id to its children 2805 parent_to_children: TempDB.HashMap(.read_write), 2806 // tracking pending parents is necessary because a child can only 2807 // be returned after all of its unconsumed parents have been returned 2808 child_to_pending_parents: TempDB.HashMap(.read_write), 2809 // queue of object ids that are ready to be returned from `next` 2810 ready_queue: TempDB.ArrayList(.read_write), 2811 ready_queue_index: u64, 2812 // the object id at the tip of this branch 2813 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2814 // the object id furthest back in the history that hasn't been consumed 2815 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2816 2817 pub fn init( 2818 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2819 io: std.Io, 2820 allocator: std.mem.Allocator, 2821 consumed_object_ids: DB.HashMap(.read_only), 2822 ref: rf.Ref, 2823 ) !CommitIterator(repo_kind, repo_opts) { 2824 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2825 errdefer { 2826 db_file.close(io); 2827 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2828 } 2829 2830 // cursors point at the db, so it needs a stable address 2831 const db_ptr = try allocator.create(TempDB); 2832 errdefer allocator.destroy(db_ptr); 2833 // the db is scratch state that gets deleted after iteration, so 2834 // there is nothing worth fsyncing 2835 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2836 2837 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2838 2839 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2840 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2841 2842 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2843 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2844 2845 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2846 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2847 2848 var self = CommitIterator(repo_kind, repo_opts){ 2849 .repo_dir = state.core.repo_dir, 2850 .db_file = db_file, 2851 .db = db_ptr, 2852 .parent_to_children = parent_to_children, 2853 .child_to_pending_parents = child_to_pending_parents, 2854 .ready_queue = ready_queue, 2855 .ready_queue_index = 0, 2856 .newest_object_id = null, 2857 .oldest_object_id = null, 2858 }; 2859 errdefer self.deinit(io, allocator); 2860 2861 try self.collect(state, io, allocator, consumed_object_ids, ref); 2862 2863 return self; 2864 } 2865 2866 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2867 self.db_file.close(io); 2868 self.repo_dir.deleteFile(io, db_name) catch {}; 2869 allocator.destroy(self.db); 2870 } 2871 2872 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2873 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2874 2875 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2876 self.ready_queue_index += 1; 2877 2878 try self.enqueueChildren(&oid); 2879 return oid; 2880 } 2881 2882 fn collect( 2883 self: *CommitIterator(repo_kind, repo_opts), 2884 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2885 io: std.Io, 2886 allocator: std.mem.Allocator, 2887 consumed_object_ids: DB.HashMap(.read_only), 2888 ref: rf.Ref, 2889 ) !void { 2890 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2891 2892 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2893 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2894 var walk_queue_index: u64 = 0; 2895 2896 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2897 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2898 2899 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2900 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2901 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2902 try walk_queue.append(.{ .bytes = &head_oid }); 2903 2904 while (walk_queue_index < try walk_queue.count()) { 2905 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2906 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2907 2908 walk_queue_index += 1; 2909 2910 // if we've seen this object id, skip it 2911 if (null != try seen_object_ids.getCursor(oid_int)) { 2912 continue; 2913 } 2914 2915 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2916 2917 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2918 defer commit_object.deinit(); 2919 2920 // if this object id has already been consumed, skip it 2921 if (null != try consumed_object_ids.getCursor(oid_int)) { 2922 continue; 2923 } 2924 2925 if (self.newest_object_id == null) self.newest_object_id = oid; 2926 self.oldest_object_id = oid; 2927 2928 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2929 var pending_parent_count: u64 = 0; 2930 for (parent_oids) |*parent_oid| { 2931 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2932 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2933 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2934 2935 // if this object id has already been consumed, skip it 2936 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2937 continue; 2938 } 2939 2940 // if this object id hasn't already been seen, add it to the walk queue 2941 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2942 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2943 } 2944 2945 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2946 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2947 try children.put(oid_int, .{ .bytes = &oid }); 2948 pending_parent_count += 1; 2949 } 2950 2951 if (pending_parent_count == 0) { 2952 try self.ready_queue.append(.{ .bytes = &oid }); 2953 } else { 2954 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2955 } 2956 } 2957 } 2958 2959 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2960 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2961 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2962 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2963 var children_iter = try children.iterator(); 2964 2965 while (try children_iter.next()) |*child_cursor| { 2966 const kv_pair = try child_cursor.readKeyValuePair(); 2967 const child_oid_int = kv_pair.hash; 2968 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2969 const pending_parent_count = try pending_parent_cursor.readUint(); 2970 2971 if (pending_parent_count <= 1) { 2972 _ = try self.child_to_pending_parents.remove(child_oid_int); 2973 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2974 try self.ready_queue.append(.{ .bytes = &child_oid }); 2975 } else { 2976 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2977 } 2978 } 2979 } 2980 2981 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2982 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2983 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2984 _ = try oid_cursor.readBytes(&oid); 2985 return oid; 2986 } 2987 }; 2988 } 2989