╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1516 else => @compileError("unsupported field type: " ++ @typeName(Field)), 1517 } 1518 } 1519 1520 fn readBytes( 1521 comptime DB: type, 1522 comptime hash_kind: hash.HashKind, 1523 allocator: std.mem.Allocator, 1524 map: DB.HashMap(.read_only), 1525 field_name: []const u8, 1526 ) ![]const u8 { 1527 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1528 return try cursor.readBytesAlloc(allocator, null); 1529 } 1530 1531 fn readUint( 1532 comptime DB: type, 1533 comptime hash_kind: hash.HashKind, 1534 map: DB.HashMap(.read_only), 1535 field_name: []const u8, 1536 ) !u64 { 1537 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1538 return try cursor.readUint(); 1539 } 1540 1541 fn readInt( 1542 comptime DB: type, 1543 comptime hash_kind: hash.HashKind, 1544 map: DB.HashMap(.read_only), 1545 field_name: []const u8, 1546 ) !i64 { 1547 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1548 return try cursor.readInt(); 1549 } 1550 1551 fn readBool( 1552 comptime DB: type, 1553 comptime hash_kind: hash.HashKind, 1554 map: DB.HashMap(.read_only), 1555 field_name: []const u8, 1556 ) !bool { 1557 const cursor = try map.getCursor(hash.hashInt(hash_kind, field_name)) orelse return error.NotFound; 1558 var buffer: [5]u8 = undefined; 1559 const bytes = try cursor.readBytesObject(&buffer); 1560 const format_tag = bytes.format_tag orelse return error.InvalidFormatTag; 1561 if (!std.mem.eql(u8, &format_tag, "bl")) return error.InvalidFormatTag; 1562 if (std.mem.eql(u8, bytes.value, "true")) return true; 1563 if (std.mem.eql(u8, bytes.value, "false")) return false; 1564 return error.InvalidBool; 1565 } 1566 1567 // 1568 // writing to xitdb 1569 // 1570 1571 pub fn upsert( 1572 comptime T: type, 1573 comptime DB: type, 1574 comptime hash_kind: hash.HashKind, 1575 map: DB.HashMap(.read_write), 1576 event: T, 1577 ) !void { 1578 switch (@typeInfo(T)) { 1579 .@"struct" => |struct_info| { 1580 comptime validateFlattenedFields(T); 1581 inline for (struct_info.fields) |field| { 1582 try upsertField(DB, hash_kind, map, field.name, field.type, @field(event, field.name)); 1583 } 1584 }, 1585 else => @compileError("upsert expects a struct"), 1586 } 1587 } 1588 1589 fn upsertField( 1590 comptime DB: type, 1591 comptime hash_kind: hash.HashKind, 1592 map: DB.HashMap(.read_write), 1593 comptime field_name: []const u8, 1594 comptime Field: type, 1595 value: Field, 1596 ) !void { 1597 const key = hash.hashInt(hash_kind, field_name); 1598 1599 switch (@typeInfo(Field)) { 1600 .pointer => |pointer_info| { 1601 if (pointer_info.size == .slice and pointer_info.child == u8) { 1602 try upsertBytes(DB, hash_kind, map, key, value); 1603 } else { 1604 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1605 } 1606 }, 1607 .array => |array_info| { 1608 if (array_info.child == u8) { 1609 try upsertBytes(DB, hash_kind, map, key, &value); 1610 } else { 1611 @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1612 } 1613 }, 1614 .int => |int_info| switch (int_info.signedness) { 1615 .unsigned => { 1616 if (try map.getCursor(key)) |value_cursor| { 1617 if (try value_cursor.readUint() == value) { 1618 return; 1619 } 1620 } 1621 1622 try map.put(key, .{ .uint = value }); 1623 }, 1624 .signed => { 1625 if (try map.getCursor(key)) |value_cursor| { 1626 if (try value_cursor.readInt() == value) { 1627 return; 1628 } 1629 } 1630 1631 try map.put(key, .{ .int = value }); 1632 }, 1633 }, 1634 .bool => { 1635 const bytes = if (value) "true" else "false"; 1636 if (try map.getCursor(key)) |value_cursor| { 1637 var buffer: [5]u8 = undefined; 1638 const existing = try value_cursor.readBytesObject(&buffer); 1639 if (existing.format_tag) |format_tag| { 1640 if (std.mem.eql(u8, &format_tag, "bl") and std.mem.eql(u8, existing.value, bytes)) { 1641 return; 1642 } 1643 } 1644 } 1645 1646 try map.put(key, .{ .bytes_object = .{ .value = bytes, .format_tag = "bl".* } }); 1647 }, 1648 .@"enum" => try upsertBytes(DB, hash_kind, map, key, @tagName(value)), 1649 .optional => |optional_info| { 1650 if (optional_info.child != []const u8) @compileError("unsupported upsert field type: " ++ @typeName(Field)); 1651 // a missing key is null 1652 if (value) |bytes| { 1653 try upsertBytes(DB, hash_kind, map, key, bytes); 1654 } else { 1655 _ = try map.remove(key); 1656 } 1657 }, 1658 // a struct field's own fields are stored in the same map 1659 .@"struct" => try upsert(Field, DB, hash_kind, map, value), 1660 else => @compileError("unsupported upsert field type: " ++ @typeName(Field)), 1661 } 1662 } 1663 1664 fn upsertBytes( 1665 comptime DB: type, 1666 comptime hash_kind: hash.HashKind, 1667 map: DB.HashMap(.read_write), 1668 key: hash.HashInt(hash_kind), 1669 value: []const u8, 1670 ) !void { 1671 var existing_cursor_maybe = try map.getCursor(key); 1672 if (existing_cursor_maybe) |*existing_cursor| { 1673 if (try bytesEqual(DB, existing_cursor, value)) { 1674 return; 1675 } 1676 } 1677 1678 var value_cursor = try map.putCursor(key); 1679 var write_buffer: [1024]u8 = undefined; 1680 var writer = try value_cursor.writer(&write_buffer); 1681 try writer.interface.writeAll(value); 1682 try writer.finish(); 1683 } 1684 1685 fn bytesEqual( 1686 comptime DB: type, 1687 cursor: *DB.Cursor(.read_only), 1688 value: []const u8, 1689 ) !bool { 1690 var read_buffer: [1024]u8 = undefined; 1691 var reader = try cursor.reader(&read_buffer); 1692 if (reader.size != value.len) { 1693 return false; 1694 } 1695 1696 var chunk_buffer: [1024]u8 = undefined; 1697 var offset: usize = 0; 1698 while (offset < value.len) { 1699 const chunk_len = @min(chunk_buffer.len, value.len - offset); 1700 try reader.interface.readSliceAll(chunk_buffer[0..chunk_len]); 1701 if (!std.mem.eql(u8, chunk_buffer[0..chunk_len], value[offset .. offset + chunk_len])) { 1702 return false; 1703 } 1704 offset += chunk_len; 1705 } 1706 1707 return true; 1708 } 1709 1710 // 1711 // CommitIterator 1712 // 1713 // differs from xit's ObjectIterator in a few ways: 1714 // 1715 // 1. iterates parents first, rather than children first 1716 // 2. `next` only returns the object id, not the full Object 1717 // 3. stores its temporary state in a file on disk to avoid OOMs 1718 // 1719 1720 pub fn CommitIterator(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1721 return struct { 1722 const DB = EventDB(repo_opts.hash); 1723 const db_name = "haxy-repo-events.db"; 1724 1725 repo_dir: std.Io.Dir, 1726 db_file: std.Io.File, 1727 db: *DB, 1728 // map of each object id to its children 1729 parent_to_children: DB.HashMap(.read_write), 1730 // tracking pending parents is necessary because a child can only 1731 // be returned after all of its unconsumed parents have been returned 1732 child_to_pending_parents: DB.HashMap(.read_write), 1733 // queue of object ids that are ready to be returned from `next` 1734 ready_queue: DB.ArrayList(.read_write), 1735 ready_queue_index: u64, 1736 // the object id at the tip of this branch 1737 newest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1738 // the object id furthest back in the history that hasn't been consumed 1739 oldest_object_id: ?[hash.byteLen(repo_opts.hash)]u8, 1740 1741 pub fn init( 1742 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1743 io: std.Io, 1744 allocator: std.mem.Allocator, 1745 consumed_object_ids: DB.HashMap(.read_only), 1746 ref: rf.Ref, 1747 ) !CommitIterator(repo_kind, repo_opts) { 1748 const db_file = try state.core.repo_dir.createFile(io, db_name, .{ .truncate = true, .lock = .exclusive, .read = true }); 1749 errdefer { 1750 db_file.close(io); 1751 state.core.repo_dir.deleteFile(io, db_name) catch {}; 1752 } 1753 1754 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 1755 errdefer allocator.destroy(buffer_ptr); 1756 1757 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 1758 errdefer buffer_ptr.deinit(); 1759 1760 const db_ptr = try allocator.create(DB); 1761 errdefer allocator.destroy(db_ptr); 1762 // the db is scratch state that gets deleted after iteration, so 1763 // there is nothing worth fsyncing 1764 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr, .fsync = false }); 1765 1766 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 1767 1768 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 1769 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 1770 1771 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 1772 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 1773 1774 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 1775 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 1776 1777 var self = CommitIterator(repo_kind, repo_opts){ 1778 .repo_dir = state.core.repo_dir, 1779 .db_file = db_file, 1780 .db = db_ptr, 1781 .parent_to_children = parent_to_children, 1782 .child_to_pending_parents = child_to_pending_parents, 1783 .ready_queue = ready_queue, 1784 .ready_queue_index = 0, 1785 .newest_object_id = null, 1786 .oldest_object_id = null, 1787 }; 1788 errdefer self.deinit(io, allocator); 1789 1790 try self.collect(state, io, allocator, consumed_object_ids, ref); 1791 1792 return self; 1793 } 1794 1795 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 1796 self.db_file.close(io); 1797 self.db.core.memory.buffer.deinit(); 1798 allocator.destroy(self.db.core.memory.buffer); 1799 self.repo_dir.deleteFile(io, db_name) catch {}; 1800 allocator.destroy(self.db); 1801 } 1802 1803 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 1804 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 1805 1806 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 1807 self.ready_queue_index += 1; 1808 1809 try self.enqueueChildren(&oid); 1810 return oid; 1811 } 1812 1813 fn collect( 1814 self: *CommitIterator(repo_kind, repo_opts), 1815 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1816 io: std.Io, 1817 allocator: std.mem.Allocator, 1818 consumed_object_ids: DB.HashMap(.read_only), 1819 ref: rf.Ref, 1820 ) !void { 1821 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 1822 1823 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 1824 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 1825 var walk_queue_index: u64 = 0; 1826 1827 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 1828 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 1829 1830 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 1831 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1832 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 1833 try walk_queue.append(.{ .bytes = &head_oid }); 1834 1835 while (walk_queue_index < try walk_queue.count()) { 1836 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 1837 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 1838 1839 walk_queue_index += 1; 1840 1841 // if we've seen this object id, skip it 1842 if (null != try seen_object_ids.getCursor(oid_int)) { 1843 continue; 1844 } 1845 1846 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 1847 1848 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 1849 defer commit_object.deinit(); 1850 1851 // if this object id has already been consumed, skip it 1852 if (null != try consumed_object_ids.getCursor(oid_int)) { 1853 continue; 1854 } 1855 1856 if (self.newest_object_id == null) self.newest_object_id = oid; 1857 self.oldest_object_id = oid; 1858 1859 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 1860 var pending_parent_count: u64 = 0; 1861 for (parent_oids) |*parent_oid| { 1862 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1863 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 1864 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 1865 1866 // if this object id has already been consumed, skip it 1867 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 1868 continue; 1869 } 1870 1871 // if this object id hasn't already been seen, add it to the walk queue 1872 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 1873 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 1874 } 1875 1876 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 1877 const children = try DB.HashSet(.read_write).init(children_cursor); 1878 try children.put(oid_int, .{ .bytes = &oid }); 1879 pending_parent_count += 1; 1880 } 1881 1882 if (pending_parent_count == 0) { 1883 try self.ready_queue.append(.{ .bytes = &oid }); 1884 } else { 1885 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 1886 } 1887 } 1888 } 1889 1890 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 1891 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 1892 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 1893 const children = try DB.HashSet(.read_only).init(children_cursor); 1894 var children_iter = try children.iterator(); 1895 1896 while (try children_iter.next()) |*child_cursor| { 1897 const kv_pair = try child_cursor.readKeyValuePair(); 1898 const child_oid_int = kv_pair.hash; 1899 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 1900 const pending_parent_count = try pending_parent_cursor.readUint(); 1901 1902 if (pending_parent_count <= 1) { 1903 _ = try self.child_to_pending_parents.remove(child_oid_int); 1904 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 1905 try self.ready_queue.append(.{ .bytes = &child_oid }); 1906 } else { 1907 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 1908 } 1909 } 1910 } 1911 1912 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 1913 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 1914 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1915 _ = try oid_cursor.readBytes(&oid); 1916 return oid; 1917 } 1918 }; 1919 } 1920