╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ PatchRev.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
182 record_maybe: ?Record, 183 arena: *std.heap.ArenaAllocator, 184 _: ?[]const u8, 185 ) !void { 186 const record_key = hash.hashInt(hash_kind, event_id); 187 const records = try DB.HashMap(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, record_map_key))); 188 189 var existing_maybe: ?Record = null; 190 const existing_cursor_maybe = try records.getCursor(record_key); 191 if (existing_cursor_maybe) |cursor| { 192 existing_maybe = try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(cursor)); 193 } 194 195 var record = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_maybe); 196 197 try validateOid(hash_kind, record.event.base_oid); 198 try validateOid(hash_kind, record.event.source_oid); 199 try validateOid(hash_kind, record.event_oid); 200 try validateOid(hash_kind, record.base_tree_oid); 201 try validateOid(hash_kind, record.head_tree_oid); 202 try validateOid(hash_kind, record.patch_oid); 203 204 if (existing_maybe) |existing| { 205 if (!std.mem.eql(u8, existing.event.base_oid, record.event.base_oid) or 206 !std.mem.eql(u8, existing.event.source_oid, record.event.source_oid) or 207 !std.mem.eql(u8, existing.event.message, record.event.message) or 208 !std.mem.eql(u8, existing.base_tree_oid, record.base_tree_oid) or 209 !std.mem.eql(u8, existing.head_tree_oid, record.head_tree_oid) or 210 !std.mem.eql(u8, existing.patch_oid, record.patch_oid)) 211 { 212 return error.PatchRevChanged; 213 } 214 record.created_order = existing.created_order; 215 record.author_email = existing.author_email; 216 record.event_oid = existing.event_oid; 217 } 218 219 const cursor = try records.putCursor(record_key); 220 try evt.upsert(Record, DB, hash_kind, try DB.HashMap(.read_write).init(cursor), record); 221 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .patchrev, existing_maybe, record); 222 223 if (existing_cursor_maybe == null) { 224 const ids = try DB.SortedSet(.read_write).init(try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key))); 225 try ids.put(&evt.orderKeyDesc(record.created_order, event_id)); 226 } 227 } 228 229 pub fn validateOid(comptime hash_kind: hash.HashKind, oid: []const u8) !void { 230 if (oid.len != hash.hexLen(hash_kind)) return error.InvalidOid; 231 var bytes: [hash.byteLen(hash_kind)]u8 = undefined; 232 _ = try std.fmt.hexToBytes(&bytes, oid); 233 if (!std.mem.eql(u8, oid, &std.fmt.bytesToHex(bytes, .lower))) return error.InvalidOid; 234 } 235 236 pub fn readById( 237 comptime DB: type, 238 comptime hash_kind: hash.HashKind, 239 haxy_moment: DB.HashMap(.read_only), 240 arena: *std.heap.ArenaAllocator, 241 id: *const [evt.event_id_size]u8, 242 ) !?Record { 243 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 244 const records = try DB.HashMap(.read_only).init(records_cursor); 245 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 246 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 247 } 248 249 pub const WithId = struct { 250 id: [evt.event_id_size]u8, 251 record: Record, 252 }; 253 254 pub fn readNewest( 255 comptime DB: type, 256 comptime hash_kind: hash.HashKind, 257 haxy_moment: DB.HashMap(.read_only), 258 arena: *std.heap.ArenaAllocator, 259 ) !?WithId { 260 const ids_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, all_id_set_key)) orelse return null; 261 const ids = try DB.SortedSet(.read_only).init(ids_cursor); 262 var iter = try ids.iteratorFromIndex(0); 263 while (try iter.next()) |cursor| { 264 const id = try evt.readOrderKeyId(DB, cursor); 265 const record = (try readById(DB, hash_kind, haxy_moment, arena, &id)) orelse continue; 266 if (!record.removed) return .{ .id = id, .record = record }; 267 } 268 return null; 269 } 270 271 // squash commits referenced only by patch revision records 272 pub fn gcRoots( 273 comptime DB: type, 274 comptime hash_kind: hash.HashKind, 275 allocator: std.mem.Allocator, 276 haxy_moment: DB.HashMap(.read_only), 277 ) ![][hash.hexLen(hash_kind)]u8 { 278 var roots: std.ArrayList([hash.hexLen(hash_kind)]u8) = .empty; 279 errdefer roots.deinit(allocator); 280 var arena = std.heap.ArenaAllocator.init(allocator); 281 defer arena.deinit(); 282 283 const ids_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, all_id_set_key)) orelse return try roots.toOwnedSlice(allocator); 284 const ids = try DB.SortedSet(.read_only).init(ids_cursor); 285 var iter = try ids.iteratorFromIndex(0); 286 while (try iter.next()) |cursor| { 287 _ = arena.reset(.retain_capacity); 288 const id = try evt.readOrderKeyId(DB, cursor); 289 const record = (try readById(DB, hash_kind, haxy_moment, &arena, &id)) orelse continue; 290 var oid: [hash.hexLen(hash_kind)]u8 = undefined; 291 if (record.patch_oid.len != oid.len) return error.InvalidOid; 292 @memcpy(&oid, record.patch_oid); 293 try roots.append(allocator, oid); 294 } 295 296 return try roots.toOwnedSlice(allocator); 297 } 298