╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1935 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1936 // the object id furthest back in the history that hasn't been consumed 1937 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1938 1939 pub fn init( 1940 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1941 io: std.Io, 1942 allocator: std.mem.Allocator, 1943 consumed_object_ids: DB.HashMap(.read_only), 1944 ref: rf.Ref, 1945 ) !CommitIterator(repo_kind, repo_opts) { 1946 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 1947 errdefer { 1948 db_file.close(io); 1949 state.core.repo_dir.deleteFile(io, db_name) catch {}; 1950 } 1951 1952 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 1953 errdefer allocator.destroy(buffer_ptr); 1954 1955 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 1956 errdefer buffer_ptr.deinit(); 1957 1958 const db_ptr = try allocator.create(DB); 1959 errdefer allocator.destroy(db_ptr); 1960 // the db is scratch state that gets deleted after iteration, so 1961 // there is nothing worth fsyncing 1962 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr, .fsync = false }); 1963 1964 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 1965 1966 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 1967 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 1968 1969 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 1970 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 1971 1972 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 1973 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 1974 1975 var self = CommitIterator(repo_kind, repo_opts){ 1976 .repo_dir = state.core.repo_dir, 1977 .db_file = db_file, 1978 .db = db_ptr, 1979 .parent_to_children = parent_to_children, 1980 .child_to_pending_parents = child_to_pending_parents, 1981 .ready_queue = ready_queue, 1982 .ready_queue_index = 0, 1983 .newest_object_id = null, 1984 .oldest_object_id = null, 1985 }; 1986 errdefer self.deinit(io, allocator); 1987 1988 try self.collect(state, io, allocator, consumed_object_ids, ref); 1989 1990 return self; 1991 } 1992 1993 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 1994 self.db_file.close(io); 1995 self.db.core.memory.buffer.deinit(); 1996 allocator.destroy(self.db.core.memory.buffer); 1997 self.repo_dir.deleteFile(io, db_name) catch {}; 1998 allocator.destroy(self.db); 1999 } 2000 2001 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2002 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2003 2004 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2005 self.ready_queue_index += 1; 2006 2007 try self.enqueueChildren(&oid); 2008 return oid; 2009 } 2010 2011 fn collect( 2012 self: *CommitIterator(repo_kind, repo_opts), 2013 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2014 io: std.Io, 2015 allocator: std.mem.Allocator, 2016 consumed_object_ids: DB.HashMap(.read_only), 2017 ref: rf.Ref, 2018 ) !void { 2019 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 2020 2021 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2022 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 2023 var walk_queue_index: u64 = 0; 2024 2025 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2026 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 2027 2028 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2029 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2030 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2031 try walk_queue.append(.{ .bytes = &head_oid }); 2032 2033 while (walk_queue_index < try walk_queue.count()) { 2034 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2035 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2036 2037 walk_queue_index += 1; 2038 2039 // if we've seen this object id, skip it 2040 if (null != try seen_object_ids.getCursor(oid_int)) { 2041 continue; 2042 } 2043 2044 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2045 2046 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2047 defer commit_object.deinit(); 2048 2049 // if this object id has already been consumed, skip it 2050 if (null != try consumed_object_ids.getCursor(oid_int)) { 2051 continue; 2052 } 2053 2054 if (self.newest_object_id == null) self.newest_object_id = oid; 2055 self.oldest_object_id = oid; 2056 2057 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2058 var pending_parent_count: u64 = 0; 2059 for (parent_oids) |*parent_oid| { 2060 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2061 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2062 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2063 2064 // if this object id has already been consumed, skip it 2065 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2066 continue; 2067 } 2068 2069 // if this object id hasn't already been seen, add it to the walk queue 2070 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2071 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2072 } 2073 2074 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2075 const children = try DB.HashSet(.read_write).init(children_cursor); 2076 try children.put(oid_int, .{ .bytes = &oid }); 2077 pending_parent_count += 1; 2078 } 2079 2080 if (pending_parent_count == 0) { 2081 try self.ready_queue.append(.{ .bytes = &oid }); 2082 } else { 2083 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2084 } 2085 } 2086 } 2087 2088 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2089 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2090 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2091 const children = try DB.HashSet(.read_only).init(children_cursor); 2092 var children_iter = try children.iterator(); 2093 2094 while (try children_iter.next()) |*child_cursor| { 2095 const kv_pair = try child_cursor.readKeyValuePair(); 2096 const child_oid_int = kv_pair.hash; 2097 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2098 const pending_parent_count = try pending_parent_cursor.readUint(); 2099 2100 if (pending_parent_count <= 1) { 2101 _ = try self.child_to_pending_parents.remove(child_oid_int); 2102 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2103 try self.ready_queue.append(.{ .bytes = &child_oid }); 2104 } else { 2105 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2106 } 2107 } 2108 } 2109 2110 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2111 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2112 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2113 _ = try oid_cursor.readBytes(&oid); 2114 return oid; 2115 } 2116 }; 2117 } 2118