╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1575 pub const CreateRepoOptions = struct { 1576 read_access: Repo.Access = .private, 1577 }; 1578 1579 // resolve a pushed `<owner>/<repo>` to the hex event id that names its on-disk 1580 // directory under the repos dir 1581 pub fn resolveOrCreateRepo( 1582 io: std.Io, 1583 allocator: std.mem.Allocator, 1584 admin_repo_path: []const u8, 1585 owner_name: []const u8, 1586 repo_name: []const u8, 1587 create_options_maybe: ?CreateRepoOptions, 1588 ) !?[event_id_size * 2]u8 { 1589 var repo = rp.Repo(.xit, admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 1590 error.RepoNotFound => return null, 1591 else => |e| return e, 1592 }; 1593 defer repo.deinit(io, allocator); 1594 1595 var arena = std.heap.ArenaAllocator.init(allocator); 1596 defer arena.deinit(); 1597 1598 const moment = try currentMoment(admin_repo_opts, &repo); 1599 1600 // an already-registered repo reuses its event id, so a re-push (or a clone) 1601 // lands in the same repo 1602 if (try Repo.readByOwnerAndName(AdminDB, admin_repo_opts.hash, moment, &arena, owner_name, repo_name)) |found| { 1603 return std.fmt.bytesToHex(found.event_id, .lower); 1604 } 1605 1606 const create_options = create_options_maybe orelse return null; 1607 1608 // the new repo is owned by the named user, read from the name index; an 1609 // unknown owner can't own a repo, so the push is rejected 1610 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(admin_repo_opts.hash, "name->user-id")) orelse return null; 1611 const name_to_user_id = try AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 1612 const owner_id_cursor = try name_to_user_id.getCursor(hash.hashInt(admin_repo_opts.hash, owner_name)) orelse return null; 1613 var owner_user_id: [event_id_size]u8 = undefined; 1614 _ = try owner_id_cursor.readBytes(&owner_user_id); 1615 1616 const owner = (try User.readById(AdminDB, admin_repo_opts.hash, moment, &arena, &owner_user_id)) orelse unreachable; 1617 1618 var id_bytes: [event_id_size]u8 = undefined; 1619 io.random(&id_bytes); 1620 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1621 1622 try consume(.server, .admin, .xit, admin_repo_opts, io, allocator, &repo, events_ref, &[_]EventWithId{.{ 1623 .id = event_id_hex, 1624 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1625 .author = .{ .name = owner.event.name, .email = owner.event.email }, 1626 .event = .{ .repo = .{ 1627 .user_id = &owner_user_id, 1628 .name = repo_name, 1629 .description = "", 1630 .read_access = create_options.read_access, 1631 } }, 1632 }}); 1633 1634 return event_id_hex; 1635 } 1636 1637 // 1638 // reading from xitdb 1639 // 1640 1641 pub fn read( 1642 comptime T: type, 1643 comptime DB: type, 1644 comptime hash_kind: hash.HashKind, 1645 arena: *std.heap.ArenaAllocator, 1646 map: DB.HashMap(.read_only), 1647 ) !T { 1648 var event: T = undefined; 1649 switch (@typeInfo(T)) { 1650 .@"struct" => |struct_info| { 1651 inline for (struct_info.fields) |field| { 1652 const cursor = try map.getCursor(hash.hashInt(hash_kind, field.name)); 1653 @field(event, field.name) = try readField(field.type, DB, hash_kind, arena, cursor); 1654 } 1655 }, 1656 else => @compileError("read expects a struct"), 1657 } 1658 return event; 1659 } 1660 1661 fn readField( 1662 comptime Field: type, 1663 comptime DB: type, 1664 comptime hash_kind: hash.HashKind, 1665 arena: *std.heap.ArenaAllocator, 1666 cursor_maybe: ?DB.Cursor(.read_only), 1667 ) !Field { 1668 if (@typeInfo(Field) == .optional) { 1669 // a missing key or a null union payload is null 1670 const cursor = cursor_maybe orelse return null; 1671 if (cursor.slot().tag == .none) return null; 1672 return try readField(@typeInfo(Field).optional.child, DB, hash_kind, arena, cursor); 1673 } 1674 const cursor = cursor_maybe orelse return error.NotFound; 1675 switch (@typeInfo(Field)) { 1676 .pointer => |pointer_info| { 1677 if (pointer_info.size != .slice or pointer_info.child != u8 or !pointer_info.is_const) { 1678 @compileError("unsupported read field type: " ++ @typeName(Field)); 1679 } 1680 return try cursor.readBytesAlloc(arena.allocator(), null); 1681 }, 1682 .array => |array_info| { 1683 if (array_info.child != u8) @compileError("unsupported read field type: " ++ @typeName(Field)); 1684 var bytes: Field = undefined; 1685 if ((try cursor.readBytes(&bytes)).len != bytes.len) return error.InvalidByteArrayLength; 1686 return bytes; 1687 }, 1688 .bool => { 1689 var buffer: [5]u8 = undefined; 1690 const bytes = try cursor.readBytesObject(&buffer); 1691 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 1692 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 1693 if (std.mem.eql(u8, bytes.value, "true")) return true; 1694 if (std.mem.eql(u8, bytes.value, "false")) return false; 1695 return error.InvalidBool; 1696 }, 1697 .int => |int_info| return switch (int_info.signedness) { 1698 .unsigned => @intCast(try cursor.readUint()), 1699 .signed => @intCast(try cursor.readInt()), 1700 }, 1701 .@"enum" => return std.meta.stringToEnum(Field, try cursor.readBytesAlloc(arena.allocator(), null)) orelse error.InvalidEnumTag, 1702 .@"union" => |union_info| { 1703 if (union_info.tag_type == null) @compileError("unsupported read field type: " ++ @typeName(Field)); 1704 switch (cursor.slot().tag) { 1705 .bytes, .short_bytes => { 1706 const name = try cursor.readBytesAlloc(arena.allocator(), null); 1707 inline for (union_info.fields) |variant| { 1708 if (variant.type == void and std.mem.eql(u8, name, variant.name)) { 1709 return @unionInit(Field, variant.name, {}); 1710 } 1711 } 1712 return error.InvalidUnion; 1713 }, 1714 .hash_map => { 1715 const variants = try DB.HashMap(.read_only).init(cursor); 1716 var iter = try variants.iterator(); 1717 const entry = (try iter.next()) orelse return error.InvalidUnion; 1718 const pair = try entry.readKeyValuePair(); 1719 if (try iter.next() != null) return error.InvalidUnion; 1720 inline for (union_info.fields) |variant| { 1721 if (variant.type != void and pair.hash == hash.hashInt(hash_kind, variant.name)) { 1722 return @unionInit(Field, variant.name, try readField(variant.type, DB, hash_kind, arena, pair.value_cursor)); 1723 } 1724 } 1725 return error.InvalidUnion; 1726 }, 1727 else => return error.InvalidUnion, 1728 } 1729 }, 1730 .@"struct" => return try read(Field, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)), 1731 else => @compileError("unsupported read field type: " ++ @typeName(Field)), 1732 } 1733 } 1734 1735 // whether two events carry the same data 1736 fn fieldsEqual(comptime T: type, a: T, b: T) bool { 1737 switch (@typeInfo(T)) { 1738 .@"struct" => |struct_info| { 1739 inline for (struct_info.fields) |field| { 1740 if (!fieldEqual(field.type, @field(a, field.name), @field(b, field.name))) return false; 1741 } 1742 }, 1743 else => @compileError("fieldsEqual expects a struct"), 1744 } 1745 1746 return true; 1747 } 1748 1749 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 1750 switch (@typeInfo(Field)) { 1751 .pointer => |pointer_info| { 1752 if (pointer_info.size == .slice and pointer_info.child == u8) { 1753 return std.mem.eql(u8, a, b); 1754 } else { 1755 @compileError("unsupported field type: " ++ @typeName(Field)); 1756 } 1757 }, 1758 .array => |array_info| { 1759 if (array_info.child == u8) { 1760 return std.mem.eql(u8, &a, &b); 1761 } else { 1762 @compileError("unsupported field type: " ++ @typeName(Field)); 1763 } 1764 }, 1765 .void => return true, 1766 .bool, .int, .@"enum" => return a == b, 1767 .@"union" => { 1768 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1769 return switch (a) { 1770 inline else => |value, tag| fieldEqual(@TypeOf(value), value, @field(b, @tagName(tag))), 1771 }; 1772 }, 1773 .@"struct" => return fieldsEqual(Field, a, b), 1774 .optional => |optional_info| { 1775 if (a) |a_value| { 1776 if (b) |b_value| return fieldEqual(optional_info.child, a_value, b_value); 1777 return false; 1778 } 1779 return b == null; 1780 }, 1781 else => @compileError("unsupported field type: " ++ @typeName(Field)), 1782 } 1783 } 1784 1785 // 1786 // writing to xitdb 1787 // 1788 1789 pub fn upsert( 1790 comptime T: type, 1791 comptime DB: type, 1792 comptime hash_kind: hash.HashKind, 1793 map: DB.HashMap(.read_write), 1794 event: T, 1795 ) !void { 1796 switch (@typeInfo(T)) { 1797 .@"struct" => |struct_info| { 1798 inline for (struct_info.fields) |field| { 1799 try upsertField(DB, hash_kind, map, field.name, field.type, @field(event, field.name)); 1800 } 1801 }, 1802 else => @compileError("upsert expects a struct"), 1803 } 1804 } 1805 1806 fn upsertField( 1807 comptime DB: type, 1808 comptime hash_kind: hash.HashKind, 1809 map: DB.HashMap(.read_write), 1810 comptime field_name: []const u8, 1811 comptime Field: type, 1812 value: Field, 1813 ) !void { 1814 const key = hash.hashInt(hash_kind, field_name); 1815 1816 switch (@typeInfo(Field)) { 1817 .pointer => |pointer_info| { 1818 if (pointer_info.size == .slice and pointer_info.child == u8) { 1819 try upsertBytes(DB, hash_kind, map, key, value); 1820 } else { 1821 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1822 } 1823 }, 1824 .array => |array_info| { 1825 if (array_info.child == u8) { 1826 try upsertBytes(DB, hash_kind, map, key, &value); 1827 } else { 1828 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1829 } 1830 }, 1831 .int => |int_info| switch (int_info.signedness) { 1832 .unsigned => { 1833 if (try map.getCursor(key)) |value_cursor| { 1834 if (try value_cursor.readUint() == value) { 1835 return; 1836 } 1837 } 1838 1839 try map.put(key, .{ .uint = value }); 1840 }, 1841 .signed => { 1842 if (try map.getCursor(key)) |value_cursor| { 1843 if (try value_cursor.readInt() == value) { 1844 return; 1845 } 1846 } 1847 1848 try map.put(key, .{ .int = value }); 1849 }, 1850 }, 1851 .bool => { 1852 const bytes = if (value) "true" else "false"; 1853 if (try map.getCursor(key)) |value_cursor| { 1854 var buffer: [5]u8 = undefined; 1855 const existing = try value_cursor.readBytesObject(&buffer); 1856 if (existing.format_tag) |format_tag| { 1857 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 1858 return; 1859 } 1860 } 1861 } 1862 1863 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 1864 }, 1865 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 1866 .@"union" => switch (value) { 1867 inline else => |payload, tag| { 1868 if (@TypeOf(payload) == void) { 1869 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 1870 } else { 1871 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 1872 if (try map.getCursor(key)) |cursor| { 1873 switch (cursor.slot().tag) { 1874 .hash_map => { 1875 const variants = try DB.HashMap(.read_only).init(cursor); 1876 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 1877 }, 1878 else => try map.put(key, .{ .slot = null }), 1879 } 1880 } 1881 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 1882 if (@typeInfo(@TypeOf(payload)) == .optional) { 1883 // keep the variant even when its payload is null 1884 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 1885 } 1886 if (try variants.getCursor(tag_key)) |cursor| { 1887 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 1888 } 1889 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 1890 } 1891 }, 1892 }, 1893 .optional => |optional_info| { 1894 // a missing key is null 1895 if (value) |child| { 1896 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 1897 } else { 1898 _ = try map.remove(key); 1899 } 1900 }, 1901 .@"struct" => try upsert( 1902 Field, 1903 DB, 1904 hash_kind, 1905 try DB.HashMap(.read_write).init(try map.putCursor(key)), 1906 value, 1907 ), 1908 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 1909 } 1910 } 1911 1912 fn upsertBytes( 1913 comptime DB: type, 1914 comptime hash_kind: hash.HashKind, 1915 map: DB.HashMap(.read_write), 1916 key: hash.HashInt(hash_kind), 1917 value: []const u8, 1918 ) !void { 1919 var existing_cursor_maybe = try map.getCursor(key); 1920 if (existing_cursor_maybe) |*existing_cursor| { 1921 if (try bytesEqual(DB, existing_cursor, value)) { 1922 return; 1923 } 1924 } 1925 1926 try map.put(key, .{ .bytes = value }); 1927 } 1928 1929 fn bytesEqual( 1930 comptime DB: type, 1931 cursor: *DB.Cursor(.read_only), 1932 value: []const u8, 1933 ) !bool { 1934 switch (cursor.slot().tag) { 1935 .bytes, .short_bytes => {}, 1936 else => return false, 1937 } 1938 var read_buffer: [1024]u8 = undefined; 1939 var reader = try cursor.reader(&read_buffer); 1940 if (reader.size != value.len) { 1941 return false; 1942 } 1943 1944 var chunk_buffer: [1024]u8 = undefined; 1945 var offset: usize = 0; 1946 while (offset < value.len) { 1947 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1948 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1949 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1950 return false; 1951 } 1952 offset += chunk_len; 1953 } 1954 1955 return true; 1956 } 1957 1958 // 1959 // CommitIterator 1960 // 1961 // differs from xit's ObjectIterator in a few ways: 1962 // 1963 // 1. iterates parents first, rather than children first 1964 // 2. `next` only returns the object id, not the full Object 1965 // 3. stores its temporary state in a file on disk to avoid OOMs 1966 // 1967 1968 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1969 return struct { 1970 const DB = EventDB(repo_opts.hash); 1971 // a plain file is used for the scratch db because each write outside 1972 // a transaction is synced anyway, so buffering would gain nothing 1973 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 1974 const db_name = "haxy-repo-events.db"; 1975 1976 repo_dir: std.Io.Dir, 1977 db_file: std.Io.File, 1978 db: *TempDB, 1979 // map of each object id to its children 1980 parent_to_children: TempDB.HashMap(.read_write), 1981 // tracking pending parents is necessary because a child can only 1982 // be returned after all of its unconsumed parents have been returned 1983 child_to_pending_parents: TempDB.HashMap(.read_write), 1984 // queue of object ids that are ready to be returned from `next` 1985 ready_queue: TempDB.ArrayList(.read_write), 1986 ready_queue_index: u64, 1987 // the object id at the tip of this branch 1988 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1989 // the object id furthest back in the history that hasn't been consumed 1990 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1991 1992 pub fn init( 1993 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1994 io: std.Io, 1995 allocator: std.mem.Allocator, 1996 consumed_object_ids: DB.HashMap(.read_only), 1997 ref: rf.Ref, 1998 ) !CommitIterator(repo_kind, repo_opts) { 1999 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2000 errdefer { 2001 db_file.close(io); 2002 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2003 } 2004 2005 // cursors point at the db, so it needs a stable address 2006 const db_ptr = try allocator.create(TempDB); 2007 errdefer allocator.destroy(db_ptr); 2008 // the db is scratch state that gets deleted after iteration, so 2009 // there is nothing worth fsyncing 2010 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2011 2012 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2013 2014 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2015 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2016 2017 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2018 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2019 2020 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2021 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2022 2023 var self = CommitIterator(repo_kind, repo_opts){ 2024 .repo_dir = state.core.repo_dir, 2025 .db_file = db_file, 2026 .db = db_ptr, 2027 .parent_to_children = parent_to_children, 2028 .child_to_pending_parents = child_to_pending_parents, 2029 .ready_queue = ready_queue, 2030 .ready_queue_index = 0, 2031 .newest_object_id = null, 2032 .oldest_object_id = null, 2033 }; 2034 errdefer self.deinit(io, allocator); 2035 2036 try self.collect(state, io, allocator, consumed_object_ids, ref); 2037 2038 return self; 2039 } 2040 2041 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2042 self.db_file.close(io); 2043 self.repo_dir.deleteFile(io, db_name) catch {}; 2044 allocator.destroy(self.db); 2045 } 2046 2047 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2048 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2049 2050 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2051 self.ready_queue_index += 1; 2052 2053 try self.enqueueChildren(&oid); 2054 return oid; 2055 } 2056 2057 fn collect( 2058 self: *CommitIterator(repo_kind, repo_opts), 2059 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2060 io: std.Io, 2061 allocator: std.mem.Allocator, 2062 consumed_object_ids: DB.HashMap(.read_only), 2063 ref: rf.Ref, 2064 ) !void { 2065 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2066 2067 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2068 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2069 var walk_queue_index: u64 = 0; 2070 2071 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2072 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2073 2074 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2075 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2076 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2077 try walk_queue.append(.{ .bytes = &head_oid }); 2078 2079 while (walk_queue_index < try walk_queue.count()) { 2080 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2081 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2082 2083 walk_queue_index += 1; 2084 2085 // if we've seen this object id, skip it 2086 if (null != try seen_object_ids.getCursor(oid_int)) { 2087 continue; 2088 } 2089 2090 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2091 2092 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2093 defer commit_object.deinit(); 2094 2095 // if this object id has already been consumed, skip it 2096 if (null != try consumed_object_ids.getCursor(oid_int)) { 2097 continue; 2098 } 2099 2100 if (self.newest_object_id == null) self.newest_object_id = oid; 2101 self.oldest_object_id = oid; 2102 2103 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2104 var pending_parent_count: u64 = 0; 2105 for (parent_oids) |*parent_oid| { 2106 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2107 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2108 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2109 2110 // if this object id has already been consumed, skip it 2111 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2112 continue; 2113 } 2114 2115 // if this object id hasn't already been seen, add it to the walk queue 2116 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2117 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2118 } 2119 2120 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2121 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2122 try children.put(oid_int, .{ .bytes = &oid }); 2123 pending_parent_count += 1; 2124 } 2125 2126 if (pending_parent_count == 0) { 2127 try self.ready_queue.append(.{ .bytes = &oid }); 2128 } else { 2129 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2130 } 2131 } 2132 } 2133 2134 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2135 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2136 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2137 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2138 var children_iter = try children.iterator(); 2139 2140 while (try children_iter.next()) |*child_cursor| { 2141 const kv_pair = try child_cursor.readKeyValuePair(); 2142 const child_oid_int = kv_pair.hash; 2143 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2144 const pending_parent_count = try pending_parent_cursor.readUint(); 2145 2146 if (pending_parent_count <= 1) { 2147 _ = try self.child_to_pending_parents.remove(child_oid_int); 2148 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2149 try self.ready_queue.append(.{ .bytes = &child_oid }); 2150 } else { 2151 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2152 } 2153 } 2154 } 2155 2156 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2157 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2158 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2159 _ = try oid_cursor.readBytes(&oid); 2160 return oid; 2161 } 2162 }; 2163 } 2164