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