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