╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1621 ) !?T.Record { 1622 var event_db_maybe: ?LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 1623 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 1624 const moment = (if (event_db_maybe) |*event_db| 1625 currentMomentFromDb(repo_opts.hash, event_db.db) 1626 else if (repo_kind == .git) 1627 return null 1628 else 1629 currentMoment(repo_opts, repo)) catch return null; 1630 return T.readById(EventDB(repo_opts.hash), repo_opts.hash, moment, arena, id); 1631 } 1632 1633 pub fn currentMoment( 1634 comptime repo_opts: rp.RepoOpts(.xit), 1635 repo: *rp.Repo(.xit, repo_opts), 1636 ) !rp.Repo(.xit, repo_opts).DB.HashMap(.read_only) { 1637 return currentMomentFromDb(repo_opts.hash, &repo.core.db); 1638 } 1639 1640 pub fn currentMomentFromDb( 1641 comptime hash_kind: hash.HashKind, 1642 db: *EventDB(hash_kind), 1643 ) !EventDB(hash_kind).HashMap(.read_only) { 1644 const DB = EventDB(hash_kind); 1645 const history = try DB.ArrayList(.read_only).init(db.rootCursor().readOnly()); 1646 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 1647 const moment = try DB.HashMap(.read_only).init(moment_cursor); 1648 return try currentMomentFromRepoMoment(hash_kind, moment); 1649 } 1650 1651 pub fn currentMomentFromRepoMoment( 1652 comptime hash_kind: hash.HashKind, 1653 moment: EventDB(hash_kind).HashMap(.read_only), 1654 ) !EventDB(hash_kind).HashMap(.read_only) { 1655 const DB = EventDB(hash_kind); 1656 const last_object_id_cursor = try moment.getCursor(hash.hashInt(hash_kind, last_object_id_key)) orelse return error.NotFound; 1657 var last_object_id: [hash.byteLen(hash_kind)]u8 = undefined; 1658 _ = try last_object_id_cursor.readBytes(&last_object_id); 1659 const haxy_history_cursor = try moment.getCursor(hash.hashInt(hash_kind, history_key)) orelse return error.NotFound; 1660 const haxy_history = try DB.ArrayList(.read_only).init(haxy_history_cursor); 1661 const haxy_moments_cursor = try haxy_history.getCursor(-1) orelse return error.NotFound; 1662 const haxy_moments = try DB.HashMap(.read_only).init(haxy_moments_cursor); 1663 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(hash_kind, &last_object_id)) orelse return error.NotFound; 1664 return try DB.HashMap(.read_only).init(haxy_moment_cursor); 1665 } 1666 1667 // build a sorted-set key from an order and event id. big-endian order makes 1668 // byte order match numeric order; the event id breaks ties. 1669 pub fn orderKey(order: u64, event_id: *const [event_id_size]u8) [@sizeOf(u64) + event_id_size]u8 { 1670 var key: [@sizeOf(u64) + event_id_size]u8 = undefined; 1671 std.mem.writeInt(u64, key[0..@sizeOf(u64)], order, .big); 1672 @memcpy(key[@sizeOf(u64)..], event_id); 1673 return key; 1674 } 1675 1676 pub fn orderKeyDesc(order: u64, event_id: *const [event_id_size]u8) [@sizeOf(u64) + event_id_size]u8 { 1677 return orderKey(std.math.maxInt(u64) - order, event_id); 1678 } 1679 1680 pub fn removedRecord( 1681 comptime T: type, 1682 comptime DB: type, 1683 comptime hash_kind: hash.HashKind, 1684 haxy_moment: DB.HashMap(.read_only), 1685 existing_maybe: ?T, 1686 ) !T { 1687 var record = existing_maybe orelse return error.EventNotFound; 1688 record.removed = true; 1689 const order_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, event_order_key)) orelse return error.CursorNotFound; 1690 record.updated_order = try order_cursor.readUint(); 1691 return record; 1692 } 1693 1694 // add or refresh one entry in the global logical-event index 1695 pub fn indexEvent( 1696 comptime DB: type, 1697 comptime hash_kind: hash.HashKind, 1698 haxy_moment: DB.HashMap(.read_write), 1699 event_id: *const [event_id_size]u8, 1700 kind: EventKind, 1701 existing_maybe: anytype, 1702 record: anytype, 1703 ) !void { 1704 const event_key = hash.hashInt(hash_kind, event_id); 1705 const kind_map_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, event_index_key)); 1706 const kind_map = try DB.HashMap(.read_write).init(kind_map_cursor); 1707 try kind_map.put(event_key, .{ .bytes = @tagName(kind) }); 1708 1709 const active_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, active_event_id_set_key)); 1710 const active = try DB.SortedSet(.read_write).init(active_cursor); 1711 const removed_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, removed_event_id_set_key)); 1712 const removed_set = try DB.SortedSet(.read_write).init(removed_cursor); 1713 1714 if (existing_maybe) |existing| { 1715 const old_key = orderKeyDesc(existing.updated_order, event_id); 1716 _ = try active.remove(&old_key); 1717 _ = try removed_set.remove(&old_key); 1718 } 1719 1720 const order_key = orderKeyDesc(record.updated_order, event_id); 1721 if (record.removed) { 1722 try removed_set.put(&order_key); 1723 } else { 1724 try active.put(&order_key); 1725 } 1726 } 1727 1728 pub fn touchThread( 1729 comptime DB: type, 1730 comptime hash_kind: hash.HashKind, 1731 haxy_moment: DB.HashMap(.read_write), 1732 thread_id: *const [event_id_size]u8, 1733 order: u64, 1734 arena: *std.heap.ArenaAllocator, 1735 ) !void { 1736 const kind_map_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, event_index_key)) orelse return; 1737 const kind_map = try DB.HashMap(.read_only).init(kind_map_cursor); 1738 const kind = (try readEventKind(hash_kind, kind_map, thread_id)) orelse return; 1739 switch (kind) { 1740 .discuss => try Discussion.touch(DB, hash_kind, haxy_moment, thread_id, order, arena), 1741 else => {}, 1742 } 1743 } 1744 1745 pub fn readEventKind( 1746 comptime hash_kind: hash.HashKind, 1747 kind_map: anytype, 1748 id: *const [event_id_size]u8, 1749 ) !?EventKind { 1750 const cursor = try kind_map.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 1751 var buffer: [64]u8 = undefined; 1752 return std.meta.stringToEnum(EventKind, try cursor.readBytes(&buffer)) orelse error.InvalidEventKind; 1753 } 1754 1755 // the email of the event's author, or null when the id names no authored event 1756 // of this kind 1757 pub fn readAuthorEmail( 1758 comptime repo_kind: rp.RepoKind, 1759 comptime repo_opts: rp.RepoOpts(repo_kind), 1760 io: std.Io, 1761 allocator: std.mem.Allocator, 1762 arena: *std.heap.ArenaAllocator, 1763 repo: *rp.Repo(repo_kind, repo_opts), 1764 kind: EventKind, 1765 id: *const [event_id_size]u8, 1766 ) !?[]const u8 { 1767 const record_map_key = switch (kind) { 1768 .issue => Issue.record_map_key, 1769 .discuss => Discussion.record_map_key, 1770 .comment => Comment.record_map_key, 1771 .attach => Attachment.record_map_key, 1772 .patchrev => PatchRev.record_map_key, 1773 .patch => Patch.record_map_key, 1774 .user, .repo, .fork => return null, 1775 }; 1776 1777 const DB = EventDB(repo_opts.hash); 1778 var event_db_maybe: ?LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 1779 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 1780 const moment = (if (event_db_maybe) |*event_db| 1781 currentMomentFromDb(repo_opts.hash, event_db.db) 1782 else if (repo_kind == .git) 1783 return null 1784 else 1785 currentMoment(repo_opts, repo)) catch return null; 1786 1787 const records_cursor = (try moment.getCursor(hash.hashInt(repo_opts.hash, record_map_key))) orelse return null; 1788 const records = try DB.HashMap(.read_only).init(records_cursor); 1789 const record_cursor = (try records.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return null; 1790 const record = try DB.HashMap(.read_only).init(record_cursor); 1791 return try readField(?[]const u8, DB, repo_opts.hash, arena, try record.getCursor(hash.hashInt(repo_opts.hash, "author_email"))); 1792 } 1793 1794 // the event id embedded in a set entry's order key 1795 pub fn readOrderKeyId(comptime DB: type, kv_pair_cursor: DB.Cursor(.read_only)) ![event_id_size]u8 { 1796 var cursor = kv_pair_cursor; 1797 const kv_pair = try cursor.readKeyValuePair(); 1798 var order_key: [@sizeOf(u64) + event_id_size]u8 = undefined; 1799 _ = try kv_pair.key_cursor.readBytes(&order_key); 1800 return order_key[@sizeOf(u64)..][0..event_id_size].*; 1801 } 1802 1803 // split a pushed "<owner>/<repo>" path into its two components, or null if it 1804 // isn't exactly two non-empty segments. 1805 pub fn parseOwnerRepoPath(path: []const u8) ?struct { owner: []const u8, name: []const u8 } { 1806 var it = std.mem.splitScalar(u8, path, '/'); 1807 const owner = it.next() orelse return null; 1808 const name = it.next() orelse return null; 1809 if (it.next() != null) return null; 1810 if (owner.len == 0 or name.len == 0) return null; 1811 1812 return .{ .owner = owner, .name = name }; 1813 } 1814 1815 pub const CreateRepoOptions = struct { 1816 read_access: Repo.Access = .private, 1817 }; 1818 1819 // the ids that locate a repo's on-disk directory 1820 pub const RepoLocation = struct { 1821 owner_id: [event_id_size]u8, 1822 repo_id: [event_id_size]u8, 1823 }; 1824 1825 // resolve a pushed `<owner>/<repo>` to the ids that locate its on-disk directory 1826 pub fn resolveOrCreateRepo( 1827 io: std.Io, 1828 allocator: std.mem.Allocator, 1829 users_dir: []const u8, 1830 admin_repo_path: []const u8, 1831 owner_name: []const u8, 1832 repo_name: []const u8, 1833 create_options_maybe: ?CreateRepoOptions, 1834 ) !?RepoLocation { 1835 var admin_repo = rp.Repo(.xit, admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 1836 error.RepoNotFound => return null, 1837 else => |e| return e, 1838 }; 1839 defer admin_repo.deinit(io, allocator); 1840 1841 var arena = std.heap.ArenaAllocator.init(allocator); 1842 defer arena.deinit(); 1843 1844 const admin_moment = try currentMoment(admin_repo_opts, &admin_repo); 1845 1846 // an unknown owner can't own a repo, so the push is rejected 1847 const owner_user_id = (try User.readIdByName(AdminDB, admin_repo_opts.hash, admin_moment, owner_name)) orelse return null; 1848 var user_repo = (try openUserRepo(io, allocator, users_dir, &owner_user_id)) orelse return null; 1849 defer user_repo.deinit(io, allocator); 1850 1851 // an already-registered repo reuses its event id, so a re-push (or a clone) 1852 // lands in the same repo 1853 if (try userMoment(&user_repo)) |moment| { 1854 if (try Repo.readByName(UserDB, user_repo_opts.hash, moment, &arena, repo_name)) |found| { 1855 return .{ .owner_id = owner_user_id, .repo_id = found.event_id }; 1856 } 1857 } 1858 1859 const create_options = create_options_maybe orelse return null; 1860 1861 const owner = (try User.readById(AdminDB, admin_repo_opts.hash, admin_moment, &arena, &owner_user_id)) orelse unreachable; 1862 1863 var id_bytes: [event_id_size]u8 = undefined; 1864 io.random(&id_bytes); 1865 try writeRepoEvent(io, allocator, users_dir, &user_repo, &owner_user_id, .{ .name = owner.event.name, .email = owner.event.email }, &id_bytes, repo_name, "", create_options.read_access); 1866 return .{ .owner_id = owner_user_id, .repo_id = id_bytes }; 1867 } 1868 1869 // create a bare repo with `hash_kind` and register it in its owner's user repo 1870 pub fn createRepo( 1871 io: std.Io, 1872 allocator: std.mem.Allocator, 1873 users_dir: []const u8, 1874 owner_id: *const [event_id_size]u8, 1875 owner: CommitAuthor, 1876 name: []const u8, 1877 description: []const u8, 1878 read_access: Repo.Access, 1879 hash_kind: hash.HashKind, 1880 ) !RepoLocation { 1881 try Repo.validateName(name); 1882 var user_repo = (try openUserRepo(io, allocator, users_dir, owner_id)) orelse return error.NotFound; 1883 defer user_repo.deinit(io, allocator); 1884 if (try userMoment(&user_repo)) |moment| { 1885 var arena = std.heap.ArenaAllocator.init(allocator); 1886 defer arena.deinit(); 1887 if (try Repo.readByName(UserDB, user_repo_opts.hash, moment, &arena, name) != null) return error.NameTaken; 1888 } 1889 1890 var id_bytes: [event_id_size]u8 = undefined; 1891 io.random(&id_bytes); 1892 1893 // the directory comes first so a listed repo always has one 1894 { 1895 const path = try repoPath(allocator, users_dir, owner_id, &id_bytes); 1896 defer allocator.free(path); 1897 switch (hash_kind) { 1898 inline else => |kind| { 1899 var repo = try rp.Repo(.xit, .{ .hash = kind }).init(io, allocator, .{ .path = path, .bare = true }); 1900 defer repo.deinit(io, allocator); 1901 }, 1902 } 1903 } 1904 1905 try writeRepoEvent(io, allocator, users_dir, &user_repo, owner_id, owner, &id_bytes, name, description, read_access); 1906 return .{ .owner_id = owner_id.*, .repo_id = id_bytes }; 1907 } 1908 1909 // create a user and their user repo, returning the new user's id 1910 pub fn createUser( 1911 io: std.Io, 1912 allocator: std.mem.Allocator, 1913 users_dir: []const u8, 1914 admin_repo: *rp.Repo(.xit, admin_repo_opts), 1915 name: []const u8, 1916 email: []const u8, 1917 password: []const u8, 1918 password_again: []const u8, 1919 ) ![event_id_size]u8 { 1920 try User.validateName(name); 1921 if (email.len == 0) return error.EmailEmpty; 1922 if (password.len == 0) return error.PasswordEmpty; 1923 if (!std.mem.eql(u8, password, password_again)) return error.PasswordMismatch; 1924 // a fresh admin repo has no moment until its first event 1925 if (currentMoment(admin_repo_opts, admin_repo)) |moment| { 1926 if (try User.readIdByName(AdminDB, admin_repo_opts.hash, moment, name) != null) return error.NameTaken; 1927 var arena = std.heap.ArenaAllocator.init(allocator); 1928 defer arena.deinit(); 1929 if (try User.readByEmail(AdminDB, admin_repo_opts.hash, moment, &arena, email) != null) return error.EmailTaken; 1930 } else |err| switch (err) { 1931 error.NotFound => {}, 1932 else => |e| return e, 1933 } 1934 1935 var password_hash_buf: [User.password_hash_max_len]u8 = undefined; 1936 const password_hash = try User.hashPassword(password, &password_hash_buf, io); 1937 1938 var id_bytes: [event_id_size]u8 = undefined; 1939 io.random(&id_bytes); 1940 1941 // the user repo comes first so a listed user can always create repos 1942 { 1943 const path = try userRepoPath(allocator, users_dir, &id_bytes); 1944 defer allocator.free(path); 1945 var user_repo = try initUserRepo(io, allocator, path); 1946 defer user_repo.deinit(io, allocator); 1947 } 1948 1949 try consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, admin_repo_opts, io, allocator, admin_repo, events_ref, &[_]EventWithId{.{ 1950 .id = std.fmt.bytesToHex(id_bytes, .lower), 1951 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1952 .author = .{ .name = name, .email = email }, 1953 .event = .{ .user = .{ .name = name, .email = email, .password_hash = password_hash } }, 1954 }}); 1955 return id_bytes; 1956 } 1957 1958 // consume a new repo's event into its owner's user repo and mark it active 1959 fn writeRepoEvent( 1960 io: std.Io, 1961 allocator: std.mem.Allocator, 1962 users_dir: []const u8, 1963 user_repo: *rp.Repo(.xit, user_repo_opts), 1964 owner_id: *const [event_id_size]u8, 1965 owner: CommitAuthor, 1966 id_bytes: *const [event_id_size]u8, 1967 name: []const u8, 1968 description: []const u8, 1969 read_access: Repo.Access, 1970 ) !void { 1971 const timestamp: u64 = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()); 1972 try consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, user_repo_opts, io, allocator, user_repo, events_ref, &[_]EventWithId{.{ 1973 .id = std.fmt.bytesToHex(id_bytes.*, .lower), 1974 .timestamp = timestamp, 1975 .author = owner, 1976 .event = .{ .repo = .{ 1977 .user_id = owner_id, 1978 .name = name, 1979 .description = description, 1980 .read_access = read_access, 1981 } }, 1982 }}); 1983 bumpRepoActivity(io, allocator, users_dir, owner_id, id_bytes, timestamp) catch |err| { 1984 std.log.warn("failed to record repo activity: {s}", .{@errorName(err)}); 1985 }; 1986 } 1987 1988 // 1989 // reading from xitdb 1990 // 1991 1992 // read just `Fields`, a subset of `T.Record`, from the record with event id 1993 // `id`, or null when there is none 1994 pub fn readRecordSubset( 1995 comptime T: type, 1996 comptime Fields: type, 1997 comptime DB: type, 1998 comptime hash_kind: hash.HashKind, 1999 haxy_moment: DB.HashMap(.read_only), 2000 arena: *std.heap.ArenaAllocator, 2001 id: []const u8, 2002 ) !?Fields { 2003 comptime assertFieldSubset(Fields, T.Record); 2004 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, T.record_map_key)) orelse return null; 2005 const records = try DB.HashMap(.read_only).init(records_cursor); 2006 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 2007 return try read(Fields, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 2008 } 2009 2010 fn assertFieldSubset(comptime Sub: type, comptime Full: type) void { 2011 for (@typeInfo(Sub).@"struct".fields) |field| { 2012 if (!@hasField(Full, field.name)) @compileError(@typeName(Full) ++ " has no field " ++ field.name); 2013 const FullField = @FieldType(Full, field.name); 2014 if (@typeInfo(field.type) == .@"struct") { 2015 assertFieldSubset(field.type, FullField); 2016 } else if (field.type != FullField) { 2017 @compileError(@typeName(Full) ++ "." ++ field.name ++ " is a " ++ @typeName(FullField)); 2018 } 2019 } 2020 } 2021 2022 pub fn read( 2023 comptime T: type, 2024 comptime DB: type, 2025 comptime hash_kind: hash.HashKind, 2026 arena: *std.heap.ArenaAllocator, 2027 map: DB.HashMap(.read_only), 2028 ) !T { 2029 var event: T = undefined; 2030 switch (@typeInfo(T)) { 2031 .@"struct" => |struct_info| { 2032 inline for (struct_info.fields) |field| { 2033 const cursor = try map.getCursor(hash.hashInt(hash_kind, field.name)); 2034 @field(event, field.name) = try readField(field.type, DB, hash_kind, arena, cursor); 2035 } 2036 }, 2037 else => @compileError("read expects a struct"), 2038 } 2039 return event; 2040 } 2041 2042 fn readField( 2043 comptime Field: type, 2044 comptime DB: type, 2045 comptime hash_kind: hash.HashKind, 2046 arena: *std.heap.ArenaAllocator, 2047 cursor_maybe: ?DB.Cursor(.read_only), 2048 ) !Field { 2049 if (@typeInfo(Field) == .optional) { 2050 // a missing key or a null union payload is null 2051 const cursor = cursor_maybe orelse return null; 2052 if (cursor.slot().tag == .none) return null; 2053 return try readField(@typeInfo(Field).optional.child, DB, hash_kind, arena, cursor); 2054 } 2055 const cursor = cursor_maybe orelse return error.NotFound; 2056 switch (@typeInfo(Field)) { 2057 .pointer => |pointer_info| { 2058 if (pointer_info.size != .slice or pointer_info.child != u8 or !pointer_info.is_const) { 2059 @compileError("unsupported read field type: " ++ @typeName(Field)); 2060 } 2061 return try cursor.readBytesAlloc(arena.allocator(), null); 2062 }, 2063 .array => |array_info| { 2064 if (array_info.child != u8) @compileError("unsupported read field type: " ++ @typeName(Field)); 2065 var bytes: Field = undefined; 2066 if ((try cursor.readBytes(&bytes)).len != bytes.len) return error.InvalidByteArrayLength; 2067 return bytes; 2068 }, 2069 .bool => { 2070 var buffer: [5]u8 = undefined; 2071 const bytes = try cursor.readBytesObject(&buffer); 2072 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 2073 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 2074 if (std.mem.eql(u8, bytes.value, "true")) return true; 2075 if (std.mem.eql(u8, bytes.value, "false")) return false; 2076 return error.InvalidBool; 2077 }, 2078 .int => |int_info| return switch (int_info.signedness) { 2079 .unsigned => @intCast(try cursor.readUint()), 2080 .signed => @intCast(try cursor.readInt()), 2081 }, 2082 .@"enum" => return std.meta.stringToEnum(Field, try cursor.readBytesAlloc(arena.allocator(), null)) orelse error.InvalidEnumTag, 2083 .@"union" => |union_info| { 2084 if (union_info.tag_type == null) @compileError("unsupported read field type: " ++ @typeName(Field)); 2085 switch (cursor.slot().tag) { 2086 .bytes, .short_bytes => { 2087 const name = try cursor.readBytesAlloc(arena.allocator(), null); 2088 inline for (union_info.fields) |variant| { 2089 if (variant.type == void and std.mem.eql(u8, name, variant.name)) { 2090 return @unionInit(Field, variant.name, {}); 2091 } 2092 } 2093 return error.InvalidUnion; 2094 }, 2095 .hash_map => { 2096 const variants = try DB.HashMap(.read_only).init(cursor); 2097 var iter = try variants.iterator(); 2098 const entry = (try iter.next()) orelse return error.InvalidUnion; 2099 const pair = try entry.readKeyValuePair(); 2100 if (try iter.next() != null) return error.InvalidUnion; 2101 inline for (union_info.fields) |variant| { 2102 if (variant.type != void and pair.hash == hash.hashInt(hash_kind, variant.name)) { 2103 return @unionInit(Field, variant.name, try readField(variant.type, DB, hash_kind, arena, pair.value_cursor)); 2104 } 2105 } 2106 return error.InvalidUnion; 2107 }, 2108 else => return error.InvalidUnion, 2109 } 2110 }, 2111 .@"struct" => return try read(Field, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)), 2112 else => @compileError("unsupported read field type: " ++ @typeName(Field)), 2113 } 2114 } 2115 2116 // whether two events carry the same data 2117 fn fieldsEqual(comptime T: type, a: T, b: T) bool { 2118 switch (@typeInfo(T)) { 2119 .@"struct" => |struct_info| { 2120 inline for (struct_info.fields) |field| { 2121 if (!fieldEqual(field.type, @field(a, field.name), @field(b, field.name))) return false; 2122 } 2123 }, 2124 else => @compileError("fieldsEqual expects a struct"), 2125 } 2126 2127 return true; 2128 } 2129 2130 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 2131 switch (@typeInfo(Field)) { 2132 .pointer => |pointer_info| { 2133 if (pointer_info.size == .slice and pointer_info.child == u8) { 2134 return std.mem.eql(u8, a, b); 2135 } else { 2136 @compileError("unsupported field type: " ++ @typeName(Field)); 2137 } 2138 }, 2139 .array => |array_info| { 2140 if (array_info.child == u8) { 2141 return std.mem.eql(u8, &a, &b); 2142 } else { 2143 @compileError("unsupported field type: " ++ @typeName(Field)); 2144 } 2145 }, 2146 .void => return true, 2147 .bool, .int, .@"enum" => return a == b, 2148 .@"union" => { 2149 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 2150 return switch (a) { 2151 inline else => |value, tag| fieldEqual(@TypeOf(value), value, @field(b, @tagName(tag))), 2152 }; 2153 }, 2154 .@"struct" => return fieldsEqual(Field, a, b), 2155 .optional => |optional_info| { 2156 if (a) |a_value| { 2157 if (b) |b_value| return fieldEqual(optional_info.child, a_value, b_value); 2158 return false; 2159 } 2160 return b == null; 2161 }, 2162 else => @compileError("unsupported field type: " ++ @typeName(Field)), 2163 } 2164 } 2165 2166 // 2167 // writing to xitdb 2168 // 2169 2170 pub fn upsert( 2171 comptime T: type, 2172 comptime DB: type, 2173 comptime hash_kind: hash.HashKind, 2174 map: DB.HashMap(.read_write), 2175 event: T, 2176 ) !void { 2177 switch (@typeInfo(T)) { 2178 .@"struct" => |struct_info| { 2179 inline for (struct_info.fields) |field| { 2180 try upsertField(DB, hash_kind, map, field.name, field.type, @field(event, field.name)); 2181 } 2182 }, 2183 else => @compileError("upsert expects a struct"), 2184 } 2185 } 2186 2187 fn upsertField( 2188 comptime DB: type, 2189 comptime hash_kind: hash.HashKind, 2190 map: DB.HashMap(.read_write), 2191 comptime field_name: []const u8, 2192 comptime Field: type, 2193 value: Field, 2194 ) !void { 2195 const key = hash.hashInt(hash_kind, field_name); 2196 2197 switch (@typeInfo(Field)) { 2198 .pointer => |pointer_info| { 2199 if (pointer_info.size == .slice and pointer_info.child == u8) { 2200 try upsertBytes(DB, hash_kind, map, key, value); 2201 } else { 2202 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2203 } 2204 }, 2205 .array => |array_info| { 2206 if (array_info.child == u8) { 2207 try upsertBytes(DB, hash_kind, map, key, &value); 2208 } else { 2209 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2210 } 2211 }, 2212 .int => |int_info| switch (int_info.signedness) { 2213 .unsigned => { 2214 if (try map.getCursor(key)) |value_cursor| { 2215 if (try value_cursor.readUint() == value) { 2216 return; 2217 } 2218 } 2219 2220 try map.put(key, .{ .uint = value }); 2221 }, 2222 .signed => { 2223 if (try map.getCursor(key)) |value_cursor| { 2224 if (try value_cursor.readInt() == value) { 2225 return; 2226 } 2227 } 2228 2229 try map.put(key, .{ .int = value }); 2230 }, 2231 }, 2232 .bool => { 2233 const bytes = if (value) "true" else "false"; 2234 if (try map.getCursor(key)) |value_cursor| { 2235 var buffer: [5]u8 = undefined; 2236 const existing = try value_cursor.readBytesObject(&buffer); 2237 if (existing.format_tag) |format_tag| { 2238 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 2239 return; 2240 } 2241 } 2242 } 2243 2244 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 2245 }, 2246 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 2247 .@"union" => switch (value) { 2248 inline else => |payload, tag| { 2249 if (@TypeOf(payload) == void) { 2250 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 2251 } else { 2252 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 2253 if (try map.getCursor(key)) |cursor| { 2254 switch (cursor.slot().tag) { 2255 .hash_map => { 2256 const variants = try DB.HashMap(.read_only).init(cursor); 2257 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 2258 }, 2259 else => try map.put(key, .{ .slot = null }), 2260 } 2261 } 2262 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 2263 if (@typeInfo(@TypeOf(payload)) == .optional) { 2264 // keep the variant even when its payload is null 2265 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 2266 } 2267 if (try variants.getCursor(tag_key)) |cursor| { 2268 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 2269 } 2270 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 2271 } 2272 }, 2273 }, 2274 .optional => |optional_info| { 2275 // a missing key is null 2276 if (value) |child| { 2277 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 2278 } else { 2279 _ = try map.remove(key); 2280 } 2281 }, 2282 .@"struct" => try upsert( 2283 Field, 2284 DB, 2285 hash_kind, 2286 try DB.HashMap(.read_write).init(try map.putCursor(key)), 2287 value, 2288 ), 2289 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 2290 } 2291 } 2292 2293 fn upsertBytes( 2294 comptime DB: type, 2295 comptime hash_kind: hash.HashKind, 2296 map: DB.HashMap(.read_write), 2297 key: hash.HashInt(hash_kind), 2298 value: []const u8, 2299 ) !void { 2300 var existing_cursor_maybe = try map.getCursor(key); 2301 if (existing_cursor_maybe) |*existing_cursor| { 2302 if (try bytesEqual(DB, existing_cursor, value)) { 2303 return; 2304 } 2305 } 2306 2307 try map.put(key, .{ .bytes = value }); 2308 } 2309 2310 fn bytesEqual( 2311 comptime DB: type, 2312 cursor: *DB.Cursor(.read_only), 2313 value: []const u8, 2314 ) !bool { 2315 switch (cursor.slot().tag) { 2316 .bytes, .short_bytes => {}, 2317 else => return false, 2318 } 2319 var read_buffer: [1024]u8 = undefined; 2320 var reader = try cursor.reader(&read_buffer); 2321 if (reader.size != value.len) { 2322 return false; 2323 } 2324 2325 var chunk_buffer: [1024]u8 = undefined; 2326 var offset: usize = 0; 2327 while (offset < value.len) { 2328 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2329 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2330 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2331 return false; 2332 } 2333 offset += chunk_len; 2334 } 2335 2336 return true; 2337 } 2338 2339 // 2340 // CommitIterator 2341 // 2342 // differs from xit's ObjectIterator in a few ways: 2343 // 2344 // 1. iterates parents first, rather than children first 2345 // 2. `next` only returns the object id, not the full Object 2346 // 3. stores its temporary state in a file on disk to avoid OOMs 2347 // 2348 2349 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2350 return struct { 2351 const DB = EventDB(repo_opts.hash); 2352 // a plain file is used for the scratch db because each write outside 2353 // a transaction is synced anyway, so buffering would gain nothing 2354 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2355 const db_name = "haxy-repo-events.db"; 2356 2357 repo_dir: std.Io.Dir, 2358 db_file: std.Io.File, 2359 db: *TempDB, 2360 // map of each object id to its children 2361 parent_to_children: TempDB.HashMap(.read_write), 2362 // tracking pending parents is necessary because a child can only 2363 // be returned after all of its unconsumed parents have been returned 2364 child_to_pending_parents: TempDB.HashMap(.read_write), 2365 // queue of object ids that are ready to be returned from `next` 2366 ready_queue: TempDB.ArrayList(.read_write), 2367 ready_queue_index: u64, 2368 // the object id at the tip of this branch 2369 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2370 // the object id furthest back in the history that hasn't been consumed 2371 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2372 2373 pub fn init( 2374 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2375 io: std.Io, 2376 allocator: std.mem.Allocator, 2377 consumed_object_ids: DB.HashMap(.read_only), 2378 ref: rf.Ref, 2379 ) !CommitIterator(repo_kind, repo_opts) { 2380 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2381 errdefer { 2382 db_file.close(io); 2383 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2384 } 2385 2386 // cursors point at the db, so it needs a stable address 2387 const db_ptr = try allocator.create(TempDB); 2388 errdefer allocator.destroy(db_ptr); 2389 // the db is scratch state that gets deleted after iteration, so 2390 // there is nothing worth fsyncing 2391 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2392 2393 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2394 2395 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2396 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2397 2398 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2399 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2400 2401 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2402 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2403 2404 var self = CommitIterator(repo_kind, repo_opts){ 2405 .repo_dir = state.core.repo_dir, 2406 .db_file = db_file, 2407 .db = db_ptr, 2408 .parent_to_children = parent_to_children, 2409 .child_to_pending_parents = child_to_pending_parents, 2410 .ready_queue = ready_queue, 2411 .ready_queue_index = 0, 2412 .newest_object_id = null, 2413 .oldest_object_id = null, 2414 }; 2415 errdefer self.deinit(io, allocator); 2416 2417 try self.collect(state, io, allocator, consumed_object_ids, ref); 2418 2419 return self; 2420 } 2421 2422 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2423 self.db_file.close(io); 2424 self.repo_dir.deleteFile(io, db_name) catch {}; 2425 allocator.destroy(self.db); 2426 } 2427 2428 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2429 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2430 2431 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2432 self.ready_queue_index += 1; 2433 2434 try self.enqueueChildren(&oid); 2435 return oid; 2436 } 2437 2438 fn collect( 2439 self: *CommitIterator(repo_kind, repo_opts), 2440 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2441 io: std.Io, 2442 allocator: std.mem.Allocator, 2443 consumed_object_ids: DB.HashMap(.read_only), 2444 ref: rf.Ref, 2445 ) !void { 2446 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2447 2448 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2449 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2450 var walk_queue_index: u64 = 0; 2451 2452 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2453 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2454 2455 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2456 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2457 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2458 try walk_queue.append(.{ .bytes = &head_oid }); 2459 2460 while (walk_queue_index < try walk_queue.count()) { 2461 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2462 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2463 2464 walk_queue_index += 1; 2465 2466 // if we've seen this object id, skip it 2467 if (null != try seen_object_ids.getCursor(oid_int)) { 2468 continue; 2469 } 2470 2471 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2472 2473 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2474 defer commit_object.deinit(); 2475 2476 // if this object id has already been consumed, skip it 2477 if (null != try consumed_object_ids.getCursor(oid_int)) { 2478 continue; 2479 } 2480 2481 if (self.newest_object_id == null) self.newest_object_id = oid; 2482 self.oldest_object_id = oid; 2483 2484 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2485 var pending_parent_count: u64 = 0; 2486 for (parent_oids) |*parent_oid| { 2487 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2488 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2489 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2490 2491 // if this object id has already been consumed, skip it 2492 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2493 continue; 2494 } 2495 2496 // if this object id hasn't already been seen, add it to the walk queue 2497 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2498 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2499 } 2500 2501 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2502 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2503 try children.put(oid_int, .{ .bytes = &oid }); 2504 pending_parent_count += 1; 2505 } 2506 2507 if (pending_parent_count == 0) { 2508 try self.ready_queue.append(.{ .bytes = &oid }); 2509 } else { 2510 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2511 } 2512 } 2513 } 2514 2515 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2516 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2517 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2518 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2519 var children_iter = try children.iterator(); 2520 2521 while (try children_iter.next()) |*child_cursor| { 2522 const kv_pair = try child_cursor.readKeyValuePair(); 2523 const child_oid_int = kv_pair.hash; 2524 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2525 const pending_parent_count = try pending_parent_cursor.readUint(); 2526 2527 if (pending_parent_count <= 1) { 2528 _ = try self.child_to_pending_parents.remove(child_oid_int); 2529 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2530 try self.ready_queue.append(.{ .bytes = &child_oid }); 2531 } else { 2532 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2533 } 2534 } 2535 } 2536 2537 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2538 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2539 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2540 _ = try oid_cursor.readBytes(&oid); 2541 return oid; 2542 } 2543 }; 2544 } 2545