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