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