╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1903 return; 1904 } 1905 } 1906 1907 var value_cursor = try map.putCursor(key); 1908 var write_buffer: [1024]u8 = undefined; 1909 var writer = try value_cursor.writer(&write_buffer); 1910 try writer.interface.writeAll(value); 1911 try writer.finish(); 1912 } 1913 1914 fn bytesEqual( 1915 comptime DB: type, 1916 cursor: *DB.Cursor(.read_only), 1917 value: []const u8, 1918 ) !bool { 1919 var read_buffer: [1024]u8 = undefined; 1920 var reader = try cursor.reader(&read_buffer); 1921 if (reader.size != value.len) { 1922 return false; 1923 } 1924 1925 var chunk_buffer: [1024]u8 = undefined; 1926 var offset: usize = 0; 1927 while (offset < value.len) { 1928 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1929 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1930 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1931 return false; 1932 } 1933 offset += chunk_len; 1934 } 1935 1936 return true; 1937 } 1938 1939 // 1940 // CommitIterator 1941 // 1942 // differs from xit's ObjectIterator in a few ways: 1943 // 1944 // 1. iterates parents first, rather than children first 1945 // 2. `next` only returns the object id, not the full Object 1946 // 3. stores its temporary state in a file on disk to avoid OOMs 1947 // 1948 1949 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1950 return struct { 1951 const DB = EventDB(repo_opts.hash); 1952 const db_name = "haxy-repo-events.db"; 1953 1954 repo_dir: std.Io.Dir, 1955 db_file: std.Io.File, 1956 db: *DB, 1957 // map of each object id to its children 1958 parent_to_children: DB.HashMap(.read_write), 1959 // tracking pending parents is necessary because a child can only 1960 // be returned after all of its unconsumed parents have been returned 1961 child_to_pending_parents: DB.HashMap(.read_write), 1962 // queue of object ids that are ready to be returned from `next` 1963 ready_queue: DB.ArrayList(.read_write), 1964 ready_queue_index: u64, 1965 // the object id at the tip of this branch 1966 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1967 // the object id furthest back in the history that hasn't been consumed 1968 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1969 1970 pub fn init( 1971 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1972 io: std.Io, 1973 allocator: std.mem.Allocator, 1974 consumed_object_ids: DB.HashMap(.read_only), 1975 ref: rf.Ref, 1976 ) !CommitIterator(repo_kind, repo_opts) { 1977 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 1978 errdefer { 1979 db_file.close(io); 1980 state.core.repo_dir.deleteFile(io, db_name) catch {}; 1981 } 1982 1983 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 1984 errdefer allocator.destroy(buffer_ptr); 1985 1986 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 1987 errdefer buffer_ptr.deinit(); 1988 1989 const db_ptr = try allocator.create(DB); 1990 errdefer allocator.destroy(db_ptr); 1991 // the db is scratch state that gets deleted after iteration, so 1992 // there is nothing worth fsyncing 1993 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr, .fsync = false }); 1994 1995 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 1996 1997 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 1998 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 1999 2000 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2001 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2002 2003 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2004 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 2005 2006 var self = CommitIterator(repo_kind, repo_opts){ 2007 .repo_dir = state.core.repo_dir, 2008 .db_file = db_file, 2009 .db = db_ptr, 2010 .parent_to_children = parent_to_children, 2011 .child_to_pending_parents = child_to_pending_parents, 2012 .ready_queue = ready_queue, 2013 .ready_queue_index = 0, 2014 .newest_object_id = null, 2015 .oldest_object_id = null, 2016 }; 2017 errdefer self.deinit(io, allocator); 2018 2019 try self.collect(state, io, allocator, consumed_object_ids, ref); 2020 2021 return self; 2022 } 2023 2024 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 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