╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1490 if (optional_info.child != []const u8) @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1491 // a missing key is null 1492 if (value) |bytes| { 1493 try upsertBytes(DB, hash_kind, map, key, bytes); 1494 } else { 1495 _ = try map.remove(key); 1496 } 1497 }, 1498 // a struct field's own fields are stored in the same map 1499 .@"struct" => try upsert(Field, DB, hash_kind, map, value), 1500 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 1501 } 1502 } 1503 1504 fn upsertBytes( 1505 comptime DB: type, 1506 comptime hash_kind: hash.HashKind, 1507 map: DB.HashMap(.read_write), 1508 key: hash.HashInt(hash_kind), 1509 value: []const u8, 1510 ) !void { 1511 var existing_cursor_maybe = try map.getCursor(key); 1512 if (existing_cursor_maybe) |*existing_cursor| { 1513 if (try bytesEqual(DB, existing_cursor, value)) { 1514 return; 1515 } 1516 } 1517 1518 var value_cursor = try map.putCursor(key); 1519 var write_buffer: [1024]u8 = undefined; 1520 var writer = try value_cursor.writer(&write_buffer); 1521 try writer.interface.writeAll(value); 1522 try writer.finish(); 1523 } 1524 1525 fn bytesEqual( 1526 comptime DB: type, 1527 cursor: *DB.Cursor(.read_only), 1528 value: []const u8, 1529 ) !bool { 1530 var read_buffer: [1024]u8 = undefined; 1531 var reader = try cursor.reader(&read_buffer); 1532 if (reader.size != value.len) { 1533 return false; 1534 } 1535 1536 var chunk_buffer: [1024]u8 = undefined; 1537 var offset: usize = 0; 1538 while (offset < value.len) { 1539 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1540 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1541 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1542 return false; 1543 } 1544 offset += chunk_len; 1545 } 1546 1547 return true; 1548 } 1549 1550 // 1551 // CommitIterator 1552 // 1553 // differs from xit's ObjectIterator in a few ways: 1554 // 1555 // 1. iterates parents first, rather than children first 1556 // 2. `next` only returns the object id, not the full Object 1557 // 3. stores its temporary state in a file on disk to avoid OOMs 1558 // 1559 1560 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1561 return struct { 1562 const DB = EventDB(repo_opts.hash); 1563 const db_name = "haxy-repo-events.db"; 1564 1565 repo_dir: std.Io.Dir, 1566 db_file: std.Io.File, 1567 db: *DB, 1568 // map of each object id to its children 1569 parent_to_children: DB.HashMap(.read_write), 1570 // tracking pending parents is necessary because a child can only 1571 // be returned after all of its unconsumed parents have been returned 1572 child_to_pending_parents: DB.HashMap(.read_write), 1573 // queue of object ids that are ready to be returned from `next` 1574 ready_queue: DB.ArrayList(.read_write), 1575 ready_queue_index: u64, 1576 // the object id at the tip of this branch 1577 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1578 // the object id furthest back in the history that hasn't been consumed 1579 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1580 1581 pub fn init( 1582 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1583 io: std.Io, 1584 allocator: std.mem.Allocator, 1585 consumed_object_ids: DB.HashMap(.read_only), 1586 ref: rf.Ref, 1587 ) !CommitIterator(repo_kind, repo_opts) { 1588 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 1589 errdefer { 1590 db_file.close(io); 1591 state.core.repo_dir.deleteFile(io, db_name) catch {}; 1592 } 1593 1594 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 1595 errdefer allocator.destroy(buffer_ptr); 1596 1597 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 1598 errdefer buffer_ptr.deinit(); 1599 1600 const db_ptr = try allocator.create(DB); 1601 errdefer allocator.destroy(db_ptr); 1602 // the db is scratch state that gets deleted after iteration, so 1603 // there is nothing worth fsyncing 1604 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr, .fsync = false }); 1605 1606 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 1607 1608 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 1609 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 1610 1611 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 1612 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 1613 1614 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 1615 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 1616 1617 var self = CommitIterator(repo_kind, repo_opts){ 1618 .repo_dir = state.core.repo_dir, 1619 .db_file = db_file, 1620 .db = db_ptr, 1621 .parent_to_children = parent_to_children, 1622 .child_to_pending_parents = child_to_pending_parents, 1623 .ready_queue = ready_queue, 1624 .ready_queue_index = 0, 1625 .newest_object_id = null, 1626 .oldest_object_id = null, 1627 }; 1628 errdefer self.deinit(io, allocator); 1629 1630 try self.collect(state, io, allocator, consumed_object_ids, ref); 1631 1632 return self; 1633 } 1634 1635 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 1636 self.db_file.close(io); 1637 self.db.core.memory.buffer.deinit(); 1638 allocator.destroy(self.db.core.memory.buffer); 1639 self.repo_dir.deleteFile(io, db_name) catch {}; 1640 allocator.destroy(self.db); 1641 } 1642 1643 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 1644 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 1645 1646 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 1647 self.ready_queue_index += 1; 1648 1649 try self.enqueueChildren(&oid); 1650 return oid; 1651 } 1652 1653 fn collect( 1654 self: *CommitIterator(repo_kind, repo_opts), 1655 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1656 io: std.Io, 1657 allocator: std.mem.Allocator, 1658 consumed_object_ids: DB.HashMap(.read_only), 1659 ref: rf.Ref, 1660 ) !void { 1661 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 1662 1663 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 1664 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 1665 var walk_queue_index: u64 = 0; 1666 1667 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 1668 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 1669 1670 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 1671 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1672 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 1673 try walk_queue.append(.{ .bytes = &head_oid }); 1674 1675 while (walk_queue_index < try walk_queue.count()) { 1676 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 1677 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 1678 1679 walk_queue_index += 1; 1680 1681 // if we've seen this object id, skip it 1682 if (null != try seen_object_ids.getCursor(oid_int)) { 1683 continue; 1684 } 1685 1686 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 1687 1688 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 1689 defer commit_object.deinit(); 1690 1691 // if this object id has already been consumed, skip it 1692 if (null != try consumed_object_ids.getCursor(oid_int)) { 1693 continue; 1694 } 1695 1696 if (self.newest_object_id == null) self.newest_object_id = oid; 1697 self.oldest_object_id = oid; 1698 1699 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 1700 var pending_parent_count: u64 = 0; 1701 for (parent_oids) |*parent_oid| { 1702 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1703 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 1704 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 1705 1706 // if this object id has already been consumed, skip it 1707 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 1708 continue; 1709 } 1710 1711 // if this object id hasn't already been seen, add it to the walk queue 1712 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 1713 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 1714 } 1715 1716 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 1717 const children = try DB.HashSet(.read_write).init(children_cursor); 1718 try children.put(oid_int, .{ .bytes = &oid }); 1719 pending_parent_count += 1; 1720 } 1721 1722 if (pending_parent_count == 0) { 1723 try self.ready_queue.append(.{ .bytes = &oid }); 1724 } else { 1725 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 1726 } 1727 } 1728 } 1729 1730 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 1731 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 1732 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 1733 const children = try DB.HashSet(.read_only).init(children_cursor); 1734 var children_iter = try children.iterator(); 1735 1736 while (try children_iter.next()) |*child_cursor| { 1737 const kv_pair = try child_cursor.readKeyValuePair(); 1738 const child_oid_int = kv_pair.hash; 1739 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 1740 const pending_parent_count = try pending_parent_cursor.readUint(); 1741 1742 if (pending_parent_count <= 1) { 1743 _ = try self.child_to_pending_parents.remove(child_oid_int); 1744 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 1745 try self.ready_queue.append(.{ .bytes = &child_oid }); 1746 } else { 1747 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 1748 } 1749 } 1750 } 1751 1752 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 1753 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 1754 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1755 _ = try oid_cursor.readBytes(&oid); 1756 return oid; 1757 } 1758 }; 1759 } 1760