╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1668 @field(event, field.name) = try field.type.decode(try readBytes(DB, hash_kind, arena.allocator(), map, field.name)); 1669 }, 1670 .optional => |optional_info| { 1671 // a missing key is null 1672 if (try map.getCursor(hash.hashInt(hash_kind, field.name))) |cursor| { 1673 if (optional_info.child == []const u8) { 1674 @field(event, field.name) = try cursor.readBytesAlloc(arena.allocator(), null); 1675 } else switch (@typeInfo(optional_info.child)) { 1676 .array => |array_info| { 1677 if (array_info.child != u8) @compileError("unsupported read field type: " ++ @typeName(field.type)); 1678 var bytes: optional_info.child = undefined; 1679 const value = try cursor.readBytes(&bytes); 1680 if (value.len != bytes.len) return error.InvalidByteArrayLength; 1681 @field(event, field.name) = bytes; 1682 }, 1683 .@"struct" => @field(event, field.name) = try read( 1684 optional_info.child, 1685 DB, 1686 hash_kind, 1687 arena, 1688 try DB.HashMap(.read_only).init(cursor), 1689 ), 1690 else => @compileError("unsupported read field type: " ++ @typeName(field.type)), 1691 } 1692 } else { 1693 @field(event, field.name) = null; 1694 } 1695 }, 1696 .@"struct" => { 1697 const cursor = try map.getCursor(hash.hashInt(hash_kind, field.name)) orelse return error.NotFound; 1698 @field(event, field.name) = try read( 1699 field.type, 1700 DB, 1701 hash_kind, 1702 arena, 1703 try DB.HashMap(.read_only).init(cursor), 1704 ); 1705 }, 1706 else => @compileError("unsupported read field type: " ++ @typeName(field.type)), 1707 } 1708 } 1709 }, 1710 else => @compileError("read expects a struct"), 1711 } 1712 1713 return event; 1714 } 1715 1716 // whether two events carry the same data 1717 fn fieldsEqual(comptime T: type, a: T, b: T) bool { 1718 switch (@typeInfo(T)) { 1719 .@"struct" => |struct_info| { 1720 inline for (struct_info.fields) |field| { 1721 if (!fieldEqual(field.type, @field(a, field.name), @field(b, field.name))) return false; 1722 } 1723 }, 1724 else => @compileError("fieldsEqual expects a struct"), 1725 } 1726 1727 return true; 1728 } 1729 1730 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 1731 switch (@typeInfo(Field)) { 1732 .pointer => |pointer_info| { 1733 if (pointer_info.size == .slice and pointer_info.child == u8) { 1734 return std.mem.eql(u8, a, b); 1735 } else { 1736 @compileError("unsupported field type: " ++ @typeName(Field)); 1737 } 1738 }, 1739 .array => |array_info| { 1740 if (array_info.child == u8) { 1741 return std.mem.eql(u8, &a, &b); 1742 } else { 1743 @compileError("unsupported field type: " ++ @typeName(Field)); 1744 } 1745 }, 1746 .void => return true, 1747 .bool, .int, .@"enum" => return a == b, 1748 .@"union" => { 1749 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1750 return switch (a) { 1751 inline else => |value, tag| fieldEqual(@TypeOf(value), value, @field(b, @tagName(tag))), 1752 }; 1753 }, 1754 .@"struct" => return fieldsEqual(Field, a, b), 1755 .optional => |optional_info| { 1756 if (a) |a_value| { 1757 if (b) |b_value| return fieldEqual(optional_info.child, a_value, b_value); 1758 return false; 1759 } 1760 return b == null; 1761 }, 1762 else => @compileError("unsupported field type: " ++ @typeName(Field)), 1763 } 1764 } 1765 1766 fn readBytes( 1767 comptime DB: type, 1768 comptime hash_kind: hash.HashKind, 1769 allocator: std.mem.Allocator, 1770 map: DB.HashMap(.read_only), 1771 field_name: []const u8, 1772 ) ![]const u8 { 1773 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1774 return try cursor.readBytesAlloc(allocator, null); 1775 } 1776 1777 fn readUint( 1778 comptime DB: type, 1779 comptime hash_kind: hash.HashKind, 1780 map: DB.HashMap(.read_only), 1781 field_name: []const u8, 1782 ) !u64 { 1783 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1784 return try cursor.readUint(); 1785 } 1786 1787 fn readInt( 1788 comptime DB: type, 1789 comptime hash_kind: hash.HashKind, 1790 map: DB.HashMap(.read_only), 1791 field_name: []const u8, 1792 ) !i64 { 1793 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1794 return try cursor.readInt(); 1795 } 1796 1797 fn readBool( 1798 comptime DB: type, 1799 comptime hash_kind: hash.HashKind, 1800 map: DB.HashMap(.read_only), 1801 field_name: []const u8, 1802 ) !bool { 1803 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1804 var buffer: [5]u8 = undefined; 1805 const bytes = try cursor.readBytesObject(&buffer); 1806 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 1807 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 1808 if (std.mem.eql(u8, bytes.value, "true")) return true; 1809 if (std.mem.eql(u8, bytes.value, "false")) return false; 1810 return error.InvalidBool; 1811 } 1812 1813 // 1814 // writing to xitdb 1815 // 1816 1817 pub fn upsert( 1818 comptime T: type, 1819 comptime DB: type, 1820 comptime hash_kind: hash.HashKind, 1821 map: DB.HashMap(.read_write), 1822 event: T, 1823 ) !void { 1824 switch (@typeInfo(T)) { 1825 .@"struct" => |struct_info| { 1826 inline for (struct_info.fields) |field| { 1827 try upsertField(DB, hash_kind, map, field.name, field.type, @field(event, field.name)); 1828 } 1829 }, 1830 else => @compileError("upsert expects a struct"), 1831 } 1832 } 1833 1834 fn upsertField( 1835 comptime DB: type, 1836 comptime hash_kind: hash.HashKind, 1837 map: DB.HashMap(.read_write), 1838 comptime field_name: []const u8, 1839 comptime Field: type, 1840 value: Field, 1841 ) !void { 1842 const key = hash.hashInt(hash_kind, field_name); 1843 1844 switch (@typeInfo(Field)) { 1845 .pointer => |pointer_info| { 1846 if (pointer_info.size == .slice and pointer_info.child == u8) { 1847 try upsertBytes(DB, hash_kind, map, key, value); 1848 } else { 1849 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1850 } 1851 }, 1852 .array => |array_info| { 1853 if (array_info.child == u8) { 1854 try upsertBytes(DB, hash_kind, map, key, &value); 1855 } else { 1856 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1857 } 1858 }, 1859 .int => |int_info| switch (int_info.signedness) { 1860 .unsigned => { 1861 if (try map.getCursor(key)) |value_cursor| { 1862 if (try value_cursor.readUint() == value) { 1863 return; 1864 } 1865 } 1866 1867 try map.put(key, .{ .uint = value }); 1868 }, 1869 .signed => { 1870 if (try map.getCursor(key)) |value_cursor| { 1871 if (try value_cursor.readInt() == value) { 1872 return; 1873 } 1874 } 1875 1876 try map.put(key, .{ .int = value }); 1877 }, 1878 }, 1879 .bool => { 1880 const bytes = if (value) "true" else "false"; 1881 if (try map.getCursor(key)) |value_cursor| { 1882 var buffer: [5]u8 = undefined; 1883 const existing = try value_cursor.readBytesObject(&buffer); 1884 if (existing.format_tag) |format_tag| { 1885 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 1886 return; 1887 } 1888 } 1889 } 1890 1891 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 1892 }, 1893 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 1894 .@"union" => { 1895 if (!@hasDecl(Field, "encode") or !@hasDecl(Field, "max_encoded_len")) { 1896 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1897 } 1898 var buffer: [Field.max_encoded_len]u8 = undefined; 1899 try upsertBytes(DB, hash_kind, map, key, try value.encode(&buffer)); 1900 }, 1901 .optional => |optional_info| { 1902 // a missing key is null 1903 if (value) |child| { 1904 if (optional_info.child == []const u8) { 1905 try upsertBytes(DB, hash_kind, map, key, child); 1906 } else switch (@typeInfo(optional_info.child)) { 1907 .array => |array_info| { 1908 if (array_info.child != u8) @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1909 try upsertBytes(DB, hash_kind, map, key, &child); 1910 }, 1911 .@"struct" => try upsert( 1912 optional_info.child, 1913 DB, 1914 hash_kind, 1915 try DB.HashMap(.read_write).init(try map.putCursor(key)), 1916 child, 1917 ), 1918 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 1919 } 1920 } else { 1921 _ = try map.remove(key); 1922 } 1923 }, 1924 .@"struct" => try upsert( 1925 Field, 1926 DB, 1927 hash_kind, 1928 try DB.HashMap(.read_write).init(try map.putCursor(key)), 1929 value, 1930 ), 1931 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 1932 } 1933 } 1934 1935 fn upsertBytes( 1936 comptime DB: type, 1937 comptime hash_kind: hash.HashKind, 1938 map: DB.HashMap(.read_write), 1939 key: hash.HashInt(hash_kind), 1940 value: []const u8, 1941 ) !void { 1942 var existing_cursor_maybe = try map.getCursor(key); 1943 if (existing_cursor_maybe) |*existing_cursor| { 1944 if (try bytesEqual(DB, existing_cursor, value)) { 1945 return; 1946 } 1947 } 1948 1949 var value_cursor = try map.putCursor(key); 1950 var write_buffer: [1024]u8 = undefined; 1951 var writer = try value_cursor.writer(&write_buffer); 1952 try writer.interface.writeAll(value); 1953 try writer.finish(); 1954 } 1955 1956 fn bytesEqual( 1957 comptime DB: type, 1958 cursor: *DB.Cursor(.read_only), 1959 value: []const u8, 1960 ) !bool { 1961 var read_buffer: [1024]u8 = undefined; 1962 var reader = try cursor.reader(&read_buffer); 1963 if (reader.size != value.len) { 1964 return false; 1965 } 1966 1967 var chunk_buffer: [1024]u8 = undefined; 1968 var offset: usize = 0; 1969 while (offset < value.len) { 1970 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1971 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1972 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1973 return false; 1974 } 1975 offset += chunk_len; 1976 } 1977 1978 return true; 1979 } 1980 1981 // 1982 // CommitIterator 1983 // 1984 // differs from xit's ObjectIterator in a few ways: 1985 // 1986 // 1. iterates parents first, rather than children first 1987 // 2. `next` only returns the object id, not the full Object 1988 // 3. stores its temporary state in a file on disk to avoid OOMs 1989 // 1990 1991 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1992 return struct { 1993 const DB = EventDB(repo_opts.hash); 1994 const db_name = "haxy-repo-events.db"; 1995 1996 repo_dir: std.Io.Dir, 1997 db_file: std.Io.File, 1998 db: *DB, 1999 // map of each object id to its children 2000 parent_to_children: DB.HashMap(.read_write), 2001 // tracking pending parents is necessary because a child can only 2002 // be returned after all of its unconsumed parents have been returned 2003 child_to_pending_parents: DB.HashMap(.read_write), 2004 // queue of object ids that are ready to be returned from `next` 2005 ready_queue: DB.ArrayList(.read_write), 2006 ready_queue_index: u64, 2007 // the object id at the tip of this branch 2008 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2009 // the object id furthest back in the history that hasn't been consumed 2010 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2011 2012 pub fn init( 2013 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2014 io: std.Io, 2015 allocator: std.mem.Allocator, 2016 consumed_object_ids: DB.HashMap(.read_only), 2017 ref: rf.Ref, 2018 ) !CommitIterator(repo_kind, repo_opts) { 2019 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2020 errdefer { 2021 db_file.close(io); 2022 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2023 } 2024 2025 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 2026 errdefer allocator.destroy(buffer_ptr); 2027 2028 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 2029 errdefer buffer_ptr.deinit(); 2030 2031 const db_ptr = try allocator.create(DB); 2032 errdefer allocator.destroy(db_ptr); 2033 // the db is scratch state that gets deleted after iteration, so 2034 // there is nothing worth fsyncing 2035 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr, .fsync = false }); 2036 2037 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 2038 2039 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2040 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 2041 2042 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2043 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2044 2045 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2046 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 2047 2048 var self = CommitIterator(repo_kind, repo_opts){ 2049 .repo_dir = state.core.repo_dir, 2050 .db_file = db_file, 2051 .db = db_ptr, 2052 .parent_to_children = parent_to_children, 2053 .child_to_pending_parents = child_to_pending_parents, 2054 .ready_queue = ready_queue, 2055 .ready_queue_index = 0, 2056 .newest_object_id = null, 2057 .oldest_object_id = null, 2058 }; 2059 errdefer self.deinit(io, allocator); 2060 2061 try self.collect(state, io, allocator, consumed_object_ids, ref); 2062 2063 return self; 2064 } 2065 2066 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2067 self.db_file.close(io); 2068 self.db.core.memory.buffer.deinit(); 2069 allocator.destroy(self.db.core.memory.buffer); 2070 self.repo_dir.deleteFile(io, db_name) catch {}; 2071 allocator.destroy(self.db); 2072 } 2073 2074 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2075 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2076 2077 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2078 self.ready_queue_index += 1; 2079 2080 try self.enqueueChildren(&oid); 2081 return oid; 2082 } 2083 2084 fn collect( 2085 self: *CommitIterator(repo_kind, repo_opts), 2086 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2087 io: std.Io, 2088 allocator: std.mem.Allocator, 2089 consumed_object_ids: DB.HashMap(.read_only), 2090 ref: rf.Ref, 2091 ) !void { 2092 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 2093 2094 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2095 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 2096 var walk_queue_index: u64 = 0; 2097 2098 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2099 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 2100 2101 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2102 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2103 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2104 try walk_queue.append(.{ .bytes = &head_oid }); 2105 2106 while (walk_queue_index < try walk_queue.count()) { 2107 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2108 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2109 2110 walk_queue_index += 1; 2111 2112 // if we've seen this object id, skip it 2113 if (null != try seen_object_ids.getCursor(oid_int)) { 2114 continue; 2115 } 2116 2117 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2118 2119 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2120 defer commit_object.deinit(); 2121 2122 // if this object id has already been consumed, skip it 2123 if (null != try consumed_object_ids.getCursor(oid_int)) { 2124 continue; 2125 } 2126 2127 if (self.newest_object_id == null) self.newest_object_id = oid; 2128 self.oldest_object_id = oid; 2129 2130 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2131 var pending_parent_count: u64 = 0; 2132 for (parent_oids) |*parent_oid| { 2133 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2134 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2135 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2136 2137 // if this object id has already been consumed, skip it 2138 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2139 continue; 2140 } 2141 2142 // if this object id hasn't already been seen, add it to the walk queue 2143 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2144 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2145 } 2146 2147 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2148 const children = try DB.HashSet(.read_write).init(children_cursor); 2149 try children.put(oid_int, .{ .bytes = &oid }); 2150 pending_parent_count += 1; 2151 } 2152 2153 if (pending_parent_count == 0) { 2154 try self.ready_queue.append(.{ .bytes = &oid }); 2155 } else { 2156 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2157 } 2158 } 2159 } 2160 2161 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2162 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2163 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2164 const children = try DB.HashSet(.read_only).init(children_cursor); 2165 var children_iter = try children.iterator(); 2166 2167 while (try children_iter.next()) |*child_cursor| { 2168 const kv_pair = try child_cursor.readKeyValuePair(); 2169 const child_oid_int = kv_pair.hash; 2170 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2171 const pending_parent_count = try pending_parent_cursor.readUint(); 2172 2173 if (pending_parent_count <= 1) { 2174 _ = try self.child_to_pending_parents.remove(child_oid_int); 2175 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2176 try self.ready_queue.append(.{ .bytes = &child_oid }); 2177 } else { 2178 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2179 } 2180 } 2181 } 2182 2183 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2184 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2185 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2186 _ = try oid_cursor.readBytes(&oid); 2187 return oid; 2188 } 2189 }; 2190 } 2191