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