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