╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1228 ) !i64 { 1229 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1230 return try cursor.readInt(); 1231 } 1232 1233 fn readBool( 1234 comptime DB: type, 1235 comptime hash_kind: hash.HashKind, 1236 map: DB.HashMap(.read_only), 1237 field_name: []const u8, 1238 ) !bool { 1239 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1240 var buffer: [5]u8 = undefined; 1241 const bytes = try cursor.readBytesObject(&buffer); 1242 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 1243 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 1244 if (std.mem.eql(u8, bytes.value, "true")) return true; 1245 if (std.mem.eql(u8, bytes.value, "false")) return false; 1246 return error.InvalidBool; 1247 } 1248 1249 // 1250 // writing to xitdb 1251 // 1252 1253 pub fn upsert( 1254 comptime T: type, 1255 comptime DB: type, 1256 comptime hash_kind: hash.HashKind, 1257 map: DB.HashMap(.read_write), 1258 event: T, 1259 ) !void { 1260 switch (@typeInfo(T)) { 1261 .@"struct" => |struct_info| { 1262 comptime validateFlattenedFields(T); 1263 inline for (struct_info.fields) |field| { 1264 try upsertField(DB, hash_kind, map, field.name, field.type, @field(event, field.name)); 1265 } 1266 }, 1267 else => @compileError("upsert expects a struct"), 1268 } 1269 } 1270 1271 fn upsertField( 1272 comptime DB: type, 1273 comptime hash_kind: hash.HashKind, 1274 map: DB.HashMap(.read_write), 1275 comptime field_name: []const u8, 1276 comptime Field: type, 1277 value: Field, 1278 ) !void { 1279 const key = hash.hashInt(hash_kind, field_name); 1280 1281 switch (@typeInfo(Field)) { 1282 .pointer => |pointer_info| { 1283 if (pointer_info.size == .slice and pointer_info.child == u8) { 1284 try upsertBytes(DB, hash_kind, map, key, value); 1285 } else { 1286 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1287 } 1288 }, 1289 .array => |array_info| { 1290 if (array_info.child == u8) { 1291 try upsertBytes(DB, hash_kind, map, key, &value); 1292 } else { 1293 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1294 } 1295 }, 1296 .int => |int_info| switch (int_info.signedness) { 1297 .unsigned => { 1298 if (try map.getCursor(key)) |value_cursor| { 1299 if (try value_cursor.readUint() == value) { 1300 return; 1301 } 1302 } 1303 1304 try map.put(key, .{ .uint = value }); 1305 }, 1306 .signed => { 1307 if (try map.getCursor(key)) |value_cursor| { 1308 if (try value_cursor.readInt() == value) { 1309 return; 1310 } 1311 } 1312 1313 try map.put(key, .{ .int = value }); 1314 }, 1315 }, 1316 .bool => { 1317 const bytes = if (value) "true" else "false"; 1318 if (try map.getCursor(key)) |value_cursor| { 1319 var buffer: [5]u8 = undefined; 1320 const existing = try value_cursor.readBytesObject(&buffer); 1321 if (existing.format_tag) |format_tag| { 1322 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 1323 return; 1324 } 1325 } 1326 } 1327 1328 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 1329 }, 1330 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 1331 .optional => |optional_info| { 1332 if (optional_info.child != []const u8) @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1333 // a missing key is null 1334 if (value) |bytes| { 1335 try upsertBytes(DB, hash_kind, map, key, bytes); 1336 } else { 1337 _ = try map.remove(key); 1338 } 1339 }, 1340 // a struct field's own fields are stored in the same map 1341 .@"struct" => try upsert(Field, DB, hash_kind, map, value), 1342 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 1343 } 1344 } 1345 1346 fn upsertBytes( 1347 comptime DB: type, 1348 comptime hash_kind: hash.HashKind, 1349 map: DB.HashMap(.read_write), 1350 key: hash.HashInt(hash_kind), 1351 value: []const u8, 1352 ) !void { 1353 var existing_cursor_maybe = try map.getCursor(key); 1354 if (existing_cursor_maybe) |*existing_cursor| { 1355 if (try bytesEqual(DB, existing_cursor, value)) { 1356 return; 1357 } 1358 } 1359 1360 var value_cursor = try map.putCursor(key); 1361 var write_buffer: [1024]u8 = undefined; 1362 var writer = try value_cursor.writer(&write_buffer); 1363 try writer.interface.writeAll(value); 1364 try writer.finish(); 1365 } 1366 1367 fn bytesEqual( 1368 comptime DB: type, 1369 cursor: *DB.Cursor(.read_only), 1370 value: []const u8, 1371 ) !bool { 1372 var read_buffer: [1024]u8 = undefined; 1373 var reader = try cursor.reader(&read_buffer); 1374 if (reader.size != value.len) { 1375 return false; 1376 } 1377 1378 var chunk_buffer: [1024]u8 = undefined; 1379 var offset: usize = 0; 1380 while (offset < value.len) { 1381 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1382 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1383 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1384 return false; 1385 } 1386 offset += chunk_len; 1387 } 1388 1389 return true; 1390 } 1391 1392 // 1393 // CommitIterator 1394 // 1395 // differs from xit's ObjectIterator in a few ways: 1396 // 1397 // 1. iterates parents first, rather than children first 1398 // 2. `next` only returns the object id, not the full Object 1399 // 3. stores its temporary state in a file on disk to avoid OOMs 1400 // 1401 1402 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1403 return struct { 1404 const DB = EventDB(repo_opts.hash); 1405 const db_name = "haxy-repo-events.db"; 1406 1407 repo_dir: std.Io.Dir, 1408 db_file: std.Io.File, 1409 db: *DB, 1410 // map of each object id to its children 1411 parent_to_children: DB.HashMap(.read_write), 1412 // tracking pending parents is necessary because a child can only 1413 // be returned after all of its unconsumed parents have been returned 1414 child_to_pending_parents: DB.HashMap(.read_write), 1415 // queue of object ids that are ready to be returned from `next` 1416 ready_queue: DB.ArrayList(.read_write), 1417 ready_queue_index: u64, 1418 // the object id at the tip of this branch 1419 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1420 // the object id furthest back in the history that hasn't been consumed 1421 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1422 1423 pub fn init( 1424 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1425 io: std.Io, 1426 allocator: std.mem.Allocator, 1427 consumed_object_ids: DB.HashMap(.read_only), 1428 ref: rf.Ref, 1429 ) !CommitIterator(repo_kind, repo_opts) { 1430 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 1431 errdefer { 1432 db_file.close(io); 1433 state.core.repo_dir.deleteFile(io, db_name) catch {}; 1434 } 1435 1436 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 1437 errdefer allocator.destroy(buffer_ptr); 1438 1439 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 1440 errdefer buffer_ptr.deinit(); 1441 1442 const db_ptr = try allocator.create(DB); 1443 errdefer allocator.destroy(db_ptr); 1444 // the db is scratch state that gets deleted after iteration, so 1445 // there is nothing worth fsyncing 1446 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr, .fsync = false }); 1447 1448 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 1449 1450 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 1451 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 1452 1453 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 1454 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 1455 1456 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 1457 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 1458 1459 var self = CommitIterator(repo_kind, repo_opts){ 1460 .repo_dir = state.core.repo_dir, 1461 .db_file = db_file, 1462 .db = db_ptr, 1463 .parent_to_children = parent_to_children, 1464 .child_to_pending_parents = child_to_pending_parents, 1465 .ready_queue = ready_queue, 1466 .ready_queue_index = 0, 1467 .newest_object_id = null, 1468 .oldest_object_id = null, 1469 }; 1470 errdefer self.deinit(io, allocator); 1471 1472 try self.collect(state, io, allocator, consumed_object_ids, ref); 1473 1474 return self; 1475 } 1476 1477 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 1478 self.db_file.close(io); 1479 self.db.core.memory.buffer.deinit(); 1480 allocator.destroy(self.db.core.memory.buffer); 1481 self.repo_dir.deleteFile(io, db_name) catch {}; 1482 allocator.destroy(self.db); 1483 } 1484 1485 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 1486 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 1487 1488 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 1489 self.ready_queue_index += 1; 1490 1491 try self.enqueueChildren(&oid); 1492 return oid; 1493 } 1494 1495 fn collect( 1496 self: *CommitIterator(repo_kind, repo_opts), 1497 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1498 io: std.Io, 1499 allocator: std.mem.Allocator, 1500 consumed_object_ids: DB.HashMap(.read_only), 1501 ref: rf.Ref, 1502 ) !void { 1503 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 1504 1505 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 1506 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 1507 var walk_queue_index: u64 = 0; 1508 1509 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 1510 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 1511 1512 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 1513 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1514 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 1515 try walk_queue.append(.{ .bytes = &head_oid }); 1516 1517 while (walk_queue_index < try walk_queue.count()) { 1518 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 1519 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 1520 1521 walk_queue_index += 1; 1522 1523 // if we've seen this object id, skip it 1524 if (null != try seen_object_ids.getCursor(oid_int)) { 1525 continue; 1526 } 1527 1528 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 1529 1530 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 1531 defer commit_object.deinit(); 1532 1533 // if this object id has already been consumed, skip it 1534 if (null != try consumed_object_ids.getCursor(oid_int)) { 1535 continue; 1536 } 1537 1538 if (self.newest_object_id == null) self.newest_object_id = oid; 1539 self.oldest_object_id = oid; 1540 1541 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 1542 var pending_parent_count: u64 = 0; 1543 for (parent_oids) |*parent_oid| { 1544 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1545 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 1546 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 1547 1548 // if this object id has already been consumed, skip it 1549 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 1550 continue; 1551 } 1552 1553 // if this object id hasn't already been seen, add it to the walk queue 1554 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 1555 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 1556 } 1557 1558 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 1559 const children = try DB.HashSet(.read_write).init(children_cursor); 1560 try children.put(oid_int, .{ .bytes = &oid }); 1561 pending_parent_count += 1; 1562 } 1563 1564 if (pending_parent_count == 0) { 1565 try self.ready_queue.append(.{ .bytes = &oid }); 1566 } else { 1567 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 1568 } 1569 } 1570 } 1571 1572 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 1573 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 1574 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 1575 const children = try DB.HashSet(.read_only).init(children_cursor); 1576 var children_iter = try children.iterator(); 1577 1578 while (try children_iter.next()) |*child_cursor| { 1579 const kv_pair = try child_cursor.readKeyValuePair(); 1580 const child_oid_int = kv_pair.hash; 1581 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 1582 const pending_parent_count = try pending_parent_cursor.readUint(); 1583 1584 if (pending_parent_count <= 1) { 1585 _ = try self.child_to_pending_parents.remove(child_oid_int); 1586 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 1587 try self.ready_queue.append(.{ .bytes = &child_oid }); 1588 } else { 1589 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 1590 } 1591 } 1592 } 1593 1594 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 1595 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 1596 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1597 _ = try oid_cursor.readBytes(&oid); 1598 return oid; 1599 } 1600 }; 1601 } 1602