╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
2835 } 2836 }, 2837 }, 2838 .optional => |optional_info| { 2839 // a missing key is null 2840 if (value) |child| { 2841 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 2842 } else { 2843 _ = try map.remove(key); 2844 } 2845 }, 2846 .@"struct" => try upsert( 2847 Field, 2848 DB, 2849 hash_kind, 2850 try DB.HashMap(.read_write).init(try map.putCursor(key)), 2851 value, 2852 ), 2853 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 2854 } 2855 } 2856 2857 fn upsertBytes( 2858 comptime DB: type, 2859 comptime hash_kind: hash.HashKind, 2860 map: DB.HashMap(.read_write), 2861 key: hash.HashInt(hash_kind), 2862 value: []const u8, 2863 ) !void { 2864 var existing_cursor_maybe = try map.getCursor(key); 2865 if (existing_cursor_maybe) |*existing_cursor| { 2866 if (try bytesEqual(DB, existing_cursor, value)) { 2867 return; 2868 } 2869 } 2870 2871 try map.put(key, .{ .bytes = value }); 2872 } 2873 2874 fn bytesEqual( 2875 comptime DB: type, 2876 cursor: *DB.Cursor(.read_only), 2877 value: []const u8, 2878 ) !bool { 2879 switch (cursor.slot().tag) { 2880 .bytes, .short_bytes => {}, 2881 else => return false, 2882 } 2883 var read_buffer: [1024]u8 = undefined; 2884 var reader = try cursor.reader(&read_buffer); 2885 if (reader.size != value.len) { 2886 return false; 2887 } 2888 2889 var chunk_buffer: [1024]u8 = undefined; 2890 var offset: usize = 0; 2891 while (offset < value.len) { 2892 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2893 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2894 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2895 return false; 2896 } 2897 offset += chunk_len; 2898 } 2899 2900 return true; 2901 } 2902 2903 // 2904 // CommitIterator 2905 // 2906 // differs from xit's ObjectIterator in a few ways: 2907 // 2908 // 1. iterates parents first, rather than children first 2909 // 2. `next` only returns the object id, not the full Object 2910 // 3. stores its temporary state in a file on disk to avoid OOMs 2911 // 2912 2913 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2914 return struct { 2915 const DB = EventDB(repo_opts.hash); 2916 // a plain file is used for the scratch db because each write outside 2917 // a transaction is synced anyway, so buffering would gain nothing 2918 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2919 const db_name = "haxy-repo-events.db"; 2920 2921 repo_dir: std.Io.Dir, 2922 db_file: std.Io.File, 2923 db: *TempDB, 2924 // map of each object id to its children 2925 parent_to_children: TempDB.HashMap(.read_write), 2926 // tracking pending parents is necessary because a child can only 2927 // be returned after all of its unconsumed parents have been returned 2928 child_to_pending_parents: TempDB.HashMap(.read_write), 2929 // queue of object ids that are ready to be returned from `next` 2930 ready_queue: TempDB.ArrayList(.read_write), 2931 ready_queue_index: u64, 2932 // the object id at the tip of this branch 2933 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2934 // the object id furthest back in the history that hasn't been consumed 2935 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2936 2937 pub fn init( 2938 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2939 io: std.Io, 2940 allocator: std.mem.Allocator, 2941 consumed_object_ids: DB.HashMap(.read_only), 2942 ref: rf.Ref, 2943 ) !CommitIterator(repo_kind, repo_opts) { 2944 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2945 errdefer { 2946 db_file.close(io); 2947 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2948 } 2949 2950 // cursors point at the db, so it needs a stable address 2951 const db_ptr = try allocator.create(TempDB); 2952 errdefer allocator.destroy(db_ptr); 2953 // the db is scratch state that gets deleted after iteration, so 2954 // there is nothing worth fsyncing 2955 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2956 2957 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2958 2959 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2960 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2961 2962 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2963 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2964 2965 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2966 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2967 2968 var self = CommitIterator(repo_kind, repo_opts){ 2969 .repo_dir = state.core.repo_dir, 2970 .db_file = db_file, 2971 .db = db_ptr, 2972 .parent_to_children = parent_to_children, 2973 .child_to_pending_parents = child_to_pending_parents, 2974 .ready_queue = ready_queue, 2975 .ready_queue_index = 0, 2976 .newest_object_id = null, 2977 .oldest_object_id = null, 2978 }; 2979 errdefer self.deinit(io, allocator); 2980 2981 try self.collect(state, io, allocator, consumed_object_ids, ref); 2982 2983 return self; 2984 } 2985 2986 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2987 self.db_file.close(io); 2988 self.repo_dir.deleteFile(io, db_name) catch {}; 2989 allocator.destroy(self.db); 2990 } 2991 2992 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2993 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2994 2995 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2996 self.ready_queue_index += 1; 2997 2998 try self.enqueueChildren(&oid); 2999 return oid; 3000 } 3001 3002 fn collect( 3003 self: *CommitIterator(repo_kind, repo_opts), 3004 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 3005 io: std.Io, 3006 allocator: std.mem.Allocator, 3007 consumed_object_ids: DB.HashMap(.read_only), 3008 ref: rf.Ref, 3009 ) !void { 3010 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 3011 3012 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 3013 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 3014 var walk_queue_index: u64 = 0; 3015 3016 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 3017 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 3018 3019 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 3020 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 3021 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 3022 try walk_queue.append(.{ .bytes = &head_oid }); 3023 3024 while (walk_queue_index < try walk_queue.count()) { 3025 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 3026 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 3027 3028 walk_queue_index += 1; 3029 3030 // if we've seen this object id, skip it 3031 if (null != try seen_object_ids.getCursor(oid_int)) { 3032 continue; 3033 } 3034 3035 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 3036 3037 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 3038 defer commit_object.deinit(); 3039 3040 // if this object id has already been consumed, skip it 3041 if (null != try consumed_object_ids.getCursor(oid_int)) { 3042 continue; 3043 } 3044 3045 if (self.newest_object_id == null) self.newest_object_id = oid; 3046 self.oldest_object_id = oid; 3047 3048 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 3049 var pending_parent_count: u64 = 0; 3050 for (parent_oids) |*parent_oid| { 3051 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 3052 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 3053 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 3054 3055 // if this object id has already been consumed, skip it 3056 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 3057 continue; 3058 } 3059 3060 // if this object id hasn't already been seen, add it to the walk queue 3061 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 3062 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 3063 } 3064 3065 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 3066 const children = try TempDB.HashSet(.read_write).init(children_cursor); 3067 try children.put(oid_int, .{ .bytes = &oid }); 3068 pending_parent_count += 1; 3069 } 3070 3071 if (pending_parent_count == 0) { 3072 try self.ready_queue.append(.{ .bytes = &oid }); 3073 } else { 3074 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 3075 } 3076 } 3077 } 3078 3079 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 3080 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 3081 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 3082 const children = try TempDB.HashSet(.read_only).init(children_cursor); 3083 var children_iter = try children.iterator(); 3084 3085 while (try children_iter.next()) |*child_cursor| { 3086 const kv_pair = try child_cursor.readKeyValuePair(); 3087 const child_oid_int = kv_pair.hash; 3088 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 3089 const pending_parent_count = try pending_parent_cursor.readUint(); 3090 3091 if (pending_parent_count <= 1) { 3092 _ = try self.child_to_pending_parents.remove(child_oid_int); 3093 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 3094 try self.ready_queue.append(.{ .bytes = &child_oid }); 3095 } else { 3096 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 3097 } 3098 } 3099 } 3100 3101 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 3102 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 3103 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 3104 _ = try oid_cursor.readBytes(&oid); 3105 return oid; 3106 } 3107 }; 3108 } 3109