╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1245 1246 const cwd = std.Io.Dir.cwd(); 1247 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1248 if (temp_dir_or_err) |*temp_dir| { 1249 temp_dir.close(io); 1250 try cwd.deleteTree(io, temp_dir_name); 1251 } else |_| {} 1252 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1253 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1254 defer temp_dir.close(io); 1255 1256 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1257 defer allocator.free(cwd_path); 1258 const work_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 1259 defer allocator.free(work_path); 1260 1261 const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; 1262 const Repo = rp.Repo(.xit, repo_opts); 1263 var repo = try Repo.init(io, allocator, .{ .path = work_path }); 1264 defer repo.deinit(io, allocator); 1265 1266 var arena = std.heap.ArenaAllocator.init(allocator); 1267 defer arena.deinit(); 1268 1269 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1270 const user_id = evt.EventWithId.randomId(prng.random()); 1271 var repo_ids: [4][evt.event_id_size]u8 = undefined; 1272 for (&repo_ids) |*id| id.* = evt.EventWithId.randomId(prng.random()); 1273 1274 var pw_buf: [evt.User.password_hash_max_len]u8 = undefined; 1275 const pw = try evt.User.hashPassword("pw", &pw_buf, io); 1276 1277 // asserts the repo-id-set holds exactly `expected` ids in that order 1278 const Check = struct { 1279 fn order( 1280 comptime DB: type, 1281 comptime hash_kind: hash.HashKind, 1282 moment: DB.HashMap(.read_only), 1283 expected: []const *const [evt.event_id_size]u8, 1284 ) !void { 1285 const cursor = try moment.getCursor(hash.hashInt(hash_kind, "repo-id-set")) orelse return error.NotFound; 1286 const set = try DB.SortedSet(.read_only).init(cursor); 1287 try std.testing.expectEqual(expected.len, try set.count()); 1288 for (expected, 0..) |id, i| { 1289 const kv = (try set.getIndexKeyValuePair(@intCast(i))) orelse return error.NotFound; 1290 // the key is orderKeyDesc ([timestamp][event-id]); its trailing bytes are the id 1291 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 1292 _ = try kv.key_cursor.readBytes(&order_key); 1293 try std.testing.expectEqualSlices(u8, id, order_key[@sizeOf(u64)..]); 1294 } 1295 } 1296 }; 1297 1298 // one user, then four repos, each with its own creation timestamp (user@100, 1299 // repos@101..104) 1300 const events = [_]evt.EventWithId{ 1301 .{ .id = std.fmt.bytesToHex(user_id, .lower), .author_email = author_email, .timestamp = 100, .event = .{ .user = .{ .name = "alice", .display_name = "Alice", .email = "alice@example.test", .password_hash = pw } } }, 1302 .{ .id = std.fmt.bytesToHex(repo_ids[0], .lower), .author_email = author_email, .timestamp = 101, .event = .{ .repo = .{ .user_id = &user_id, .name = "repo0", .description = "d0" } } }, 1303 .{ .id = std.fmt.bytesToHex(repo_ids[1], .lower), .author_email = author_email, .timestamp = 102, .event = .{ .repo = .{ .user_id = &user_id, .name = "repo1", .description = "d1" } } }, 1304 .{ .id = std.fmt.bytesToHex(repo_ids[2], .lower), .author_email = author_email, .timestamp = 103, .event = .{ .repo = .{ .user_id = &user_id, .name = "repo2", .description = "d2" } } }, 1305 .{ .id = std.fmt.bytesToHex(repo_ids[3], .lower), .author_email = author_email, .timestamp = 104, .event = .{ .repo = .{ .user_id = &user_id, .name = "repo3", .description = "d3" } } }, 1306 }; 1307 try evt.consume(.xit, repo_opts, io, allocator, &repo, evt.events_ref, &events); 1308 1309 { 1310 const moment = try evt.currentMoment(repo_opts, &repo); 1311 try Check.order(Repo.DB, repo_opts.hash, moment, &.{ &repo_ids[3], &repo_ids[2], &repo_ids[1], &repo_ids[0] }); 1312 1313 // the single user shows up in its own ordered set 1314 const ucur = try moment.getCursor(hash.hashInt(repo_opts.hash, "user-id-set")) orelse return error.NotFound; 1315 const uset = try Repo.DB.SortedSet(.read_only).init(ucur); 1316 try std.testing.expectEqual(1, try uset.count()); 1317 } 1318 1319 // delete repo1 -> dense order (no tombstone) 1320 try evt.consume(.xit, repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{ 1321 .{ .id = std.fmt.bytesToHex(repo_ids[1], .lower), .author_email = author_email, .timestamp = 200, .event = .{ .repo = null } }, 1322 }); 1323 { 1324 const moment = try evt.currentMoment(repo_opts, &repo); 1325 try Check.order(Repo.DB, repo_opts.hash, moment, &.{ &repo_ids[3], &repo_ids[2], &repo_ids[0] }); 1326 } 1327 1328 // update repo0 at a later timestamp -> keeps its original place 1329 try evt.consume(.xit, repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{ 1330 .{ .id = std.fmt.bytesToHex(repo_ids[0], .lower), .author_email = author_email, .timestamp = 300, .event = .{ .repo = .{ .user_id = &user_id, .name = "repo0", .description = "updated" } } }, 1331 }); 1332 { 1333 const moment = try evt.currentMoment(repo_opts, &repo); 1334 try Check.order(Repo.DB, repo_opts.hash, moment, &.{ &repo_ids[3], &repo_ids[2], &repo_ids[0] }); 1335 1336 // the value really was updated 1337 const e2r_cur = try moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->repo")) orelse return error.NotFound; 1338 const e2r = try Repo.DB.HashMap(.read_only).init(e2r_cur); 1339 const rc = try e2r.getCursor(hash.hashInt(repo_opts.hash, &repo_ids[0])) orelse return error.NotFound; 1340 const rm = try Repo.DB.HashMap(.read_only).init(rc); 1341 const re = try evt.read(evt.Repo.Record, Repo.DB, repo_opts.hash, &arena, rm); 1342 try std.testing.expectEqualStrings("updated", re.event.description); 1343 } 1344 } 1345