╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1325 .target_wins => continue, 1326 } 1327 1328 // removed records do not carry field conflicts 1329 if (merged.removed) continue; 1330 1331 const conflicts_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, T.conflicts_key)); 1332 const conflicts = try DB.SortedMap(.read_write).init(conflicts_cursor); 1333 const id_to_field_to_oid_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, T.id_to_field_to_oid_key)); 1334 const id_to_field_to_oid = try DB.HashMap(.read_write).init(id_to_field_to_oid_cursor); 1335 const merged_key = orderKeyDesc(merged.created_order, &event_id); 1336 1337 var parent_field_oids: ?DB.SortedMap(.read_only) = null; 1338 if (parent_id_to_field_to_oid) |map| { 1339 if (try map.getCursor(record_key)) |field_oids_cursor| { 1340 parent_field_oids = try DB.SortedMap(.read_only).init(field_oids_cursor); 1341 } 1342 } 1343 // created only once a field actually comes from the parent 1344 var field_oids: ?DB.SortedMap(.read_write) = null; 1345 1346 // the fields both sides changed differently, space separated 1347 var conflicted_fields: [conflictedFieldsMaxLen(T)]u8 = undefined; 1348 var conflicted_len: usize = 0; 1349 1350 inline for (std.meta.fields(T), 0..) |field, i| { 1351 switch (outcome[i]) { 1352 // the target's value stands, so its oid does too 1353 .kept => {}, 1354 // the parent set this value, so its commit takes the field. a 1355 // parent with no oid took its own value from a merge, leaving 1356 // the field unattributed rather than crediting the target's 1357 // commit for a value it no longer holds. 1358 .parent => { 1359 if (field_oids == null) { 1360 field_oids = try DB.SortedMap(.read_write).init(try id_to_field_to_oid.putCursor(record_key)); 1361 } 1362 const map = field_oids orelse unreachable; 1363 const parent_oid = if (parent_field_oids) |parent_map| try parent_map.getCursor(field.name) else null; 1364 if (parent_oid) |oid_cursor| { 1365 try map.put(field.name, .{ .slot = oid_cursor.slot() }); 1366 } else { 1367 _ = try map.remove(field.name); 1368 } 1369 }, 1370 .conflicted => { 1371 if (conflicted_len > 0) { 1372 conflicted_fields[conflicted_len] = ' '; 1373 conflicted_len += 1; 1374 } 1375 @memcpy(conflicted_fields[conflicted_len..][0..field.name.len], field.name); 1376 conflicted_len += field.name.len; 1377 }, 1378 } 1379 } 1380 1381 if (conflicted_len > 0) { 1382 // each side's whole record is stored by reference, so the view can 1383 // show any of them or take theirs wholesale 1384 _ = try conflicts.remove(&merged_key); 1385 const conflict = try DB.HashMap(.read_write).init(try conflicts.putCursor(&merged_key)); 1386 try conflict.put(hash.hashInt(hash_kind, conflicted_fields_key), .{ .bytes = conflicted_fields[0..conflicted_len] }); 1387 try conflict.put(hash.hashInt(hash_kind, base_record_key), .{ .slot = baseline_slot }); 1388 try conflict.put(hash.hashInt(hash_kind, their_record_key), .{ .slot = parent_slot }); 1389 if (parent_field_oids) |map| { 1390 try conflict.put(hash.hashInt(hash_kind, their_field_to_oid_key), .{ .slot = map.slot() }); 1391 } 1392 continue; 1393 } 1394 1395 // created_order never changes, so the parent keys this record's conflict 1396 // identically, and it carries over when the target has none, unless the 1397 // parent's entry is unchanged from the baseline, meaning the target 1398 // inherited it too and resolving it removed it here. 1399 if (parent_conflicts) |map| { 1400 if (null == try conflicts.getCursor(&merged_key)) { 1401 if (try map.getSlot(&merged_key)) |conflict_slot| { 1402 const baseline_conflict_slot = if (baseline_conflicts) |baseline_map| try baseline_map.getSlot(&merged_key) else null; 1403 const resolved = if (baseline_conflict_slot) |slot| slot.eql(conflict_slot) else false; 1404 if (!resolved) try conflicts.put(&merged_key, .{ .slot = conflict_slot }); 1405 } 1406 } 1407 } 1408 } 1409 } 1410 1411 pub fn readFromRepo( 1412 comptime T: type, 1413 comptime repo_kind: rp.RepoKind, 1414 comptime repo_opts: rp.RepoOpts(repo_kind), 1415 io: std.Io, 1416 allocator: std.mem.Allocator, 1417 arena: *std.heap.ArenaAllocator, 1418 repo: *rp.Repo(repo_kind, repo_opts), 1419 id: *const [event_id_size]u8, 1420 ) !?T.Record { 1421 var event_db_maybe: ?LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 1422 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 1423 const moment = (if (event_db_maybe) |*event_db| 1424 currentMomentFromDb(repo_opts.hash, event_db.db) 1425 else if (repo_kind == .git) 1426 return null 1427 else 1428 currentMoment(repo_opts, repo)) catch return null; 1429 return T.readById(EventDB(repo_opts.hash), repo_opts.hash, moment, arena, id); 1430 } 1431 1432 pub fn currentMoment( 1433 comptime repo_opts: rp.RepoOpts(.xit), 1434 repo: *rp.Repo(.xit, repo_opts), 1435 ) !rp.Repo(.xit, repo_opts).DB.HashMap(.read_only) { 1436 return currentMomentFromDb(repo_opts.hash, &repo.core.db); 1437 } 1438 1439 pub fn currentMomentFromDb( 1440 comptime hash_kind: hash.HashKind, 1441 db: *EventDB(hash_kind), 1442 ) !EventDB(hash_kind).HashMap(.read_only) { 1443 const DB = EventDB(hash_kind); 1444 const history = try DB.ArrayList(.read_only).init(db.rootCursor().readOnly()); 1445 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 1446 const moment = try DB.HashMap(.read_only).init(moment_cursor); 1447 return try currentMomentFromRepoMoment(hash_kind, moment); 1448 } 1449 1450 pub fn currentMomentFromRepoMoment( 1451 comptime hash_kind: hash.HashKind, 1452 moment: EventDB(hash_kind).HashMap(.read_only), 1453 ) !EventDB(hash_kind).HashMap(.read_only) { 1454 const DB = EventDB(hash_kind); 1455 const last_object_id_cursor = try moment.getCursor(hash.hashInt(hash_kind, last_object_id_key)) orelse return error.NotFound; 1456 var last_object_id: [hash.byteLen(hash_kind)]u8 = undefined; 1457 _ = try last_object_id_cursor.readBytes(&last_object_id); 1458 const haxy_history_cursor = try moment.getCursor(hash.hashInt(hash_kind, history_key)) orelse return error.NotFound; 1459 const haxy_history = try DB.ArrayList(.read_only).init(haxy_history_cursor); 1460 const haxy_moments_cursor = try haxy_history.getCursor(-1) orelse return error.NotFound; 1461 const haxy_moments = try DB.HashMap(.read_only).init(haxy_moments_cursor); 1462 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(hash_kind, &last_object_id)) orelse return error.NotFound; 1463 return try DB.HashMap(.read_only).init(haxy_moment_cursor); 1464 } 1465 1466 // build a sorted-set key from an order and event id. big-endian order makes 1467 // byte order match numeric order; the event id breaks ties. 1468 pub fn orderKey(order: u64, event_id: *const [event_id_size]u8) [@sizeOf(u64) + event_id_size]u8 { 1469 var key: [@sizeOf(u64) + event_id_size]u8 = undefined; 1470 std.mem.writeInt(u64, key[0..@sizeOf(u64)], order, .big); 1471 @memcpy(key[@sizeOf(u64)..], event_id); 1472 return key; 1473 } 1474 1475 pub fn orderKeyDesc(order: u64, event_id: *const [event_id_size]u8) [@sizeOf(u64) + event_id_size]u8 { 1476 return orderKey(std.math.maxInt(u64) - order, event_id); 1477 } 1478 1479 pub fn removedRecord( 1480 comptime T: type, 1481 comptime DB: type, 1482 comptime hash_kind: hash.HashKind, 1483 haxy_moment: DB.HashMap(.read_only), 1484 existing_maybe: ?T, 1485 ) !T { 1486 var record = existing_maybe orelse return error.EventNotFound; 1487 record.removed = true; 1488 const order_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, event_order_key)) orelse return error.CursorNotFound; 1489 record.updated_order = try order_cursor.readUint(); 1490 return record; 1491 } 1492 1493 // add or refresh one entry in the global logical-event index 1494 pub fn indexEvent( 1495 comptime DB: type, 1496 comptime hash_kind: hash.HashKind, 1497 haxy_moment: DB.HashMap(.read_write), 1498 event_id: *const [event_id_size]u8, 1499 kind: EventKind, 1500 existing_maybe: anytype, 1501 record: anytype, 1502 ) !void { 1503 const event_key = hash.hashInt(hash_kind, event_id); 1504 const kind_map_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, event_index_key)); 1505 const kind_map = try DB.HashMap(.read_write).init(kind_map_cursor); 1506 try kind_map.put(event_key, .{ .bytes = @tagName(kind) }); 1507 1508 const active_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, active_event_id_set_key)); 1509 const active = try DB.SortedSet(.read_write).init(active_cursor); 1510 const removed_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, removed_event_id_set_key)); 1511 const removed_set = try DB.SortedSet(.read_write).init(removed_cursor); 1512 1513 if (existing_maybe) |existing| { 1514 const old_key = orderKeyDesc(existing.updated_order, event_id); 1515 _ = try active.remove(&old_key); 1516 _ = try removed_set.remove(&old_key); 1517 } 1518 1519 const order_key = orderKeyDesc(record.updated_order, event_id); 1520 if (record.removed) { 1521 try removed_set.put(&order_key); 1522 } else { 1523 try active.put(&order_key); 1524 } 1525 } 1526 1527 pub fn touchThread( 1528 comptime DB: type, 1529 comptime hash_kind: hash.HashKind, 1530 haxy_moment: DB.HashMap(.read_write), 1531 thread_id: *const [event_id_size]u8, 1532 order: u64, 1533 arena: *std.heap.ArenaAllocator, 1534 ) !void { 1535 const kind_map_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, event_index_key)) orelse return; 1536 const kind_map = try DB.HashMap(.read_only).init(kind_map_cursor); 1537 const kind = (try readEventKind(hash_kind, kind_map, thread_id)) orelse return; 1538 switch (kind) { 1539 .discuss => try Discussion.touch(DB, hash_kind, haxy_moment, thread_id, order, arena), 1540 else => {}, 1541 } 1542 } 1543 1544 pub fn readEventKind( 1545 comptime hash_kind: hash.HashKind, 1546 kind_map: anytype, 1547 id: *const [event_id_size]u8, 1548 ) !?EventKind { 1549 const cursor = try kind_map.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 1550 var buffer: [64]u8 = undefined; 1551 return std.meta.stringToEnum(EventKind, try cursor.readBytes(&buffer)) orelse error.InvalidEventKind; 1552 } 1553 1554 // the email of the event's author, or null when the id names no authored event 1555 // of this kind 1556 pub fn readAuthorEmail( 1557 comptime repo_kind: rp.RepoKind, 1558 comptime repo_opts: rp.RepoOpts(repo_kind), 1559 io: std.Io, 1560 allocator: std.mem.Allocator, 1561 arena: *std.heap.ArenaAllocator, 1562 repo: *rp.Repo(repo_kind, repo_opts), 1563 kind: EventKind, 1564 id: *const [event_id_size]u8, 1565 ) !?[]const u8 { 1566 const record_map_key = switch (kind) { 1567 .issue => Issue.record_map_key, 1568 .discuss => Discussion.record_map_key, 1569 .comment => Comment.record_map_key, 1570 .attach => Attachment.record_map_key, 1571 .patchrev => PatchRev.record_map_key, 1572 .patch => Patch.record_map_key, 1573 .user, .repo, .fork => return null, 1574 }; 1575 1576 const DB = EventDB(repo_opts.hash); 1577 var event_db_maybe: ?LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 1578 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 1579 const moment = (if (event_db_maybe) |*event_db| 1580 currentMomentFromDb(repo_opts.hash, event_db.db) 1581 else if (repo_kind == .git) 1582 return null 1583 else 1584 currentMoment(repo_opts, repo)) catch return null; 1585 1586 const records_cursor = (try moment.getCursor(hash.hashInt(repo_opts.hash, record_map_key))) orelse return null; 1587 const records = try DB.HashMap(.read_only).init(records_cursor); 1588 const record_cursor = (try records.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return null; 1589 const record = try DB.HashMap(.read_only).init(record_cursor); 1590 return try readField(?[]const u8, DB, repo_opts.hash, arena, try record.getCursor(hash.hashInt(repo_opts.hash, "author_email"))); 1591 } 1592 1593 // the event id embedded in a set entry's order key 1594 pub fn readOrderKeyId(comptime DB: type, kv_pair_cursor: DB.Cursor(.read_only)) ![event_id_size]u8 { 1595 var cursor = kv_pair_cursor; 1596 const kv_pair = try cursor.readKeyValuePair(); 1597 var order_key: [@sizeOf(u64) + event_id_size]u8 = undefined; 1598 _ = try kv_pair.key_cursor.readBytes(&order_key); 1599 return order_key[@sizeOf(u64)..][0..event_id_size].*; 1600 } 1601 1602 // split a pushed "<owner>/<repo>" path into its two components, or null if it 1603 // isn't exactly two non-empty segments. 1604 pub fn parseOwnerRepoPath(path: []const u8) ?struct { owner: []const u8, name: []const u8 } { 1605 var it = std.mem.splitScalar(u8, path, '/'); 1606 const owner = it.next() orelse return null; 1607 const name = it.next() orelse return null; 1608 if (it.next() != null) return null; 1609 if (owner.len == 0 or name.len == 0) return null; 1610 1611 return .{ .owner = owner, .name = name }; 1612 } 1613 1614 pub const CreateRepoOptions = struct { 1615 read_access: Repo.Access = .private, 1616 }; 1617 1618 // resolve a pushed `<owner>/<repo>` to the hex event id that names its on-disk 1619 // directory under the repos dir 1620 pub fn resolveOrCreateRepo( 1621 io: std.Io, 1622 allocator: std.mem.Allocator, 1623 admin_repo_path: []const u8, 1624 owner_name: []const u8, 1625 repo_name: []const u8, 1626 create_options_maybe: ?CreateRepoOptions, 1627 ) !?[event_id_size * 2]u8 { 1628 var repo = rp.Repo(.xit, admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 1629 error.RepoNotFound => return null, 1630 else => |e| return e, 1631 }; 1632 defer repo.deinit(io, allocator); 1633 1634 var arena = std.heap.ArenaAllocator.init(allocator); 1635 defer arena.deinit(); 1636 1637 const moment = try currentMoment(admin_repo_opts, &repo); 1638 1639 // an already-registered repo reuses its event id, so a re-push (or a clone) 1640 // lands in the same repo 1641 if (try Repo.readByOwnerAndName(AdminDB, admin_repo_opts.hash, moment, &arena, owner_name, repo_name)) |found| { 1642 return std.fmt.bytesToHex(found.event_id, .lower); 1643 } 1644 1645 const create_options = create_options_maybe orelse return null; 1646 1647 // the new repo is owned by the named user, read from the name index; an 1648 // unknown owner can't own a repo, so the push is rejected 1649 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(admin_repo_opts.hash, "name->user-id")) orelse return null; 1650 const name_to_user_id = try AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 1651 const owner_id_cursor = try name_to_user_id.getCursor(hash.hashInt(admin_repo_opts.hash, owner_name)) orelse return null; 1652 var owner_user_id: [event_id_size]u8 = undefined; 1653 _ = try owner_id_cursor.readBytes(&owner_user_id); 1654 1655 const owner = (try User.readById(AdminDB, admin_repo_opts.hash, moment, &arena, &owner_user_id)) orelse unreachable; 1656 1657 var id_bytes: [event_id_size]u8 = undefined; 1658 io.random(&id_bytes); 1659 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1660 1661 try consume(.server, .admin, .xit, admin_repo_opts, io, allocator, &repo, events_ref, &[_]EventWithId{.{ 1662 .id = event_id_hex, 1663 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1664 .author = .{ .name = owner.event.name, .email = owner.event.email }, 1665 .event = .{ .repo = .{ 1666 .user_id = &owner_user_id, 1667 .name = repo_name, 1668 .description = "", 1669 .read_access = create_options.read_access, 1670 } }, 1671 }}); 1672 1673 return event_id_hex; 1674 } 1675 1676 // 1677 // reading from xitdb 1678 // 1679 1680 pub fn read( 1681 comptime T: type, 1682 comptime DB: type, 1683 comptime hash_kind: hash.HashKind, 1684 arena: *std.heap.ArenaAllocator, 1685 map: DB.HashMap(.read_only), 1686 ) !T { 1687 var event: T = undefined; 1688 switch (@typeInfo(T)) { 1689 .@"struct" => |struct_info| { 1690 inline for (struct_info.fields) |field| { 1691 const cursor = try map.getCursor(hash.hashInt(hash_kind, field.name)); 1692 @field(event, field.name) = try readField(field.type, DB, hash_kind, arena, cursor); 1693 } 1694 }, 1695 else => @compileError("read expects a struct"), 1696 } 1697 return event; 1698 } 1699 1700 fn readField( 1701 comptime Field: type, 1702 comptime DB: type, 1703 comptime hash_kind: hash.HashKind, 1704 arena: *std.heap.ArenaAllocator, 1705 cursor_maybe: ?DB.Cursor(.read_only), 1706 ) !Field { 1707 if (@typeInfo(Field) == .optional) { 1708 // a missing key or a null union payload is null 1709 const cursor = cursor_maybe orelse return null; 1710 if (cursor.slot().tag == .none) return null; 1711 return try readField(@typeInfo(Field).optional.child, DB, hash_kind, arena, cursor); 1712 } 1713 const cursor = cursor_maybe orelse return error.NotFound; 1714 switch (@typeInfo(Field)) { 1715 .pointer => |pointer_info| { 1716 if (pointer_info.size != .slice or pointer_info.child != u8 or !pointer_info.is_const) { 1717 @compileError("unsupported read field type: " ++ @typeName(Field)); 1718 } 1719 return try cursor.readBytesAlloc(arena.allocator(), null); 1720 }, 1721 .array => |array_info| { 1722 if (array_info.child != u8) @compileError("unsupported read field type: " ++ @typeName(Field)); 1723 var bytes: Field = undefined; 1724 if ((try cursor.readBytes(&bytes)).len != bytes.len) return error.InvalidByteArrayLength; 1725 return bytes; 1726 }, 1727 .bool => { 1728 var buffer: [5]u8 = undefined; 1729 const bytes = try cursor.readBytesObject(&buffer); 1730 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 1731 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 1732 if (std.mem.eql(u8, bytes.value, "true")) return true; 1733 if (std.mem.eql(u8, bytes.value, "false")) return false; 1734 return error.InvalidBool; 1735 }, 1736 .int => |int_info| return switch (int_info.signedness) { 1737 .unsigned => @intCast(try cursor.readUint()), 1738 .signed => @intCast(try cursor.readInt()), 1739 }, 1740 .@"enum" => return std.meta.stringToEnum(Field, try cursor.readBytesAlloc(arena.allocator(), null)) orelse error.InvalidEnumTag, 1741 .@"union" => |union_info| { 1742 if (union_info.tag_type == null) @compileError("unsupported read field type: " ++ @typeName(Field)); 1743 switch (cursor.slot().tag) { 1744 .bytes, .short_bytes => { 1745 const name = try cursor.readBytesAlloc(arena.allocator(), null); 1746 inline for (union_info.fields) |variant| { 1747 if (variant.type == void and std.mem.eql(u8, name, variant.name)) { 1748 return @unionInit(Field, variant.name, {}); 1749 } 1750 } 1751 return error.InvalidUnion; 1752 }, 1753 .hash_map => { 1754 const variants = try DB.HashMap(.read_only).init(cursor); 1755 var iter = try variants.iterator(); 1756 const entry = (try iter.next()) orelse return error.InvalidUnion; 1757 const pair = try entry.readKeyValuePair(); 1758 if (try iter.next() != null) return error.InvalidUnion; 1759 inline for (union_info.fields) |variant| { 1760 if (variant.type != void and pair.hash == hash.hashInt(hash_kind, variant.name)) { 1761 return @unionInit(Field, variant.name, try readField(variant.type, DB, hash_kind, arena, pair.value_cursor)); 1762 } 1763 } 1764 return error.InvalidUnion; 1765 }, 1766 else => return error.InvalidUnion, 1767 } 1768 }, 1769 .@"struct" => return try read(Field, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)), 1770 else => @compileError("unsupported read field type: " ++ @typeName(Field)), 1771 } 1772 } 1773 1774 // whether two events carry the same data 1775 fn fieldsEqual(comptime T: type, a: T, b: T) bool { 1776 switch (@typeInfo(T)) { 1777 .@"struct" => |struct_info| { 1778 inline for (struct_info.fields) |field| { 1779 if (!fieldEqual(field.type, @field(a, field.name), @field(b, field.name))) return false; 1780 } 1781 }, 1782 else => @compileError("fieldsEqual expects a struct"), 1783 } 1784 1785 return true; 1786 } 1787 1788 pub fn fieldEqual(comptime Field: type, a: Field, b: Field) bool { 1789 switch (@typeInfo(Field)) { 1790 .pointer => |pointer_info| { 1791 if (pointer_info.size == .slice and pointer_info.child == u8) { 1792 return std.mem.eql(u8, a, b); 1793 } else { 1794 @compileError("unsupported field type: " ++ @typeName(Field)); 1795 } 1796 }, 1797 .array => |array_info| { 1798 if (array_info.child == u8) { 1799 return std.mem.eql(u8, &a, &b); 1800 } else { 1801 @compileError("unsupported field type: " ++ @typeName(Field)); 1802 } 1803 }, 1804 .void => return true, 1805 .bool, .int, .@"enum" => return a == b, 1806 .@"union" => { 1807 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1808 return switch (a) { 1809 inline else => |value, tag| fieldEqual(@TypeOf(value), value, @field(b, @tagName(tag))), 1810 }; 1811 }, 1812 .@"struct" => return fieldsEqual(Field, a, b), 1813 .optional => |optional_info| { 1814 if (a) |a_value| { 1815 if (b) |b_value| return fieldEqual(optional_info.child, a_value, b_value); 1816 return false; 1817 } 1818 return b == null; 1819 }, 1820 else => @compileError("unsupported field type: " ++ @typeName(Field)), 1821 } 1822 } 1823 1824 // 1825 // writing to xitdb 1826 // 1827 1828 pub fn upsert( 1829 comptime T: type, 1830 comptime DB: type, 1831 comptime hash_kind: hash.HashKind, 1832 map: DB.HashMap(.read_write), 1833 event: T, 1834 ) !void { 1835 switch (@typeInfo(T)) { 1836 .@"struct" => |struct_info| { 1837 inline for (struct_info.fields) |field| { 1838 try upsertField(DB, hash_kind, map, field.name, field.type, @field(event, field.name)); 1839 } 1840 }, 1841 else => @compileError("upsert expects a struct"), 1842 } 1843 } 1844 1845 fn upsertField( 1846 comptime DB: type, 1847 comptime hash_kind: hash.HashKind, 1848 map: DB.HashMap(.read_write), 1849 comptime field_name: []const u8, 1850 comptime Field: type, 1851 value: Field, 1852 ) !void { 1853 const key = hash.hashInt(hash_kind, field_name); 1854 1855 switch (@typeInfo(Field)) { 1856 .pointer => |pointer_info| { 1857 if (pointer_info.size == .slice and pointer_info.child == u8) { 1858 try upsertBytes(DB, hash_kind, map, key, value); 1859 } else { 1860 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1861 } 1862 }, 1863 .array => |array_info| { 1864 if (array_info.child == u8) { 1865 try upsertBytes(DB, hash_kind, map, key, &value); 1866 } else { 1867 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1868 } 1869 }, 1870 .int => |int_info| switch (int_info.signedness) { 1871 .unsigned => { 1872 if (try map.getCursor(key)) |value_cursor| { 1873 if (try value_cursor.readUint() == value) { 1874 return; 1875 } 1876 } 1877 1878 try map.put(key, .{ .uint = value }); 1879 }, 1880 .signed => { 1881 if (try map.getCursor(key)) |value_cursor| { 1882 if (try value_cursor.readInt() == value) { 1883 return; 1884 } 1885 } 1886 1887 try map.put(key, .{ .int = value }); 1888 }, 1889 }, 1890 .bool => { 1891 const bytes = if (value) "true" else "false"; 1892 if (try map.getCursor(key)) |value_cursor| { 1893 var buffer: [5]u8 = undefined; 1894 const existing = try value_cursor.readBytesObject(&buffer); 1895 if (existing.format_tag) |format_tag| { 1896 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 1897 return; 1898 } 1899 } 1900 } 1901 1902 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 1903 }, 1904 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 1905 .@"union" => switch (value) { 1906 inline else => |payload, tag| { 1907 if (@TypeOf(payload) == void) { 1908 try upsertBytes(DB, hash_kind, map, key, @tagName(tag)); 1909 } else { 1910 const tag_key = hash.hashInt(hash_kind, @tagName(tag)); 1911 if (try map.getCursor(key)) |cursor| { 1912 switch (cursor.slot().tag) { 1913 .hash_map => { 1914 const variants = try DB.HashMap(.read_only).init(cursor); 1915 if (try variants.getCursor(tag_key) == null) try map.put(key, .{ .slot = null }); 1916 }, 1917 else => try map.put(key, .{ .slot = null }), 1918 } 1919 } 1920 const variants = try DB.HashMap(.read_write).init(try map.putCursor(key)); 1921 if (@typeInfo(@TypeOf(payload)) == .optional) { 1922 // keep the variant even when its payload is null 1923 if (payload == null) return try variants.put(tag_key, .{ .slot = null }); 1924 } 1925 if (try variants.getCursor(tag_key)) |cursor| { 1926 if (cursor.slot().tag == .none) _ = try variants.remove(tag_key); 1927 } 1928 try upsertField(DB, hash_kind, variants, @tagName(tag), @TypeOf(payload), payload); 1929 } 1930 }, 1931 }, 1932 .optional => |optional_info| { 1933 // a missing key is null 1934 if (value) |child| { 1935 try upsertField(DB, hash_kind, map, field_name, optional_info.child, child); 1936 } else { 1937 _ = try map.remove(key); 1938 } 1939 }, 1940 .@"struct" => try upsert( 1941 Field, 1942 DB, 1943 hash_kind, 1944 try DB.HashMap(.read_write).init(try map.putCursor(key)), 1945 value, 1946 ), 1947 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 1948 } 1949 } 1950 1951 fn upsertBytes( 1952 comptime DB: type, 1953 comptime hash_kind: hash.HashKind, 1954 map: DB.HashMap(.read_write), 1955 key: hash.HashInt(hash_kind), 1956 value: []const u8, 1957 ) !void { 1958 var existing_cursor_maybe = try map.getCursor(key); 1959 if (existing_cursor_maybe) |*existing_cursor| { 1960 if (try bytesEqual(DB, existing_cursor, value)) { 1961 return; 1962 } 1963 } 1964 1965 try map.put(key, .{ .bytes = value }); 1966 } 1967 1968 fn bytesEqual( 1969 comptime DB: type, 1970 cursor: *DB.Cursor(.read_only), 1971 value: []const u8, 1972 ) !bool { 1973 switch (cursor.slot().tag) { 1974 .bytes, .short_bytes => {}, 1975 else => return false, 1976 } 1977 var read_buffer: [1024]u8 = undefined; 1978 var reader = try cursor.reader(&read_buffer); 1979 if (reader.size != value.len) { 1980 return false; 1981 } 1982 1983 var chunk_buffer: [1024]u8 = undefined; 1984 var offset: usize = 0; 1985 while (offset < value.len) { 1986 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1987 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1988 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1989 return false; 1990 } 1991 offset += chunk_len; 1992 } 1993 1994 return true; 1995 } 1996 1997 // 1998 // CommitIterator 1999 // 2000 // differs from xit's ObjectIterator in a few ways: 2001 // 2002 // 1. iterates parents first, rather than children first 2003 // 2. `next` only returns the object id, not the full Object 2004 // 3. stores its temporary state in a file on disk to avoid OOMs 2005 // 2006 2007 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 2008 return struct { 2009 const DB = EventDB(repo_opts.hash); 2010 // a plain file is used for the scratch db because each write outside 2011 // a transaction is synced anyway, so buffering would gain nothing 2012 const TempDB = xit.xitdb.Database(.file, hash.HashInt(repo_opts.hash)); 2013 const db_name = "haxy-repo-events.db"; 2014 2015 repo_dir: std.Io.Dir, 2016 db_file: std.Io.File, 2017 db: *TempDB, 2018 // map of each object id to its children 2019 parent_to_children: TempDB.HashMap(.read_write), 2020 // tracking pending parents is necessary because a child can only 2021 // be returned after all of its unconsumed parents have been returned 2022 child_to_pending_parents: TempDB.HashMap(.read_write), 2023 // queue of object ids that are ready to be returned from `next` 2024 ready_queue: TempDB.ArrayList(.read_write), 2025 ready_queue_index: u64, 2026 // the object id at the tip of this branch 2027 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2028 // the object id furthest back in the history that hasn't been consumed 2029 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 2030 2031 pub fn init( 2032 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2033 io: std.Io, 2034 allocator: std.mem.Allocator, 2035 consumed_object_ids: DB.HashMap(.read_only), 2036 ref: rf.Ref, 2037 ) !CommitIterator(repo_kind, repo_opts) { 2038 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 2039 errdefer { 2040 db_file.close(io); 2041 state.core.repo_dir.deleteFile(io, db_name) catch {}; 2042 } 2043 2044 // cursors point at the db, so it needs a stable address 2045 const db_ptr = try allocator.create(TempDB); 2046 errdefer allocator.destroy(db_ptr); 2047 // the db is scratch state that gets deleted after iteration, so 2048 // there is nothing worth fsyncing 2049 db_ptr.* = try TempDB.init(.{ .io = io, .file = db_file, .fsync = false }); 2050 2051 const map = try TempDB.HashMap(.read_write).init(db_ptr.rootCursor()); 2052 2053 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 2054 const parent_to_children = try TempDB.HashMap(.read_write).init(parent_to_children_cursor); 2055 2056 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 2057 const child_to_pending_parents = try TempDB.HashMap(.read_write).init(child_to_pending_parents_cursor); 2058 2059 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 2060 const ready_queue = try TempDB.ArrayList(.read_write).init(ready_queue_cursor); 2061 2062 var self = CommitIterator(repo_kind, repo_opts){ 2063 .repo_dir = state.core.repo_dir, 2064 .db_file = db_file, 2065 .db = db_ptr, 2066 .parent_to_children = parent_to_children, 2067 .child_to_pending_parents = child_to_pending_parents, 2068 .ready_queue = ready_queue, 2069 .ready_queue_index = 0, 2070 .newest_object_id = null, 2071 .oldest_object_id = null, 2072 }; 2073 errdefer self.deinit(io, allocator); 2074 2075 try self.collect(state, io, allocator, consumed_object_ids, ref); 2076 2077 return self; 2078 } 2079 2080 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 2081 self.db_file.close(io); 2082 self.repo_dir.deleteFile(io, db_name) catch {}; 2083 allocator.destroy(self.db); 2084 } 2085 2086 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 2087 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 2088 2089 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 2090 self.ready_queue_index += 1; 2091 2092 try self.enqueueChildren(&oid); 2093 return oid; 2094 } 2095 2096 fn collect( 2097 self: *CommitIterator(repo_kind, repo_opts), 2098 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 2099 io: std.Io, 2100 allocator: std.mem.Allocator, 2101 consumed_object_ids: DB.HashMap(.read_only), 2102 ref: rf.Ref, 2103 ) !void { 2104 const map = try TempDB.HashMap(.read_write).init(self.db.rootCursor()); 2105 2106 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 2107 const walk_queue = try TempDB.ArrayList(.read_write).init(walk_queue_cursor); 2108 var walk_queue_index: u64 = 0; 2109 2110 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 2111 const seen_object_ids = try TempDB.HashSet(.read_write).init(seen_object_ids_cursor); 2112 2113 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 2114 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2115 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 2116 try walk_queue.append(.{ .bytes = &head_oid }); 2117 2118 while (walk_queue_index < try walk_queue.count()) { 2119 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 2120 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 2121 2122 walk_queue_index += 1; 2123 2124 // if we've seen this object id, skip it 2125 if (null != try seen_object_ids.getCursor(oid_int)) { 2126 continue; 2127 } 2128 2129 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 2130 2131 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 2132 defer commit_object.deinit(); 2133 2134 // if this object id has already been consumed, skip it 2135 if (null != try consumed_object_ids.getCursor(oid_int)) { 2136 continue; 2137 } 2138 2139 if (self.newest_object_id == null) self.newest_object_id = oid; 2140 self.oldest_object_id = oid; 2141 2142 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 2143 var pending_parent_count: u64 = 0; 2144 for (parent_oids) |*parent_oid| { 2145 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2146 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 2147 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 2148 2149 // if this object id has already been consumed, skip it 2150 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 2151 continue; 2152 } 2153 2154 // if this object id hasn't already been seen, add it to the walk queue 2155 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 2156 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 2157 } 2158 2159 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 2160 const children = try TempDB.HashSet(.read_write).init(children_cursor); 2161 try children.put(oid_int, .{ .bytes = &oid }); 2162 pending_parent_count += 1; 2163 } 2164 2165 if (pending_parent_count == 0) { 2166 try self.ready_queue.append(.{ .bytes = &oid }); 2167 } else { 2168 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 2169 } 2170 } 2171 } 2172 2173 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 2174 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 2175 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 2176 const children = try TempDB.HashSet(.read_only).init(children_cursor); 2177 var children_iter = try children.iterator(); 2178 2179 while (try children_iter.next()) |*child_cursor| { 2180 const kv_pair = try child_cursor.readKeyValuePair(); 2181 const child_oid_int = kv_pair.hash; 2182 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 2183 const pending_parent_count = try pending_parent_cursor.readUint(); 2184 2185 if (pending_parent_count <= 1) { 2186 _ = try self.child_to_pending_parents.remove(child_oid_int); 2187 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 2188 try self.ready_queue.append(.{ .bytes = &child_oid }); 2189 } else { 2190 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 2191 } 2192 } 2193 } 2194 2195 fn readOidFromList(list: TempDB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 2196 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 2197 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 2198 _ = try oid_cursor.readBytes(&oid); 2199 return oid; 2200 } 2201 }; 2202 } 2203