╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1742 try conflict.put(hash.hashInt(hash_kind, their_record_key), .{ .slot = parent_slot }); 1743 if (parent_field_oids) |map| { 1744 try conflict.put(hash.hashInt(hash_kind, their_field_to_oid_key), .{ .slot = map.slot() }); 1745 } 1746 continue; 1747 } 1748 1749 // created_order never changes, so the parent keys this record's conflict 1750 // identically, and it carries over when the target has none, unless the 1751 // parent's entry is unchanged from the baseline, meaning the target 1752 // inherited it too and resolving it removed it here. 1753 if (parent_conflicts) |map| { 1754 if (null == try conflicts.getCursor(&merged_key)) { 1755 if (try map.getSlot(&merged_key)) |conflict_slot| { 1756 const baseline_conflict_slot = if (baseline_conflicts) |baseline_map| try baseline_map.getSlot(&merged_key) else null; 1757 const resolved = if (baseline_conflict_slot) |slot| slot.eql(conflict_slot) else false; 1758 if (!resolved) try conflicts.put(&merged_key, .{ .slot = conflict_slot }); 1759 } 1760 } 1761 } 1762 } 1763 } 1764 1765 // a comment, attachment or assignment a write names under a thread 1766 pub const ThreadChild = union(enum) { 1767 comment: *const [event_id_size]u8, 1768 attach: *const [event_id_size]u8, 1769 assign: *const [event_id_size]u8, 1770 }; 1771 1772 // whether `thread_id` names a thread of `thread_kind` in the repo's events, and 1773 // `child`, when given, belongs to it. a caller's url can claim any kind for any 1774 // id, so every write under a thread checks its claim here first. 1775 pub fn threadHolds( 1776 comptime repo_kind: rp.RepoKind, 1777 comptime repo_opts: rp.RepoOpts(repo_kind), 1778 io: std.Io, 1779 allocator: std.mem.Allocator, 1780 repo: *rp.Repo(repo_kind, repo_opts), 1781 thread_kind: EventKind, 1782 thread_id: *const [event_id_size]u8, 1783 child_maybe: ?ThreadChild, 1784 ) !bool { 1785 const DB = EventDB(repo_opts.hash); 1786 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; 1787 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 1788 const moment = (if (event_db_maybe) |*event_db| 1789 currentMomentFromDb(repo_opts.hash, event_db.db) 1790 else if (repo_kind == .git) 1791 return false 1792 else 1793 currentMoment(repo_opts, repo)) catch return false; 1794 1795 // only these kinds are threads 1796 switch (thread_kind) { 1797 .discuss, .issue, .patch => {}, 1798 else => return false, 1799 } 1800 const kinds_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, event_index_key)) orelse return false; 1801 const kinds = try DB.HashMap(.read_only).init(kinds_cursor); 1802 if ((try readEventKind(repo_opts.hash, kinds, thread_id)) != thread_kind) return false; 1803 1804 const child = child_maybe orelse return true; 1805 var arena = std.heap.ArenaAllocator.init(allocator); 1806 defer arena.deinit(); 1807 const thread_id_hex = std.fmt.bytesToHex(thread_id.*, .lower); 1808 switch (child) { 1809 .comment => |id| { 1810 const comment = (try readRecordSubset(Comment, struct { event: struct { thread_id: [event_id_size * 2]u8 } }, DB, repo_opts.hash, moment, &arena, id)) orelse return false; 1811 return std.mem.eql(u8, &comment.event.thread_id, &thread_id_hex); 1812 }, 1813 .attach => |id| { 1814 const attachment = (try readRecordSubset(Attachment, struct { event: struct { target_id: [event_id_size * 2]u8 } }, DB, repo_opts.hash, moment, &arena, id)) orelse return false; 1815 return std.mem.eql(u8, &attachment.event.target_id, &thread_id_hex); 1816 }, 1817 // only an active assignment can be removed 1818 .assign => |id| { 1819 const assignment = (try readRecordSubset(Assignment, struct { removed: bool, event: struct { thread_id: [event_id_size * 2]u8 } }, DB, repo_opts.hash, moment, &arena, id)) orelse return false; 1820 return !assignment.removed and std.mem.eql(u8, &assignment.event.thread_id, &thread_id_hex); 1821 }, 1822 } 1823 } 1824 1825 // remove an event a thread's page names: the thread itself, or a comment, 1826 // attachment or assignment, which must belong to it 1827 pub fn removeInThread( 1828 host: Host, 1829 comptime repo_kind: rp.RepoKind, 1830 comptime repo_opts: rp.RepoOpts(repo_kind), 1831 io: std.Io, 1832 allocator: std.mem.Allocator, 1833 repo: *rp.Repo(repo_kind, repo_opts), 1834 thread_kind: EventKind, 1835 thread_id: *const [event_id_size]u8, 1836 kind: EventKind, 1837 id: *const [event_id_size]u8, 1838 author: CommitAuthor, 1839 ) !void { 1840 const child_maybe: ?ThreadChild = switch (kind) { 1841 .comment => .{ .comment = id }, 1842 .attach => .{ .attach = id }, 1843 .assign => .{ .assign = id }, 1844 else => null, 1845 }; 1846 if (child_maybe) |child| { 1847 if (!try threadHolds(repo_kind, repo_opts, io, allocator, repo, thread_kind, thread_id, child)) return error.EventNotFound; 1848 } 1849 try remove(host, .repo, repo_kind, repo_opts, io, allocator, repo, id, kind, author); 1850 } 1851 1852 pub fn readFromRepo( 1853 comptime T: type, 1854 comptime repo_kind: rp.RepoKind, 1855 comptime repo_opts: rp.RepoOpts(repo_kind), 1856 io: std.Io, 1857 allocator: std.mem.Allocator, 1858 arena: *std.heap.ArenaAllocator, 1859 repo: *rp.Repo(repo_kind, repo_opts), 1860 id: *const [event_id_size]u8, 1861 ) !?T.Record { 1862 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; 1863 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 1864 const moment = (if (event_db_maybe) |*event_db| 1865 currentMomentFromDb(repo_opts.hash, event_db.db) 1866 else if (repo_kind == .git) 1867 return null 1868 else 1869 currentMoment(repo_opts, repo)) catch return null; 1870 return T.readById(EventDB(repo_opts.hash), repo_opts.hash, moment, arena, id); 1871 } 1872 1873 pub fn currentMoment( 1874 comptime repo_opts: rp.RepoOpts(.xit), 1875 repo: *rp.Repo(.xit, repo_opts), 1876 ) !rp.Repo(.xit, repo_opts).DB.HashMap(.read_only) { 1877 return currentMomentFromDb(repo_opts.hash, &repo.core.db); 1878 } 1879 1880 pub fn currentMomentFromDb( 1881 comptime hash_kind: hash.HashKind, 1882 db: *EventDB(hash_kind), 1883 ) !EventDB(hash_kind).HashMap(.read_only) { 1884 const DB = EventDB(hash_kind); 1885 const history = try DB.ArrayList(.read_only).init(db.rootCursor().readOnly()); 1886 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 1887 const moment = try DB.HashMap(.read_only).init(moment_cursor); 1888 return try currentMomentFromRepoMoment(hash_kind, moment); 1889 } 1890 1891 pub fn currentMomentFromRepoMoment( 1892 comptime hash_kind: hash.HashKind, 1893 moment: EventDB(hash_kind).HashMap(.read_only), 1894 ) !EventDB(hash_kind).HashMap(.read_only) { 1895 const DB = EventDB(hash_kind); 1896 const last_object_id_cursor = try moment.getCursor(hash.hashInt(hash_kind, last_object_id_key)) orelse return error.NotFound; 1897 var last_object_id: [hash.byteLen(hash_kind)]u8 = undefined; 1898 _ = try last_object_id_cursor.readBytes(&last_object_id); 1899 const haxy_history_cursor = try moment.getCursor(hash.hashInt(hash_kind, history_key)) orelse return error.NotFound; 1900 const haxy_history = try DB.ArrayList(.read_only).init(haxy_history_cursor); 1901 const haxy_moments_cursor = try haxy_history.getCursor(-1) orelse return error.NotFound; 1902 const haxy_moments = try DB.HashMap(.read_only).init(haxy_moments_cursor); 1903 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(hash_kind, &last_object_id)) orelse return error.NotFound; 1904 return try DB.HashMap(.read_only).init(haxy_moment_cursor); 1905 } 1906 1907 // build a sorted-set key from an order and event id. big-endian order makes 1908 // byte order match numeric order; the event id breaks ties. 1909 pub fn orderKey(order: u64, event_id: *const [event_id_size]u8) [@sizeOf(u64) + event_id_size]u8 { 1910 var key: [@sizeOf(u64) + event_id_size]u8 = undefined; 1911 std.mem.writeInt(u64, key[0..@sizeOf(u64)], order, .big); 1912 @memcpy(key[@sizeOf(u64)..], event_id); 1913 return key; 1914 } 1915 1916 pub fn orderKeyDesc(order: u64, event_id: *const [event_id_size]u8) [@sizeOf(u64) + event_id_size]u8 { 1917 return orderKey(std.math.maxInt(u64) - order, event_id); 1918 } 1919 1920 pub fn removedRecord( 1921 comptime T: type, 1922 comptime DB: type, 1923 comptime hash_kind: hash.HashKind, 1924 haxy_moment: DB.HashMap(.read_only), 1925 existing_maybe: ?T, 1926 ) !T { 1927 var record = existing_maybe orelse return error.EventNotFound; 1928 record.removed = true; 1929 const order_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, event_order_key)) orelse return error.CursorNotFound; 1930 record.updated_order = try order_cursor.readUint(); 1931 return record; 1932 } 1933 1934 // add or refresh one entry in the global logical-event index 1935 pub fn indexEvent( 1936 comptime DB: type, 1937 comptime hash_kind: hash.HashKind, 1938 haxy_moment: DB.HashMap(.read_write), 1939 event_id: *const [event_id_size]u8, 1940 kind: EventKind, 1941 existing_maybe: anytype, 1942 record: anytype, 1943 ) !void { 1944 const event_key = hash.hashInt(hash_kind, event_id); 1945 const kind_map_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, event_index_key)); 1946 const kind_map = try DB.HashMap(.read_write).init(kind_map_cursor); 1947 try kind_map.put(event_key, .{ .bytes = @tagName(kind) }); 1948 1949 const active_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, active_event_id_set_key)); 1950 const active = try DB.SortedSet(.read_write).init(active_cursor); 1951 const removed_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, removed_event_id_set_key)); 1952 const removed_set = try DB.SortedSet(.read_write).init(removed_cursor); 1953 1954 if (existing_maybe) |existing| { 1955 const old_key = orderKeyDesc(existing.updated_order, event_id); 1956 _ = try active.remove(&old_key); 1957 _ = try removed_set.remove(&old_key); 1958 } 1959 1960 const order_key = orderKeyDesc(record.updated_order, event_id); 1961 if (record.removed) { 1962 try removed_set.put(&order_key); 1963 } else { 1964 try active.put(&order_key); 1965 } 1966 } 1967 1968 pub fn touchThread( 1969 comptime DB: type, 1970 comptime hash_kind: hash.HashKind, 1971 haxy_moment: DB.HashMap(.read_write), 1972 thread_id: *const [event_id_size]u8, 1973 order: u64, 1974 arena: *std.heap.ArenaAllocator, 1975 ) !void { 1976 const kind_map_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, event_index_key)) orelse return; 1977 const kind_map = try DB.HashMap(.read_only).init(kind_map_cursor); 1978 const kind = (try readEventKind(hash_kind, kind_map, thread_id)) orelse return; 1979 switch (kind) { 1980 .discuss => try Discussion.touch(DB, hash_kind, haxy_moment, thread_id, order, arena), 1981 else => {}, 1982 } 1983 } 1984 1985 pub fn readEventKind( 1986 comptime hash_kind: hash.HashKind, 1987 kind_map: anytype, 1988 id: *const [event_id_size]u8, 1989 ) !?EventKind { 1990 const cursor = try kind_map.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 1991 var buffer: [64]u8 = undefined; 1992 return std.meta.stringToEnum(EventKind, try cursor.readBytes(&buffer)) orelse error.InvalidEventKind; 1993 } 1994 1995 // the email of the event's author, or null when the id names no authored event 1996 // of this kind 1997 pub fn readAuthorEmail( 1998 comptime repo_kind: rp.RepoKind, 1999 comptime repo_opts: rp.RepoOpts(repo_kind), 2000 io: std.Io, 2001 allocator: std.mem.Allocator, 2002 arena: *std.heap.ArenaAllocator, 2003 repo: *rp.Repo(repo_kind, repo_opts), 2004 kind: EventKind, 2005 id: *const [event_id_size]u8, 2006 ) !?[]const u8 { 2007 const record_map_key = switch (kind) { 2008 .discuss => Discussion.record_map_key, 2009 .issue => Issue.record_map_key, 2010 .comment => Comment.record_map_key, 2011 .attach => Attachment.record_map_key, 2012 .assign => Assignment.record_map_key, 2013 .patchrev => PatchRev.record_map_key, 2014 .patch => Patch.record_map_key, 2015 .user, .repo, .fork, .grant, .notify => return null, 2016 }; 2017 2018 const DB = EventDB(repo_opts.hash); 2019 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; 2020 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 2021 const moment = (if (event_db_maybe) |*event_db| 2022 currentMomentFromDb(repo_opts.hash, event_db.db) 2023 else if (repo_kind == .git) 2024 return null 2025 else 2026 currentMoment(repo_opts, repo)) catch return null; 2027 2028 const records_cursor = (try moment.getCursor(hash.hashInt(repo_opts.hash, record_map_key))) orelse return null; 2029 const records = try DB.HashMap(.read_only).init(records_cursor); 2030 const record_cursor = (try records.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return null; 2031 const record = try DB.HashMap(.read_only).init(record_cursor); 2032 return try readField(?[]const u8, DB, repo_opts.hash, arena, try record.getCursor(hash.hashInt(repo_opts.hash, "author_email"))); 2033 } 2034 2035 // the event id embedded in a set entry's order key 2036 pub fn readOrderKeyId(comptime DB: type, kv_pair_cursor: DB.Cursor(.read_only)) ![event_id_size]u8 { 2037 var cursor = kv_pair_cursor; 2038 const kv_pair = try cursor.readKeyValuePair(); 2039 var order_key: [@sizeOf(u64) + event_id_size]u8 = undefined; 2040 _ = try kv_pair.key_cursor.readBytes(&order_key); 2041 return order_key[@sizeOf(u64)..][0..event_id_size].*; 2042 } 2043 2044 // split a pushed "<owner>:<repo>" path into its two components, or null if it 2045 // isn't exactly two non-empty parts with no "/". 2046 pub fn parseOwnerRepoPath(path: []const u8) ?struct { owner: []const u8, name: []const u8 } { 2047 if (std.mem.indexOfScalar(u8, path, '/') != null) return null; 2048 var it = std.mem.splitScalar(u8, path, ':'); 2049 const owner = it.next() orelse return null; 2050 const name = it.next() orelse return null; 2051 if (it.next() != null) return null; 2052 if (owner.len == 0 or name.len == 0) return null; 2053 2054 return .{ .owner = owner, .name = name }; 2055 } 2056 2057 pub const CreateRepoOptions = struct { 2058 read_access: Repo.Access = .private, 2059 }; 2060 2061 // the ids that locate a repo's on-disk directory 2062 pub const RepoLocation = struct { 2063 owner_id: [event_id_size]u8, 2064 repo_id: [event_id_size]u8, 2065 }; 2066 2067 // resolve a pushed `<owner>:<repo>` to the ids that locate its on-disk directory 2068 pub fn resolveOrCreateRepo( 2069 io: std.Io, 2070 allocator: std.mem.Allocator, 2071 users_dir: []const u8, 2072 admin_repo_path: []const u8, 2073 owner_name: []const u8, 2074 repo_name: []const u8, 2075 create_options_maybe: ?CreateRepoOptions, 2076 ) !?RepoLocation { 2077 var admin_repo = rp.Repo(.xit, admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 2078 error.RepoNotFound => return null, 2079 else => |e| return e, 2080 }; 2081 defer admin_repo.deinit(io, allocator); 2082 2083 var arena = std.heap.ArenaAllocator.init(allocator); 2084 defer arena.deinit(); 2085 2086 const admin_moment = try currentMoment(admin_repo_opts, &admin_repo); 2087 2088 // an unknown owner can't own a repo, so the push is rejected 2089 const owner_user_id = (try User.readIdByName(AdminDB, admin_repo_opts.hash, admin_moment, owner_name)) orelse return null; 2090 var user_repo = (try openUserRepo(io, allocator, users_dir, &owner_user_id)) orelse return null; 2091 defer user_repo.deinit(io, allocator); 2092 2093 // an already-registered repo reuses its event id, so a re-push (or a clone) 2094 // lands in the same repo 2095 if (try userMoment(&user_repo)) |moment| { 2096 if (try Repo.readByName(UserDB, user_repo_opts.hash, moment, &arena, repo_name)) |found| { 2097 return .{ .owner_id = owner_user_id, .repo_id = found.event_id }; 2098 } 2099 } 2100 2101 const create_options = create_options_maybe orelse return null; 2102 2103 const owner = (try User.readById(AdminDB, admin_repo_opts.hash, admin_moment, &arena, &owner_user_id)) orelse unreachable; 2104 2105 var id_bytes: [event_id_size]u8 = undefined; 2106 io.random(&id_bytes); 2107 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); 2108 return .{ .owner_id = owner_user_id, .repo_id = id_bytes }; 2109 } 2110 2111 // create a bare repo with `hash_kind` and register it in its owner's user repo 2112 pub fn createRepo( 2113 io: std.Io, 2114 allocator: std.mem.Allocator, 2115 users_dir: []const u8, 2116 owner_id: *const [event_id_size]u8, 2117 owner: CommitAuthor, 2118 name: []const u8, 2119 description: []const u8, 2120 read_access: Repo.Access, 2121 hash_kind: hash.HashKind, 2122 ) !RepoLocation { 2123 try Repo.validateName(name); 2124 var user_repo = (try openUserRepo(io, allocator, users_dir, owner_id)) orelse return error.NotFound; 2125 defer user_repo.deinit(io, allocator); 2126 if (try userMoment(&user_repo)) |moment| { 2127 var arena = std.heap.ArenaAllocator.init(allocator); 2128 defer arena.deinit(); 2129 if (try Repo.readByName(UserDB, user_repo_opts.hash, moment, &arena, name) != null) return error.NameTaken; 2130 } 2131 2132 var id_bytes: [event_id_size]u8 = undefined; 2133 io.random(&id_bytes); 2134 2135 // the directory comes first so a listed repo always has one 2136 { 2137 const path = try repoPath(allocator, users_dir, owner_id, &id_bytes); 2138 defer allocator.free(path); 2139 switch (hash_kind) { 2140 inline else => |kind| { 2141 var repo = try rp.Repo(.xit, .{ .hash = kind }).init(io, allocator, .{ .path = path, .bare = true }); 2142 defer repo.deinit(io, allocator); 2143 }, 2144 } 2145 } 2146 2147 try writeRepoEvent(io, allocator, users_dir, &user_repo, owner_id, owner, &id_bytes, name, description, read_access); 2148 return .{ .owner_id = owner_id.*, .repo_id = id_bytes }; 2149 } 2150 2151 // change the name, description, read access, and tab roles of the repo 2152 // named `old_name` in its owner's user repo 2153 pub fn updateRepo( 2154 io: std.Io, 2155 allocator: std.mem.Allocator, 2156 users_dir: []const u8, 2157 owner_id: *const [event_id_size]u8, 2158 author: CommitAuthor, 2159 old_name: []const u8, 2160 name: []const u8, 2161 description: []const u8, 2162 read_access: Repo.Access, 2163 discuss_role: ?Repo.Role, 2164 issue_role: ?Repo.Role, 2165 patch_role: ?Repo.Role, 2166 ) !void { 2167 try Repo.validateName(name); 2168 var user_repo = (try openUserRepo(io, allocator, users_dir, owner_id)) orelse return error.NotFound; 2169 defer user_repo.deinit(io, allocator); 2170 var arena = std.heap.ArenaAllocator.init(allocator); 2171 defer arena.deinit(); 2172 const moment = (try userMoment(&user_repo)) orelse return error.NotFound; 2173 const found = (try Repo.readByName(UserDB, user_repo_opts.hash, moment, &arena, old_name)) orelse return error.NotFound; 2174 if (try Repo.readByName(UserDB, user_repo_opts.hash, moment, &arena, name)) |taken| { 2175 if (!std.mem.eql(u8, &taken.event_id, &found.event_id)) return error.NameTaken; 2176 } 2177 2178 var event = found.repo.event; 2179 event.name = name; 2180 event.description = description; 2181 event.read_access = read_access; 2182 event.discuss_role = discuss_role; 2183 event.issue_role = issue_role; 2184 event.patch_role = patch_role; 2185 try consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, user_repo_opts, io, allocator, &user_repo, events_ref, &[_]EventWithId{.{ 2186 .id = std.fmt.bytesToHex(found.event_id, .lower), 2187 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2188 .author = author, 2189 .event = .{ .repo = event }, 2190 }}); 2191 } 2192 2193 // grant `user_id` `role` on the repo `repo_id` in its owner's user repo, 2194 // replacing any role they held 2195 pub fn grantRole( 2196 io: std.Io, 2197 allocator: std.mem.Allocator, 2198 users_dir: []const u8, 2199 owner_id: *const [event_id_size]u8, 2200 repo_id: *const [event_id_size]u8, 2201 user_id: *const [event_id_size]u8, 2202 role: Repo.Role, 2203 author: CommitAuthor, 2204 ) !void { 2205 var user_repo = (try openUserRepo(io, allocator, users_dir, owner_id)) orelse return error.NotFound; 2206 defer user_repo.deinit(io, allocator); 2207 try consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, user_repo_opts, io, allocator, &user_repo, events_ref, &[_]EventWithId{.{ 2208 .id = std.fmt.bytesToHex(Grant.idOf(repo_id, user_id), .lower), 2209 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2210 .author = author, 2211 .event = .{ .grant = .{ .target_id = std.fmt.bytesToHex(repo_id.*, .lower), .user_id = std.fmt.bytesToHex(user_id.*, .lower), .role = role } }, 2212 }}); 2213 } 2214 2215 // revoke `user_id`'s grant on the repo `repo_id` in its owner's user repo 2216 pub fn revokeRole( 2217 io: std.Io, 2218 allocator: std.mem.Allocator, 2219 users_dir: []const u8, 2220 owner_id: *const [event_id_size]u8, 2221 repo_id: *const [event_id_size]u8, 2222 user_id: *const [event_id_size]u8, 2223 author: CommitAuthor, 2224 ) !void { 2225 var user_repo = (try openUserRepo(io, allocator, users_dir, owner_id)) orelse return error.NotFound; 2226 defer user_repo.deinit(io, allocator); 2227 // a revoked grant stays in the kind index, which is all `remove` checks 2228 { 2229 var arena = std.heap.ArenaAllocator.init(allocator); 2230 defer arena.deinit(); 2231 const moment = (try userMoment(&user_repo)) orelse return error.EventNotFound; 2232 if (try Grant.readRole(UserDB, user_repo_opts.hash, moment, &arena, repo_id, user_id) == null) return error.EventNotFound; 2233 } 2234 try remove(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, user_repo_opts, io, allocator, &user_repo, &Grant.idOf(repo_id, user_id), .grant, author); 2235 } 2236 2237 // add `branch` to the protected branches of the repo `repo_id` in its owner's 2238 // user repo, or take it out when `protect` is false. taking out a branch that 2239 // isn't protected writes nothing. 2240 pub fn protectBranch( 2241 io: std.Io, 2242 allocator: std.mem.Allocator, 2243 users_dir: []const u8, 2244 owner_id: *const [event_id_size]u8, 2245 repo_id: *const [event_id_size]u8, 2246 branch: []const u8, 2247 protect: bool, 2248 author: CommitAuthor, 2249 ) !void { 2250 var user_repo = (try openUserRepo(io, allocator, users_dir, owner_id)) orelse return error.NotFound; 2251 defer user_repo.deinit(io, allocator); 2252 var arena = std.heap.ArenaAllocator.init(allocator); 2253 defer arena.deinit(); 2254 const aa = arena.allocator(); 2255 const moment = (try userMoment(&user_repo)) orelse return error.NotFound; 2256 const repo = (try Repo.readById(UserDB, user_repo_opts.hash, moment, &arena, repo_id)) orelse return error.NotFound; 2257 if (repo.removed) return error.NotFound; 2258 2259 var branches: std.ArrayList(u8) = .empty; 2260 var count: usize = 0; 2261 var held = false; 2262 for (try repo.event.protectedBranchNames(aa)) |name| { 2263 if (std.mem.eql(u8, name, branch)) { 2264 held = true; 2265 if (!protect) continue; 2266 } 2267 if (branches.items.len > 0) try branches.append(aa, '\n'); 2268 try branches.appendSlice(aa, name); 2269 count += 1; 2270 } 2271 if (protect) { 2272 if (held) return error.BranchProtected; 2273 if (count >= Repo.protected_branch_max_count) return error.TooManyProtectedBranches; 2274 if (branches.items.len > 0) try branches.append(aa, '\n'); 2275 try branches.appendSlice(aa, branch); 2276 } else if (!held) return; 2277 2278 var event = repo.event; 2279 event.protected_branches = branches.items; 2280 try consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, user_repo_opts, io, allocator, &user_repo, events_ref, &[_]EventWithId{.{ 2281 .id = std.fmt.bytesToHex(repo_id.*, .lower), 2282 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2283 .author = author, 2284 .event = .{ .repo = event }, 2285 }}); 2286 } 2287 2288 // create a user and their user repo, returning the new user's id 2289 pub fn createUser( 2290 io: std.Io, 2291 allocator: std.mem.Allocator, 2292 users_dir: []const u8, 2293 admin_repo: *rp.Repo(.xit, admin_repo_opts), 2294 name: []const u8, 2295 email: []const u8, 2296 password: []const u8, 2297 password_again: []const u8, 2298 ) ![event_id_size]u8 { 2299 try User.validateName(name); 2300 if (email.len == 0) return error.EmailEmpty; 2301 if (password.len == 0) return error.PasswordEmpty; 2302 if (!std.mem.eql(u8, password, password_again)) return error.PasswordMismatch; 2303 // a fresh admin repo has no moment until its first event 2304 if (currentMoment(admin_repo_opts, admin_repo)) |moment| { 2305 if (try User.readIdByName(AdminDB, admin_repo_opts.hash, moment, name) != null) return error.NameTaken; 2306 var arena = std.heap.ArenaAllocator.init(allocator); 2307 defer arena.deinit(); 2308 if (try User.readByEmail(AdminDB, admin_repo_opts.hash, moment, &arena, email) != null) return error.EmailTaken; 2309 } else |err| switch (err) { 2310 error.NotFound => {}, 2311 else => |e| return e, 2312 } 2313 2314 var password_hash_buf: [User.password_hash_max_len]u8 = undefined; 2315 const password_hash = try User.hashPassword(password, &password_hash_buf, io); 2316 2317 var id_bytes: [event_id_size]u8 = undefined; 2318 io.random(&id_bytes); 2319 2320 // the user repo comes first so a listed user can always create repos 2321 { 2322 const path = try userRepoPath(allocator, users_dir, &id_bytes); 2323 defer allocator.free(path); 2324 var user_repo = try initUserRepo(io, allocator, path); 2325 defer user_repo.deinit(io, allocator); 2326 } 2327 2328 try consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, admin_repo_opts, io, allocator, admin_repo, events_ref, &[_]EventWithId{.{ 2329 .id = std.fmt.bytesToHex(id_bytes, .lower), 2330 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2331 .author = .{ .name = name, .email = email }, 2332 .event = .{ .user = .{ .name = name, .email = email, .password_hash = password_hash } }, 2333 }}); 2334 return id_bytes; 2335 } 2336 2337 // change a user's email, and their password when the password fields are 2338 // filled in 2339 pub fn updateUser( 2340 io: std.Io, 2341 allocator: std.mem.Allocator, 2342 users_dir: []const u8, 2343 admin_repo: *rp.Repo(.xit, admin_repo_opts), 2344 user_id: *const [event_id_size]u8, 2345 email: []const u8, 2346 current_password: []const u8, 2347 new_password: []const u8, 2348 new_password_again: []const u8, 2349 ) !void { 2350 if (email.len == 0) return error.EmailEmpty; 2351 var arena = std.heap.ArenaAllocator.init(allocator); 2352 defer arena.deinit(); 2353 const moment = try currentMoment(admin_repo_opts, admin_repo); 2354 const user = (try User.readById(AdminDB, admin_repo_opts.hash, moment, &arena, user_id)) orelse return error.NotFound; 2355 if (user.removed) return error.NotFound; 2356 if (!std.mem.eql(u8, email, user.event.email) and try User.readByEmail(AdminDB, admin_repo_opts.hash, moment, &arena, email) != null) return error.EmailTaken; 2357 2358 var event = user.event; 2359 event.email = email; 2360 var password_hash_buf: [User.password_hash_max_len]u8 = undefined; 2361 if (current_password.len + new_password.len + new_password_again.len > 0) { 2362 if (!User.verifyPassword(user.event.password_hash, current_password)) return error.WrongPassword; 2363 if (new_password.len == 0) return error.PasswordEmpty; 2364 if (!std.mem.eql(u8, new_password, new_password_again)) return error.PasswordMismatch; 2365 event.password_hash = try User.hashPassword(new_password, &password_hash_buf, io); 2366 } 2367 2368 try consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, admin_repo_opts, io, allocator, admin_repo, events_ref, &[_]EventWithId{.{ 2369 .id = std.fmt.bytesToHex(user_id.*, .lower), 2370 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2371 .author = .{ .name = event.name, .email = email }, 2372 .event = .{ .user = event }, 2373 }}); 2374 } 2375 2376 // add an authorized_keys line to the user's ssh keys 2377 pub fn addUserKey( 2378 io: std.Io, 2379 allocator: std.mem.Allocator, 2380 users_dir: []const u8, 2381 admin_repo: *rp.Repo(.xit, admin_repo_opts), 2382 user_id: *const [event_id_size]u8, 2383 key_text: []const u8, 2384 ) !void { 2385 const key = std.mem.trim(u8, key_text, " \t\r"); 2386 // the keys are stored one per line 2387 if (std.mem.indexOfScalar(u8, key, '\n') != null) return error.InvalidKey; 2388 const fingerprint = User.fingerprintOfKey(key) orelse return error.InvalidKey; 2389 // the key types the ssh server verifies 2390 const key_type = std.mem.sliceTo(key, ' '); 2391 if (!std.mem.eql(u8, key_type, "ssh-ed25519") and !std.mem.eql(u8, key_type, "ssh-rsa")) return error.UnsupportedKey; 2392 try writeUserKeys(io, allocator, users_dir, admin_repo, user_id, &fingerprint, key); 2393 } 2394 2395 // remove the user's ssh key with this fingerprint. removing a key they don't 2396 // hold writes nothing. 2397 pub fn removeUserKey( 2398 io: std.Io, 2399 allocator: std.mem.Allocator, 2400 users_dir: []const u8, 2401 admin_repo: *rp.Repo(.xit, admin_repo_opts), 2402 user_id: *const [event_id_size]u8, 2403 fingerprint: *const [User.fingerprint_len]u8, 2404 ) !void { 2405 try writeUserKeys(io, allocator, users_dir, admin_repo, user_id, fingerprint, null); 2406 } 2407 2408 // rewrite the user's ssh keys with `added` appended, or without the key with 2409 // `fingerprint` when nothing is added 2410 fn writeUserKeys( 2411 io: std.Io, 2412 allocator: std.mem.Allocator, 2413 users_dir: []const u8, 2414 admin_repo: *rp.Repo(.xit, admin_repo_opts), 2415 user_id: *const [event_id_size]u8, 2416 fingerprint: *const [User.fingerprint_len]u8, 2417 added: ?[]const u8, 2418 ) !void { 2419 var arena = std.heap.ArenaAllocator.init(allocator); 2420 defer arena.deinit(); 2421 const moment = try currentMoment(admin_repo_opts, admin_repo); 2422 const user = (try User.readById(AdminDB, admin_repo_opts.hash, moment, &arena, user_id)) orelse return error.NotFound; 2423 if (user.removed) return error.NotFound; 2424 2425 var keys: std.ArrayList(u8) = .empty; 2426 var count: usize = 0; 2427 var held = false; 2428 var lines = std.mem.splitScalar(u8, user.event.ssh_keys, '\n'); 2429 while (lines.next()) |line| { 2430 const line_fingerprint = User.fingerprintOfKey(line) orelse continue; 2431 if (std.mem.eql(u8, &line_fingerprint, fingerprint)) { 2432 held = true; 2433 if (added == null) continue; 2434 } 2435 if (keys.items.len > 0) try keys.append(arena.allocator(), '\n'); 2436 try keys.appendSlice(arena.allocator(), line); 2437 count += 1; 2438 } 2439 if (added) |key| { 2440 if (held) return error.KeyAdded; 2441 if (count >= User.ssh_key_max_count) return error.TooManyKeys; 2442 if (keys.items.len > 0) try keys.append(arena.allocator(), '\n'); 2443 try keys.appendSlice(arena.allocator(), key); 2444 } else if (!held) return; 2445 2446 var event = user.event; 2447 event.ssh_keys = keys.items; 2448 try consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, admin_repo_opts, io, allocator, admin_repo, events_ref, &[_]EventWithId{.{ 2449 .id = std.fmt.bytesToHex(user_id.*, .lower), 2450 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2451 .author = .{ .name = event.name, .email = event.email }, 2452 .event = .{ .user = event }, 2453 }}); 2454 } 2455 2456 // consume a new repo's event into its owner's user repo and mark it active 2457 fn writeRepoEvent( 2458 io: std.Io, 2459 allocator: std.mem.Allocator, 2460 users_dir: []const u8, 2461 user_repo: *rp.Repo(.xit, user_repo_opts), 2462 owner_id: *const [event_id_size]u8, 2463 owner: CommitAuthor, 2464 id_bytes: *const [event_id_size]u8, 2465 name: []const u8, 2466 description: []const u8, 2467 read_access: Repo.Access, 2468 ) !void { 2469 const timestamp: u64 = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()); 2470 try consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, user_repo_opts, io, allocator, user_repo, events_ref, &[_]EventWithId{.{ 2471 .id = std.fmt.bytesToHex(id_bytes.*, .lower), 2472 .timestamp = timestamp, 2473 .author = owner, 2474 .event = .{ .repo = .{ 2475 .user_id = std.fmt.bytesToHex(owner_id.*, .lower), 2476 .name = name, 2477 .description = description, 2478 .read_access = read_access, 2479 } }, 2480 }}); 2481 bumpRepoActivity(io, allocator, users_dir, owner_id, id_bytes, timestamp) catch |err| { 2482 std.log.warn("failed to record repo activity: {s}", .{@errorName(err)}); 2483 }; 2484 } 2485 2486 // 2487 // reading from xitdb 2488 // 2489 2490 // read just `Fields`, a subset of `T.Record`, from the record with event id 2491 // `id`, or null when there is none 2492 pub fn readRecordSubset( 2493 comptime T: type, 2494 comptime Fields: type, 2495 comptime DB: type, 2496 comptime hash_kind: hash.HashKind, 2497 haxy_moment: DB.HashMap(.read_only), 2498 arena: *std.heap.ArenaAllocator, 2499 id: []const u8, 2500 ) !?Fields { 2501 comptime assertFieldSubset(Fields, T.Record); 2502 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, T.record_map_key)) orelse return null; 2503 const records = try DB.HashMap(.read_only).init(records_cursor); 2504 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 2505 return try read(Fields, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 2506 } 2507 2508 fn assertFieldSubset(comptime Sub: type, comptime Full: type) void { 2509 const info = @typeInfo(Sub).@"struct"; 2510 for (info.field_names, info.field_types) |field_name, field_type| { 2511 if (!@hasField(Full, field_name)) @compileError(@typeName(Full) ++ " has no field " ++ field_name); 2512 const FullField = @FieldType(Full, field_name); 2513 if (@typeInfo(field_type) == .@"struct") { 2514 assertFieldSubset(field_type, FullField); 2515 } else if (field_type != FullField) { 2516 @compileError(@typeName(Full) ++ "." ++ field_name ++ " is a " ++ @typeName(FullField)); 2517 } 2518 } 2519 } 2520 2521 pub fn read( 2522 comptime T: type, 2523 comptime DB: type, 2524 comptime hash_kind: hash.HashKind, 2525 arena: *std.heap.ArenaAllocator, 2526 map: DB.HashMap(.read_only), 2527 ) !T { 2528 var event: T = undefined; 2529 switch (@typeInfo(T)) { 2530 .@"struct" => |struct_info| { 2531 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2532 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)); 2533 @field(event, field_name) = try readField(field_type, DB, hash_kind, arena, cursor); 2534 } 2535 }, 2536 else => @compileError("read expects a struct"), 2537 } 2538 return event; 2539 } 2540 2541 fn readField( 2542 comptime Field: type, 2543 comptime DB: type, 2544 comptime hash_kind: hash.HashKind, 2545 arena: *std.heap.ArenaAllocator, 2546 cursor_maybe: ?DB.Cursor(.read_only), 2547 ) !Field { 2548 if (@typeInfo(Field) == .optional) { 2549 // a missing key or a null union payload is null 2550 const cursor = cursor_maybe orelse return null; 2551 if (cursor.slot().tag == .none) return null; 2552 return try readField(@typeInfo(Field).optional.child, DB, hash_kind, arena, cursor); 2553 } 2554 const cursor = cursor_maybe orelse return error.NotFound; 2555 switch (@typeInfo(Field)) { 2556 .pointer => |pointer_info| { 2557 if (pointer_info.size != .slice or pointer_info.child != u8 or !pointer_info.attrs.@"const") { 2558 @compileError("unsupported read field type: " ++ @typeName(Field)); 2559 } 2560 return try cursor.readBytesAlloc(arena.allocator(), null); 2561 }, 2562 .array => |array_info| { 2563 if (array_info.child != u8) @compileError("unsupported read field type: " ++ @typeName(Field)); 2564 var bytes: Field = undefined; 2565 if ((try cursor.readBytes(&bytes)).len != bytes.len) return error.InvalidByteArrayLength; 2566 return bytes; 2567 }, 2568 .bool => { 2569 var buffer: [5]u8 = undefined; 2570 const bytes = try cursor.readBytesObject(&buffer); 2571 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 2572 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 2573 if (std.mem.eql(u8, bytes.value, "true")) return true; 2574 if (std.mem.eql(u8, bytes.value, "false")) return false; 2575 return error.InvalidBool; 2576 }, 2577 .int => |int_info| return switch (int_info.signedness) { 2578 .unsigned => @intCast(try cursor.readUint()), 2579 .signed => @intCast(try cursor.readInt()), 2580 }, 2581 .@"enum" => return std.meta.stringToEnum(Field, try cursor.readBytesAlloc(arena.allocator(), null)) orelse error.InvalidEnumTag, 2582 .@"union" => |union_info| { 2583 if (union_info.tag_type == null) @compileError("unsupported read field type: " ++ @typeName(Field)); 2584 switch (cursor.slot().tag) { 2585 .bytes, .short_bytes => { 2586 const name = try cursor.readBytesAlloc(arena.allocator(), null); 2587 inline for (union_info.field_names, union_info.field_types) |variant_name, variant_type| { 2588 if (variant_type == void and std.mem.eql(u8, name, variant_name)) { 2589 return @unionInit(Field, variant_name, {}); 2590 } 2591 } 2592 return error.InvalidUnion; 2593 }, 2594 .hash_map => { 2595 const variants = try DB.HashMap(.read_only).init(cursor); 2596 var iter = try variants.iterator(); 2597 const entry = (try iter.next()) orelse return error.InvalidUnion; 2598 const pair = try entry.readKeyValuePair(); 2599 if (try iter.next() != null) return error.InvalidUnion; 2600 inline for (union_info.field_names, union_info.field_types) |variant_name, variant_type| { 2601 if (variant_type != void and pair.hash == hash.hashInt(hash_kind, variant_name)) { 2602 return @unionInit(Field, variant_name, try readField(variant_type, DB, hash_kind, arena, pair.value_cursor)); 2603 } 2604 } 2605 return error.InvalidUnion; 2606 }, 2607 else => return error.InvalidUnion, 2608 } 2609 }, 2610 .@"struct" => return try read(Field, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)), 2611 else => @compileError("unsupported read field type: " ++ @typeName(Field)), 2612 } 2613 } 2614 2615 // whether two events carry the same data 2616 fn fieldsEqual(comptime T: type, a: T, b: T) bool { 2617 switch (@typeInfo(T)) { 2618 .@"struct" => |struct_info| { 2619 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2620 if (!fieldEqual(field_type, @field(a, field_name), @field(b, field_name))) return false; 2621 } 2622 }, 2623 else => @compileError("fieldsEqual expects a struct"), 2624 } 2625 2626 return true; 2627 } 2628 2629 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 2630 switch (@typeInfo(Field)) { 2631 .pointer => |pointer_info| { 2632 if (pointer_info.size == .slice and pointer_info.child == u8) { 2633 return std.mem.eql(u8, a, b); 2634 } else { 2635 @compileError("unsupported field type: " ++ @typeName(Field)); 2636 } 2637 }, 2638 .array => |array_info| { 2639 if (array_info.child == u8) { 2640 return std.mem.eql(u8, &a, &b); 2641 } else { 2642 @compileError("unsupported field type: " ++ @typeName(Field)); 2643 } 2644 }, 2645 .void => return true, 2646 .bool, .int, .@"enum" => return a == b, 2647 .@"union" => { 2648 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 2649 return switch (a) { 2650 inline else => |value, tag| fieldEqual(@TypeOf(value), value, @field(b, @tagName(tag))), 2651 }; 2652 }, 2653 .@"struct" => return fieldsEqual(Field, a, b), 2654 .optional => |optional_info| { 2655 if (a) |a_value| { 2656 if (b) |b_value| return fieldEqual(optional_info.child, a_value, b_value); 2657 return false; 2658 } 2659 return b == null; 2660 }, 2661 else => @compileError("unsupported field type: " ++ @typeName(Field)), 2662 } 2663 } 2664 2665 // 2666 // writing to xitdb 2667 // 2668 2669 pub fn upsert( 2670 comptime T: type, 2671 comptime DB: type, 2672 comptime hash_kind: hash.HashKind, 2673 map: DB.HashMap(.read_write), 2674 event: T, 2675 ) !void { 2676 switch (@typeInfo(T)) { 2677 .@"struct" => |struct_info| { 2678 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2679 try upsertField(DB, hash_kind, map, field_name, field_type, @field(event, field_name)); 2680 } 2681 }, 2682 else => @compileError("upsert expects a struct"), 2683 } 2684 } 2685 2686 fn upsertField( 2687 comptime DB: type, 2688 comptime hash_kind: hash.HashKind, 2689 map: DB.HashMap(.read_write), 2690 comptime field_name: []const u8, 2691 comptime Field: type, 2692 value: Field, 2693 ) !void { 2694 const key = hash.hashInt(hash_kind, field_name); 2695 2696 switch (@typeInfo(Field)) { 2697 .pointer => |pointer_info| { 2698 if (pointer_info.size == .slice and pointer_info.child == u8) { 2699 try upsertBytes(DB, hash_kind, map, key, value); 2700 } else { 2701 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2702 } 2703 }, 2704 .array => |array_info| { 2705 if (array_info.child == u8) { 2706 try upsertBytes(DB, hash_kind, map, key, &value); 2707 } else { 2708 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2709 } 2710 }, 2711 .int => |int_info| switch (int_info.signedness) { 2712 .unsigned => { 2713 if (try map.getCursor(key)) |value_cursor| { 2714 if (try value_cursor.readUint() == value) { 2715 return; 2716 } 2717 } 2718 2719 try map.put(key, .{ .uint = value }); 2720 }, 2721 .signed => { 2722 if (try map.getCursor(key)) |value_cursor| { 2723 if (try value_cursor.readInt() == value) { 2724 return; 2725 } 2726 } 2727 2728 try map.put(key, .{ .int = value }); 2729 }, 2730 }, 2731 .bool => { 2732 const bytes = if (value) "true" else "false"; 2733 if (try map.getCursor(key)) |value_cursor| { 2734 var buffer: [5]u8 = undefined; 2735 const existing = try value_cursor.readBytesObject(&buffer); 2736 if (existing.format_tag) |format_tag| { 2737 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 2738 return; 2739 } 2740 } 2741 } 2742 2743 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 2744 }, 2745 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 2746 .@"union" => switch (value) { 2747 inline else => |payload, tag| { 2748 if (@TypeOf(payload) == void) { 2749 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 2750 } else { 2751 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 2752 if (try map.getCursor(key)) |cursor| { 2753 switch (cursor.slot().tag) { 2754 .hash_map => { 2755 const variants = try DB.HashMap(.read_only).init(cursor); 2756 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 2757 }, 2758 else => try map.put(key, .{ .slot = null }), 2759 } 2760 } 2761 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 2762 if (@typeInfo(@TypeOf(payload)) == .optional) { 2763 // keep the variant even when its payload is null 2764 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 2765 } 2766 if (try variants.getCursor(tag_key)) |cursor| { 2767 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 2768 } 2769 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 2770 } 2771 }, 2772 }, 2773 .optional => |optional_info| { 2774 // a missing key is null 2775 if (value) |child| { 2776 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 2777 } else { 2778 _ = try map.remove(key); 2779 } 2780 }, 2781 .@"struct" => try upsert( 2782 Field, 2783 DB, 2784 hash_kind, 2785 try DB.HashMap(.read_write).init(try map.putCursor(key)), 2786 value, 2787 ), 2788 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 2789 } 2790 } 2791 2792 fn upsertBytes( 2793 comptime DB: type, 2794 comptime hash_kind: hash.HashKind, 2795 map: DB.HashMap(.read_write), 2796 key: hash.HashInt(hash_kind), 2797 value: []const u8, 2798 ) !void { 2799 var existing_cursor_maybe = try map.getCursor(key); 2800 if (existing_cursor_maybe) |*existing_cursor| { 2801 if (try bytesEqual(DB, existing_cursor, value)) { 2802 return; 2803 } 2804 } 2805 2806 try map.put(key, .{ .bytes = value }); 2807 } 2808 2809 fn bytesEqual( 2810 comptime DB: type, 2811 cursor: *DB.Cursor(.read_only), 2812 value: []const u8, 2813 ) !bool { 2814 switch (cursor.slot().tag) { 2815 .bytes, .short_bytes => {}, 2816 else => return false, 2817 } 2818 var read_buffer: [1024]u8 = undefined; 2819 var reader = try cursor.reader(&read_buffer); 2820 if (reader.size != value.len) { 2821 return false; 2822 } 2823 2824 var chunk_buffer: [1024]u8 = undefined; 2825 var offset: usize = 0; 2826 while (offset < value.len) { 2827 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2828 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2829 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2830 return false; 2831 } 2832 offset += chunk_len; 2833 } 2834 2835 return true; 2836 } 2837 2838 // 2839 // CommitIterator 2840 // 2841 // differs from xit's ObjectIterator in a few ways: 2842 // 2843 // 1. iterates parents first, rather than children first 2844 // 2. `next` only returns the object id, not the full Object 2845 // 3. stores its temporary state in a file on disk to avoid OOMs 2846 // 2847 2848 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2849 return struct { 2850 const DB = EventDB(repo_opts.hash); 2851 // a plain file is used for the scratch db because each write outside 2852 // a transaction is synced anyway, so buffering would gain nothing 2853 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2854 const db_name = "haxy-repo-events.db"; 2855 2856 repo_dir: std.Io.Dir, 2857 db_file: std.Io.File, 2858 db: *TempDB, 2859 // map of each object id to its children 2860 parent_to_children: TempDB.HashMap(.read_write), 2861 // tracking pending parents is necessary because a child can only 2862 // be returned after all of its unconsumed parents have been returned 2863 child_to_pending_parents: TempDB.HashMap(.read_write), 2864 // queue of object ids that are ready to be returned from `next` 2865 ready_queue: TempDB.ArrayList(.read_write), 2866 ready_queue_index: u64, 2867 // the object id at the tip of this branch 2868 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2869 // the object id furthest back in the history that hasn't been consumed 2870 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2871 2872 pub fn init( 2873 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2874 io: std.Io, 2875 allocator: std.mem.Allocator, 2876 consumed_object_ids: DB.HashMap(.read_only), 2877 ref: rf.Ref, 2878 ) !CommitIterator(repo_kind, repo_opts) { 2879 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2880 errdefer { 2881 db_file.close(io); 2882 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2883 } 2884 2885 // cursors point at the db, so it needs a stable address 2886 const db_ptr = try allocator.create(TempDB); 2887 errdefer allocator.destroy(db_ptr); 2888 // the db is scratch state that gets deleted after iteration, so 2889 // there is nothing worth fsyncing 2890 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2891 2892 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2893 2894 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2895 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2896 2897 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2898 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2899 2900 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2901 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2902 2903 var self = CommitIterator(repo_kind, repo_opts){ 2904 .repo_dir = state.core.repo_dir, 2905 .db_file = db_file, 2906 .db = db_ptr, 2907 .parent_to_children = parent_to_children, 2908 .child_to_pending_parents = child_to_pending_parents, 2909 .ready_queue = ready_queue, 2910 .ready_queue_index = 0, 2911 .newest_object_id = null, 2912 .oldest_object_id = null, 2913 }; 2914 errdefer self.deinit(io, allocator); 2915 2916 try self.collect(state, io, allocator, consumed_object_ids, ref); 2917 2918 return self; 2919 } 2920 2921 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2922 self.db_file.close(io); 2923 self.repo_dir.deleteFile(io, db_name) catch {}; 2924 allocator.destroy(self.db); 2925 } 2926 2927 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2928 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2929 2930 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2931 self.ready_queue_index += 1; 2932 2933 try self.enqueueChildren(&oid); 2934 return oid; 2935 } 2936 2937 fn collect( 2938 self: *CommitIterator(repo_kind, repo_opts), 2939 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2940 io: std.Io, 2941 allocator: std.mem.Allocator, 2942 consumed_object_ids: DB.HashMap(.read_only), 2943 ref: rf.Ref, 2944 ) !void { 2945 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2946 2947 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2948 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2949 var walk_queue_index: u64 = 0; 2950 2951 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2952 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2953 2954 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2955 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2956 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2957 try walk_queue.append(.{ .bytes = &head_oid }); 2958 2959 while (walk_queue_index < try walk_queue.count()) { 2960 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2961 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2962 2963 walk_queue_index += 1; 2964 2965 // if we've seen this object id, skip it 2966 if (null != try seen_object_ids.getCursor(oid_int)) { 2967 continue; 2968 } 2969 2970 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2971 2972 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2973 defer commit_object.deinit(); 2974 2975 // if this object id has already been consumed, skip it 2976 if (null != try consumed_object_ids.getCursor(oid_int)) { 2977 continue; 2978 } 2979 2980 if (self.newest_object_id == null) self.newest_object_id = oid; 2981 self.oldest_object_id = oid; 2982 2983 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2984 var pending_parent_count: u64 = 0; 2985 for (parent_oids) |*parent_oid| { 2986 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2987 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2988 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2989 2990 // if this object id has already been consumed, skip it 2991 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2992 continue; 2993 } 2994 2995 // if this object id hasn't already been seen, add it to the walk queue 2996 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2997 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2998 } 2999 3000 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 3001 const children = try TempDB.HashSet(.read_write).init(children_cursor); 3002 try children.put(oid_int, .{ .bytes = &oid }); 3003 pending_parent_count += 1; 3004 } 3005 3006 if (pending_parent_count == 0) { 3007 try self.ready_queue.append(.{ .bytes = &oid }); 3008 } else { 3009 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 3010 } 3011 } 3012 } 3013 3014 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 3015 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 3016 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 3017 const children = try TempDB.HashSet(.read_only).init(children_cursor); 3018 var children_iter = try children.iterator(); 3019 3020 while (try children_iter.next()) |*child_cursor| { 3021 const kv_pair = try child_cursor.readKeyValuePair(); 3022 const child_oid_int = kv_pair.hash; 3023 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 3024 const pending_parent_count = try pending_parent_cursor.readUint(); 3025 3026 if (pending_parent_count <= 1) { 3027 _ = try self.child_to_pending_parents.remove(child_oid_int); 3028 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 3029 try self.ready_queue.append(.{ .bytes = &child_oid }); 3030 } else { 3031 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 3032 } 3033 } 3034 } 3035 3036 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 3037 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 3038 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 3039 _ = try oid_cursor.readBytes(&oid); 3040 return oid; 3041 } 3042 }; 3043 } 3044