╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1785 errdefer allocator.destroy(buffer_ptr); 1786 1787 buffer_ptr.* = std.Io.Writer.Allocating.init(allocator); 1788 errdefer buffer_ptr.deinit(); 1789 1790 const db_ptr = try allocator.create(DB); 1791 errdefer allocator.destroy(db_ptr); 1792 // the db is scratch state that gets deleted after iteration, so 1793 // there is nothing worth fsyncing 1794 db_ptr.* = try DB.init(.{ .io = io, .file = db_file, .buffer = buffer_ptr, .fsync = false }); 1795 1796 const map = try DB.HashMap(.read_write).init(db_ptr.rootCursor()); 1797 1798 const parent_to_children_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "parent->children")); 1799 const parent_to_children = try DB.HashMap(.read_write).init(parent_to_children_cursor); 1800 1801 const child_to_pending_parents_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "child->pending-parents")); 1802 const child_to_pending_parents = try DB.HashMap(.read_write).init(child_to_pending_parents_cursor); 1803 1804 const ready_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "ready-queue")); 1805 const ready_queue = try DB.ArrayList(.read_write).init(ready_queue_cursor); 1806 1807 var self = CommitIterator(repo_kind, repo_opts){ 1808 .repo_dir = state.core.repo_dir, 1809 .db_file = db_file, 1810 .db = db_ptr, 1811 .parent_to_children = parent_to_children, 1812 .child_to_pending_parents = child_to_pending_parents, 1813 .ready_queue = ready_queue, 1814 .ready_queue_index = 0, 1815 .newest_object_id = null, 1816 .oldest_object_id = null, 1817 }; 1818 errdefer self.deinit(io, allocator); 1819 1820 try self.collect(state, io, allocator, consumed_object_ids, ref); 1821 1822 return self; 1823 } 1824 1825 pub fn deinit(self: *CommitIterator(repo_kind, repo_opts), io: std.Io, allocator: std.mem.Allocator) void { 1826 self.db_file.close(io); 1827 self.db.core.memory.buffer.deinit(); 1828 allocator.destroy(self.db.core.memory.buffer); 1829 self.repo_dir.deleteFile(io, db_name) catch {}; 1830 allocator.destroy(self.db); 1831 } 1832 1833 pub fn next(self: *CommitIterator(repo_kind, repo_opts)) !?[hash.byteLen(repo_opts.hash)]u8 { 1834 if (self.ready_queue_index >= try self.ready_queue.count()) return null; 1835 1836 const oid = try readOidFromList(self.ready_queue.readOnly(), self.ready_queue_index); 1837 self.ready_queue_index += 1; 1838 1839 try self.enqueueChildren(&oid); 1840 return oid; 1841 } 1842 1843 fn collect( 1844 self: *CommitIterator(repo_kind, repo_opts), 1845 state: rp.Repo(repo_kind, repo_opts).State(.read_only), 1846 io: std.Io, 1847 allocator: std.mem.Allocator, 1848 consumed_object_ids: DB.HashMap(.read_only), 1849 ref: rf.Ref, 1850 ) !void { 1851 const map = try DB.HashMap(.read_write).init(self.db.rootCursor()); 1852 1853 const walk_queue_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "walk-queue")); 1854 const walk_queue = try DB.ArrayList(.read_write).init(walk_queue_cursor); 1855 var walk_queue_index: u64 = 0; 1856 1857 const seen_object_ids_cursor = try map.putCursor(hash.hashInt(repo_opts.hash, "seen-object-ids")); 1858 const seen_object_ids = try DB.HashSet(.read_write).init(seen_object_ids_cursor); 1859 1860 const head_oid_hex = (try rf.readRecur(repo_kind, repo_opts, state, io, .{ .ref = ref })) orelse return error.OidNotFound; 1861 var head_oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1862 _ = try std.fmt.hexToBytes(&head_oid, &head_oid_hex); 1863 try walk_queue.append(.{ .bytes = &head_oid }); 1864 1865 while (walk_queue_index < try walk_queue.count()) { 1866 const oid = try readOidFromList(walk_queue.readOnly(), walk_queue_index); 1867 const oid_int = hash.bytesToInt(repo_opts.hash, &oid); 1868 1869 walk_queue_index += 1; 1870 1871 // if we've seen this object id, skip it 1872 if (null != try seen_object_ids.getCursor(oid_int)) { 1873 continue; 1874 } 1875 1876 try seen_object_ids.put(oid_int, .{ .bytes = &oid }); 1877 1878 var commit_object = try obj.Object(repo_kind, repo_opts).init(state, io, allocator, &std.fmt.bytesToHex(oid, .lower)); 1879 defer commit_object.deinit(); 1880 1881 // if this object id has already been consumed, skip it 1882 if (null != try consumed_object_ids.getCursor(oid_int)) { 1883 continue; 1884 } 1885 1886 if (self.newest_object_id == null) self.newest_object_id = oid; 1887 self.oldest_object_id = oid; 1888 1889 const parent_oids = commit_object.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 1890 var pending_parent_count: u64 = 0; 1891 for (parent_oids) |*parent_oid| { 1892 var parent_oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1893 _ = try std.fmt.hexToBytes(&parent_oid_bytes, parent_oid); 1894 const parent_oid_int = hash.bytesToInt(repo_opts.hash, &parent_oid_bytes); 1895 1896 // if this object id has already been consumed, skip it 1897 if (null != try consumed_object_ids.getCursor(parent_oid_int)) { 1898 continue; 1899 } 1900 1901 // if this object id hasn't already been seen, add it to the walk queue 1902 if (null == try seen_object_ids.getCursor(parent_oid_int)) { 1903 try walk_queue.append(.{ .bytes = &parent_oid_bytes }); 1904 } 1905 1906 const children_cursor = try self.parent_to_children.putCursor(parent_oid_int); 1907 const children = try DB.HashSet(.read_write).init(children_cursor); 1908 try children.put(oid_int, .{ .bytes = &oid }); 1909 pending_parent_count += 1; 1910 } 1911 1912 if (pending_parent_count == 0) { 1913 try self.ready_queue.append(.{ .bytes = &oid }); 1914 } else { 1915 try self.child_to_pending_parents.put(oid_int, .{ .uint = pending_parent_count }); 1916 } 1917 } 1918 } 1919 1920 fn enqueueChildren(self: *CommitIterator(repo_kind, repo_opts), oid: *const [hash.byteLen(repo_opts.hash)]u8) !void { 1921 const oid_int = hash.bytesToInt(repo_opts.hash, oid); 1922 const children_cursor = try self.parent_to_children.getCursor(oid_int) orelse return; 1923 const children = try DB.HashSet(.read_only).init(children_cursor); 1924 var children_iter = try children.iterator(); 1925 1926 while (try children_iter.next()) |*child_cursor| { 1927 const kv_pair = try child_cursor.readKeyValuePair(); 1928 const child_oid_int = kv_pair.hash; 1929 const pending_parent_cursor = try self.child_to_pending_parents.getCursor(child_oid_int) orelse return error.CursorNotFound; 1930 const pending_parent_count = try pending_parent_cursor.readUint(); 1931 1932 if (pending_parent_count <= 1) { 1933 _ = try self.child_to_pending_parents.remove(child_oid_int); 1934 const child_oid = hash.intToBytes(hash.HashInt(repo_opts.hash), child_oid_int); 1935 try self.ready_queue.append(.{ .bytes = &child_oid }); 1936 } else { 1937 try self.child_to_pending_parents.put(child_oid_int, .{ .uint = pending_parent_count - 1 }); 1938 } 1939 } 1940 } 1941 1942 fn readOidFromList(list: DB.ArrayList(.read_only), index: u64) ![hash.byteLen(repo_opts.hash)]u8 { 1943 const oid_cursor = try list.getCursor(index) orelse return error.CursorNotFound; 1944 var oid: [hash.byteLen(repo_opts.hash)]u8 = undefined; 1945 _ = try oid_cursor.readBytes(&oid); 1946 return oid; 1947 } 1948 }; 1949 } 1950