╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2330 .@"union" => switch (value) { 2331 inline else => |payload, tag| { 2332 if (@TypeOf(payload) == void) { 2333 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 2334 } else { 2335 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 2336 if (try map.getCursor(key)) |cursor| { 2337 switch (cursor.slot().tag) { 2338 .hash_map => { 2339 const variants = try DB.HashMap(.read_only).init(cursor); 2340 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 2341 }, 2342 else => try map.put(key, .{ .slot = null }), 2343 } 2344 } 2345 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 2346 if (@typeInfo(@TypeOf(payload)) == .optional) { 2347 // keep the variant even when its payload is null 2348 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 2349 } 2350 if (try variants.getCursor(tag_key)) |cursor| { 2351 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 2352 } 2353 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 2354 } 2355 }, 2356 }, 2357 .optional => |optional_info| { 2358 // a missing key is null 2359 if (value) |child| { 2360 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 2361 } else { 2362 _ = try map.remove(key); 2363 } 2364 }, 2365 .@"struct" => try upsert( 2366 Field, 2367 DB, 2368 hash_kind, 2369 try DB.HashMap(.read_write).init(try map.putCursor(key)), 2370 value, 2371 ), 2372 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 2373 } 2374 } 2375 2376 fn upsertBytes( 2377 comptime DB: type, 2378 comptime hash_kind: hash.HashKind, 2379 map: DB.HashMap(.read_write), 2380 key: hash.HashInt(hash_kind), 2381 value: []const u8, 2382 ) !void { 2383 var existing_cursor_maybe = try map.getCursor(key); 2384 if (existing_cursor_maybe) |*existing_cursor| { 2385 if (try bytesEqual(DB, existing_cursor, value)) { 2386 return; 2387 } 2388 } 2389 2390 try map.put(key, .{ .bytes = value }); 2391 } 2392 2393 fn bytesEqual( 2394 comptime DB: type, 2395 cursor: *DB.Cursor(.read_only), 2396 value: []const u8, 2397 ) !bool { 2398 switch (cursor.slot().tag) { 2399 .bytes, .short_bytes => {}, 2400 else => return false, 2401 } 2402 var read_buffer: [1024]u8 = undefined; 2403 var reader = try cursor.reader(&read_buffer); 2404 if (reader.size != value.len) { 2405 return false; 2406 } 2407 2408 var chunk_buffer: [1024]u8 = undefined; 2409 var offset: usize = 0; 2410 while (offset < value.len) { 2411 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2412 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2413 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2414 return false; 2415 } 2416 offset += chunk_len; 2417 } 2418 2419 return true; 2420 } 2421 2422 // 2423 // CommitIterator 2424 // 2425 // differs from xit's ObjectIterator in a few ways: 2426 // 2427 // 1. iterates parents first, rather than children first 2428 // 2. `next` only returns the object id, not the full Object 2429 // 3. stores its temporary state in a file on disk to avoid OOMs 2430 // 2431 2432 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2433 return struct { 2434 const DB = EventDB(repo_opts.hash); 2435 // a plain file is used for the scratch db because each write outside 2436 // a transaction is synced anyway, so buffering would gain nothing 2437 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2438 const db_name = "haxy-repo-events.db"; 2439 2440 repo_dir: std.Io.Dir, 2441 db_file: std.Io.File, 2442 db: *TempDB, 2443 // map of each object id to its children 2444 parent_to_children: TempDB.HashMap(.read_write), 2445 // tracking pending parents is necessary because a child can only 2446 // be returned after all of its unconsumed parents have been returned 2447 child_to_pending_parents: TempDB.HashMap(.read_write), 2448 // queue of object ids that are ready to be returned from `next` 2449 ready_queue: TempDB.ArrayList(.read_write), 2450 ready_queue_index: u64, 2451 // the object id at the tip of this branch 2452 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2453 // the object id furthest back in the history that hasn't been consumed 2454 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2455 2456 pub fn init( 2457 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2458 io: std.Io, 2459 allocator: std.mem.Allocator, 2460 consumed_object_ids: DB.HashMap(.read_only), 2461 ref: rf.Ref, 2462 ) !CommitIterator(repo_kind, repo_opts) { 2463 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2464 errdefer { 2465 db_file.close(io); 2466 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2467 } 2468 2469 // cursors point at the db, so it needs a stable address 2470 const db_ptr = try allocator.create(TempDB); 2471 errdefer allocator.destroy(db_ptr); 2472 // the db is scratch state that gets deleted after iteration, so 2473 // there is nothing worth fsyncing 2474 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2475 2476 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2477 2478 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2479 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2480 2481 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2482 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2483 2484 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2485 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2486 2487 var self = CommitIterator(repo_kind, repo_opts){ 2488 .repo_dir = state.core.repo_dir, 2489 .db_file = db_file, 2490 .db = db_ptr, 2491 .parent_to_children = parent_to_children, 2492 .child_to_pending_parents = child_to_pending_parents, 2493 .ready_queue = ready_queue, 2494 .ready_queue_index = 0, 2495 .newest_object_id = null, 2496 .oldest_object_id = null, 2497 }; 2498 errdefer self.deinit(io, allocator); 2499 2500 try self.collect(state, io, allocator, consumed_object_ids, ref); 2501 2502 return self; 2503 } 2504 2505 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2506 self.db_file.close(io); 2507 self.repo_dir.deleteFile(io, db_name) catch {}; 2508 allocator.destroy(self.db); 2509 } 2510 2511 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2512 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2513 2514 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2515 self.ready_queue_index += 1; 2516 2517 try self.enqueueChildren(&oid); 2518 return oid; 2519 } 2520 2521 fn collect( 2522 self: *CommitIterator(repo_kind, repo_opts), 2523 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2524 io: std.Io, 2525 allocator: std.mem.Allocator, 2526 consumed_object_ids: DB.HashMap(.read_only), 2527 ref: rf.Ref, 2528 ) !void { 2529 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2530 2531 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2532 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2533 var walk_queue_index: u64 = 0; 2534 2535 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2536 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2537 2538 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2539 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2540 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2541 try walk_queue.append(.{ .bytes = &head_oid }); 2542 2543 while (walk_queue_index < try walk_queue.count()) { 2544 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2545 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2546 2547 walk_queue_index += 1; 2548 2549 // if we've seen this object id, skip it 2550 if (null != try seen_object_ids.getCursor(oid_int)) { 2551 continue; 2552 } 2553 2554 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2555 2556 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2557 defer commit_object.deinit(); 2558 2559 // if this object id has already been consumed, skip it 2560 if (null != try consumed_object_ids.getCursor(oid_int)) { 2561 continue; 2562 } 2563 2564 if (self.newest_object_id == null) self.newest_object_id = oid; 2565 self.oldest_object_id = oid; 2566 2567 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2568 var pending_parent_count: u64 = 0; 2569 for (parent_oids) |*parent_oid| { 2570 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2571 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2572 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2573 2574 // if this object id has already been consumed, skip it 2575 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2576 continue; 2577 } 2578 2579 // if this object id hasn't already been seen, add it to the walk queue 2580 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2581 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2582 } 2583 2584 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2585 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2586 try children.put(oid_int, .{ .bytes = &oid }); 2587 pending_parent_count += 1; 2588 } 2589 2590 if (pending_parent_count == 0) { 2591 try self.ready_queue.append(.{ .bytes = &oid }); 2592 } else { 2593 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2594 } 2595 } 2596 } 2597 2598 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2599 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2600 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2601 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2602 var children_iter = try children.iterator(); 2603 2604 while (try children_iter.next()) |*child_cursor| { 2605 const kv_pair = try child_cursor.readKeyValuePair(); 2606 const child_oid_int = kv_pair.hash; 2607 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2608 const pending_parent_count = try pending_parent_cursor.readUint(); 2609 2610 if (pending_parent_count <= 1) { 2611 _ = try self.child_to_pending_parents.remove(child_oid_int); 2612 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2613 try self.ready_queue.append(.{ .bytes = &child_oid }); 2614 } else { 2615 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2616 } 2617 } 2618 } 2619 2620 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2621 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2622 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2623 _ = try oid_cursor.readBytes(&oid); 2624 return oid; 2625 } 2626 }; 2627 } 2628