╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
981 } 982 983 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 984 }, 985 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 986 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 987 } 988 } 989 990 fn upsertBytes( 991 comptime DB: type, 992 comptime hash_kind: hash.HashKind, 993 map: DB.HashMap(.read_write), 994 key: hash.HashInt(hash_kind), 995 value: []const u8, 996 ) !void { 997 var existing_cursor_maybe = try map.getCursor(key); 998 if (existing_cursor_maybe) |*existing_cursor| { 999 if (try bytesEqual(DB, existing_cursor, value)) { 1000 return; 1001 } 1002 } 1003 1004 var value_cursor = try map.putCursor(key); 1005 var write_buffer: [1024]u8 = undefined; 1006 var writer = try value_cursor.writer(&write_buffer); 1007 try writer.interface.writeAll(value); 1008 try writer.finish(); 1009 } 1010 1011 fn bytesEqual( 1012 comptime DB: type, 1013 cursor: *DB.Cursor(.read_only), 1014 value: []const u8, 1015 ) !bool { 1016 var read_buffer: [1024]u8 = undefined; 1017 var reader = try cursor.reader(&read_buffer); 1018 if (reader.size != value.len) { 1019 return false; 1020 } 1021 1022 var chunk_buffer: [1024]u8 = undefined; 1023 var offset: usize = 0; 1024 while (offset < value.len) { 1025 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1026 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1027 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1028 return false; 1029 } 1030 offset += chunk_len; 1031 } 1032 1033 return true; 1034 } 1035 1036 // 1037 // CommitIterator 1038 // 1039 // differs from xit's ObjectIterator in a few ways: 1040 // 1041 // 1. iterates parents first, rather than children first 1042 // 2. `next` only returns the object id, not the full Object 1043 // 3. stores its temporary state in a file on disk to avoid OOMs 1044 // 1045 1046 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1047 return struct { 1048 const DB = EventDB(repo_opts.hash); 1049 const db_name = "haxy-repo-events.db"; 1050 1051 repo_dir: std.Io.Dir, 1052 db_file: std.Io.File, 1053 db: *DB, 1054 // map of each object id to its children 1055 parent_to_children: DB.HashMap(.read_write), 1056 // tracking pending parents is necessary because a child can only 1057 // be returned after all of its unconsumed parents have been returned 1058 child_to_pending_parents: DB.HashMap(.read_write), 1059 // queue of object ids that are ready to be returned from `next` 1060 ready_queue: DB.ArrayList(.read_write), 1061 ready_queue_index: u64, 1062 // the object id at the tip of this branch 1063 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1064 // the object id furthest back in the history that hasn't been consumed 1065 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1066 1067 pub fn init( 1068 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1069 io: std.Io, 1070 allocator: std.mem.Allocator, 1071 consumed_object_ids: DB.HashMap(.read_only), 1072 ref: rf.Ref, 1073 ) !CommitIterator(repo_kind, repo_opts) { 1074 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 1075 errdefer { 1076 db_file.close(io); 1077 state.core.repo_dir.deleteFile(io, db_name) catch {}; 1078 } 1079 1080 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 1081 errdefer allocator.destroy(buffer_ptr); 1082 1083 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 1084 errdefer buffer_ptr.deinit(); 1085 1086 const db_ptr = try allocator.create(DB); 1087 errdefer allocator.destroy(db_ptr); 1088 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr }); 1089 1090 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 1091 1092 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 1093 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 1094 1095 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 1096 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 1097 1098 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 1099 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 1100 1101 var self = CommitIterator(repo_kind, repo_opts){ 1102 .repo_dir = state.core.repo_dir, 1103 .db_file = db_file, 1104 .db = db_ptr, 1105 .parent_to_children = parent_to_children, 1106 .child_to_pending_parents = child_to_pending_parents, 1107 .ready_queue = ready_queue, 1108 .ready_queue_index = 0, 1109 .newest_object_id = null, 1110 .oldest_object_id = null, 1111 }; 1112 errdefer self.deinit(io, allocator); 1113 1114 try self.collect(state, io, allocator, consumed_object_ids, ref); 1115 1116 return self; 1117 } 1118 1119 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 1120 self.db_file.close(io); 1121 self.db.core.memory.buffer.deinit(); 1122 allocator.destroy(self.db.core.memory.buffer); 1123 self.repo_dir.deleteFile(io, db_name) catch {}; 1124 allocator.destroy(self.db); 1125 } 1126 1127 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 1128 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 1129 1130 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 1131 self.ready_queue_index += 1; 1132 1133 try self.enqueueChildren(&oid); 1134 return oid; 1135 } 1136 1137 fn collect( 1138 self: *CommitIterator(repo_kind, repo_opts), 1139 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1140 io: std.Io, 1141 allocator: std.mem.Allocator, 1142 consumed_object_ids: DB.HashMap(.read_only), 1143 ref: rf.Ref, 1144 ) !void { 1145 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 1146 1147 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 1148 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 1149 var walk_queue_index: u64 = 0; 1150 1151 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 1152 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 1153 1154 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 1155 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1156 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 1157 try walk_queue.append(.{ .bytes = &head_oid }); 1158 1159 while (walk_queue_index < try walk_queue.count()) { 1160 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 1161 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 1162 1163 walk_queue_index += 1; 1164 1165 // if we've seen this object id, skip it 1166 if (null != try seen_object_ids.getCursor(oid_int)) { 1167 continue; 1168 } 1169 1170 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 1171 1172 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 1173 defer commit_object.deinit(); 1174 1175 // if this object id has already been consumed, skip it 1176 if (null != try consumed_object_ids.getCursor(oid_int)) { 1177 continue; 1178 } 1179 1180 if (self.newest_object_id == null) self.newest_object_id = oid; 1181 self.oldest_object_id = oid; 1182 1183 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 1184 var pending_parent_count: u64 = 0; 1185 for (parent_oids) |*parent_oid| { 1186 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1187 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 1188 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 1189 1190 // if this object id has already been consumed, skip it 1191 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 1192 continue; 1193 } 1194 1195 // if this object id hasn't already been seen, add it to the walk queue 1196 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 1197 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 1198 } 1199 1200 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 1201 const children = try DB.HashSet(.read_write).init(children_cursor); 1202 try children.put(oid_int, .{ .bytes = &oid }); 1203 pending_parent_count += 1; 1204 } 1205 1206 if (pending_parent_count == 0) { 1207 try self.ready_queue.append(.{ .bytes = &oid }); 1208 } else { 1209 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 1210 } 1211 } 1212 } 1213 1214 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 1215 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 1216 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 1217 const children = try DB.HashSet(.read_only).init(children_cursor); 1218 var children_iter = try children.iterator(); 1219 1220 while (try children_iter.next()) |*child_cursor| { 1221 const kv_pair = try child_cursor.readKeyValuePair(); 1222 const child_oid_int = kv_pair.hash; 1223 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 1224 const pending_parent_count = try pending_parent_cursor.readUint(); 1225 1226 if (pending_parent_count <= 1) { 1227 _ = try self.child_to_pending_parents.remove(child_oid_int); 1228 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 1229 try self.ready_queue.append(.{ .bytes = &child_oid }); 1230 } else { 1231 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 1232 } 1233 } 1234 } 1235 1236 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 1237 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 1238 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1239 _ = try oid_cursor.readBytes(&oid); 1240 return oid; 1241 } 1242 }; 1243 } 1244