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