╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1980 return false; 1981 } 1982 1983 var chunk_buffer: [1024]u8 = undefined; 1984 var offset: usize = 0; 1985 while (offset < value.len) { 1986 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1987 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1988 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1989 return false; 1990 } 1991 offset += chunk_len; 1992 } 1993 1994 return true; 1995 } 1996 1997 // 1998 // CommitIterator 1999 // 2000 // differs from xit's ObjectIterator in a few ways: 2001 // 2002 // 1. iterates parents first, rather than children first 2003 // 2. `next` only returns the object id, not the full Object 2004 // 3. stores its temporary state in a file on disk to avoid OOMs 2005 // 2006 2007 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2008 return struct { 2009 const DB = EventDB(repo_opts.hash); 2010 // a plain file is used for the scratch db because each write outside 2011 // a transaction is synced anyway, so buffering would gain nothing 2012 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2013 const db_name = "haxy-repo-events.db"; 2014 2015 repo_dir: std.Io.Dir, 2016 db_file: std.Io.File, 2017 db: *TempDB, 2018 // map of each object id to its children 2019 parent_to_children: TempDB.HashMap(.read_write), 2020 // tracking pending parents is necessary because a child can only 2021 // be returned after all of its unconsumed parents have been returned 2022 child_to_pending_parents: TempDB.HashMap(.read_write), 2023 // queue of object ids that are ready to be returned from `next` 2024 ready_queue: TempDB.ArrayList(.read_write), 2025 ready_queue_index: u64, 2026 // the object id at the tip of this branch 2027 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2028 // the object id furthest back in the history that hasn't been consumed 2029 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2030 2031 pub fn init( 2032 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2033 io: std.Io, 2034 allocator: std.mem.Allocator, 2035 consumed_object_ids: DB.HashMap(.read_only), 2036 ref: rf.Ref, 2037 ) !CommitIterator(repo_kind, repo_opts) { 2038 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2039 errdefer { 2040 db_file.close(io); 2041 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2042 } 2043 2044 // cursors point at the db, so it needs a stable address 2045 const db_ptr = try allocator.create(TempDB); 2046 errdefer allocator.destroy(db_ptr); 2047 // the db is scratch state that gets deleted after iteration, so 2048 // there is nothing worth fsyncing 2049 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2050 2051 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2052 2053 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2054 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2055 2056 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2057 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2058 2059 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2060 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2061 2062 var self = CommitIterator(repo_kind, repo_opts){ 2063 .repo_dir = state.core.repo_dir, 2064 .db_file = db_file, 2065 .db = db_ptr, 2066 .parent_to_children = parent_to_children, 2067 .child_to_pending_parents = child_to_pending_parents, 2068 .ready_queue = ready_queue, 2069 .ready_queue_index = 0, 2070 .newest_object_id = null, 2071 .oldest_object_id = null, 2072 }; 2073 errdefer self.deinit(io, allocator); 2074 2075 try self.collect(state, io, allocator, consumed_object_ids, ref); 2076 2077 return self; 2078 } 2079 2080 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2081 self.db_file.close(io); 2082 self.repo_dir.deleteFile(io, db_name) catch {}; 2083 allocator.destroy(self.db); 2084 } 2085 2086 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2087 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2088 2089 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2090 self.ready_queue_index += 1; 2091 2092 try self.enqueueChildren(&oid); 2093 return oid; 2094 } 2095 2096 fn collect( 2097 self: *CommitIterator(repo_kind, repo_opts), 2098 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2099 io: std.Io, 2100 allocator: std.mem.Allocator, 2101 consumed_object_ids: DB.HashMap(.read_only), 2102 ref: rf.Ref, 2103 ) !void { 2104 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2105 2106 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2107 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2108 var walk_queue_index: u64 = 0; 2109 2110 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2111 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2112 2113 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2114 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2115 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2116 try walk_queue.append(.{ .bytes = &head_oid }); 2117 2118 while (walk_queue_index < try walk_queue.count()) { 2119 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2120 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2121 2122 walk_queue_index += 1; 2123 2124 // if we've seen this object id, skip it 2125 if (null != try seen_object_ids.getCursor(oid_int)) { 2126 continue; 2127 } 2128 2129 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2130 2131 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2132 defer commit_object.deinit(); 2133 2134 // if this object id has already been consumed, skip it 2135 if (null != try consumed_object_ids.getCursor(oid_int)) { 2136 continue; 2137 } 2138 2139 if (self.newest_object_id == null) self.newest_object_id = oid; 2140 self.oldest_object_id = oid; 2141 2142 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2143 var pending_parent_count: u64 = 0; 2144 for (parent_oids) |*parent_oid| { 2145 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2146 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2147 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2148 2149 // if this object id has already been consumed, skip it 2150 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2151 continue; 2152 } 2153 2154 // if this object id hasn't already been seen, add it to the walk queue 2155 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2156 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2157 } 2158 2159 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2160 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2161 try children.put(oid_int, .{ .bytes = &oid }); 2162 pending_parent_count += 1; 2163 } 2164 2165 if (pending_parent_count == 0) { 2166 try self.ready_queue.append(.{ .bytes = &oid }); 2167 } else { 2168 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2169 } 2170 } 2171 } 2172 2173 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2174 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2175 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2176 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2177 var children_iter = try children.iterator(); 2178 2179 while (try children_iter.next()) |*child_cursor| { 2180 const kv_pair = try child_cursor.readKeyValuePair(); 2181 const child_oid_int = kv_pair.hash; 2182 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2183 const pending_parent_count = try pending_parent_cursor.readUint(); 2184 2185 if (pending_parent_count <= 1) { 2186 _ = try self.child_to_pending_parents.remove(child_oid_int); 2187 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2188 try self.ready_queue.append(.{ .bytes = &child_oid }); 2189 } else { 2190 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2191 } 2192 } 2193 } 2194 2195 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2196 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2197 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2198 _ = try oid_cursor.readBytes(&oid); 2199 return oid; 2200 } 2201 }; 2202 } 2203