╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
2157 owner_id: *const [event_id_size]u8, 2158 author: CommitAuthor, 2159 old_name: []const u8, 2160 name: []const u8, 2161 description: []const u8, 2162 read_access: Repo.Access, 2163 discuss_role: ?Repo.Role, 2164 issue_role: ?Repo.Role, 2165 patch_role: ?Repo.Role, 2166 ) !void { 2167 try Repo.validateName(name); 2168 var user_repo = (try openUserRepo(io, allocator, users_dir, owner_id)) orelse return error.NotFound; 2169 defer user_repo.deinit(io, allocator); 2170 var arena = std.heap.ArenaAllocator.init(allocator); 2171 defer arena.deinit(); 2172 const moment = (try userMoment(&user_repo)) orelse return error.NotFound; 2173 const found = (try Repo.readByName(UserDB, user_repo_opts.hash, moment, &arena, old_name)) orelse return error.NotFound; 2174 if (try Repo.readByName(UserDB, user_repo_opts.hash, moment, &arena, name)) |taken| { 2175 if (!std.mem.eql(u8, &taken.event_id, &found.event_id)) return error.NameTaken; 2176 } 2177 2178 var event = found.repo.event; 2179 event.name = name; 2180 event.description = description; 2181 event.read_access = read_access; 2182 event.discuss_role = discuss_role; 2183 event.issue_role = issue_role; 2184 event.patch_role = patch_role; 2185 try consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, user_repo_opts, io, allocator, &user_repo, events_ref, &[_]EventWithId{.{ 2186 .id = std.fmt.bytesToHex(found.event_id, .lower), 2187 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2188 .author = author, 2189 .event = .{ .repo = event }, 2190 }}); 2191 } 2192 2193 // grant `user_id` `role` on the repo `repo_id` in its owner's user repo, 2194 // replacing any role they held 2195 pub fn grantRole( 2196 io: std.Io, 2197 allocator: std.mem.Allocator, 2198 users_dir: []const u8, 2199 owner_id: *const [event_id_size]u8, 2200 repo_id: *const [event_id_size]u8, 2201 user_id: *const [event_id_size]u8, 2202 role: Repo.Role, 2203 author: CommitAuthor, 2204 ) !void { 2205 var user_repo = (try openUserRepo(io, allocator, users_dir, owner_id)) orelse return error.NotFound; 2206 defer user_repo.deinit(io, allocator); 2207 try consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, user_repo_opts, io, allocator, &user_repo, events_ref, &[_]EventWithId{.{ 2208 .id = std.fmt.bytesToHex(Grant.idOf(repo_id, user_id), .lower), 2209 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2210 .author = author, 2211 .event = .{ .grant = .{ .target_id = std.fmt.bytesToHex(repo_id.*, .lower), .user_id = std.fmt.bytesToHex(user_id.*, .lower), .role = role } }, 2212 }}); 2213 } 2214 2215 // revoke `user_id`'s grant on the repo `repo_id` in its owner's user repo 2216 pub fn revokeRole( 2217 io: std.Io, 2218 allocator: std.mem.Allocator, 2219 users_dir: []const u8, 2220 owner_id: *const [event_id_size]u8, 2221 repo_id: *const [event_id_size]u8, 2222 user_id: *const [event_id_size]u8, 2223 author: CommitAuthor, 2224 ) !void { 2225 var user_repo = (try openUserRepo(io, allocator, users_dir, owner_id)) orelse return error.NotFound; 2226 defer user_repo.deinit(io, allocator); 2227 // a revoked grant stays in the kind index, which is all `remove` checks 2228 { 2229 var arena = std.heap.ArenaAllocator.init(allocator); 2230 defer arena.deinit(); 2231 const moment = (try userMoment(&user_repo)) orelse return error.EventNotFound; 2232 if (try Grant.readRole(UserDB, user_repo_opts.hash, moment, &arena, repo_id, user_id) == null) return error.EventNotFound; 2233 } 2234 try remove(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, user_repo_opts, io, allocator, &user_repo, &Grant.idOf(repo_id, user_id), .grant, author); 2235 } 2236 2237 // add `branch` to the protected branches of the repo `repo_id` in its owner's 2238 // user repo, or take it out when `protect` is false. taking out a branch that 2239 // isn't protected writes nothing. 2240 pub fn protectBranch( 2241 io: std.Io, 2242 allocator: std.mem.Allocator, 2243 users_dir: []const u8, 2244 owner_id: *const [event_id_size]u8, 2245 repo_id: *const [event_id_size]u8, 2246 branch: []const u8, 2247 protect: bool, 2248 author: CommitAuthor, 2249 ) !void { 2250 var user_repo = (try openUserRepo(io, allocator, users_dir, owner_id)) orelse return error.NotFound; 2251 defer user_repo.deinit(io, allocator); 2252 var arena = std.heap.ArenaAllocator.init(allocator); 2253 defer arena.deinit(); 2254 const aa = arena.allocator(); 2255 const moment = (try userMoment(&user_repo)) orelse return error.NotFound; 2256 const repo = (try Repo.readById(UserDB, user_repo_opts.hash, moment, &arena, repo_id)) orelse return error.NotFound; 2257 if (repo.removed) return error.NotFound; 2258 2259 var branches: std.ArrayList(u8) = .empty; 2260 var count: usize = 0; 2261 var held = false; 2262 for (try repo.event.protectedBranchNames(aa)) |name| { 2263 if (std.mem.eql(u8, name, branch)) { 2264 held = true; 2265 if (!protect) continue; 2266 } 2267 if (branches.items.len > 0) try branches.append(aa, '\n'); 2268 try branches.appendSlice(aa, name); 2269 count += 1; 2270 } 2271 if (protect) { 2272 if (held) return error.BranchProtected; 2273 if (count >= Repo.protected_branch_max_count) return error.TooManyProtectedBranches; 2274 if (branches.items.len > 0) try branches.append(aa, '\n'); 2275 try branches.appendSlice(aa, branch); 2276 } else if (!held) return; 2277 2278 var event = repo.event; 2279 event.protected_branches = branches.items; 2280 try consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, user_repo_opts, io, allocator, &user_repo, events_ref, &[_]EventWithId{.{ 2281 .id = std.fmt.bytesToHex(repo_id.*, .lower), 2282 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2283 .author = author, 2284 .event = .{ .repo = event }, 2285 }}); 2286 } 2287 2288 // create a user and their user repo, returning the new user's id 2289 pub fn createUser( 2290 io: std.Io, 2291 allocator: std.mem.Allocator, 2292 users_dir: []const u8, 2293 admin_repo: *rp.Repo(.xit, admin_repo_opts), 2294 name: []const u8, 2295 email: []const u8, 2296 password: []const u8, 2297 password_again: []const u8, 2298 ) ![event_id_size]u8 { 2299 try User.validateName(name); 2300 if (email.len == 0) return error.EmailEmpty; 2301 if (password.len == 0) return error.PasswordEmpty; 2302 if (!std.mem.eql(u8, password, password_again)) return error.PasswordMismatch; 2303 // a fresh admin repo has no moment until its first event 2304 if (currentMoment(admin_repo_opts, admin_repo)) |moment| { 2305 if (try User.readIdByName(AdminDB, admin_repo_opts.hash, moment, name) != null) return error.NameTaken; 2306 var arena = std.heap.ArenaAllocator.init(allocator); 2307 defer arena.deinit(); 2308 if (try User.readByEmail(AdminDB, admin_repo_opts.hash, moment, &arena, email) != null) return error.EmailTaken; 2309 } else |err| switch (err) { 2310 error.NotFound => {}, 2311 else => |e| return e, 2312 } 2313 2314 var password_hash_buf: [User.password_hash_max_len]u8 = undefined; 2315 const password_hash = try User.hashPassword(password, &password_hash_buf, io); 2316 2317 var id_bytes: [event_id_size]u8 = undefined; 2318 io.random(&id_bytes); 2319 2320 // the user repo comes first so a listed user can always create repos 2321 { 2322 const path = try userRepoPath(allocator, users_dir, &id_bytes); 2323 defer allocator.free(path); 2324 var user_repo = try initUserRepo(io, allocator, path); 2325 defer user_repo.deinit(io, allocator); 2326 } 2327 2328 try consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, admin_repo_opts, io, allocator, admin_repo, events_ref, &[_]EventWithId{.{ 2329 .id = std.fmt.bytesToHex(id_bytes, .lower), 2330 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2331 .author = .{ .name = name, .email = email }, 2332 .event = .{ .user = .{ .name = name, .email = email, .password_hash = password_hash } }, 2333 }}); 2334 return id_bytes; 2335 } 2336 2337 // change a user's email, and their password when the password fields are 2338 // filled in 2339 pub fn updateUser( 2340 io: std.Io, 2341 allocator: std.mem.Allocator, 2342 users_dir: []const u8, 2343 admin_repo: *rp.Repo(.xit, admin_repo_opts), 2344 user_id: *const [event_id_size]u8, 2345 email: []const u8, 2346 current_password: []const u8, 2347 new_password: []const u8, 2348 new_password_again: []const u8, 2349 ) !void { 2350 if (email.len == 0) return error.EmailEmpty; 2351 var arena = std.heap.ArenaAllocator.init(allocator); 2352 defer arena.deinit(); 2353 const moment = try currentMoment(admin_repo_opts, admin_repo); 2354 const user = (try User.readById(AdminDB, admin_repo_opts.hash, moment, &arena, user_id)) orelse return error.NotFound; 2355 if (user.removed) return error.NotFound; 2356 if (!std.mem.eql(u8, email, user.event.email) and try User.readByEmail(AdminDB, admin_repo_opts.hash, moment, &arena, email) != null) return error.EmailTaken; 2357 2358 var event = user.event; 2359 event.email = email; 2360 var password_hash_buf: [User.password_hash_max_len]u8 = undefined; 2361 if (current_password.len + new_password.len + new_password_again.len > 0) { 2362 if (!User.verifyPassword(user.event.password_hash, current_password)) return error.WrongPassword; 2363 if (new_password.len == 0) return error.PasswordEmpty; 2364 if (!std.mem.eql(u8, new_password, new_password_again)) return error.PasswordMismatch; 2365 event.password_hash = try User.hashPassword(new_password, &password_hash_buf, io); 2366 } 2367 2368 try consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, admin_repo_opts, io, allocator, admin_repo, events_ref, &[_]EventWithId{.{ 2369 .id = std.fmt.bytesToHex(user_id.*, .lower), 2370 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2371 .author = .{ .name = event.name, .email = email }, 2372 .event = .{ .user = event }, 2373 }}); 2374 } 2375 2376 // add an authorized_keys line to the user's ssh keys 2377 pub fn addUserKey( 2378 io: std.Io, 2379 allocator: std.mem.Allocator, 2380 users_dir: []const u8, 2381 admin_repo: *rp.Repo(.xit, admin_repo_opts), 2382 user_id: *const [event_id_size]u8, 2383 key_text: []const u8, 2384 ) !void { 2385 const key = std.mem.trim(u8, key_text, " \t\r"); 2386 // the keys are stored one per line 2387 if (std.mem.indexOfScalar(u8, key, '\n') != null) return error.InvalidKey; 2388 const fingerprint = User.fingerprintOfKey(key) orelse return error.InvalidKey; 2389 // the key types the ssh server verifies 2390 const key_type = std.mem.sliceTo(key, ' '); 2391 if (!std.mem.eql(u8, key_type, "ssh-ed25519") and !std.mem.eql(u8, key_type, "ssh-rsa")) return error.UnsupportedKey; 2392 try writeUserKeys(io, allocator, users_dir, admin_repo, user_id, &fingerprint, key); 2393 } 2394 2395 // remove the user's ssh key with this fingerprint. removing a key they don't 2396 // hold writes nothing. 2397 pub fn removeUserKey( 2398 io: std.Io, 2399 allocator: std.mem.Allocator, 2400 users_dir: []const u8, 2401 admin_repo: *rp.Repo(.xit, admin_repo_opts), 2402 user_id: *const [event_id_size]u8, 2403 fingerprint: *const [User.fingerprint_len]u8, 2404 ) !void { 2405 try writeUserKeys(io, allocator, users_dir, admin_repo, user_id, fingerprint, null); 2406 } 2407 2408 // rewrite the user's ssh keys with `added` appended, or without the key with 2409 // `fingerprint` when nothing is added 2410 fn writeUserKeys( 2411 io: std.Io, 2412 allocator: std.mem.Allocator, 2413 users_dir: []const u8, 2414 admin_repo: *rp.Repo(.xit, admin_repo_opts), 2415 user_id: *const [event_id_size]u8, 2416 fingerprint: *const [User.fingerprint_len]u8, 2417 added: ?[]const u8, 2418 ) !void { 2419 var arena = std.heap.ArenaAllocator.init(allocator); 2420 defer arena.deinit(); 2421 const moment = try currentMoment(admin_repo_opts, admin_repo); 2422 const user = (try User.readById(AdminDB, admin_repo_opts.hash, moment, &arena, user_id)) orelse return error.NotFound; 2423 if (user.removed) return error.NotFound; 2424 2425 var keys: std.ArrayList(u8) = .empty; 2426 var count: usize = 0; 2427 var held = false; 2428 var lines = std.mem.splitScalar(u8, user.event.ssh_keys, '\n'); 2429 while (lines.next()) |line| { 2430 const line_fingerprint = User.fingerprintOfKey(line) orelse continue; 2431 if (std.mem.eql(u8, &line_fingerprint, fingerprint)) { 2432 held = true; 2433 if (added == null) continue; 2434 } 2435 if (keys.items.len > 0) try keys.append(arena.allocator(), '\n'); 2436 try keys.appendSlice(arena.allocator(), line); 2437 count += 1; 2438 } 2439 if (added) |key| { 2440 if (held) return error.KeyAdded; 2441 if (count >= User.ssh_key_max_count) return error.TooManyKeys; 2442 if (keys.items.len > 0) try keys.append(arena.allocator(), '\n'); 2443 try keys.appendSlice(arena.allocator(), key); 2444 } else if (!held) return; 2445 2446 var event = user.event; 2447 event.ssh_keys = keys.items; 2448 try consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, admin_repo_opts, io, allocator, admin_repo, events_ref, &[_]EventWithId{.{ 2449 .id = std.fmt.bytesToHex(user_id.*, .lower), 2450 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2451 .author = .{ .name = event.name, .email = event.email }, 2452 .event = .{ .user = event }, 2453 }}); 2454 } 2455 2456 // consume a new repo's event into its owner's user repo and mark it active 2457 fn writeRepoEvent( 2458 io: std.Io, 2459 allocator: std.mem.Allocator, 2460 users_dir: []const u8, 2461 user_repo: *rp.Repo(.xit, user_repo_opts), 2462 owner_id: *const [event_id_size]u8, 2463 owner: CommitAuthor, 2464 id_bytes: *const [event_id_size]u8, 2465 name: []const u8, 2466 description: []const u8, 2467 read_access: Repo.Access, 2468 ) !void { 2469 const timestamp: u64 = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()); 2470 try consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, user_repo_opts, io, allocator, user_repo, events_ref, &[_]EventWithId{.{ 2471 .id = std.fmt.bytesToHex(id_bytes.*, .lower), 2472 .timestamp = timestamp, 2473 .author = owner, 2474 .event = .{ .repo = .{ 2475 .user_id = std.fmt.bytesToHex(owner_id.*, .lower), 2476 .name = name, 2477 .description = description, 2478 .read_access = read_access, 2479 } }, 2480 }}); 2481 bumpRepoActivity(io, allocator, users_dir, owner_id, id_bytes, timestamp) catch |err| { 2482 std.log.warn("failed to record repo activity: {s}", .{@errorName(err)}); 2483 }; 2484 } 2485 2486 // 2487 // reading from xitdb 2488 // 2489 2490 // read just `Fields`, a subset of `T.Record`, from the record with event id 2491 // `id`, or null when there is none 2492 pub fn readRecordSubset( 2493 comptime T: type, 2494 comptime Fields: type, 2495 comptime DB: type, 2496 comptime hash_kind: hash.HashKind, 2497 haxy_moment: DB.HashMap(.read_only), 2498 arena: *std.heap.ArenaAllocator, 2499 id: []const u8, 2500 ) !?Fields { 2501 comptime assertFieldSubset(Fields, T.Record); 2502 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, T.record_map_key)) orelse return null; 2503 const records = try DB.HashMap(.read_only).init(records_cursor); 2504 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 2505 return try read(Fields, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 2506 } 2507 2508 fn assertFieldSubset(comptime Sub: type, comptime Full: type) void { 2509 const info = @typeInfo(Sub).@"struct"; 2510 for (info.field_names, info.field_types) |field_name, field_type| { 2511 if (!@hasField(Full, field_name)) @compileError(@typeName(Full) ++ " has no field " ++ field_name); 2512 const FullField = @FieldType(Full, field_name); 2513 if (@typeInfo(field_type) == .@"struct") { 2514 assertFieldSubset(field_type, FullField); 2515 } else if (field_type != FullField) { 2516 @compileError(@typeName(Full) ++ "." ++ field_name ++ " is a " ++ @typeName(FullField)); 2517 } 2518 } 2519 } 2520 2521 pub fn read( 2522 comptime T: type, 2523 comptime DB: type, 2524 comptime hash_kind: hash.HashKind, 2525 arena: *std.heap.ArenaAllocator, 2526 map: DB.HashMap(.read_only), 2527 ) !T { 2528 var event: T = undefined; 2529 switch (@typeInfo(T)) { 2530 .@"struct" => |struct_info| { 2531 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2532 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)); 2533 @field(event, field_name) = try readField(field_type, DB, hash_kind, arena, cursor); 2534 } 2535 }, 2536 else => @compileError("read expects a struct"), 2537 } 2538 return event; 2539 } 2540 2541 fn readField( 2542 comptime Field: type, 2543 comptime DB: type, 2544 comptime hash_kind: hash.HashKind, 2545 arena: *std.heap.ArenaAllocator, 2546 cursor_maybe: ?DB.Cursor(.read_only), 2547 ) !Field { 2548 if (@typeInfo(Field) == .optional) { 2549 // a missing key or a null union payload is null 2550 const cursor = cursor_maybe orelse return null; 2551 if (cursor.slot().tag == .none) return null; 2552 return try readField(@typeInfo(Field).optional.child, DB, hash_kind, arena, cursor); 2553 } 2554 const cursor = cursor_maybe orelse return error.NotFound; 2555 switch (@typeInfo(Field)) { 2556 .pointer => |pointer_info| { 2557 if (pointer_info.size != .slice or pointer_info.child != u8 or !pointer_info.attrs.@"const") { 2558 @compileError("unsupported read field type: " ++ @typeName(Field)); 2559 } 2560 return try cursor.readBytesAlloc(arena.allocator(), null); 2561 }, 2562 .array => |array_info| { 2563 if (array_info.child != u8) @compileError("unsupported read field type: " ++ @typeName(Field)); 2564 var bytes: Field = undefined; 2565 if ((try cursor.readBytes(&bytes)).len != bytes.len) return error.InvalidByteArrayLength; 2566 return bytes; 2567 }, 2568 .bool => { 2569 var buffer: [5]u8 = undefined; 2570 const bytes = try cursor.readBytesObject(&buffer); 2571 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 2572 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 2573 if (std.mem.eql(u8, bytes.value, "true")) return true; 2574 if (std.mem.eql(u8, bytes.value, "false")) return false; 2575 return error.InvalidBool; 2576 }, 2577 .int => |int_info| return switch (int_info.signedness) { 2578 .unsigned => @intCast(try cursor.readUint()), 2579 .signed => @intCast(try cursor.readInt()), 2580 }, 2581 .@"enum" => return std.meta.stringToEnum(Field, try cursor.readBytesAlloc(arena.allocator(), null)) orelse error.InvalidEnumTag, 2582 .@"union" => |union_info| { 2583 if (union_info.tag_type == null) @compileError("unsupported read field type: " ++ @typeName(Field)); 2584 switch (cursor.slot().tag) { 2585 .bytes, .short_bytes => { 2586 const name = try cursor.readBytesAlloc(arena.allocator(), null); 2587 inline for (union_info.field_names, union_info.field_types) |variant_name, variant_type| { 2588 if (variant_type == void and std.mem.eql(u8, name, variant_name)) { 2589 return @unionInit(Field, variant_name, {}); 2590 } 2591 } 2592 return error.InvalidUnion; 2593 }, 2594 .hash_map => { 2595 const variants = try DB.HashMap(.read_only).init(cursor); 2596 var iter = try variants.iterator(); 2597 const entry = (try iter.next()) orelse return error.InvalidUnion; 2598 const pair = try entry.readKeyValuePair(); 2599 if (try iter.next() != null) return error.InvalidUnion; 2600 inline for (union_info.field_names, union_info.field_types) |variant_name, variant_type| { 2601 if (variant_type != void and pair.hash == hash.hashInt(hash_kind, variant_name)) { 2602 return @unionInit(Field, variant_name, try readField(variant_type, DB, hash_kind, arena, pair.value_cursor)); 2603 } 2604 } 2605 return error.InvalidUnion; 2606 }, 2607 else => return error.InvalidUnion, 2608 } 2609 }, 2610 .@"struct" => return try read(Field, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)), 2611 else => @compileError("unsupported read field type: " ++ @typeName(Field)), 2612 } 2613 } 2614 2615 // whether two events carry the same data 2616 fn fieldsEqual(comptime T: type, a: T, b: T) bool { 2617 switch (@typeInfo(T)) { 2618 .@"struct" => |struct_info| { 2619 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2620 if (!fieldEqual(field_type, @field(a, field_name), @field(b, field_name))) return false; 2621 } 2622 }, 2623 else => @compileError("fieldsEqual expects a struct"), 2624 } 2625 2626 return true; 2627 } 2628 2629 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 2630 switch (@typeInfo(Field)) { 2631 .pointer => |pointer_info| { 2632 if (pointer_info.size == .slice and pointer_info.child == u8) { 2633 return std.mem.eql(u8, a, b); 2634 } else { 2635 @compileError("unsupported field type: " ++ @typeName(Field)); 2636 } 2637 }, 2638 .array => |array_info| { 2639 if (array_info.child == u8) { 2640 return std.mem.eql(u8, &a, &b); 2641 } else { 2642 @compileError("unsupported field type: " ++ @typeName(Field)); 2643 } 2644 }, 2645 .void => return true, 2646 .bool, .int, .@"enum" => return a == b, 2647 .@"union" => { 2648 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 2649 return switch (a) { 2650 inline else => |value, tag| fieldEqual(@TypeOf(value), value, @field(b, @tagName(tag))), 2651 }; 2652 }, 2653 .@"struct" => return fieldsEqual(Field, a, b), 2654 .optional => |optional_info| { 2655 if (a) |a_value| { 2656 if (b) |b_value| return fieldEqual(optional_info.child, a_value, b_value); 2657 return false; 2658 } 2659 return b == null; 2660 }, 2661 else => @compileError("unsupported field type: " ++ @typeName(Field)), 2662 } 2663 } 2664 2665 // 2666 // writing to xitdb 2667 // 2668 2669 pub fn upsert( 2670 comptime T: type, 2671 comptime DB: type, 2672 comptime hash_kind: hash.HashKind, 2673 map: DB.HashMap(.read_write), 2674 event: T, 2675 ) !void { 2676 switch (@typeInfo(T)) { 2677 .@"struct" => |struct_info| { 2678 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2679 try upsertField(DB, hash_kind, map, field_name, field_type, @field(event, field_name)); 2680 } 2681 }, 2682 else => @compileError("upsert expects a struct"), 2683 } 2684 } 2685 2686 fn upsertField( 2687 comptime DB: type, 2688 comptime hash_kind: hash.HashKind, 2689 map: DB.HashMap(.read_write), 2690 comptime field_name: []const u8, 2691 comptime Field: type, 2692 value: Field, 2693 ) !void { 2694 const key = hash.hashInt(hash_kind, field_name); 2695 2696 switch (@typeInfo(Field)) { 2697 .pointer => |pointer_info| { 2698 if (pointer_info.size == .slice and pointer_info.child == u8) { 2699 try upsertBytes(DB, hash_kind, map, key, value); 2700 } else { 2701 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2702 } 2703 }, 2704 .array => |array_info| { 2705 if (array_info.child == u8) { 2706 try upsertBytes(DB, hash_kind, map, key, &value); 2707 } else { 2708 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2709 } 2710 }, 2711 .int => |int_info| switch (int_info.signedness) { 2712 .unsigned => { 2713 if (try map.getCursor(key)) |value_cursor| { 2714 if (try value_cursor.readUint() == value) { 2715 return; 2716 } 2717 } 2718 2719 try map.put(key, .{ .uint = value }); 2720 }, 2721 .signed => { 2722 if (try map.getCursor(key)) |value_cursor| { 2723 if (try value_cursor.readInt() == value) { 2724 return; 2725 } 2726 } 2727 2728 try map.put(key, .{ .int = value }); 2729 }, 2730 }, 2731 .bool => { 2732 const bytes = if (value) "true" else "false"; 2733 if (try map.getCursor(key)) |value_cursor| { 2734 var buffer: [5]u8 = undefined; 2735 const existing = try value_cursor.readBytesObject(&buffer); 2736 if (existing.format_tag) |format_tag| { 2737 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 2738 return; 2739 } 2740 } 2741 } 2742 2743 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 2744 }, 2745 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 2746 .@"union" => switch (value) { 2747 inline else => |payload, tag| { 2748 if (@TypeOf(payload) == void) { 2749 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 2750 } else { 2751 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 2752 if (try map.getCursor(key)) |cursor| { 2753 switch (cursor.slot().tag) { 2754 .hash_map => { 2755 const variants = try DB.HashMap(.read_only).init(cursor); 2756 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 2757 }, 2758 else => try map.put(key, .{ .slot = null }), 2759 } 2760 } 2761 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 2762 if (@typeInfo(@TypeOf(payload)) == .optional) { 2763 // keep the variant even when its payload is null 2764 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 2765 } 2766 if (try variants.getCursor(tag_key)) |cursor| { 2767 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 2768 } 2769 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 2770 } 2771 }, 2772 }, 2773 .optional => |optional_info| { 2774 // a missing key is null 2775 if (value) |child| { 2776 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 2777 } else { 2778 _ = try map.remove(key); 2779 } 2780 }, 2781 .@"struct" => try upsert( 2782 Field, 2783 DB, 2784 hash_kind, 2785 try DB.HashMap(.read_write).init(try map.putCursor(key)), 2786 value, 2787 ), 2788 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 2789 } 2790 } 2791 2792 fn upsertBytes( 2793 comptime DB: type, 2794 comptime hash_kind: hash.HashKind, 2795 map: DB.HashMap(.read_write), 2796 key: hash.HashInt(hash_kind), 2797 value: []const u8, 2798 ) !void { 2799 var existing_cursor_maybe = try map.getCursor(key); 2800 if (existing_cursor_maybe) |*existing_cursor| { 2801 if (try bytesEqual(DB, existing_cursor, value)) { 2802 return; 2803 } 2804 } 2805 2806 try map.put(key, .{ .bytes = value }); 2807 } 2808 2809 fn bytesEqual( 2810 comptime DB: type, 2811 cursor: *DB.Cursor(.read_only), 2812 value: []const u8, 2813 ) !bool { 2814 switch (cursor.slot().tag) { 2815 .bytes, .short_bytes => {}, 2816 else => return false, 2817 } 2818 var read_buffer: [1024]u8 = undefined; 2819 var reader = try cursor.reader(&read_buffer); 2820 if (reader.size != value.len) { 2821 return false; 2822 } 2823 2824 var chunk_buffer: [1024]u8 = undefined; 2825 var offset: usize = 0; 2826 while (offset < value.len) { 2827 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2828 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2829 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2830 return false; 2831 } 2832 offset += chunk_len; 2833 } 2834 2835 return true; 2836 } 2837 2838 // 2839 // CommitIterator 2840 // 2841 // differs from xit's ObjectIterator in a few ways: 2842 // 2843 // 1. iterates parents first, rather than children first 2844 // 2. `next` only returns the object id, not the full Object 2845 // 3. stores its temporary state in a file on disk to avoid OOMs 2846 // 2847 2848 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2849 return struct { 2850 const DB = EventDB(repo_opts.hash); 2851 // a plain file is used for the scratch db because each write outside 2852 // a transaction is synced anyway, so buffering would gain nothing 2853 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2854 const db_name = "haxy-repo-events.db"; 2855 2856 repo_dir: std.Io.Dir, 2857 db_file: std.Io.File, 2858 db: *TempDB, 2859 // map of each object id to its children 2860 parent_to_children: TempDB.HashMap(.read_write), 2861 // tracking pending parents is necessary because a child can only 2862 // be returned after all of its unconsumed parents have been returned 2863 child_to_pending_parents: TempDB.HashMap(.read_write), 2864 // queue of object ids that are ready to be returned from `next` 2865 ready_queue: TempDB.ArrayList(.read_write), 2866 ready_queue_index: u64, 2867 // the object id at the tip of this branch 2868 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2869 // the object id furthest back in the history that hasn't been consumed 2870 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2871 2872 pub fn init( 2873 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2874 io: std.Io, 2875 allocator: std.mem.Allocator, 2876 consumed_object_ids: DB.HashMap(.read_only), 2877 ref: rf.Ref, 2878 ) !CommitIterator(repo_kind, repo_opts) { 2879 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2880 errdefer { 2881 db_file.close(io); 2882 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2883 } 2884 2885 // cursors point at the db, so it needs a stable address 2886 const db_ptr = try allocator.create(TempDB); 2887 errdefer allocator.destroy(db_ptr); 2888 // the db is scratch state that gets deleted after iteration, so 2889 // there is nothing worth fsyncing 2890 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2891 2892 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2893 2894 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2895 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2896 2897 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2898 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2899 2900 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2901 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2902 2903 var self = CommitIterator(repo_kind, repo_opts){ 2904 .repo_dir = state.core.repo_dir, 2905 .db_file = db_file, 2906 .db = db_ptr, 2907 .parent_to_children = parent_to_children, 2908 .child_to_pending_parents = child_to_pending_parents, 2909 .ready_queue = ready_queue, 2910 .ready_queue_index = 0, 2911 .newest_object_id = null, 2912 .oldest_object_id = null, 2913 }; 2914 errdefer self.deinit(io, allocator); 2915 2916 try self.collect(state, io, allocator, consumed_object_ids, ref); 2917 2918 return self; 2919 } 2920 2921 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2922 self.db_file.close(io); 2923 self.repo_dir.deleteFile(io, db_name) catch {}; 2924 allocator.destroy(self.db); 2925 } 2926 2927 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2928 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2929 2930 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2931 self.ready_queue_index += 1; 2932 2933 try self.enqueueChildren(&oid); 2934 return oid; 2935 } 2936 2937 fn collect( 2938 self: *CommitIterator(repo_kind, repo_opts), 2939 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2940 io: std.Io, 2941 allocator: std.mem.Allocator, 2942 consumed_object_ids: DB.HashMap(.read_only), 2943 ref: rf.Ref, 2944 ) !void { 2945 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2946 2947 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2948 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2949 var walk_queue_index: u64 = 0; 2950 2951 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2952 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2953 2954 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2955 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2956 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2957 try walk_queue.append(.{ .bytes = &head_oid }); 2958 2959 while (walk_queue_index < try walk_queue.count()) { 2960 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2961 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2962 2963 walk_queue_index += 1; 2964 2965 // if we've seen this object id, skip it 2966 if (null != try seen_object_ids.getCursor(oid_int)) { 2967 continue; 2968 } 2969 2970 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2971 2972 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2973 defer commit_object.deinit(); 2974 2975 // if this object id has already been consumed, skip it 2976 if (null != try consumed_object_ids.getCursor(oid_int)) { 2977 continue; 2978 } 2979 2980 if (self.newest_object_id == null) self.newest_object_id = oid; 2981 self.oldest_object_id = oid; 2982 2983 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2984 var pending_parent_count: u64 = 0; 2985 for (parent_oids) |*parent_oid| { 2986 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2987 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2988 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2989 2990 // if this object id has already been consumed, skip it 2991 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2992 continue; 2993 } 2994 2995 // if this object id hasn't already been seen, add it to the walk queue 2996 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2997 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2998 } 2999 3000 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 3001 const children = try TempDB.HashSet(.read_write).init(children_cursor); 3002 try children.put(oid_int, .{ .bytes = &oid }); 3003 pending_parent_count += 1; 3004 } 3005 3006 if (pending_parent_count == 0) { 3007 try self.ready_queue.append(.{ .bytes = &oid }); 3008 } else { 3009 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 3010 } 3011 } 3012 } 3013 3014 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 3015 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 3016 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 3017 const children = try TempDB.HashSet(.read_only).init(children_cursor); 3018 var children_iter = try children.iterator(); 3019 3020 while (try children_iter.next()) |*child_cursor| { 3021 const kv_pair = try child_cursor.readKeyValuePair(); 3022 const child_oid_int = kv_pair.hash; 3023 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 3024 const pending_parent_count = try pending_parent_cursor.readUint(); 3025 3026 if (pending_parent_count <= 1) { 3027 _ = try self.child_to_pending_parents.remove(child_oid_int); 3028 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 3029 try self.ready_queue.append(.{ .bytes = &child_oid }); 3030 } else { 3031 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 3032 } 3033 } 3034 } 3035 3036 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 3037 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 3038 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 3039 _ = try oid_cursor.readBytes(&oid); 3040 return oid; 3041 } 3042 }; 3043 } 3044