╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2025 self.db_file.close(io); 2026 self.db.core.memory.buffer.deinit(); 2027 allocator.destroy(self.db.core.memory.buffer); 2028 self.repo_dir.deleteFile(io, db_name) catch {}; 2029 allocator.destroy(self.db); 2030 } 2031 2032 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2033 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2034 2035 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2036 self.ready_queue_index += 1; 2037 2038 try self.enqueueChildren(&oid); 2039 return oid; 2040 } 2041 2042 fn collect( 2043 self: *CommitIterator(repo_kind, repo_opts), 2044 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2045 io: std.Io, 2046 allocator: std.mem.Allocator, 2047 consumed_object_ids: DB.HashMap(.read_only), 2048 ref: rf.Ref, 2049 ) !void { 2050 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 2051 2052 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2053 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 2054 var walk_queue_index: u64 = 0; 2055 2056 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2057 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 2058 2059 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2060 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2061 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2062 try walk_queue.append(.{ .bytes = &head_oid }); 2063 2064 while (walk_queue_index < try walk_queue.count()) { 2065 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2066 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2067 2068 walk_queue_index += 1; 2069 2070 // if we've seen this object id, skip it 2071 if (null != try seen_object_ids.getCursor(oid_int)) { 2072 continue; 2073 } 2074 2075 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2076 2077 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2078 defer commit_object.deinit(); 2079 2080 // if this object id has already been consumed, skip it 2081 if (null != try consumed_object_ids.getCursor(oid_int)) { 2082 continue; 2083 } 2084 2085 if (self.newest_object_id == null) self.newest_object_id = oid; 2086 self.oldest_object_id = oid; 2087 2088 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2089 var pending_parent_count: u64 = 0; 2090 for (parent_oids) |*parent_oid| { 2091 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2092 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2093 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2094 2095 // if this object id has already been consumed, skip it 2096 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2097 continue; 2098 } 2099 2100 // if this object id hasn't already been seen, add it to the walk queue 2101 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2102 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2103 } 2104 2105 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2106 const children = try DB.HashSet(.read_write).init(children_cursor); 2107 try children.put(oid_int, .{ .bytes = &oid }); 2108 pending_parent_count += 1; 2109 } 2110 2111 if (pending_parent_count == 0) { 2112 try self.ready_queue.append(.{ .bytes = &oid }); 2113 } else { 2114 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2115 } 2116 } 2117 } 2118 2119 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2120 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2121 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2122 const children = try DB.HashSet(.read_only).init(children_cursor); 2123 var children_iter = try children.iterator(); 2124 2125 while (try children_iter.next()) |*child_cursor| { 2126 const kv_pair = try child_cursor.readKeyValuePair(); 2127 const child_oid_int = kv_pair.hash; 2128 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2129 const pending_parent_count = try pending_parent_cursor.readUint(); 2130 2131 if (pending_parent_count <= 1) { 2132 _ = try self.child_to_pending_parents.remove(child_oid_int); 2133 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2134 try self.ready_queue.append(.{ .bytes = &child_oid }); 2135 } else { 2136 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2137 } 2138 } 2139 } 2140 2141 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2142 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2143 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2144 _ = try oid_cursor.readBytes(&oid); 2145 return oid; 2146 } 2147 }; 2148 } 2149