╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
549 .signed => { 550 if (try map.getCursor(key)) |value_cursor| { 551 if (try value_cursor.readInt() == value) { 552 return; 553 } 554 } 555 556 try map.put(key, .{ .int = value }); 557 }, 558 }, 559 .bool => { 560 const bytes = if (value) "true" else "false"; 561 if (try map.getCursor(key)) |value_cursor| { 562 var buffer: [5]u8 = undefined; 563 const existing = try value_cursor.readBytesObject(&buffer); 564 if (existing.format_tag) |format_tag| { 565 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 566 return; 567 } 568 } 569 } 570 571 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 572 }, 573 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 574 } 575 } 576 577 fn upsertBytes( 578 comptime DB: type, 579 comptime hash_kind: hash.HashKind, 580 map: DB.HashMap(.read_write), 581 key: hash.HashInt(hash_kind), 582 value: []const u8, 583 ) !void { 584 var existing_cursor_maybe = try map.getCursor(key); 585 if (existing_cursor_maybe) |*existing_cursor| { 586 if (try bytesEqual(DB, existing_cursor, value)) { 587 return; 588 } 589 } 590 591 var value_cursor = try map.putCursor(key); 592 var write_buffer: [1024]u8 = undefined; 593 var writer = try value_cursor.writer(&write_buffer); 594 try writer.interface.writeAll(value); 595 try writer.finish(); 596 } 597 598 fn bytesEqual( 599 comptime DB: type, 600 cursor: *DB.Cursor(.read_only), 601 value: []const u8, 602 ) !bool { 603 var read_buffer: [1024]u8 = undefined; 604 var reader = try cursor.reader(&read_buffer); 605 if (reader.size != value.len) { 606 return false; 607 } 608 609 var chunk_buffer: [1024]u8 = undefined; 610 var offset: usize = 0; 611 while (offset < value.len) { 612 const chunk_len = @min(chunk_buffer.len, value.len - offset); 613 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 614 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 615 return false; 616 } 617 offset += chunk_len; 618 } 619 620 return true; 621 } 622 623 // 624 // CommitIterator 625 // 626 // differs from xit's ObjectIterator in a few ways: 627 // 628 // 1. iterates parents first, rather than children first 629 // 2. `next` only returns the object id, not the full Object 630 // 3. stores its temporary state in a file on disk to avoid OOMs 631 // 632 633 pub fn CommitIterator(comptime repo_opts: rp.RepoOpts(.xit)) type { 634 return struct { 635 const DB = rp.Repo(.xit, repo_opts).DB; 636 const db_name = "haxy-repo-events.db"; 637 638 repo_dir: std.Io.Dir, 639 db_file: std.Io.File, 640 db: *DB, 641 // map of each object id to its children 642 parent_to_children: DB.HashMap(.read_write), 643 // tracking pending parents is necessary because a child can only 644 // be returned after all of its unconsumed parents have been returned 645 child_to_pending_parents: DB.HashMap(.read_write), 646 // queue of object ids that are ready to be returned from `next` 647 ready_queue: DB.ArrayList(.read_write), 648 ready_queue_index: u64, 649 // the object id at the tip of this branch 650 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 651 // the object id furthest back in the history that hasn't been consumed 652 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 653 654 pub fn init( 655 state: rp.Repo(.xit, repo_opts).State(.read_only), 656 io: std.Io, 657 allocator: std.mem.Allocator, 658 consumed_object_ids: DB.HashMap(.read_only), 659 ref: rf.Ref, 660 ) !CommitIterator(repo_opts) { 661 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 662 errdefer { 663 db_file.close(io); 664 state.core.repo_dir.deleteFile(io, db_name) catch {}; 665 } 666 667 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 668 errdefer allocator.destroy(buffer_ptr); 669 670 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 671 errdefer buffer_ptr.deinit(); 672 673 const db_ptr = try allocator.create(DB); 674 errdefer allocator.destroy(db_ptr); 675 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr }); 676 677 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 678 679 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 680 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 681 682 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 683 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 684 685 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 686 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 687 688 var self = CommitIterator(repo_opts){ 689 .repo_dir = state.core.repo_dir, 690 .db_file = db_file, 691 .db = db_ptr, 692 .parent_to_children = parent_to_children, 693 .child_to_pending_parents = child_to_pending_parents, 694 .ready_queue = ready_queue, 695 .ready_queue_index = 0, 696 .newest_object_id = null, 697 .oldest_object_id = null, 698 }; 699 errdefer self.deinit(io, allocator); 700 701 try self.collect(state, io, allocator, consumed_object_ids, ref); 702 703 return self; 704 } 705 706 pub fn deinit(self: *CommitIterator(repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 707 self.db_file.close(io); 708 self.db.core.memory.buffer.deinit(); 709 allocator.destroy(self.db.core.memory.buffer); 710 self.repo_dir.deleteFile(io, db_name) catch {}; 711 allocator.destroy(self.db); 712 } 713 714 pub fn next(self: *CommitIterator(repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 715 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 716 717 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 718 self.ready_queue_index += 1; 719 720 try self.enqueueChildren(&oid); 721 return oid; 722 } 723 724 fn collect( 725 self: *CommitIterator(repo_opts), 726 state: rp.Repo(.xit, repo_opts).State(.read_only), 727 io: std.Io, 728 allocator: std.mem.Allocator, 729 consumed_object_ids: DB.HashMap(.read_only), 730 ref: rf.Ref, 731 ) !void { 732 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 733 734 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 735 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 736 var walk_queue_index: u64 = 0; 737 738 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 739 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 740 741 const head_oid_hex = (try rf.readRecur(.xit, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 742 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 743 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 744 try walk_queue.append(.{ .bytes = &head_oid }); 745 746 while (walk_queue_index < try walk_queue.count()) { 747 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 748 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 749 750 walk_queue_index += 1; 751 752 // if we've seen this object id, skip it 753 if (null != try seen_object_ids.getCursor(oid_int)) { 754 continue; 755 } 756 757 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 758 759 var commit_object = try obj.Object(.xit, repo_opts, .full).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 760 defer commit_object.deinit(); 761 762 // if this object id has already been consumed, skip it 763 if (null != try consumed_object_ids.getCursor(oid_int)) { 764 continue; 765 } 766 767 if (self.newest_object_id == null) self.newest_object_id = oid; 768 self.oldest_object_id = oid; 769 770 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 771 var pending_parent_count: u64 = 0; 772 for (parent_oids) |*parent_oid| { 773 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 774 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 775 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 776 777 // if this object id has already been consumed, skip it 778 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 779 continue; 780 } 781 782 // if this object id hasn't already been seen, add it to the walk queue 783 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 784 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 785 } 786 787 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 788 const children = try DB.HashSet(.read_write).init(children_cursor); 789 try children.put(oid_int, .{ .bytes = &oid }); 790 pending_parent_count += 1; 791 } 792 793 if (pending_parent_count == 0) { 794 try self.ready_queue.append(.{ .bytes = &oid }); 795 } else { 796 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 797 } 798 } 799 } 800 801 fn enqueueChildren(self: *CommitIterator(repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 802 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 803 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 804 const children = try DB.HashSet(.read_only).init(children_cursor); 805 var children_iter = try children.iterator(); 806 807 while (try children_iter.next()) |*child_cursor| { 808 const kv_pair = try child_cursor.readKeyValuePair(); 809 const child_oid_int = kv_pair.hash; 810 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 811 const pending_parent_count = try pending_parent_cursor.readUint(); 812 813 if (pending_parent_count <= 1) { 814 _ = try self.child_to_pending_parents.remove(child_oid_int); 815 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 816 try self.ready_queue.append(.{ .bytes = &child_oid }); 817 } else { 818 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 819 } 820 } 821 } 822 823 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 824 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 825 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 826 _ = try oid_cursor.readBytes(&oid); 827 return oid; 828 } 829 }; 830 } 831