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