╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
858 .name = "ziglings", 859 .description = "Learn the Zig programming language by fixing tiny broken programs", 860 .enable_issue = true, 861 }, 862 }, 863 }, 864 }; 865 866 // 867 // insert users and repos as commits in the repo 868 // 869 870 { 871 var json: std.Io.Writer.Allocating = .init(allocator); 872 defer json.deinit(); 873 874 for (events_to_consume) |event| { 875 json.clearRetainingCapacity(); 876 877 try std.json.Stringify.value(event, .{}, &json.writer); 878 879 // commit the event into a special branch 880 _ = try repo.commitAtRef(io, allocator, .{ .message = json.written() }, null, .{ .kind = .head, .name = "haxy/meta" }); 881 } 882 } 883 884 // 885 // consume events into the database 886 // 887 888 { 889 try evt.consume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }); 890 891 const history = try Repo.DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 892 893 // read the moment we just created 894 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 895 const moment = try Repo.DB.HashMap(.read_only).init(moment_cursor); 896 897 // get the last object id 898 const last_object_id_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id")) orelse return error.NotFound; 899 var last_object_id: [hash.byteLen(repo_opts.hash)]u8 = undefined; 900 _ = try last_object_id_cursor.readBytes(&last_object_id); 901 902 const haxy_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy")) orelse return error.NotFound; 903 const haxy = try Repo.DB.ArrayList(.read_only).init(haxy_cursor); 904 905 try std.testing.expectEqual(1, try haxy.count()); 906 907 const haxy_moments_cursor = try haxy.getCursor(-1) orelse return error.NotFound; 908 const haxy_moments = try Repo.DB.HashMap(.read_only).init(haxy_moments_cursor); 909 910 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &last_object_id)) orelse return error.NotFound; 911 const haxy_moment = try Repo.DB.HashMap(.read_only).init(haxy_moment_cursor); 912 913 // get the map of users 914 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->user")) orelse return error.NotFound; 915 const event_id_to_user = try Repo.DB.HashMap(.read_only).init(event_id_to_user_cursor); 916 917 // get the user out of the map that was edited 918 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(repo_opts.hash, &user_event_id)) orelse return error.NotFound; 919 const user_map = try Repo.DB.HashMap(.read_only).init(user_cursor); 920 const user_event = try evt.read(evt.User, Repo.DB, repo_opts.hash, &arena, user_map); 921 922 // the password was correctly edited 923 try std.testing.expectEqualStrings(events_to_consume[1].event.user.?.password_hash, user_event.password_hash); 924 925 // get the map of repos 926 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->repo")) orelse return error.NotFound; 927 const event_id_to_repo = try Repo.DB.HashMap(.read_only).init(event_id_to_repo_cursor); 928 929 // get the repo out of the map 930 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(repo_opts.hash, &repo_event_id)) orelse return error.NotFound; 931 const repo_map = try Repo.DB.HashMap(.read_only).init(repo_cursor); 932 const repo_event = try evt.read(evt.Repo, Repo.DB, repo_opts.hash, &arena, repo_map); 933 934 try std.testing.expectEqualSlices(u8, &user_event_id, repo_event.user_id); 935 try std.testing.expectEqualStrings("ziglings", repo_event.name); 936 937 // get the repos created by the user 938 const user_id_to_repos_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "user-id->repos")) orelse return error.NotFound; 939 const user_id_to_repos = try Repo.DB.HashMap(.read_only).init(user_id_to_repos_cursor); 940 941 const user_repos_cursor = try user_id_to_repos.getCursor(hash.hashInt(repo_opts.hash, &user_event_id)) orelse return error.NotFound; 942 const user_repos = try Repo.DB.CountedHashSet(.read_only).init(user_repos_cursor); 943 944 try std.testing.expectEqual(1, try user_repos.count()); 945 946 try std.testing.expect(null != try user_repos.getCursor(hash.hashInt(repo_opts.hash, &repo_event_id))); 947 } 948 949 // 950 // remove the repo 951 // 952 953 const events_to_consume2 = [_]evt.EventWithId{ 954 .{ 955 .id = std.fmt.bytesToHex(repo_event_id, .lower), 956 .event = .{ .repo = null }, 957 }, 958 }; 959 960 { 961 var json: std.Io.Writer.Allocating = .init(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.EventWithId{ 1025 .{ 1026 .id = std.fmt.bytesToHex(user_event_id, .lower), 1027 .event = .{ .user = null }, 1028 }, 1029 }; 1030 1031 { 1032 var json: std.Io.Writer.Allocating = .init(allocator); 1033 defer json.deinit(); 1034 1035 for (events_to_consume3) |event| { 1036 json.clearRetainingCapacity(); 1037 1038 try std.json.Stringify.value(event, .{}, &json.writer); 1039 1040 // commit the event into a special branch 1041 _ = try repo.commitAtRef(io, allocator, .{ .message = json.written() }, null, .{ .kind = .head, .name = "haxy/meta" }); 1042 } 1043 } 1044 1045 // 1046 // consume the user removal into the database 1047 // 1048 1049 { 1050 try evt.consume(repo_opts, io, allocator, &repo, .{ .kind = .head, .name = "haxy/meta" }); 1051 1052 const history = try Repo.DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 1053 1054 // read the moment we just created 1055 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 1056 const moment = try Repo.DB.HashMap(.read_only).init(moment_cursor); 1057 1058 // get the last object id 1059 const last_object_id_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id")) orelse return error.NotFound; 1060 var last_object_id: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1061 _ = try last_object_id_cursor.readBytes(&last_object_id); 1062 1063 const haxy_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy")) orelse return error.NotFound; 1064 const haxy = try Repo.DB.ArrayList(.read_only).init(haxy_cursor); 1065 1066 try std.testing.expectEqual(3, try haxy.count()); 1067 1068 const haxy_moments_cursor = try haxy.getCursor(-1) orelse return error.NotFound; 1069 const haxy_moments = try Repo.DB.HashMap(.read_only).init(haxy_moments_cursor); 1070 1071 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &last_object_id)) orelse return error.NotFound; 1072 const haxy_moment = try Repo.DB.HashMap(.read_only).init(haxy_moment_cursor); 1073 1074 // get the map of users 1075 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->user")) orelse return error.NotFound; 1076 const event_id_to_user = try Repo.DB.HashMap(.read_only).init(event_id_to_user_cursor); 1077 1078 try std.testing.expect(null == try event_id_to_user.getCursor(hash.hashInt(repo_opts.hash, &user_event_id))); 1079 } 1080 } 1081