╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ PatchRev.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
118 119 if (existing_cursor_maybe == null) { 120 const ids = try DB.SortedSet(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key))); 121 try ids.put(&evt.orderKeyDesc(record.created_order, event_id)); 122 } 123 } 124 125 fn validateOid(comptime hash_kind: hash.HashKind, oid: []const u8) !void { 126 var bytes: [hash.byteLen(hash_kind)]u8 = undefined; 127 _ = try std.fmt.hexToBytes(&bytes, oid); 128 if (!std.mem.eql(u8, oid, &std.fmt.bytesToHex(bytes, .lower))) return error.InvalidOid; 129 } 130 131 pub fn readById( 132 comptime DB: type, 133 comptime hash_kind: hash.HashKind, 134 haxy_moment: DB.HashMap(.read_only), 135 arena: *std.heap.ArenaAllocator, 136 id: *const [evt.event_id_size]u8, 137 ) !?Record { 138 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 139 const records = try DB.HashMap(.read_only).init(records_cursor); 140 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 141 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 142 } 143 144 pub const WithId = struct { 145 id: [evt.event_id_size]u8, 146 record: Record, 147 }; 148 149 pub fn readNewest( 150 comptime DB: type, 151 comptime hash_kind: hash.HashKind, 152 haxy_moment: DB.HashMap(.read_only), 153 arena: *std.heap.ArenaAllocator, 154 ) !?WithId { 155 const ids_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, id_set_key)) orelse return null; 156 const ids = try DB.SortedSet(.read_only).init(ids_cursor); 157 var iter = try ids.iteratorFromIndex(0); 158 while (try iter.next()) |cursor| { 159 const id = try evt.readOrderKeyId(DB, cursor); 160 const record = (try readById(DB, hash_kind, haxy_moment, arena, &id)) orelse continue; 161 if (!record.removed) return .{ .id = id, .record = record }; 162 } 163 return null; 164 } 165 166 // derived commits referenced only by patch revision records 167 pub fn gcRoots( 168 comptime DB: type, 169 comptime hash_kind: hash.HashKind, 170 allocator: std.mem.Allocator, 171 haxy_moment: DB.HashMap(.read_only), 172 ) ![][hash.hexLen(hash_kind)]u8 { 173 var roots: std.ArrayList([hash.hexLen(hash_kind)]u8) = .empty; 174 errdefer roots.deinit(allocator); 175 var arena = std.heap.ArenaAllocator.init(allocator); 176 defer arena.deinit(); 177 178 const ids_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, id_set_key)) orelse return try roots.toOwnedSlice(allocator); 179 const ids = try DB.SortedSet(.read_only).init(ids_cursor); 180 var iter = try ids.iteratorFromIndex(0); 181 while (try iter.next()) |cursor| { 182 _ = arena.reset(.retain_capacity); 183 const id = try evt.readOrderKeyId(DB, cursor); 184 const record = (try readById(DB, hash_kind, haxy_moment, &arena, &id)) orelse continue; 185 var oid: [hash.hexLen(hash_kind)]u8 = undefined; 186 if (record.patch_oid.len != oid.len) return error.InvalidOid; 187 @memcpy(&oid, record.patch_oid); 188 try roots.append(allocator, oid); 189 } 190 191 return try roots.toOwnedSlice(allocator); 192 } 193