╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1733 db_file.close(io); 1734 state.core.repo_dir.deleteFile(io, db_name) catch {}; 1735 } 1736 1737 const buffer_ptr = try allocator.create(std.Io.Writer.Allocating); 1738 errdefer allocator.destroy(buffer_ptr); 1739 1740 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 1741 errdefer buffer_ptr.deinit(); 1742 1743 const db_ptr = try allocator.create(DB); 1744 errdefer allocator.destroy(db_ptr); 1745 // the db is scratch state that gets deleted after iteration, so 1746 // there is nothing worth fsyncing 1747 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr, .fsync = false }); 1748 1749 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 1750 1751 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 1752 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 1753 1754 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 1755 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 1756 1757 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 1758 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 1759 1760 var self = CommitIterator(repo_kind, repo_opts){ 1761 .repo_dir = state.core.repo_dir, 1762 .db_file = db_file, 1763 .db = db_ptr, 1764 .parent_to_children = parent_to_children, 1765 .child_to_pending_parents = child_to_pending_parents, 1766 .ready_queue = ready_queue, 1767 .ready_queue_index = 0, 1768 .newest_object_id = null, 1769 .oldest_object_id = null, 1770 }; 1771 errdefer self.deinit(io, allocator); 1772 1773 try self.collect(state, io, allocator, consumed_object_ids, ref); 1774 1775 return self; 1776 } 1777 1778 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 1779 self.db_file.close(io); 1780 self.db.core.memory.buffer.deinit(); 1781 allocator.destroy(self.db.core.memory.buffer); 1782 self.repo_dir.deleteFile(io, db_name) catch {}; 1783 allocator.destroy(self.db); 1784 } 1785 1786 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 1787 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 1788 1789 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 1790 self.ready_queue_index += 1; 1791 1792 try self.enqueueChildren(&oid); 1793 return oid; 1794 } 1795 1796 fn collect( 1797 self: *CommitIterator(repo_kind, repo_opts), 1798 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1799 io: std.Io, 1800 allocator: std.mem.Allocator, 1801 consumed_object_ids: DB.HashMap(.read_only), 1802 ref: rf.Ref, 1803 ) !void { 1804 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 1805 1806 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 1807 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 1808 var walk_queue_index: u64 = 0; 1809 1810 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 1811 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 1812 1813 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 1814 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1815 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 1816 try walk_queue.append(.{ .bytes = &head_oid }); 1817 1818 while (walk_queue_index < try walk_queue.count()) { 1819 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 1820 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 1821 1822 walk_queue_index += 1; 1823 1824 // if we've seen this object id, skip it 1825 if (null != try seen_object_ids.getCursor(oid_int)) { 1826 continue; 1827 } 1828 1829 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 1830 1831 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 1832 defer commit_object.deinit(); 1833 1834 // if this object id has already been consumed, skip it 1835 if (null != try consumed_object_ids.getCursor(oid_int)) { 1836 continue; 1837 } 1838 1839 if (self.newest_object_id == null) self.newest_object_id = oid; 1840 self.oldest_object_id = oid; 1841 1842 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 1843 var pending_parent_count: u64 = 0; 1844 for (parent_oids) |*parent_oid| { 1845 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1846 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 1847 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 1848 1849 // if this object id has already been consumed, skip it 1850 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 1851 continue; 1852 } 1853 1854 // if this object id hasn't already been seen, add it to the walk queue 1855 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 1856 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 1857 } 1858 1859 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 1860 const children = try DB.HashSet(.read_write).init(children_cursor); 1861 try children.put(oid_int, .{ .bytes = &oid }); 1862 pending_parent_count += 1; 1863 } 1864 1865 if (pending_parent_count == 0) { 1866 try self.ready_queue.append(.{ .bytes = &oid }); 1867 } else { 1868 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 1869 } 1870 } 1871 } 1872 1873 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 1874 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 1875 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 1876 const children = try DB.HashSet(.read_only).init(children_cursor); 1877 var children_iter = try children.iterator(); 1878 1879 while (try children_iter.next()) |*child_cursor| { 1880 const kv_pair = try child_cursor.readKeyValuePair(); 1881 const child_oid_int = kv_pair.hash; 1882 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 1883 const pending_parent_count = try pending_parent_cursor.readUint(); 1884 1885 if (pending_parent_count <= 1) { 1886 _ = try self.child_to_pending_parents.remove(child_oid_int); 1887 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 1888 try self.ready_queue.append(.{ .bytes = &child_oid }); 1889 } else { 1890 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 1891 } 1892 } 1893 } 1894 1895 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 1896 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 1897 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1898 _ = try oid_cursor.readBytes(&oid); 1899 return oid; 1900 } 1901 }; 1902 } 1903