╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1097 return event; 1098 } 1099 1100 // whether two events carry the same data. `created_ts` comes from the commit 1101 // rather than the payload, so it isn't compared. 1102 fn fieldsEqual(comptime T: type, a: T, b: T) bool { 1103 switch (@typeInfo(T)) { 1104 .@"struct" => |struct_info| { 1105 inline for (struct_info.fields) |field| { 1106 if (comptime std.mem.eql(u8, field.name, "created_ts")) continue; 1107 if (!fieldEqual(field.type, @field(a, field.name), @field(b, field.name))) return false; 1108 } 1109 }, 1110 else => @compileError("fieldsEqual expects a struct"), 1111 } 1112 1113 return true; 1114 } 1115 1116 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 1117 switch (@typeInfo(Field)) { 1118 .pointer => |pointer_info| { 1119 if (pointer_info.size == .slice and pointer_info.child == u8) { 1120 return std.mem.eql(u8, a, b); 1121 } else { 1122 @compileError("unsupported field type: " ++ @typeName(Field)); 1123 } 1124 }, 1125 .array => |array_info| { 1126 if (array_info.child == u8) { 1127 return std.mem.eql(u8, &a, &b); 1128 } else { 1129 @compileError("unsupported field type: " ++ @typeName(Field)); 1130 } 1131 }, 1132 .bool, .int, .@"enum" => return a == b, 1133 else => @compileError("unsupported field type: " ++ @typeName(Field)), 1134 } 1135 } 1136 1137 fn readBytes( 1138 comptime DB: type, 1139 comptime hash_kind: hash.HashKind, 1140 allocator: std.mem.Allocator, 1141 map: DB.HashMap(.read_only), 1142 field_name: []const u8, 1143 ) ![]const u8 { 1144 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1145 return try cursor.readBytesAlloc(allocator, null); 1146 } 1147 1148 fn readUint( 1149 comptime DB: type, 1150 comptime hash_kind: hash.HashKind, 1151 map: DB.HashMap(.read_only), 1152 field_name: []const u8, 1153 ) !u64 { 1154 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1155 return try cursor.readUint(); 1156 } 1157 1158 fn readInt( 1159 comptime DB: type, 1160 comptime hash_kind: hash.HashKind, 1161 map: DB.HashMap(.read_only), 1162 field_name: []const u8, 1163 ) !i64 { 1164 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1165 return try cursor.readInt(); 1166 } 1167 1168 fn readBool( 1169 comptime DB: type, 1170 comptime hash_kind: hash.HashKind, 1171 map: DB.HashMap(.read_only), 1172 field_name: []const u8, 1173 ) !bool { 1174 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1175 var buffer: [5]u8 = undefined; 1176 const bytes = try cursor.readBytesObject(&buffer); 1177 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 1178 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 1179 if (std.mem.eql(u8, bytes.value, "true")) return true; 1180 if (std.mem.eql(u8, bytes.value, "false")) return false; 1181 return error.InvalidBool; 1182 } 1183 1184 // 1185 // writing to xitdb 1186 // 1187 1188 pub fn upsert( 1189 comptime T: type, 1190 comptime DB: type, 1191 comptime hash_kind: hash.HashKind, 1192 map: DB.HashMap(.read_write), 1193 event: T, 1194 ) !void { 1195 switch (@typeInfo(T)) { 1196 .@"struct" => |struct_info| { 1197 inline for (struct_info.fields) |field| { 1198 try upsertField(DB, hash_kind, map, field.name, field.type, @field(event, field.name)); 1199 } 1200 }, 1201 else => @compileError("upsert expects a struct"), 1202 } 1203 } 1204 1205 fn upsertField( 1206 comptime DB: type, 1207 comptime hash_kind: hash.HashKind, 1208 map: DB.HashMap(.read_write), 1209 comptime field_name: []const u8, 1210 comptime Field: type, 1211 value: Field, 1212 ) !void { 1213 const key = hash.hashInt(hash_kind, field_name); 1214 1215 switch (@typeInfo(Field)) { 1216 .pointer => |pointer_info| { 1217 if (pointer_info.size == .slice and pointer_info.child == u8) { 1218 try upsertBytes(DB, hash_kind, map, key, value); 1219 } else { 1220 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1221 } 1222 }, 1223 .array => |array_info| { 1224 if (array_info.child == u8) { 1225 try upsertBytes(DB, hash_kind, map, key, &value); 1226 } else { 1227 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1228 } 1229 }, 1230 .int => |int_info| switch (int_info.signedness) { 1231 .unsigned => { 1232 if (try map.getCursor(key)) |value_cursor| { 1233 if (try value_cursor.readUint() == value) { 1234 return; 1235 } 1236 } 1237 1238 try map.put(key, .{ .uint = value }); 1239 }, 1240 .signed => { 1241 if (try map.getCursor(key)) |value_cursor| { 1242 if (try value_cursor.readInt() == value) { 1243 return; 1244 } 1245 } 1246 1247 try map.put(key, .{ .int = value }); 1248 }, 1249 }, 1250 .bool => { 1251 const bytes = if (value) "true" else "false"; 1252 if (try map.getCursor(key)) |value_cursor| { 1253 var buffer: [5]u8 = undefined; 1254 const existing = try value_cursor.readBytesObject(&buffer); 1255 if (existing.format_tag) |format_tag| { 1256 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 1257 return; 1258 } 1259 } 1260 } 1261 1262 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 1263 }, 1264 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 1265 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 1266 } 1267 } 1268 1269 fn upsertBytes( 1270 comptime DB: type, 1271 comptime hash_kind: hash.HashKind, 1272 map: DB.HashMap(.read_write), 1273 key: hash.HashInt(hash_kind), 1274 value: []const u8, 1275 ) !void { 1276 var existing_cursor_maybe = try map.getCursor(key); 1277 if (existing_cursor_maybe) |*existing_cursor| { 1278 if (try bytesEqual(DB, existing_cursor, value)) { 1279 return; 1280 } 1281 } 1282 1283 var value_cursor = try map.putCursor(key); 1284 var write_buffer: [1024]u8 = undefined; 1285 var writer = try value_cursor.writer(&write_buffer); 1286 try writer.interface.writeAll(value); 1287 try writer.finish(); 1288 } 1289 1290 fn bytesEqual( 1291 comptime DB: type, 1292 cursor: *DB.Cursor(.read_only), 1293 value: []const u8, 1294 ) !bool { 1295 var read_buffer: [1024]u8 = undefined; 1296 var reader = try cursor.reader(&read_buffer); 1297 if (reader.size != value.len) { 1298 return false; 1299 } 1300 1301 var chunk_buffer: [1024]u8 = undefined; 1302 var offset: usize = 0; 1303 while (offset < value.len) { 1304 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1305 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1306 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1307 return false; 1308 } 1309 offset += chunk_len; 1310 } 1311 1312 return true; 1313 } 1314 1315 // 1316 // CommitIterator 1317 // 1318 // differs from xit's ObjectIterator in a few ways: 1319 // 1320 // 1. iterates parents first, rather than children first 1321 // 2. `next` only returns the object id, not the full Object 1322 // 3. stores its temporary state in a file on disk to avoid OOMs 1323 // 1324 1325 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1326 return struct { 1327 const DB = EventDB(repo_opts.hash); 1328 const db_name = "haxy-repo-events.db"; 1329 1330 repo_dir: std.Io.Dir, 1331 db_file: std.Io.File, 1332 db: *DB, 1333 // map of each object id to its children 1334 parent_to_children: DB.HashMap(.read_write), 1335 // tracking pending parents is necessary because a child can only 1336 // be returned after all of its unconsumed parents have been returned 1337 child_to_pending_parents: DB.HashMap(.read_write), 1338 // queue of object ids that are ready to be returned from `next` 1339 ready_queue: DB.ArrayList(.read_write), 1340 ready_queue_index: u64, 1341 // the object id at the tip of this branch 1342 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1343 // the object id furthest back in the history that hasn't been consumed 1344 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1345 1346 pub fn init( 1347 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1348 io: std.Io, 1349 allocator: std.mem.Allocator, 1350 consumed_object_ids: DB.HashMap(.read_only), 1351 ref: rf.Ref, 1352 ) !CommitIterator(repo_kind, repo_opts) { 1353 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 1354 errdefer { 1355 db_file.close(io); 1356 state.core.repo_dir.deleteFile(io, db_name) catch {}; 1357 } 1358 1359 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 1360 errdefer allocator.destroy(buffer_ptr); 1361 1362 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 1363 errdefer buffer_ptr.deinit(); 1364 1365 const db_ptr = try allocator.create(DB); 1366 errdefer allocator.destroy(db_ptr); 1367 // the db is scratch state that gets deleted after iteration, so 1368 // there is nothing worth fsyncing 1369 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr, .fsync = false }); 1370 1371 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 1372 1373 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 1374 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 1375 1376 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 1377 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 1378 1379 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 1380 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 1381 1382 var self = CommitIterator(repo_kind, repo_opts){ 1383 .repo_dir = state.core.repo_dir, 1384 .db_file = db_file, 1385 .db = db_ptr, 1386 .parent_to_children = parent_to_children, 1387 .child_to_pending_parents = child_to_pending_parents, 1388 .ready_queue = ready_queue, 1389 .ready_queue_index = 0, 1390 .newest_object_id = null, 1391 .oldest_object_id = null, 1392 }; 1393 errdefer self.deinit(io, allocator); 1394 1395 try self.collect(state, io, allocator, consumed_object_ids, ref); 1396 1397 return self; 1398 } 1399 1400 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 1401 self.db_file.close(io); 1402 self.db.core.memory.buffer.deinit(); 1403 allocator.destroy(self.db.core.memory.buffer); 1404 self.repo_dir.deleteFile(io, db_name) catch {}; 1405 allocator.destroy(self.db); 1406 } 1407 1408 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 1409 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 1410 1411 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 1412 self.ready_queue_index += 1; 1413 1414 try self.enqueueChildren(&oid); 1415 return oid; 1416 } 1417 1418 fn collect( 1419 self: *CommitIterator(repo_kind, repo_opts), 1420 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1421 io: std.Io, 1422 allocator: std.mem.Allocator, 1423 consumed_object_ids: DB.HashMap(.read_only), 1424 ref: rf.Ref, 1425 ) !void { 1426 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 1427 1428 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 1429 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 1430 var walk_queue_index: u64 = 0; 1431 1432 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 1433 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 1434 1435 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 1436 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1437 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 1438 try walk_queue.append(.{ .bytes = &head_oid }); 1439 1440 while (walk_queue_index < try walk_queue.count()) { 1441 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 1442 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 1443 1444 walk_queue_index += 1; 1445 1446 // if we've seen this object id, skip it 1447 if (null != try seen_object_ids.getCursor(oid_int)) { 1448 continue; 1449 } 1450 1451 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 1452 1453 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 1454 defer commit_object.deinit(); 1455 1456 // if this object id has already been consumed, skip it 1457 if (null != try consumed_object_ids.getCursor(oid_int)) { 1458 continue; 1459 } 1460 1461 if (self.newest_object_id == null) self.newest_object_id = oid; 1462 self.oldest_object_id = oid; 1463 1464 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 1465 var pending_parent_count: u64 = 0; 1466 for (parent_oids) |*parent_oid| { 1467 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1468 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 1469 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 1470 1471 // if this object id has already been consumed, skip it 1472 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 1473 continue; 1474 } 1475 1476 // if this object id hasn't already been seen, add it to the walk queue 1477 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 1478 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 1479 } 1480 1481 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 1482 const children = try DB.HashSet(.read_write).init(children_cursor); 1483 try children.put(oid_int, .{ .bytes = &oid }); 1484 pending_parent_count += 1; 1485 } 1486 1487 if (pending_parent_count == 0) { 1488 try self.ready_queue.append(.{ .bytes = &oid }); 1489 } else { 1490 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 1491 } 1492 } 1493 } 1494 1495 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 1496 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 1497 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 1498 const children = try DB.HashSet(.read_only).init(children_cursor); 1499 var children_iter = try children.iterator(); 1500 1501 while (try children_iter.next()) |*child_cursor| { 1502 const kv_pair = try child_cursor.readKeyValuePair(); 1503 const child_oid_int = kv_pair.hash; 1504 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 1505 const pending_parent_count = try pending_parent_cursor.readUint(); 1506 1507 if (pending_parent_count <= 1) { 1508 _ = try self.child_to_pending_parents.remove(child_oid_int); 1509 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 1510 try self.ready_queue.append(.{ .bytes = &child_oid }); 1511 } else { 1512 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 1513 } 1514 } 1515 } 1516 1517 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 1518 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 1519 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1520 _ = try oid_cursor.readBytes(&oid); 1521 return oid; 1522 } 1523 }; 1524 } 1525