╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
2487 .author = .{ .name = event.name, .email = event.email }, 2488 .event = .{ .user = event }, 2489 }}); 2490 } 2491 2492 // consume a new repo's event into its owner's user repo and mark it active 2493 fn writeRepoEvent( 2494 io: std.Io, 2495 allocator: std.mem.Allocator, 2496 users_dir: []const u8, 2497 user_repo: *rp.Repo(.xit, user_repo_opts), 2498 owner_id: *const [event_id_size]u8, 2499 owner: CommitAuthor, 2500 id_bytes: *const [event_id_size]u8, 2501 name: []const u8, 2502 description: []const u8, 2503 read_access: Repo.Access, 2504 ) !void { 2505 const timestamp: u64 = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()); 2506 try consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, user_repo_opts, io, allocator, user_repo, events_ref, &[_]EventWithId{.{ 2507 .id = std.fmt.bytesToHex(id_bytes.*, .lower), 2508 .timestamp = timestamp, 2509 .author = owner, 2510 .event = .{ .repo = .{ 2511 .user_id = std.fmt.bytesToHex(owner_id.*, .lower), 2512 .name = name, 2513 .description = description, 2514 .read_access = read_access, 2515 } }, 2516 }}); 2517 bumpRepoActivity(io, allocator, users_dir, owner_id, id_bytes, timestamp) catch |err| { 2518 std.log.warn("failed to record repo activity: {s}", .{@errorName(err)}); 2519 }; 2520 } 2521 2522 // 2523 // reading from xitdb 2524 // 2525 2526 // read just `Fields`, a subset of `T.Record`, from the record with event id 2527 // `id`, or null when there is none 2528 pub fn readRecordSubset( 2529 comptime T: type, 2530 comptime Fields: type, 2531 comptime DB: type, 2532 comptime hash_kind: hash.HashKind, 2533 haxy_moment: DB.HashMap(.read_only), 2534 arena: *std.heap.ArenaAllocator, 2535 id: []const u8, 2536 ) !?Fields { 2537 comptime assertFieldSubset(Fields, T.Record); 2538 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, T.record_map_key)) orelse return null; 2539 const records = try DB.HashMap(.read_only).init(records_cursor); 2540 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 2541 return try read(Fields, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 2542 } 2543 2544 fn assertFieldSubset(comptime Sub: type, comptime Full: type) void { 2545 const info = @typeInfo(Sub).@"struct"; 2546 for (info.field_names, info.field_types) |field_name, field_type| { 2547 if (!@hasField(Full, field_name)) @compileError(@typeName(Full) ++ " has no field " ++ field_name); 2548 const FullField = @FieldType(Full, field_name); 2549 if (@typeInfo(field_type) == .@"struct") { 2550 assertFieldSubset(field_type, FullField); 2551 } else if (field_type != FullField) { 2552 @compileError(@typeName(Full) ++ "." ++ field_name ++ " is a " ++ @typeName(FullField)); 2553 } 2554 } 2555 } 2556 2557 pub fn read( 2558 comptime T: type, 2559 comptime DB: type, 2560 comptime hash_kind: hash.HashKind, 2561 arena: *std.heap.ArenaAllocator, 2562 map: DB.HashMap(.read_only), 2563 ) !T { 2564 var event: T = undefined; 2565 switch (@typeInfo(T)) { 2566 .@"struct" => |struct_info| { 2567 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2568 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)); 2569 @field(event, field_name) = try readField(field_type, DB, hash_kind, arena, cursor); 2570 } 2571 }, 2572 else => @compileError("read expects a struct"), 2573 } 2574 return event; 2575 } 2576 2577 fn readField( 2578 comptime Field: type, 2579 comptime DB: type, 2580 comptime hash_kind: hash.HashKind, 2581 arena: *std.heap.ArenaAllocator, 2582 cursor_maybe: ?DB.Cursor(.read_only), 2583 ) !Field { 2584 if (@typeInfo(Field) == .optional) { 2585 // a missing key or a null union payload is null 2586 const cursor = cursor_maybe orelse return null; 2587 if (cursor.slot().tag == .none) return null; 2588 return try readField(@typeInfo(Field).optional.child, DB, hash_kind, arena, cursor); 2589 } 2590 const cursor = cursor_maybe orelse return error.NotFound; 2591 switch (@typeInfo(Field)) { 2592 .pointer => |pointer_info| { 2593 if (pointer_info.size != .slice or pointer_info.child != u8 or !pointer_info.attrs.@"const") { 2594 @compileError("unsupported read field type: " ++ @typeName(Field)); 2595 } 2596 return try cursor.readBytesAlloc(arena.allocator(), null); 2597 }, 2598 .array => |array_info| { 2599 if (array_info.child != u8) @compileError("unsupported read field type: " ++ @typeName(Field)); 2600 var bytes: Field = undefined; 2601 if ((try cursor.readBytes(&bytes)).len != bytes.len) return error.InvalidByteArrayLength; 2602 return bytes; 2603 }, 2604 .bool => { 2605 var buffer: [5]u8 = undefined; 2606 const bytes = try cursor.readBytesObject(&buffer); 2607 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 2608 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 2609 if (std.mem.eql(u8, bytes.value, "true")) return true; 2610 if (std.mem.eql(u8, bytes.value, "false")) return false; 2611 return error.InvalidBool; 2612 }, 2613 .int => |int_info| return switch (int_info.signedness) { 2614 .unsigned => @intCast(try cursor.readUint()), 2615 .signed => @intCast(try cursor.readInt()), 2616 }, 2617 .@"enum" => return std.meta.stringToEnum(Field, try cursor.readBytesAlloc(arena.allocator(), null)) orelse error.InvalidEnumTag, 2618 .@"union" => |union_info| { 2619 if (union_info.tag_type == null) @compileError("unsupported read field type: " ++ @typeName(Field)); 2620 switch (cursor.slot().tag) { 2621 .bytes, .short_bytes => { 2622 const name = try cursor.readBytesAlloc(arena.allocator(), null); 2623 inline for (union_info.field_names, union_info.field_types) |variant_name, variant_type| { 2624 if (variant_type == void and std.mem.eql(u8, name, variant_name)) { 2625 return @unionInit(Field, variant_name, {}); 2626 } 2627 } 2628 return error.InvalidUnion; 2629 }, 2630 .hash_map => { 2631 const variants = try DB.HashMap(.read_only).init(cursor); 2632 var iter = try variants.iterator(); 2633 const entry = (try iter.next()) orelse return error.InvalidUnion; 2634 const pair = try entry.readKeyValuePair(); 2635 if (try iter.next() != null) return error.InvalidUnion; 2636 inline for (union_info.field_names, union_info.field_types) |variant_name, variant_type| { 2637 if (variant_type != void and pair.hash == hash.hashInt(hash_kind, variant_name)) { 2638 return @unionInit(Field, variant_name, try readField(variant_type, DB, hash_kind, arena, pair.value_cursor)); 2639 } 2640 } 2641 return error.InvalidUnion; 2642 }, 2643 else => return error.InvalidUnion, 2644 } 2645 }, 2646 .@"struct" => return try read(Field, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)), 2647 else => @compileError("unsupported read field type: " ++ @typeName(Field)), 2648 } 2649 } 2650 2651 // whether two events carry the same data 2652 fn fieldsEqual(comptime T: type, a: T, b: T) bool { 2653 switch (@typeInfo(T)) { 2654 .@"struct" => |struct_info| { 2655 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2656 if (!fieldEqual(field_type, @field(a, field_name), @field(b, field_name))) return false; 2657 } 2658 }, 2659 else => @compileError("fieldsEqual expects a struct"), 2660 } 2661 2662 return true; 2663 } 2664 2665 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 2666 switch (@typeInfo(Field)) { 2667 .pointer => |pointer_info| { 2668 if (pointer_info.size == .slice and pointer_info.child == u8) { 2669 return std.mem.eql(u8, a, b); 2670 } else { 2671 @compileError("unsupported field type: " ++ @typeName(Field)); 2672 } 2673 }, 2674 .array => |array_info| { 2675 if (array_info.child == u8) { 2676 return std.mem.eql(u8, &a, &b); 2677 } else { 2678 @compileError("unsupported field type: " ++ @typeName(Field)); 2679 } 2680 }, 2681 .void => return true, 2682 .bool, .int, .@"enum" => return a == b, 2683 .@"union" => { 2684 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 2685 return switch (a) { 2686 inline else => |value, tag| fieldEqual(@TypeOf(value), value, @field(b, @tagName(tag))), 2687 }; 2688 }, 2689 .@"struct" => return fieldsEqual(Field, a, b), 2690 .optional => |optional_info| { 2691 if (a) |a_value| { 2692 if (b) |b_value| return fieldEqual(optional_info.child, a_value, b_value); 2693 return false; 2694 } 2695 return b == null; 2696 }, 2697 else => @compileError("unsupported field type: " ++ @typeName(Field)), 2698 } 2699 } 2700 2701 // 2702 // writing to xitdb 2703 // 2704 2705 pub fn upsert( 2706 comptime T: type, 2707 comptime DB: type, 2708 comptime hash_kind: hash.HashKind, 2709 map: DB.HashMap(.read_write), 2710 event: T, 2711 ) !void { 2712 switch (@typeInfo(T)) { 2713 .@"struct" => |struct_info| { 2714 inline for (struct_info.field_names, struct_info.field_types) |field_name, field_type| { 2715 try upsertField(DB, hash_kind, map, field_name, field_type, @field(event, field_name)); 2716 } 2717 }, 2718 else => @compileError("upsert expects a struct"), 2719 } 2720 } 2721 2722 fn upsertField( 2723 comptime DB: type, 2724 comptime hash_kind: hash.HashKind, 2725 map: DB.HashMap(.read_write), 2726 comptime field_name: []const u8, 2727 comptime Field: type, 2728 value: Field, 2729 ) !void { 2730 const key = hash.hashInt(hash_kind, field_name); 2731 2732 switch (@typeInfo(Field)) { 2733 .pointer => |pointer_info| { 2734 if (pointer_info.size == .slice and pointer_info.child == u8) { 2735 try upsertBytes(DB, hash_kind, map, key, value); 2736 } else { 2737 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2738 } 2739 }, 2740 .array => |array_info| { 2741 if (array_info.child == u8) { 2742 try upsertBytes(DB, hash_kind, map, key, &value); 2743 } else { 2744 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 2745 } 2746 }, 2747 .int => |int_info| switch (int_info.signedness) { 2748 .unsigned => { 2749 if (try map.getCursor(key)) |value_cursor| { 2750 if (try value_cursor.readUint() == value) { 2751 return; 2752 } 2753 } 2754 2755 try map.put(key, .{ .uint = value }); 2756 }, 2757 .signed => { 2758 if (try map.getCursor(key)) |value_cursor| { 2759 if (try value_cursor.readInt() == value) { 2760 return; 2761 } 2762 } 2763 2764 try map.put(key, .{ .int = value }); 2765 }, 2766 }, 2767 .bool => { 2768 const bytes = if (value) "true" else "false"; 2769 if (try map.getCursor(key)) |value_cursor| { 2770 var buffer: [5]u8 = undefined; 2771 const existing = try value_cursor.readBytesObject(&buffer); 2772 if (existing.format_tag) |format_tag| { 2773 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 2774 return; 2775 } 2776 } 2777 } 2778 2779 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 2780 }, 2781 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 2782 .@"union" => switch (value) { 2783 inline else => |payload, tag| { 2784 if (@TypeOf(payload) == void) { 2785 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 2786 } else { 2787 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 2788 if (try map.getCursor(key)) |cursor| { 2789 switch (cursor.slot().tag) { 2790 .hash_map => { 2791 const variants = try DB.HashMap(.read_only).init(cursor); 2792 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 2793 }, 2794 else => try map.put(key, .{ .slot = null }), 2795 } 2796 } 2797 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 2798 if (@typeInfo(@TypeOf(payload)) == .optional) { 2799 // keep the variant even when its payload is null 2800 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 2801 } 2802 if (try variants.getCursor(tag_key)) |cursor| { 2803 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 2804 } 2805 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 2806 } 2807 }, 2808 }, 2809 .optional => |optional_info| { 2810 // a missing key is null 2811 if (value) |child| { 2812 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 2813 } else { 2814 _ = try map.remove(key); 2815 } 2816 }, 2817 .@"struct" => try upsert( 2818 Field, 2819 DB, 2820 hash_kind, 2821 try DB.HashMap(.read_write).init(try map.putCursor(key)), 2822 value, 2823 ), 2824 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 2825 } 2826 } 2827 2828 fn upsertBytes( 2829 comptime DB: type, 2830 comptime hash_kind: hash.HashKind, 2831 map: DB.HashMap(.read_write), 2832 key: hash.HashInt(hash_kind), 2833 value: []const u8, 2834 ) !void { 2835 var existing_cursor_maybe = try map.getCursor(key); 2836 if (existing_cursor_maybe) |*existing_cursor| { 2837 if (try bytesEqual(DB, existing_cursor, value)) { 2838 return; 2839 } 2840 } 2841 2842 try map.put(key, .{ .bytes = value }); 2843 } 2844 2845 fn bytesEqual( 2846 comptime DB: type, 2847 cursor: *DB.Cursor(.read_only), 2848 value: []const u8, 2849 ) !bool { 2850 switch (cursor.slot().tag) { 2851 .bytes, .short_bytes => {}, 2852 else => return false, 2853 } 2854 var read_buffer: [1024]u8 = undefined; 2855 var reader = try cursor.reader(&read_buffer); 2856 if (reader.size != value.len) { 2857 return false; 2858 } 2859 2860 var chunk_buffer: [1024]u8 = undefined; 2861 var offset: usize = 0; 2862 while (offset < value.len) { 2863 const chunk_len = @min(chunk_buffer.len, value.len - offset); 2864 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 2865 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 2866 return false; 2867 } 2868 offset += chunk_len; 2869 } 2870 2871 return true; 2872 } 2873 2874 // 2875 // CommitIterator 2876 // 2877 // differs from xit's ObjectIterator in a few ways: 2878 // 2879 // 1. iterates parents first, rather than children first 2880 // 2. `next` only returns the object id, not the full Object 2881 // 3. stores its temporary state in a file on disk to avoid OOMs 2882 // 2883 2884 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2885 return struct { 2886 const DB = EventDB(repo_opts.hash); 2887 // a plain file is used for the scratch db because each write outside 2888 // a transaction is synced anyway, so buffering would gain nothing 2889 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2890 const db_name = "haxy-repo-events.db"; 2891 2892 repo_dir: std.Io.Dir, 2893 db_file: std.Io.File, 2894 db: *TempDB, 2895 // map of each object id to its children 2896 parent_to_children: TempDB.HashMap(.read_write), 2897 // tracking pending parents is necessary because a child can only 2898 // be returned after all of its unconsumed parents have been returned 2899 child_to_pending_parents: TempDB.HashMap(.read_write), 2900 // queue of object ids that are ready to be returned from `next` 2901 ready_queue: TempDB.ArrayList(.read_write), 2902 ready_queue_index: u64, 2903 // the object id at the tip of this branch 2904 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2905 // the object id furthest back in the history that hasn't been consumed 2906 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2907 2908 pub fn init( 2909 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2910 io: std.Io, 2911 allocator: std.mem.Allocator, 2912 consumed_object_ids: DB.HashMap(.read_only), 2913 ref: rf.Ref, 2914 ) !CommitIterator(repo_kind, repo_opts) { 2915 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2916 errdefer { 2917 db_file.close(io); 2918 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2919 } 2920 2921 // cursors point at the db, so it needs a stable address 2922 const db_ptr = try allocator.create(TempDB); 2923 errdefer allocator.destroy(db_ptr); 2924 // the db is scratch state that gets deleted after iteration, so 2925 // there is nothing worth fsyncing 2926 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2927 2928 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2929 2930 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2931 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2932 2933 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2934 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2935 2936 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2937 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2938 2939 var self = CommitIterator(repo_kind, repo_opts){ 2940 .repo_dir = state.core.repo_dir, 2941 .db_file = db_file, 2942 .db = db_ptr, 2943 .parent_to_children = parent_to_children, 2944 .child_to_pending_parents = child_to_pending_parents, 2945 .ready_queue = ready_queue, 2946 .ready_queue_index = 0, 2947 .newest_object_id = null, 2948 .oldest_object_id = null, 2949 }; 2950 errdefer self.deinit(io, allocator); 2951 2952 try self.collect(state, io, allocator, consumed_object_ids, ref); 2953 2954 return self; 2955 } 2956 2957 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2958 self.db_file.close(io); 2959 self.repo_dir.deleteFile(io, db_name) catch {}; 2960 allocator.destroy(self.db); 2961 } 2962 2963 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2964 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2965 2966 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2967 self.ready_queue_index += 1; 2968 2969 try self.enqueueChildren(&oid); 2970 return oid; 2971 } 2972 2973 fn collect( 2974 self: *CommitIterator(repo_kind, repo_opts), 2975 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2976 io: std.Io, 2977 allocator: std.mem.Allocator, 2978 consumed_object_ids: DB.HashMap(.read_only), 2979 ref: rf.Ref, 2980 ) !void { 2981 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2982 2983 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2984 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2985 var walk_queue_index: u64 = 0; 2986 2987 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2988 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2989 2990 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2991 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2992 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2993 try walk_queue.append(.{ .bytes = &head_oid }); 2994 2995 while (walk_queue_index < try walk_queue.count()) { 2996 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2997 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2998 2999 walk_queue_index += 1; 3000 3001 // if we've seen this object id, skip it 3002 if (null != try seen_object_ids.getCursor(oid_int)) { 3003 continue; 3004 } 3005 3006 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 3007 3008 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 3009 defer commit_object.deinit(); 3010 3011 // if this object id has already been consumed, skip it 3012 if (null != try consumed_object_ids.getCursor(oid_int)) { 3013 continue; 3014 } 3015 3016 if (self.newest_object_id == null) self.newest_object_id = oid; 3017 self.oldest_object_id = oid; 3018 3019 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 3020 var pending_parent_count: u64 = 0; 3021 for (parent_oids) |*parent_oid| { 3022 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 3023 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 3024 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 3025 3026 // if this object id has already been consumed, skip it 3027 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 3028 continue; 3029 } 3030 3031 // if this object id hasn't already been seen, add it to the walk queue 3032 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 3033 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 3034 } 3035 3036 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 3037 const children = try TempDB.HashSet(.read_write).init(children_cursor); 3038 try children.put(oid_int, .{ .bytes = &oid }); 3039 pending_parent_count += 1; 3040 } 3041 3042 if (pending_parent_count == 0) { 3043 try self.ready_queue.append(.{ .bytes = &oid }); 3044 } else { 3045 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 3046 } 3047 } 3048 } 3049 3050 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 3051 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 3052 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 3053 const children = try TempDB.HashSet(.read_only).init(children_cursor); 3054 var children_iter = try children.iterator(); 3055 3056 while (try children_iter.next()) |*child_cursor| { 3057 const kv_pair = try child_cursor.readKeyValuePair(); 3058 const child_oid_int = kv_pair.hash; 3059 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 3060 const pending_parent_count = try pending_parent_cursor.readUint(); 3061 3062 if (pending_parent_count <= 1) { 3063 _ = try self.child_to_pending_parents.remove(child_oid_int); 3064 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 3065 try self.ready_queue.append(.{ .bytes = &child_oid }); 3066 } else { 3067 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 3068 } 3069 } 3070 } 3071 3072 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 3073 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 3074 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 3075 _ = try oid_cursor.readBytes(&oid); 3076 return oid; 3077 } 3078 }; 3079 } 3080