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