╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
363 364 fn upsert( 365 comptime DB: type, 366 comptime hash_kind: hash.HashKind, 367 map: DB.HashMap(.read_write), 368 comptime Data: type, 369 data: Data, 370 ) !void { 371 switch (@typeInfo(Data)) { 372 .@"struct" => |struct_info| { 373 inline for (struct_info.fields) |field| { 374 try upsertField(DB, hash_kind, map, field.name, field.type, @field(data, field.name)); 375 } 376 }, 377 else => @compileError("upsert expects a struct"), 378 } 379 } 380 381 fn mergeChangedMapEntries( 382 comptime DB: type, 383 target: DB.HashMap(.read_write), 384 parent: DB.HashMap(.read_only), 385 baseline: DB.HashMap(.read_only), 386 comptime is_top_level: bool, 387 ) !void { 388 var parent_iter = try parent.iterator(); 389 while (try parent_iter.next()) |kv_pair_cursor| { 390 const kv_pair = try kv_pair_cursor.readKeyValuePair(); 391 392 const baseline_value_cursor = try baseline.getCursor(kv_pair.hash) orelse { 393 if ((try target.getCursor(kv_pair.hash)) != null) { 394 return error.MergeConflict; 395 } 396 397 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 398 continue; 399 }; 400 401 const tag = kv_pair.value_cursor.slot().tag; 402 403 if (tag != baseline_value_cursor.slot().tag) { 404 return error.UnexpectedTag; 405 } 406 407 if (kv_pair.value_cursor.slot().value == baseline_value_cursor.slot().value) { 408 continue; 409 } 410 411 if (tag == .hash_map) { 412 const target_existing_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 413 if (target_existing_cursor.slot().tag != .hash_map) { 414 return error.MergeConflict; 415 } 416 417 const target_child_cursor = try target.putCursor(kv_pair.hash); 418 const target_child = try DB.HashMap(.read_write).init(target_child_cursor); 419 const parent_child = try DB.HashMap(.read_only).init(kv_pair.value_cursor); 420 const baseline_child = try DB.HashMap(.read_only).init(baseline_value_cursor); 421 try mergeChangedMapEntries(DB, target_child, parent_child, baseline_child, false); 422 } else if (is_top_level) { 423 // the moment-index is a top-level key in the haxy moment 424 // and is not a map. we don't want to do any merge of this. 425 continue; 426 } else { 427 const target_value_cursor = try target.getCursor(kv_pair.hash) orelse return error.MergeConflict; 428 if (target_value_cursor.slot().value != baseline_value_cursor.slot().value) { 429 return error.MergeConflict; 430 } 431 432 try target.put(kv_pair.hash, .{ .slot = kv_pair.value_cursor.slot() }); 433 } 434 } 435 } 436 437 fn upsertField( 438 comptime DB: type, 439 comptime hash_kind: hash.HashKind, 440 map: DB.HashMap(.read_write), 441 comptime field_name: []const u8, 442 comptime Field: type, 443 value: Field, 444 ) !void { 445 const key = hash.hashInt(hash_kind, field_name); 446 447 switch (@typeInfo(Field)) { 448 .pointer => |pointer_info| { 449 if (pointer_info.size == .slice and pointer_info.child == u8) { 450 try upsertBytes(DB, hash_kind, map, key, value); 451 } else { 452 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 453 } 454 }, 455 .array => |array_info| { 456 if (array_info.child == u8) { 457 try upsertBytes(DB, hash_kind, map, key, &value); 458 } else { 459 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 460 } 461 }, 462 .int => |int_info| switch (int_info.signedness) { 463 .unsigned => { 464 if (try map.getCursor(key)) |value_cursor| { 465 if (try value_cursor.readUint() == value) { 466 return; 467 } 468 } 469 470 try map.put(key, .{ .uint = value }); 471 }, 472 .signed => { 473 if (try map.getCursor(key)) |value_cursor| { 474 if (try value_cursor.readInt() == value) { 475 return; 476 } 477 } 478 479 try map.put(key, .{ .int = value }); 480 }, 481 }, 482 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 483 } 484 } 485 486 fn upsertBytes( 487 comptime DB: type, 488 comptime hash_kind: hash.HashKind, 489 map: DB.HashMap(.read_write), 490 key: hash.HashInt(hash_kind), 491 value: []const u8, 492 ) !void { 493 var existing_cursor_maybe = try map.getCursor(key); 494 if (existing_cursor_maybe) |*existing_cursor| { 495 if (try bytesEqual(DB, existing_cursor, value)) { 496 return; 497 } 498 } 499 500 var value_cursor = try map.putCursor(key); 501 var write_buffer: [1024]u8 = undefined; 502 var writer = try value_cursor.writer(&write_buffer); 503 try writer.interface.writeAll(value); 504 try writer.finish(); 505 } 506 507 fn bytesEqual( 508 comptime DB: type, 509 cursor: *DB.Cursor(.read_only), 510 value: []const u8, 511 ) !bool { 512 var read_buffer: [1024]u8 = undefined; 513 var reader = try cursor.reader(&read_buffer); 514 if (reader.size != value.len) { 515 return false; 516 } 517 518 var chunk_buffer: [1024]u8 = undefined; 519 var offset: usize = 0; 520 while (offset < value.len) { 521 const chunk_len = @min(chunk_buffer.len, value.len - offset); 522 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 523 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 524 return false; 525 } 526 offset += chunk_len; 527 } 528 529 return true; 530 } 531