╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1375 if (it.next() != null) return null; 1376 if (owner.len == 0 or name.len == 0) return null; 1377 1378 return .{ .owner = owner, .name = name }; 1379 } 1380 1381 // resolve a pushed `<owner>/<repo>` to the hex event id that names its on-disk 1382 // directory under the repos dir 1383 pub fn resolveOrCreateRepo( 1384 io: std.Io, 1385 allocator: std.mem.Allocator, 1386 admin_repo_path: []const u8, 1387 owner_name: []const u8, 1388 repo_name: []const u8, 1389 create_if_missing: bool, 1390 ) !?[event_id_size * 2]u8 { 1391 var repo = rp.Repo(.xit, admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 1392 error.RepoNotFound => return null, 1393 else => |e| return e, 1394 }; 1395 defer repo.deinit(io, allocator); 1396 1397 var arena = std.heap.ArenaAllocator.init(allocator); 1398 defer arena.deinit(); 1399 1400 const moment = try currentMoment(admin_repo_opts, &repo); 1401 1402 // an already-registered repo reuses its event id, so a re-push (or a clone) 1403 // lands in the same repo 1404 if (try Repo.readByOwnerAndName(AdminDB, admin_repo_opts.hash, moment, &arena, owner_name, repo_name)) |found| { 1405 return std.fmt.bytesToHex(found.event_id, .lower); 1406 } 1407 1408 if (!create_if_missing) return null; 1409 1410 // the new repo is owned by the named user, read from the name index; an 1411 // unknown owner can't own a repo, so the push is rejected 1412 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(admin_repo_opts.hash, "name->user-id")) orelse return null; 1413 const name_to_user_id = try AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 1414 const owner_id_cursor = try name_to_user_id.getCursor(hash.hashInt(admin_repo_opts.hash, owner_name)) orelse return null; 1415 var owner_user_id: [event_id_size]u8 = undefined; 1416 _ = try owner_id_cursor.readBytes(&owner_user_id); 1417 1418 const owner = (try User.readById(AdminDB, admin_repo_opts.hash, moment, &arena, &owner_user_id)) orelse unreachable; 1419 1420 var id_bytes: [event_id_size]u8 = undefined; 1421 io.random(&id_bytes); 1422 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1423 1424 try consume(.xit, admin_repo_opts, io, allocator, &repo, events_ref, &[_]EventWithId{.{ 1425 .id = event_id_hex, 1426 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1427 .author = .{ .name = owner.event.name, .email = owner.event.email }, 1428 .event = .{ .repo = .{ 1429 .user_id = &owner_user_id, 1430 .name = repo_name, 1431 .description = "", 1432 } }, 1433 }}); 1434 1435 return event_id_hex; 1436 } 1437 1438 // 1439 // reading from xitdb 1440 // 1441 1442 // a struct field's fields share its container's map, so their names must not 1443 // collide with the container's or two fields would silently share a key 1444 fn validateFlattenedFields(comptime T: type) void { 1445 comptime { 1446 for (std.meta.fields(T)) |field| { 1447 if (@typeInfo(field.type) != .@"struct") continue; 1448 for (std.meta.fields(field.type)) |nested| { 1449 if (@hasField(T, nested.name)) 1450 @compileError("flattened field name collision in " ++ @typeName(T) ++ ": " ++ nested.name); 1451 } 1452 } 1453 } 1454 } 1455 1456 pub fn read( 1457 comptime T: type, 1458 comptime DB: type, 1459 comptime hash_kind: hash.HashKind, 1460 arena: *std.heap.ArenaAllocator, 1461 map: DB.HashMap(.read_only), 1462 ) !T { 1463 var event: T = undefined; 1464 1465 switch (@typeInfo(T)) { 1466 .@"struct" => |struct_info| { 1467 comptime validateFlattenedFields(T); 1468 inline for (struct_info.fields) |field| { 1469 switch (@typeInfo(field.type)) { 1470 .pointer => |pointer_info| { 1471 if (pointer_info.size == .slice and pointer_info.child == u8 and pointer_info.is_const) { 1472 @field(event, field.name) = try readBytes(DB, hash_kind, arena.allocator(), map, field.name); 1473 } else { 1474 @compileError("unsupported read field type: " ++ @typeName(field.type)); 1475 } 1476 }, 1477 .array => |array_info| { 1478 if (array_info.child == u8) { 1479 const bytes = try readBytes(DB, hash_kind, arena.allocator(), map, field.name); 1480 if (bytes.len != array_info.len) return error.InvalidByteArrayLength; 1481 @memcpy(@field(event, field.name)[0..], bytes); 1482 } else { 1483 @compileError("unsupported read field type: " ++ @typeName(field.type)); 1484 } 1485 }, 1486 .bool => @field(event, field.name) = try readBool(DB, hash_kind, map, field.name), 1487 .int => |int_info| switch (int_info.signedness) { 1488 .unsigned => @field(event, field.name) = @intCast(try readUint(DB, hash_kind, map, field.name)), 1489 .signed => @field(event, field.name) = @intCast(try readInt(DB, hash_kind, map, field.name)), 1490 }, 1491 .@"enum" => { 1492 const bytes = try readBytes(DB, hash_kind, arena.allocator(), map, field.name); 1493 @field(event, field.name) = std.meta.stringToEnum(field.type, bytes) orelse return error.InvalidEnumTag; 1494 }, 1495 .optional => |optional_info| { 1496 if (optional_info.child != []const u8) @compileError("unsupported read field type: " ++ @typeName(field.type)); 1497 // a missing key is null 1498 @field(event, field.name) = if (try map.getCursor(hash.hashInt(hash_kind, field.name))) |cursor| 1499 try cursor.readBytesAlloc(arena.allocator(), null) 1500 else 1501 null; 1502 }, 1503 // a struct field's own fields are read from the same map 1504 .@"struct" => @field(event, field.name) = try read(field.type, DB, hash_kind, arena, map), 1505 else => @compileError("unsupported read field type: " ++ @typeName(field.type)), 1506 } 1507 } 1508 }, 1509 else => @compileError("read expects a struct"), 1510 } 1511 1512 return event; 1513 } 1514 1515 // whether two events carry the same data 1516 fn fieldsEqual(comptime T: type, a: T, b: T) bool { 1517 switch (@typeInfo(T)) { 1518 .@"struct" => |struct_info| { 1519 inline for (struct_info.fields) |field| { 1520 if (!fieldEqual(field.type, @field(a, field.name), @field(b, field.name))) return false; 1521 } 1522 }, 1523 else => @compileError("fieldsEqual expects a struct"), 1524 } 1525 1526 return true; 1527 } 1528 1529 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 1530 switch (@typeInfo(Field)) { 1531 .pointer => |pointer_info| { 1532 if (pointer_info.size == .slice and pointer_info.child == u8) { 1533 return std.mem.eql(u8, a, b); 1534 } else { 1535 @compileError("unsupported field type: " ++ @typeName(Field)); 1536 } 1537 }, 1538 .array => |array_info| { 1539 if (array_info.child == u8) { 1540 return std.mem.eql(u8, &a, &b); 1541 } else { 1542 @compileError("unsupported field type: " ++ @typeName(Field)); 1543 } 1544 }, 1545 .bool, .int, .@"enum" => return a == b, 1546 else => @compileError("unsupported field type: " ++ @typeName(Field)), 1547 } 1548 } 1549 1550 fn readBytes( 1551 comptime DB: type, 1552 comptime hash_kind: hash.HashKind, 1553 allocator: std.mem.Allocator, 1554 map: DB.HashMap(.read_only), 1555 field_name: []const u8, 1556 ) ![]const u8 { 1557 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1558 return try cursor.readBytesAlloc(allocator, null); 1559 } 1560 1561 fn readUint( 1562 comptime DB: type, 1563 comptime hash_kind: hash.HashKind, 1564 map: DB.HashMap(.read_only), 1565 field_name: []const u8, 1566 ) !u64 { 1567 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1568 return try cursor.readUint(); 1569 } 1570 1571 fn readInt( 1572 comptime DB: type, 1573 comptime hash_kind: hash.HashKind, 1574 map: DB.HashMap(.read_only), 1575 field_name: []const u8, 1576 ) !i64 { 1577 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1578 return try cursor.readInt(); 1579 } 1580 1581 fn readBool( 1582 comptime DB: type, 1583 comptime hash_kind: hash.HashKind, 1584 map: DB.HashMap(.read_only), 1585 field_name: []const u8, 1586 ) !bool { 1587 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1588 var buffer: [5]u8 = undefined; 1589 const bytes = try cursor.readBytesObject(&buffer); 1590 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 1591 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 1592 if (std.mem.eql(u8, bytes.value, "true")) return true; 1593 if (std.mem.eql(u8, bytes.value, "false")) return false; 1594 return error.InvalidBool; 1595 } 1596 1597 // 1598 // writing to xitdb 1599 // 1600 1601 pub fn upsert( 1602 comptime T: type, 1603 comptime DB: type, 1604 comptime hash_kind: hash.HashKind, 1605 map: DB.HashMap(.read_write), 1606 event: T, 1607 ) !void { 1608 switch (@typeInfo(T)) { 1609 .@"struct" => |struct_info| { 1610 comptime validateFlattenedFields(T); 1611 inline for (struct_info.fields) |field| { 1612 try upsertField(DB, hash_kind, map, field.name, field.type, @field(event, field.name)); 1613 } 1614 }, 1615 else => @compileError("upsert expects a struct"), 1616 } 1617 } 1618 1619 fn upsertField( 1620 comptime DB: type, 1621 comptime hash_kind: hash.HashKind, 1622 map: DB.HashMap(.read_write), 1623 comptime field_name: []const u8, 1624 comptime Field: type, 1625 value: Field, 1626 ) !void { 1627 const key = hash.hashInt(hash_kind, field_name); 1628 1629 switch (@typeInfo(Field)) { 1630 .pointer => |pointer_info| { 1631 if (pointer_info.size == .slice and pointer_info.child == u8) { 1632 try upsertBytes(DB, hash_kind, map, key, value); 1633 } else { 1634 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1635 } 1636 }, 1637 .array => |array_info| { 1638 if (array_info.child == u8) { 1639 try upsertBytes(DB, hash_kind, map, key, &value); 1640 } else { 1641 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1642 } 1643 }, 1644 .int => |int_info| switch (int_info.signedness) { 1645 .unsigned => { 1646 if (try map.getCursor(key)) |value_cursor| { 1647 if (try value_cursor.readUint() == value) { 1648 return; 1649 } 1650 } 1651 1652 try map.put(key, .{ .uint = value }); 1653 }, 1654 .signed => { 1655 if (try map.getCursor(key)) |value_cursor| { 1656 if (try value_cursor.readInt() == value) { 1657 return; 1658 } 1659 } 1660 1661 try map.put(key, .{ .int = value }); 1662 }, 1663 }, 1664 .bool => { 1665 const bytes = if (value) "true" else "false"; 1666 if (try map.getCursor(key)) |value_cursor| { 1667 var buffer: [5]u8 = undefined; 1668 const existing = try value_cursor.readBytesObject(&buffer); 1669 if (existing.format_tag) |format_tag| { 1670 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 1671 return; 1672 } 1673 } 1674 } 1675 1676 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 1677 }, 1678 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 1679 .optional => |optional_info| { 1680 if (optional_info.child != []const u8) @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1681 // a missing key is null 1682 if (value) |bytes| { 1683 try upsertBytes(DB, hash_kind, map, key, bytes); 1684 } else { 1685 _ = try map.remove(key); 1686 } 1687 }, 1688 // a struct field's own fields are stored in the same map 1689 .@"struct" => try upsert(Field, DB, hash_kind, map, value), 1690 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 1691 } 1692 } 1693 1694 fn upsertBytes( 1695 comptime DB: type, 1696 comptime hash_kind: hash.HashKind, 1697 map: DB.HashMap(.read_write), 1698 key: hash.HashInt(hash_kind), 1699 value: []const u8, 1700 ) !void { 1701 var existing_cursor_maybe = try map.getCursor(key); 1702 if (existing_cursor_maybe) |*existing_cursor| { 1703 if (try bytesEqual(DB, existing_cursor, value)) { 1704 return; 1705 } 1706 } 1707 1708 var value_cursor = try map.putCursor(key); 1709 var write_buffer: [1024]u8 = undefined; 1710 var writer = try value_cursor.writer(&write_buffer); 1711 try writer.interface.writeAll(value); 1712 try writer.finish(); 1713 } 1714 1715 fn bytesEqual( 1716 comptime DB: type, 1717 cursor: *DB.Cursor(.read_only), 1718 value: []const u8, 1719 ) !bool { 1720 var read_buffer: [1024]u8 = undefined; 1721 var reader = try cursor.reader(&read_buffer); 1722 if (reader.size != value.len) { 1723 return false; 1724 } 1725 1726 var chunk_buffer: [1024]u8 = undefined; 1727 var offset: usize = 0; 1728 while (offset < value.len) { 1729 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1730 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1731 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1732 return false; 1733 } 1734 offset += chunk_len; 1735 } 1736 1737 return true; 1738 } 1739 1740 // 1741 // CommitIterator 1742 // 1743 // differs from xit's ObjectIterator in a few ways: 1744 // 1745 // 1. iterates parents first, rather than children first 1746 // 2. `next` only returns the object id, not the full Object 1747 // 3. stores its temporary state in a file on disk to avoid OOMs 1748 // 1749 1750 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1751 return struct { 1752 const DB = EventDB(repo_opts.hash); 1753 const db_name = "haxy-repo-events.db"; 1754 1755 repo_dir: std.Io.Dir, 1756 db_file: std.Io.File, 1757 db: *DB, 1758 // map of each object id to its children 1759 parent_to_children: DB.HashMap(.read_write), 1760 // tracking pending parents is necessary because a child can only 1761 // be returned after all of its unconsumed parents have been returned 1762 child_to_pending_parents: DB.HashMap(.read_write), 1763 // queue of object ids that are ready to be returned from `next` 1764 ready_queue: DB.ArrayList(.read_write), 1765 ready_queue_index: u64, 1766 // the object id at the tip of this branch 1767 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1768 // the object id furthest back in the history that hasn't been consumed 1769 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1770 1771 pub fn init( 1772 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1773 io: std.Io, 1774 allocator: std.mem.Allocator, 1775 consumed_object_ids: DB.HashMap(.read_only), 1776 ref: rf.Ref, 1777 ) !CommitIterator(repo_kind, repo_opts) { 1778 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 1779 errdefer { 1780 db_file.close(io); 1781 state.core.repo_dir.deleteFile(io, db_name) catch {}; 1782 } 1783 1784 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 1785 errdefer allocator.destroy(buffer_ptr); 1786 1787 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 1788 errdefer buffer_ptr.deinit(); 1789 1790 const db_ptr = try allocator.create(DB); 1791 errdefer allocator.destroy(db_ptr); 1792 // the db is scratch state that gets deleted after iteration, so 1793 // there is nothing worth fsyncing 1794 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr, .fsync = false }); 1795 1796 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 1797 1798 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 1799 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 1800 1801 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 1802 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 1803 1804 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 1805 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 1806 1807 var self = CommitIterator(repo_kind, repo_opts){ 1808 .repo_dir = state.core.repo_dir, 1809 .db_file = db_file, 1810 .db = db_ptr, 1811 .parent_to_children = parent_to_children, 1812 .child_to_pending_parents = child_to_pending_parents, 1813 .ready_queue = ready_queue, 1814 .ready_queue_index = 0, 1815 .newest_object_id = null, 1816 .oldest_object_id = null, 1817 }; 1818 errdefer self.deinit(io, allocator); 1819 1820 try self.collect(state, io, allocator, consumed_object_ids, ref); 1821 1822 return self; 1823 } 1824 1825 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 1826 self.db_file.close(io); 1827 self.db.core.memory.buffer.deinit(); 1828 allocator.destroy(self.db.core.memory.buffer); 1829 self.repo_dir.deleteFile(io, db_name) catch {}; 1830 allocator.destroy(self.db); 1831 } 1832 1833 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 1834 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 1835 1836 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 1837 self.ready_queue_index += 1; 1838 1839 try self.enqueueChildren(&oid); 1840 return oid; 1841 } 1842 1843 fn collect( 1844 self: *CommitIterator(repo_kind, repo_opts), 1845 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1846 io: std.Io, 1847 allocator: std.mem.Allocator, 1848 consumed_object_ids: DB.HashMap(.read_only), 1849 ref: rf.Ref, 1850 ) !void { 1851 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 1852 1853 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 1854 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 1855 var walk_queue_index: u64 = 0; 1856 1857 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 1858 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 1859 1860 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 1861 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1862 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 1863 try walk_queue.append(.{ .bytes = &head_oid }); 1864 1865 while (walk_queue_index < try walk_queue.count()) { 1866 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 1867 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 1868 1869 walk_queue_index += 1; 1870 1871 // if we've seen this object id, skip it 1872 if (null != try seen_object_ids.getCursor(oid_int)) { 1873 continue; 1874 } 1875 1876 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 1877 1878 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 1879 defer commit_object.deinit(); 1880 1881 // if this object id has already been consumed, skip it 1882 if (null != try consumed_object_ids.getCursor(oid_int)) { 1883 continue; 1884 } 1885 1886 if (self.newest_object_id == null) self.newest_object_id = oid; 1887 self.oldest_object_id = oid; 1888 1889 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 1890 var pending_parent_count: u64 = 0; 1891 for (parent_oids) |*parent_oid| { 1892 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1893 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 1894 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 1895 1896 // if this object id has already been consumed, skip it 1897 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 1898 continue; 1899 } 1900 1901 // if this object id hasn't already been seen, add it to the walk queue 1902 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 1903 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 1904 } 1905 1906 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 1907 const children = try DB.HashSet(.read_write).init(children_cursor); 1908 try children.put(oid_int, .{ .bytes = &oid }); 1909 pending_parent_count += 1; 1910 } 1911 1912 if (pending_parent_count == 0) { 1913 try self.ready_queue.append(.{ .bytes = &oid }); 1914 } else { 1915 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 1916 } 1917 } 1918 } 1919 1920 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 1921 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 1922 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 1923 const children = try DB.HashSet(.read_only).init(children_cursor); 1924 var children_iter = try children.iterator(); 1925 1926 while (try children_iter.next()) |*child_cursor| { 1927 const kv_pair = try child_cursor.readKeyValuePair(); 1928 const child_oid_int = kv_pair.hash; 1929 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 1930 const pending_parent_count = try pending_parent_cursor.readUint(); 1931 1932 if (pending_parent_count <= 1) { 1933 _ = try self.child_to_pending_parents.remove(child_oid_int); 1934 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 1935 try self.ready_queue.append(.{ .bytes = &child_oid }); 1936 } else { 1937 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 1938 } 1939 } 1940 } 1941 1942 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 1943 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 1944 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1945 _ = try oid_cursor.readBytes(&oid); 1946 return oid; 1947 } 1948 }; 1949 } 1950