╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
296 // implies that an event has already been processed, but 297 // if that's true then there would be a last_object_id. 298 return error.UnexpectedParent; 299 } 300 } 301 302 // iterate over the events in reverse order 303 // (they are sorted by most recent events first) 304 for (0..repo_events.items.len) |i| { 305 const repo_event = repo_events.items[repo_events.items.len - i - 1]; 306 307 // create a moment for this object id 308 var haxy_moment_cursor = try haxy_moments.putCursor(hash.bytesToInt(repo_opts.hash, &repo_event.oid)); 309 310 // if there was a previous object id, set this haxy moment's initial value to it. 311 // this efficiently "clones" the map so we make further modifications based on it. 312 if (repo_event.parent_oids.len > 0) { 313 var first_parent_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 314 _ = try std.fmt.hexToBytes(&first_parent_oid, &repo_event.parent_oids[0]); 315 316 const first_parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &first_parent_oid)) orelse return error.CursorNotFound; 317 try haxy_moment_cursor.write(.{ .slot = first_parent_haxy_moment_cursor.slot() }); 318 } 319 320 const haxy_moment = try DB.HashMap(.read_write).init(haxy_moment_cursor); 321 322 // merge changes from every parent after the first parent. the common 323 // ancestor is the baseline, so a later parent only contributes values 324 // it changed relative to the merge base. 325 if (repo_event.parent_oids.len > 1) { 326 for (repo_event.parent_oids[1..]) |*parent_oid| { 327 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 328 _ = try std.fmt.hexToBytes(&oid, parent_oid); 329 330 const parent_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &oid)) orelse return error.CursorNotFound; 331 const parent_haxy_moment = try DB.HashMap(.read_only).init(parent_haxy_moment_cursor); 332 333 const baseline_oid_hex = try mrg.commonAncestor(.xit, repo_opts, state.readOnly(), io, allocator, &repo_event.parent_oids[0], parent_oid); 334 var baseline_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 335 _ = try std.fmt.hexToBytes(&baseline_oid, &baseline_oid_hex); 336 const baseline_haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &baseline_oid)) orelse return error.CursorNotFound; 337 const baseline_haxy_moment = try DB.HashMap(.read_only).init(baseline_haxy_moment_cursor); 338 339 try mergeChangedMapEntries(DB, haxy_moment, parent_haxy_moment, baseline_haxy_moment, true); 340 } 341 } 342 343 // associate this moment with the index it will first appear at in the haxy list. 344 // this will be important later so we can truncate that list if the user ever 345 // rebases starting at this object id. 346 try haxy_moment.put(hash.hashInt(repo_opts.hash, "moment-index"), .{ .uint = try haxy.count() - 1 }); 347 348 // consume the event unless it's a merge commit 349 if (repo_event.parent_oids.len <= 1) { 350 var commit_object = try obj.Object(.xit, repo_opts, .full).init(state.readOnly(), io, allocator, &std.fmt.bytesToHex(repo_event.oid, .lower)); 351 defer commit_object.deinit(); 352 353 // read the message from the commit 354 try commit_object.object_reader.seekTo(commit_object.content.commit.message_position); 355 const message = try commit_object.object_reader.interface.allocRemaining(arena.allocator(), .unlimited); 356 357 const event = try std.json.parseFromSliceLeaky(Event, arena.allocator(), message, .{}); 358 359 // get the id of the current event as bytes 360 var current_event_id: [event_id_size]u8 = undefined; 361 _ = try std.fmt.hexToBytes(&current_event_id, &event.id); 362 363 switch (event.data) { 364 .remove => |data| { 365 const removed_key = hash.hashInt(repo_opts.hash, &current_event_id); 366 switch (data.event_kind) { 367 .remove => return error.InvalidRemoveKind, 368 .user => { 369 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->user")); 370 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 371 if (!try event_id_to_user.remove(removed_key)) return error.EventNotFound; 372 373 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "user-id->repos")); 374 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 375 _ = try user_id_to_repos.remove(removed_key); 376 }, 377 .repo => { 378 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->repo")); 379 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 380 381 if (try event_id_to_repo.getCursor(removed_key)) |existing_repo_cursor| { 382 const existing_repo_map = try DB.HashMap(.read_only).init(existing_repo_cursor); 383 const existing_repo = try EventData.read(DB, repo_opts.hash, arena.allocator(), existing_repo_map, .repo); 384 const payload = existing_repo.repo; 385 386 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "user-id->repos")); 387 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 388 389 const user_key = hash.hashInt(repo_opts.hash, payload.user_id); 390 const user_repos_cursor = try user_id_to_repos.putCursor(user_key); 391 const user_repos = try DB.CountedHashSet(.read_write).init(user_repos_cursor); 392 _ = try user_repos.remove(removed_key); 393 } 394 if (!try event_id_to_repo.remove(removed_key)) return error.EventNotFound; 395 }, 396 .issue => { 397 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->issue")); 398 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 399 if (!try event_id_to_issue.remove(removed_key)) return error.EventNotFound; 400 }, 401 } 402 }, 403 .user => |data| { 404 const event_id_to_user_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->user")); 405 const event_id_to_user = try DB.HashMap(.read_write).init(event_id_to_user_cursor); 406 407 const user_key = hash.hashInt(repo_opts.hash, &current_event_id); 408 const user_cursor = try event_id_to_user.putCursor(user_key); 409 const user_map = try DB.HashMap(.read_write).init(user_cursor); 410 try upsert(DB, repo_opts.hash, user_map, @TypeOf(data), data); 411 }, 412 .repo => |data| { 413 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->repo")); 414 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 415 416 const repo_key = hash.hashInt(repo_opts.hash, &current_event_id); 417 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 418 const repo_map = try DB.HashMap(.read_write).init(repo_cursor); 419 try upsert(DB, repo_opts.hash, repo_map, @TypeOf(data), data); 420 421 const user_id_to_repos_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "user-id->repos")); 422 const user_id_to_repos = try DB.HashMap(.read_write).init(user_id_to_repos_cursor); 423 424 const user_repos_cursor = try user_id_to_repos.putCursor(hash.hashInt(repo_opts.hash, data.user_id)); 425 const user_repos = try DB.CountedHashSet(.read_write).init(user_repos_cursor); 426 try user_repos.put(repo_key, .{ .bytes = &current_event_id }); 427 }, 428 .issue => |data| { 429 const event_id_to_issue_cursor = try haxy_moment.putCursor(hash.hashInt(repo_opts.hash, "event-id->issue")); 430 const event_id_to_issue = try DB.HashMap(.read_write).init(event_id_to_issue_cursor); 431 432 const issue_key = hash.hashInt(repo_opts.hash, &current_event_id); 433 const issue_cursor = try event_id_to_issue.putCursor(issue_key); 434 const issue = try DB.HashMap(.read_write).init(issue_cursor); 435 try upsert(DB, repo_opts.hash, issue, @TypeOf(data), data); 436 }, 437 } 438 } 439 440 // the current object id is now the last one 441 last_object_id_maybe = repo_event.oid; 442 443 // prevent any of the data created above from being mutated by future iterations of this loop 444 try state.core.db.freeze(); 445 } 446 447 if (last_object_id_maybe) |*last_object_id| { 448 try state.extra.moment.put(hash.hashInt(repo_opts.hash, "haxy-last-object-id"), .{ .bytes = last_object_id }); 449 } 450 } 451 452 fn upsert( 453 comptime DB: type, 454 comptime hash_kind: hash.HashKind, 455 map: DB.HashMap(.read_write), 456 comptime Data: type, 457 data: Data, 458 ) !void { 459 switch (@typeInfo(Data)) { 460 .@"struct" => |struct_info| { 461 inline for (struct_info.fields) |field| { 462 try upsertField(DB, hash_kind, map, field.name, field.type, @field(data, field.name)); 463 } 464 }, 465 else => @compileError("upsert expects a struct"), 466 } 467 } 468 469 fn mergeChangedMapEntries( 470 comptime DB: type, 471 target: DB.HashMap(.read_write), 472 parent: DB.HashMap(.read_only), 473 baseline: DB.HashMap(.read_only), 474 comptime is_top_level: bool, 475 ) !void { 476 var parent_iter = try parent.iterator(); 477 while (try parent_iter.next()) |kv_pair_cursor| { 478 const kv_pair = try kv_pair_cursor.readKeyValuePair(); 479 480 const baseline_value_cursor = try baseline.getCursor(kv_pair.hash) orelse { 481 if ((try target.getCursor(kv_pair.hash)) != null) { 482 return error.MergeConflict; 483 } 484 485 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 486 continue; 487 }; 488 489 const tag = kv_pair.value_cursor.slot().tag; 490 491 if (tag != baseline_value_cursor.slot().tag) { 492 return error.UnexpectedTag; 493 } 494 495 if (kv_pair.value_cursor.slot().value == baseline_value_cursor.slot().value) { 496 continue; 497 } 498 499 if (tag == .hash_map) { 500 const target_existing_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 501 if (target_existing_cursor.slot().tag != .hash_map) { 502 return error.MergeConflict; 503 } 504 505 const target_child_cursor = try target.putCursor(kv_pair.hash); 506 const target_child = try DB.HashMap(.read_write).init(target_child_cursor); 507 const parent_child = try DB.HashMap(.read_only).init(kv_pair.value_cursor); 508 const baseline_child = try DB.HashMap(.read_only).init(baseline_value_cursor); 509 try mergeChangedMapEntries(DB, target_child, parent_child, baseline_child, false); 510 } else if (is_top_level) { 511 // the moment-index is a top-level key in the haxy moment 512 // and is not a map. we don't want to do any merge of this. 513 continue; 514 } else { 515 const target_value_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 516 if (target_value_cursor.slot().value != baseline_value_cursor.slot().value) { 517 return error.MergeConflict; 518 } 519 520 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 521 } 522 } 523 } 524 525 fn upsertField( 526 comptime DB: type, 527 comptime hash_kind: hash.HashKind, 528 map: DB.HashMap(.read_write), 529 comptime field_name: []const u8, 530 comptime Field: type, 531 value: Field, 532 ) !void { 533 const key = hash.hashInt(hash_kind, field_name); 534 535 switch (@typeInfo(Field)) { 536 .pointer => |pointer_info| { 537 if (pointer_info.size == .slice and pointer_info.child == u8) { 538 try upsertBytes(DB, hash_kind, map, key, value); 539 } else { 540 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 541 } 542 }, 543 .array => |array_info| { 544 if (array_info.child == u8) { 545 try upsertBytes(DB, hash_kind, map, key, &value); 546 } else { 547 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 548 } 549 }, 550 .int => |int_info| switch (int_info.signedness) { 551 .unsigned => { 552 if (try map.getCursor(key)) |value_cursor| { 553 if (try value_cursor.readUint() == value) { 554 return; 555 } 556 } 557 558 try map.put(key, .{ .uint = value }); 559 }, 560 .signed => { 561 if (try map.getCursor(key)) |value_cursor| { 562 if (try value_cursor.readInt() == value) { 563 return; 564 } 565 } 566 567 try map.put(key, .{ .int = value }); 568 }, 569 }, 570 .bool => { 571 const bytes = if (value) "true" else "false"; 572 if (try map.getCursor(key)) |value_cursor| { 573 var buffer: [5]u8 = undefined; 574 const existing = try value_cursor.readBytesObject(&buffer); 575 if (existing.format_tag != null and 576 std.mem.eql(u8, &existing.format_tag.?, "bl") and 577 std.mem.eql(u8, existing.value, bytes)) 578 { 579 return; 580 } 581 } 582 583 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 584 }, 585 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 586 } 587 } 588 589 fn upsertBytes( 590 comptime DB: type, 591 comptime hash_kind: hash.HashKind, 592 map: DB.HashMap(.read_write), 593 key: hash.HashInt(hash_kind), 594 value: []const u8, 595 ) !void { 596 var existing_cursor_maybe = try map.getCursor(key); 597 if (existing_cursor_maybe) |*existing_cursor| { 598 if (try bytesEqual(DB, existing_cursor, value)) { 599 return; 600 } 601 } 602 603 var value_cursor = try map.putCursor(key); 604 var write_buffer: [1024]u8 = undefined; 605 var writer = try value_cursor.writer(&write_buffer); 606 try writer.interface.writeAll(value); 607 try writer.finish(); 608 } 609 610 fn bytesEqual( 611 comptime DB: type, 612 cursor: *DB.Cursor(.read_only), 613 value: []const u8, 614 ) !bool { 615 var read_buffer: [1024]u8 = undefined; 616 var reader = try cursor.reader(&read_buffer); 617 if (reader.size != value.len) { 618 return false; 619 } 620 621 var chunk_buffer: [1024]u8 = undefined; 622 var offset: usize = 0; 623 while (offset < value.len) { 624 const chunk_len = @min(chunk_buffer.len, value.len - offset); 625 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 626 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 627 return false; 628 } 629 offset += chunk_len; 630 } 631 632 return true; 633 } 634