╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
562 } 563 } 564 565 // 566 // check out the haxy/meta branch and merge the haxy/other branch into it 567 // 568 569 const merge_oid = blk: { 570 var result = try repo.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "haxy/meta" } } }); 571 defer result.deinit(); 572 573 var merge = try repo.merge(io, allocator, .{ .kind = .full, .action = .{ .new = .{ .source = &.{.{ .ref = .{ .kind = .head, .name = "haxy/other" } }} } } }, null); 574 defer merge.deinit(); 575 576 try std.testing.expect(.success == merge.result); 577 578 break :blk merge.result.success.oid; 579 }; 580 581 // 582 // consume events into the database 583 // 584 585 { 586 try evt.consume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }); 587 588 const history = try Repo.DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 589 590 // read the moment we just created 591 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 592 const moment = try Repo.DB.HashMap(.read_only).init(moment_cursor); 593 594 // get the last object id 595 const last_object_id_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id")) orelse return error.NotFound; 596 var last_object_id: [hash.byteLen(repo_opts.hash)]u8 = undefined; 597 _ = try last_object_id_cursor.readBytes(&last_object_id); 598 599 const haxy_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy")) orelse return error.NotFound; 600 const haxy = try Repo.DB.ArrayList(.read_only).init(haxy_cursor); 601 602 try std.testing.expectEqual(2, try haxy.count()); 603 604 const haxy_moments_cursor = try haxy.getCursor(-1) orelse return error.NotFound; 605 const haxy_moments = try Repo.DB.HashMap(.read_only).init(haxy_moments_cursor); 606 607 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &last_object_id)) orelse return error.NotFound; 608 const haxy_moment = try Repo.DB.HashMap(.read_only).init(haxy_moment_cursor); 609 610 // get the map of issues 611 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse return error.NotFound; 612 const event_id_to_issue = try Repo.DB.HashMap(.read_only).init(event_id_to_issue_cursor); 613 614 // make sure one of the original issues is still there 615 { 616 // get the issue out of the map 617 var first_issue_id: [evt.event_id_size]u8 = undefined; 618 _ = try std.fmt.hexToBytes(&first_issue_id, &events_to_consume[0].id); 619 const first_issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &first_issue_id)) orelse return error.NotFound; 620 const first_issue_map = try Repo.DB.HashMap(.read_only).init(first_issue_cursor); 621 const first_issue = try evt.EventData.read(Repo.DB, repo_opts.hash, arena.allocator(), first_issue_map, .issue); 622 623 try std.testing.expectEqualStrings(events_to_consume[0].data.issue.?.description, first_issue.issue.?.description); 624 try std.testing.expectEqualStrings(events_to_consume[0].data.issue.?.tags, first_issue.issue.?.tags); 625 } 626 627 // make sure one of the new issues is there 628 { 629 // get the issue out of the map 630 var first_issue_id: [evt.event_id_size]u8 = undefined; 631 _ = try std.fmt.hexToBytes(&first_issue_id, &events_to_consume2[0].id); 632 const first_issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &first_issue_id)) orelse return error.NotFound; 633 const first_issue_map = try Repo.DB.HashMap(.read_only).init(first_issue_cursor); 634 const first_issue = try evt.EventData.read(Repo.DB, repo_opts.hash, arena.allocator(), first_issue_map, .issue); 635 636 try std.testing.expectEqualStrings(events_to_consume2[0].data.issue.?.description, first_issue.issue.?.description); 637 try std.testing.expectEqualStrings(events_to_consume2[0].data.issue.?.tags, first_issue.issue.?.tags); 638 } 639 } 640 641 // 642 // insert issues as commits in the repo 643 // 644 645 { 646 var json: std.Io.Writer.Allocating = .init(std.testing.allocator); 647 defer json.deinit(); 648 649 for (events_to_consume, 0..) |event, i| { 650 json.clearRetainingCapacity(); 651 652 try std.json.Stringify.value(event, .{}, &json.writer); 653 654 // commit the event into a special branch 655 _ = try repo.commitAtRef( 656 io, 657 allocator, 658 // make the first event have no parents so we completely rebase the branch, 659 // undoing the merge that we did above 660 .{ .parent_oids = if (i == 0) &.{} else null, .message = json.written() }, 661 null, 662 .{ .kind = .head, .name = "haxy/meta" }, 663 ); 664 } 665 } 666 667 // 668 // consume events into the database 669 // 670 671 { 672 try evt.consume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }); 673 674 const history = try Repo.DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 675 676 // read the moment we just created 677 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 678 const moment = try Repo.DB.HashMap(.read_only).init(moment_cursor); 679 680 // get the last object id 681 const last_object_id_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id")) orelse return error.NotFound; 682 var last_object_id: [hash.byteLen(repo_opts.hash)]u8 = undefined; 683 _ = try last_object_id_cursor.readBytes(&last_object_id); 684 685 const haxy_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy")) orelse return error.NotFound; 686 const haxy = try Repo.DB.ArrayList(.read_only).init(haxy_cursor); 687 688 try std.testing.expectEqual(1, try haxy.count()); 689 690 const haxy_moments_cursor = try haxy.getCursor(-1) orelse return error.NotFound; 691 const haxy_moments = try Repo.DB.HashMap(.read_only).init(haxy_moments_cursor); 692 693 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &last_object_id)) orelse return error.NotFound; 694 const haxy_moment = try Repo.DB.HashMap(.read_only).init(haxy_moment_cursor); 695 696 // get the map of issues 697 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse return error.NotFound; 698 const event_id_to_issue = try Repo.DB.HashMap(.read_only).init(event_id_to_issue_cursor); 699 700 // make sure the new issue is not there 701 { 702 // get the issue out of the map 703 var first_issue_id: [evt.event_id_size]u8 = undefined; 704 _ = try std.fmt.hexToBytes(&first_issue_id, &events_to_consume2[0].id); 705 const first_issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &first_issue_id)); 706 try std.testing.expect(null == first_issue_cursor); 707 } 708 } 709 710 // 711 // define test events that modify the same issue in conflicting ways 712 // 713 714 const events_to_consume3 = [_]evt.Event{ 715 .{ 716 .id = events_to_consume[0].id, 717 .data = .{ 718 .issue = .{ 719 .title = "Login form clears password on validation error", 720 .description = "Submitting an invalid email address resets the password field. Preserve the field value and show an inline validation message.", 721 .tags = "bug\x00priority-low\x00ui", 722 }, 723 }, 724 }, 725 .{ 726 .id = events_to_consume[0].id, 727 .data = .{ 728 .issue = .{ 729 .title = "Login form clears password on validation error", 730 .description = "Submitting an invalid email address resets the password field. Preserve the field value and show an inline validation message.", 731 .tags = "bug\x00priority-medium\x00ui", 732 }, 733 }, 734 }, 735 }; 736 737 // 738 // insert issues as commits in the repo on different branches 739 // 740 741 { 742 var json: std.Io.Writer.Allocating = .init(std.testing.allocator); 743 defer json.deinit(); 744 745 for (events_to_consume3, 0..) |event, i| { 746 json.clearRetainingCapacity(); 747 748 try std.json.Stringify.value(event, .{}, &json.writer); 749 750 // commit the event into a special branch 751 _ = try repo.commitAtRef( 752 io, 753 allocator, 754 .{ .parent_oids = &.{merge_oid}, .message = json.written() }, 755 null, 756 .{ .kind = .head, .name = switch (i) { 757 0 => "haxy/meta", 758 1 => "haxy/other", 759 else => unreachable, 760 } }, 761 ); 762 } 763 } 764 765 // 766 // check out the haxy/meta branch and merge the haxy/other branch into it 767 // 768 769 { 770 var result = try repo.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "haxy/meta" } } }); 771 defer result.deinit(); 772 773 var merge = try repo.merge(io, allocator, .{ .kind = .full, .action = .{ .new = .{ .source = &.{.{ .ref = .{ .kind = .head, .name = "haxy/other" } }} } } }, null); 774 defer merge.deinit(); 775 776 try std.testing.expect(.success == merge.result); 777 } 778 779 // 780 // consume events into the database 781 // 782 783 try std.testing.expectError(error.MergeConflict, evt.consume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" })); 784 } 785 786 test "user and repo" { 787 const io = std.testing.io; 788 const allocator = std.testing.allocator; 789 const temp_dir_name = "temp-event-user-and-repo"; 790 791 // create the temp dir 792 const cwd = std.Io.Dir.cwd(); 793 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 794 if (temp_dir_or_err) |*temp_dir| { 795 temp_dir.close(io); 796 try cwd.deleteTree(io, temp_dir_name); 797 } else |_| {} 798 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 799 defer cwd.deleteTree(io, temp_dir_name) catch {}; 800 defer temp_dir.close(io); 801 802 const cwd_path = try std.process.currentPathAlloc(io, allocator); 803 defer allocator.free(cwd_path); 804 805 const work_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 806 defer allocator.free(work_path); 807 808 const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; 809 const Repo = rp.Repo(.xit, repo_opts); 810 var repo = try Repo.init(io, allocator, .{ .path = work_path }); 811 defer repo.deinit(io, allocator); 812 813 var arena = std.heap.ArenaAllocator.init(allocator); 814 defer arena.deinit(); 815 816 // 817 // define test events 818 // 819 820 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 821 const user_event_id = evt.randomId(prng.random()); 822 const repo_event_id = evt.randomId(prng.random()); 823 824 var first_password_hash_buf: [usr.password_hash_max_len]u8 = undefined; 825 const first_password_hash = try usr.hashPassword("correct horse battery staple", &first_password_hash_buf, io); 826 827 var second_password_hash_buf: [usr.password_hash_max_len]u8 = undefined; 828 const second_password_hash = try usr.hashPassword("Tr0ub4dor&3", &second_password_hash_buf, io); 829 830 const events_to_consume = [_]evt.Event{ 831 .{ 832 .id = std.fmt.bytesToHex(user_event_id, .lower), 833 .data = .{ 834 .user = .{ 835 .name = "Alice Example", 836 .email = "alice@example.test", 837 .password_hash = first_password_hash, 838 }, 839 }, 840 }, 841 // this event edits the previous one because it has the same id 842 .{ 843 .id = std.fmt.bytesToHex(user_event_id, .lower), 844 .data = .{ 845 .user = .{ 846 .name = "Alice Example", 847 .email = "alice@example.test", 848 .password_hash = second_password_hash, 849 }, 850 }, 851 }, 852 .{ 853 .id = std.fmt.bytesToHex(repo_event_id, .lower), 854 .data = .{ 855 .repo = .{ 856 .user_id = &user_event_id, 857 .name = "ziglings", 858 .enable_issue = true, 859 }, 860 }, 861 }, 862 }; 863 864 // 865 // insert users and repos as commits in the repo 866 // 867 868 { 869 var json: std.Io.Writer.Allocating = .init(std.testing.allocator); 870 defer json.deinit(); 871 872 for (events_to_consume) |event| { 873 json.clearRetainingCapacity(); 874 875 try std.json.Stringify.value(event, .{}, &json.writer); 876 877 // commit the event into a special branch 878 _ = try repo.commitAtRef(io, allocator, .{ .message = json.written() }, null, .{ .kind = .head, .name = "haxy/meta" }); 879 } 880 } 881 882 // 883 // consume events into the database 884 // 885 886 { 887 try evt.consume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }); 888 889 const history = try Repo.DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 890 891 // read the moment we just created 892 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 893 const moment = try Repo.DB.HashMap(.read_only).init(moment_cursor); 894 895 // get the last object id 896 const last_object_id_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id")) orelse return error.NotFound; 897 var last_object_id: [hash.byteLen(repo_opts.hash)]u8 = undefined; 898 _ = try last_object_id_cursor.readBytes(&last_object_id); 899 900 const haxy_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy")) orelse return error.NotFound; 901 const haxy = try Repo.DB.ArrayList(.read_only).init(haxy_cursor); 902 903 try std.testing.expectEqual(1, try haxy.count()); 904 905 const haxy_moments_cursor = try haxy.getCursor(-1) orelse return error.NotFound; 906 const haxy_moments = try Repo.DB.HashMap(.read_only).init(haxy_moments_cursor); 907 908 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &last_object_id)) orelse return error.NotFound; 909 const haxy_moment = try Repo.DB.HashMap(.read_only).init(haxy_moment_cursor); 910 911 // get the map of users 912 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->user")) orelse return error.NotFound; 913 const event_id_to_user = try Repo.DB.HashMap(.read_only).init(event_id_to_user_cursor); 914 915 // get the user out of the map that was edited 916 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(repo_opts.hash, &user_event_id)) orelse return error.NotFound; 917 const user_map = try Repo.DB.HashMap(.read_only).init(user_cursor); 918 const user = try evt.EventData.read(Repo.DB, repo_opts.hash, arena.allocator(), user_map, .user); 919 920 // the password was correctly edited 921 try std.testing.expectEqualStrings(events_to_consume[1].data.user.?.password_hash, user.user.?.password_hash); 922 923 // get the map of repos 924 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->repo")) orelse return error.NotFound; 925 const event_id_to_repo = try Repo.DB.HashMap(.read_only).init(event_id_to_repo_cursor); 926 927 // get the repo out of the map 928 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(repo_opts.hash, &repo_event_id)) orelse return error.NotFound; 929 const repo_map = try Repo.DB.HashMap(.read_only).init(repo_cursor); 930 const repo_event = try evt.EventData.read(Repo.DB, repo_opts.hash, arena.allocator(), repo_map, .repo); 931 932 try std.testing.expectEqualSlices(u8, &user_event_id, repo_event.repo.?.user_id); 933 try std.testing.expectEqualStrings("ziglings", repo_event.repo.?.name); 934 935 // get the repos created by the user 936 const user_id_to_repos_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "user-id->repos")) orelse return error.NotFound; 937 const user_id_to_repos = try Repo.DB.HashMap(.read_only).init(user_id_to_repos_cursor); 938 939 const user_repos_cursor = try user_id_to_repos.getCursor(hash.hashInt(repo_opts.hash, &user_event_id)) orelse return error.NotFound; 940 const user_repos = try Repo.DB.CountedHashSet(.read_only).init(user_repos_cursor); 941 942 try std.testing.expectEqual(1, try user_repos.count()); 943 944 try std.testing.expect(null != try user_repos.getCursor(hash.hashInt(repo_opts.hash, &repo_event_id))); 945 } 946 947 // 948 // remove the repo 949 // 950 951 const events_to_consume2 = [_]evt.Event{ 952 .{ 953 .id = std.fmt.bytesToHex(repo_event_id, .lower), 954 .data = .{ 955 .repo = null, 956 }, 957 }, 958 }; 959 960 { 961 var json: std.Io.Writer.Allocating = .init(std.testing.allocator); 962 defer json.deinit(); 963 964 for (events_to_consume2) |event| { 965 json.clearRetainingCapacity(); 966 967 try std.json.Stringify.value(event, .{}, &json.writer); 968 969 // commit the event into a special branch 970 _ = try repo.commitAtRef(io, allocator, .{ .message = json.written() }, null, .{ .kind = .head, .name = "haxy/meta" }); 971 } 972 } 973 974 // 975 // consume the removal into the database 976 // 977 978 { 979 try evt.consume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }); 980 981 const history = try Repo.DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 982 983 // read the moment we just created 984 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 985 const moment = try Repo.DB.HashMap(.read_only).init(moment_cursor); 986 987 // get the last object id 988 const last_object_id_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id")) orelse return error.NotFound; 989 var last_object_id: [hash.byteLen(repo_opts.hash)]u8 = undefined; 990 _ = try last_object_id_cursor.readBytes(&last_object_id); 991 992 const haxy_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy")) orelse return error.NotFound; 993 const haxy = try Repo.DB.ArrayList(.read_only).init(haxy_cursor); 994 995 try std.testing.expectEqual(2, try haxy.count()); 996 997 const haxy_moments_cursor = try haxy.getCursor(-1) orelse return error.NotFound; 998 const haxy_moments = try Repo.DB.HashMap(.read_only).init(haxy_moments_cursor); 999 1000 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &last_object_id)) orelse return error.NotFound; 1001 const haxy_moment = try Repo.DB.HashMap(.read_only).init(haxy_moment_cursor); 1002 1003 // get the map of repos 1004 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->repo")) orelse return error.NotFound; 1005 const event_id_to_repo = try Repo.DB.HashMap(.read_only).init(event_id_to_repo_cursor); 1006 1007 try std.testing.expect(null == try event_id_to_repo.getCursor(hash.hashInt(repo_opts.hash, &repo_event_id))); 1008 1009 // get the repos created by the user 1010 const user_id_to_repos_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "user-id->repos")) orelse return error.NotFound; 1011 const user_id_to_repos = try Repo.DB.HashMap(.read_only).init(user_id_to_repos_cursor); 1012 1013 const user_repos_cursor = try user_id_to_repos.getCursor(hash.hashInt(repo_opts.hash, &user_event_id)) orelse return error.NotFound; 1014 const user_repos = try Repo.DB.CountedHashSet(.read_only).init(user_repos_cursor); 1015 1016 try std.testing.expectEqual(0, try user_repos.count()); 1017 try std.testing.expect(null == try user_repos.getCursor(hash.hashInt(repo_opts.hash, &repo_event_id))); 1018 } 1019 1020 // 1021 // remove the user 1022 // 1023 1024 const events_to_consume3 = [_]evt.Event{ 1025 .{ 1026 .id = std.fmt.bytesToHex(user_event_id, .lower), 1027 .data = .{ 1028 .user = null, 1029 }, 1030 }, 1031 }; 1032 1033 { 1034 var json: std.Io.Writer.Allocating = .init(std.testing.allocator); 1035 defer json.deinit(); 1036 1037 for (events_to_consume3) |event| { 1038 json.clearRetainingCapacity(); 1039 1040 try std.json.Stringify.value(event, .{}, &json.writer); 1041 1042 // commit the event into a special branch 1043 _ = try repo.commitAtRef(io, allocator, .{ .message = json.written() }, null, .{ .kind = .head, .name = "haxy/meta" }); 1044 } 1045 } 1046 1047 // 1048 // consume the user removal into the database 1049 // 1050 1051 { 1052 try evt.consume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }); 1053 1054 const history = try Repo.DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 1055 1056 // read the moment we just created 1057 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 1058 const moment = try Repo.DB.HashMap(.read_only).init(moment_cursor); 1059 1060 // get the last object id 1061 const last_object_id_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id")) orelse return error.NotFound; 1062 var last_object_id: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1063 _ = try last_object_id_cursor.readBytes(&last_object_id); 1064 1065 const haxy_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy")) orelse return error.NotFound; 1066 const haxy = try Repo.DB.ArrayList(.read_only).init(haxy_cursor); 1067 1068 try std.testing.expectEqual(3, try haxy.count()); 1069 1070 const haxy_moments_cursor = try haxy.getCursor(-1) orelse return error.NotFound; 1071 const haxy_moments = try Repo.DB.HashMap(.read_only).init(haxy_moments_cursor); 1072 1073 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &last_object_id)) orelse return error.NotFound; 1074 const haxy_moment = try Repo.DB.HashMap(.read_only).init(haxy_moment_cursor); 1075 1076 // get the map of users 1077 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->user")) orelse return error.NotFound; 1078 const event_id_to_user = try Repo.DB.HashMap(.read_only).init(event_id_to_user_cursor); 1079 1080 try std.testing.expect(null == try event_id_to_user.getCursor(hash.hashInt(repo_opts.hash, &user_event_id))); 1081 } 1082 } 1083