╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2166 db: *TempDB, 2167 // map of each object id to its children 2168 parent_to_children: TempDB.HashMap(.read_write), 2169 // tracking pending parents is necessary because a child can only 2170 // be returned after all of its unconsumed parents have been returned 2171 child_to_pending_parents: TempDB.HashMap(.read_write), 2172 // queue of object ids that are ready to be returned from `next` 2173 ready_queue: TempDB.ArrayList(.read_write), 2174 ready_queue_index: u64, 2175 // the object id at the tip of this branch 2176 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2177 // the object id furthest back in the history that hasn't been consumed 2178 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2179 2180 pub fn init( 2181 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2182 io: std.Io, 2183 allocator: std.mem.Allocator, 2184 consumed_object_ids: DB.HashMap(.read_only), 2185 ref: rf.Ref, 2186 ) !CommitIterator(repo_kind, repo_opts) { 2187 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2188 errdefer { 2189 db_file.close(io); 2190 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2191 } 2192 2193 // cursors point at the db, so it needs a stable address 2194 const db_ptr = try allocator.create(TempDB); 2195 errdefer allocator.destroy(db_ptr); 2196 // the db is scratch state that gets deleted after iteration, so 2197 // there is nothing worth fsyncing 2198 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2199 2200 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2201 2202 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2203 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2204 2205 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2206 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2207 2208 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2209 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2210 2211 var self = CommitIterator(repo_kind, repo_opts){ 2212 .repo_dir = state.core.repo_dir, 2213 .db_file = db_file, 2214 .db = db_ptr, 2215 .parent_to_children = parent_to_children, 2216 .child_to_pending_parents = child_to_pending_parents, 2217 .ready_queue = ready_queue, 2218 .ready_queue_index = 0, 2219 .newest_object_id = null, 2220 .oldest_object_id = null, 2221 }; 2222 errdefer self.deinit(io, allocator); 2223 2224 try self.collect(state, io, allocator, consumed_object_ids, ref); 2225 2226 return self; 2227 } 2228 2229 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2230 self.db_file.close(io); 2231 self.repo_dir.deleteFile(io, db_name) catch {}; 2232 allocator.destroy(self.db); 2233 } 2234 2235 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2236 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2237 2238 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2239 self.ready_queue_index += 1; 2240 2241 try self.enqueueChildren(&oid); 2242 return oid; 2243 } 2244 2245 fn collect( 2246 self: *CommitIterator(repo_kind, repo_opts), 2247 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2248 io: std.Io, 2249 allocator: std.mem.Allocator, 2250 consumed_object_ids: DB.HashMap(.read_only), 2251 ref: rf.Ref, 2252 ) !void { 2253 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2254 2255 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2256 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2257 var walk_queue_index: u64 = 0; 2258 2259 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2260 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2261 2262 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2263 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2264 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2265 try walk_queue.append(.{ .bytes = &head_oid }); 2266 2267 while (walk_queue_index < try walk_queue.count()) { 2268 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2269 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2270 2271 walk_queue_index += 1; 2272 2273 // if we've seen this object id, skip it 2274 if (null != try seen_object_ids.getCursor(oid_int)) { 2275 continue; 2276 } 2277 2278 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2279 2280 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2281 defer commit_object.deinit(); 2282 2283 // if this object id has already been consumed, skip it 2284 if (null != try consumed_object_ids.getCursor(oid_int)) { 2285 continue; 2286 } 2287 2288 if (self.newest_object_id == null) self.newest_object_id = oid; 2289 self.oldest_object_id = oid; 2290 2291 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2292 var pending_parent_count: u64 = 0; 2293 for (parent_oids) |*parent_oid| { 2294 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2295 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2296 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2297 2298 // if this object id has already been consumed, skip it 2299 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2300 continue; 2301 } 2302 2303 // if this object id hasn't already been seen, add it to the walk queue 2304 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2305 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2306 } 2307 2308 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2309 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2310 try children.put(oid_int, .{ .bytes = &oid }); 2311 pending_parent_count += 1; 2312 } 2313 2314 if (pending_parent_count == 0) { 2315 try self.ready_queue.append(.{ .bytes = &oid }); 2316 } else { 2317 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2318 } 2319 } 2320 } 2321 2322 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2323 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2324 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2325 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2326 var children_iter = try children.iterator(); 2327 2328 while (try children_iter.next()) |*child_cursor| { 2329 const kv_pair = try child_cursor.readKeyValuePair(); 2330 const child_oid_int = kv_pair.hash; 2331 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2332 const pending_parent_count = try pending_parent_cursor.readUint(); 2333 2334 if (pending_parent_count <= 1) { 2335 _ = try self.child_to_pending_parents.remove(child_oid_int); 2336 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2337 try self.ready_queue.append(.{ .bytes = &child_oid }); 2338 } else { 2339 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2340 } 2341 } 2342 } 2343 2344 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2345 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2346 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2347 _ = try oid_cursor.readBytes(&oid); 2348 return oid; 2349 } 2350 }; 2351 } 2352