╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1828 if (array_info.child == u8) { 1829 return std.mem.eql(u8, &a, &b); 1830 } else { 1831 @compileError("unsupported field type: " ++ @typeName(Field)); 1832 } 1833 }, 1834 .void => return true, 1835 .bool, .int, .@"enum" => return a == b, 1836 .@"union" => { 1837 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1838 return switch (a) { 1839 inline else => |value, tag| fieldEqual(@TypeOf(value), value, @field(b, @tagName(tag))), 1840 }; 1841 }, 1842 .@"struct" => return fieldsEqual(Field, a, b), 1843 .optional => |optional_info| { 1844 if (a) |a_value| { 1845 if (b) |b_value| return fieldEqual(optional_info.child, a_value, b_value); 1846 return false; 1847 } 1848 return b == null; 1849 }, 1850 else => @compileError("unsupported field type: " ++ @typeName(Field)), 1851 } 1852 } 1853 1854 // 1855 // writing to xitdb 1856 // 1857 1858 pub fn upsert( 1859 comptime T: type, 1860 comptime DB: type, 1861 comptime hash_kind: hash.HashKind, 1862 map: DB.HashMap(.read_write), 1863 event: T, 1864 ) !void { 1865 switch (@typeInfo(T)) { 1866 .@"struct" => |struct_info| { 1867 inline for (struct_info.fields) |field| { 1868 try upsertField(DB, hash_kind, map, field.name, field.type, @field(event, field.name)); 1869 } 1870 }, 1871 else => @compileError("upsert expects a struct"), 1872 } 1873 } 1874 1875 fn upsertField( 1876 comptime DB: type, 1877 comptime hash_kind: hash.HashKind, 1878 map: DB.HashMap(.read_write), 1879 comptime field_name: []const u8, 1880 comptime Field: type, 1881 value: Field, 1882 ) !void { 1883 const key = hash.hashInt(hash_kind, field_name); 1884 1885 switch (@typeInfo(Field)) { 1886 .pointer => |pointer_info| { 1887 if (pointer_info.size == .slice and pointer_info.child == u8) { 1888 try upsertBytes(DB, hash_kind, map, key, value); 1889 } else { 1890 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1891 } 1892 }, 1893 .array => |array_info| { 1894 if (array_info.child == u8) { 1895 try upsertBytes(DB, hash_kind, map, key, &value); 1896 } else { 1897 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1898 } 1899 }, 1900 .int => |int_info| switch (int_info.signedness) { 1901 .unsigned => { 1902 if (try map.getCursor(key)) |value_cursor| { 1903 if (try value_cursor.readUint() == value) { 1904 return; 1905 } 1906 } 1907 1908 try map.put(key, .{ .uint = value }); 1909 }, 1910 .signed => { 1911 if (try map.getCursor(key)) |value_cursor| { 1912 if (try value_cursor.readInt() == value) { 1913 return; 1914 } 1915 } 1916 1917 try map.put(key, .{ .int = value }); 1918 }, 1919 }, 1920 .bool => { 1921 const bytes = if (value) "true" else "false"; 1922 if (try map.getCursor(key)) |value_cursor| { 1923 var buffer: [5]u8 = undefined; 1924 const existing = try value_cursor.readBytesObject(&buffer); 1925 if (existing.format_tag) |format_tag| { 1926 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 1927 return; 1928 } 1929 } 1930 } 1931 1932 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 1933 }, 1934 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 1935 .@"union" => switch (value) { 1936 inline else => |payload, tag| { 1937 if (@TypeOf(payload) == void) { 1938 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 1939 } else { 1940 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 1941 if (try map.getCursor(key)) |cursor| { 1942 switch (cursor.slot().tag) { 1943 .hash_map => { 1944 const variants = try DB.HashMap(.read_only).init(cursor); 1945 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 1946 }, 1947 else => try map.put(key, .{ .slot = null }), 1948 } 1949 } 1950 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 1951 if (@typeInfo(@TypeOf(payload)) == .optional) { 1952 // keep the variant even when its payload is null 1953 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 1954 } 1955 if (try variants.getCursor(tag_key)) |cursor| { 1956 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 1957 } 1958 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 1959 } 1960 }, 1961 }, 1962 .optional => |optional_info| { 1963 // a missing key is null 1964 if (value) |child| { 1965 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 1966 } else { 1967 _ = try map.remove(key); 1968 } 1969 }, 1970 .@"struct" => try upsert( 1971 Field, 1972 DB, 1973 hash_kind, 1974 try DB.HashMap(.read_write).init(try map.putCursor(key)), 1975 value, 1976 ), 1977 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 1978 } 1979 } 1980 1981 fn upsertBytes( 1982 comptime DB: type, 1983 comptime hash_kind: hash.HashKind, 1984 map: DB.HashMap(.read_write), 1985 key: hash.HashInt(hash_kind), 1986 value: []const u8, 1987 ) !void { 1988 var existing_cursor_maybe = try map.getCursor(key); 1989 if (existing_cursor_maybe) |*existing_cursor| { 1990 if (try bytesEqual(DB, existing_cursor, value)) { 1991 return; 1992 } 1993 } 1994 1995 try map.put(key, .{ .bytes = value }); 1996 } 1997 1998 fn bytesEqual( 1999 comptime DB: type, 2000 cursor: *DB.Cursor(.read_only), 2001 value: []const u8, 2002 ) !bool { 2003 switch (cursor.slot().tag) { 2004 .bytes, .short_bytes => {}, 2005 else => return false, 2006 } 2007 var read_buffer: [1024]u8 = undefined; 2008 var reader = try cursor.reader(&read_buffer); 2009 if (reader.size != value.len) { 2010 return false; 2011 } 2012 2013 var chunk_buffer: [1024]u8 = undefined; 2014 var offset: usize = 0; 2015 while (offset < value.len) { 2016 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2017 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2018 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2019 return false; 2020 } 2021 offset += chunk_len; 2022 } 2023 2024 return true; 2025 } 2026 2027 // 2028 // CommitIterator 2029 // 2030 // differs from xit's ObjectIterator in a few ways: 2031 // 2032 // 1. iterates parents first, rather than children first 2033 // 2. `next` only returns the object id, not the full Object 2034 // 3. stores its temporary state in a file on disk to avoid OOMs 2035 // 2036 2037 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2038 return struct { 2039 const DB = EventDB(repo_opts.hash); 2040 // a plain file is used for the scratch db because each write outside 2041 // a transaction is synced anyway, so buffering would gain nothing 2042 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2043 const db_name = "haxy-repo-events.db"; 2044 2045 repo_dir: std.Io.Dir, 2046 db_file: std.Io.File, 2047 db: *TempDB, 2048 // map of each object id to its children 2049 parent_to_children: TempDB.HashMap(.read_write), 2050 // tracking pending parents is necessary because a child can only 2051 // be returned after all of its unconsumed parents have been returned 2052 child_to_pending_parents: TempDB.HashMap(.read_write), 2053 // queue of object ids that are ready to be returned from `next` 2054 ready_queue: TempDB.ArrayList(.read_write), 2055 ready_queue_index: u64, 2056 // the object id at the tip of this branch 2057 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2058 // the object id furthest back in the history that hasn't been consumed 2059 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2060 2061 pub fn init( 2062 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2063 io: std.Io, 2064 allocator: std.mem.Allocator, 2065 consumed_object_ids: DB.HashMap(.read_only), 2066 ref: rf.Ref, 2067 ) !CommitIterator(repo_kind, repo_opts) { 2068 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2069 errdefer { 2070 db_file.close(io); 2071 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2072 } 2073 2074 // cursors point at the db, so it needs a stable address 2075 const db_ptr = try allocator.create(TempDB); 2076 errdefer allocator.destroy(db_ptr); 2077 // the db is scratch state that gets deleted after iteration, so 2078 // there is nothing worth fsyncing 2079 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2080 2081 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2082 2083 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2084 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2085 2086 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2087 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2088 2089 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2090 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2091 2092 var self = CommitIterator(repo_kind, repo_opts){ 2093 .repo_dir = state.core.repo_dir, 2094 .db_file = db_file, 2095 .db = db_ptr, 2096 .parent_to_children = parent_to_children, 2097 .child_to_pending_parents = child_to_pending_parents, 2098 .ready_queue = ready_queue, 2099 .ready_queue_index = 0, 2100 .newest_object_id = null, 2101 .oldest_object_id = null, 2102 }; 2103 errdefer self.deinit(io, allocator); 2104 2105 try self.collect(state, io, allocator, consumed_object_ids, ref); 2106 2107 return self; 2108 } 2109 2110 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2111 self.db_file.close(io); 2112 self.repo_dir.deleteFile(io, db_name) catch {}; 2113 allocator.destroy(self.db); 2114 } 2115 2116 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2117 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2118 2119 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2120 self.ready_queue_index += 1; 2121 2122 try self.enqueueChildren(&oid); 2123 return oid; 2124 } 2125 2126 fn collect( 2127 self: *CommitIterator(repo_kind, repo_opts), 2128 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2129 io: std.Io, 2130 allocator: std.mem.Allocator, 2131 consumed_object_ids: DB.HashMap(.read_only), 2132 ref: rf.Ref, 2133 ) !void { 2134 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2135 2136 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2137 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2138 var walk_queue_index: u64 = 0; 2139 2140 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2141 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2142 2143 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2144 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2145 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2146 try walk_queue.append(.{ .bytes = &head_oid }); 2147 2148 while (walk_queue_index < try walk_queue.count()) { 2149 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2150 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2151 2152 walk_queue_index += 1; 2153 2154 // if we've seen this object id, skip it 2155 if (null != try seen_object_ids.getCursor(oid_int)) { 2156 continue; 2157 } 2158 2159 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2160 2161 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2162 defer commit_object.deinit(); 2163 2164 // if this object id has already been consumed, skip it 2165 if (null != try consumed_object_ids.getCursor(oid_int)) { 2166 continue; 2167 } 2168 2169 if (self.newest_object_id == null) self.newest_object_id = oid; 2170 self.oldest_object_id = oid; 2171 2172 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2173 var pending_parent_count: u64 = 0; 2174 for (parent_oids) |*parent_oid| { 2175 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2176 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2177 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2178 2179 // if this object id has already been consumed, skip it 2180 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2181 continue; 2182 } 2183 2184 // if this object id hasn't already been seen, add it to the walk queue 2185 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2186 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2187 } 2188 2189 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2190 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2191 try children.put(oid_int, .{ .bytes = &oid }); 2192 pending_parent_count += 1; 2193 } 2194 2195 if (pending_parent_count == 0) { 2196 try self.ready_queue.append(.{ .bytes = &oid }); 2197 } else { 2198 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2199 } 2200 } 2201 } 2202 2203 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2204 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2205 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2206 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2207 var children_iter = try children.iterator(); 2208 2209 while (try children_iter.next()) |*child_cursor| { 2210 const kv_pair = try child_cursor.readKeyValuePair(); 2211 const child_oid_int = kv_pair.hash; 2212 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2213 const pending_parent_count = try pending_parent_cursor.readUint(); 2214 2215 if (pending_parent_count <= 1) { 2216 _ = try self.child_to_pending_parents.remove(child_oid_int); 2217 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2218 try self.ready_queue.append(.{ .bytes = &child_oid }); 2219 } else { 2220 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2221 } 2222 } 2223 } 2224 2225 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2226 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2227 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2228 _ = try oid_cursor.readBytes(&oid); 2229 return oid; 2230 } 2231 }; 2232 } 2233