From 60caa16b99c60713c5ae27832d76809a2b4a2d5a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 16:15:14 -0300 Subject: blob cache: the trim keeps the most recently used texts that fit under the cap, the newest first, and lets the rest go, where it let the least recently used go until the rest was under it: a text past the cap alone (a big file's, edited since the client joined) took every older one with it, the small ones that fit too, so a client after it with that cap was sent them all again (daemon.py's far clients E and F at a 1 MiB cap: F found nothing on disk, the 6.5 KB notes gone with the 1.1 MB file's texts; now the notes stay); tests: replica.zig's 'the blob cache stays under its cap, the least recently used going first, the file compacted' becomes '... going first and one past the cap alone without them, the file compacted', a 20 KB text past its 15 KB cap put newest, gone, and the three older ones kept Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/Db.zig | 58 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 25 deletions(-) (limited to 'src/Db.zig') diff --git a/src/Db.zig b/src/Db.zig index 95a4eede..684604c7 100644 --- a/src/Db.zig +++ b/src/Db.zig @@ -815,10 +815,10 @@ fn liveSize(db: *Disk) !u64 { /// the least recently used first (a hit takes a new number) /// size, next the texts' bytes, the next number /// -/// Past the cap (PARDES_BLOB_CACHE_MB, else 1 GiB) the oldest go, and the -/// file is compacted into `cache.xitdb.new` and renamed over it: one -/// transaction and one compaction, at a far client's end (`trim`), never on -/// a frame. A text read back is hashed again: one torn or changed is a miss +/// Past the cap (PARDES_BLOB_CACHE_MB, else 1 GiB) the most recently used +/// that fit stay and the rest go, and the file is compacted into +/// `cache.xitdb.new` and renamed over it: one transaction and one +/// compaction, at a far client's end (`trim`), never on a frame. A text read back is hashed again: one torn or changed is a miss /// and goes, never wrong content; a file that is no database is started /// afresh (it holds nothing a join cannot send again). Each use opens the /// file for itself: a client's reader, its storing thread and its end each @@ -994,9 +994,11 @@ pub const Cache = struct { }{ .blobs = blobs, .hits = hits }) catch {}; } - /// Under the cap: the least recently used texts gone, in one - /// transaction, and the file compacted into `.new` and renamed - /// over it once any went (what no moment holds is the file's growth). + /// Under the cap: the most recently used texts kept while they fit, the + /// newest first, and the rest gone, in one transaction (one past the cap + /// alone goes without taking the older ones with it); then the file + /// compacted into `.new` and renamed over it once any went (what + /// no moment holds is the file's growth). pub fn trim(c: *const Cache, io: std.Io) void { var u: Use = undefined; c.use(io, &u) catch return; @@ -1009,32 +1011,38 @@ pub const Cache = struct { went: *bool, pub fn run(ctx: @This(), cursor: *Disk.Cursor(.read_write)) !void { const root = try Disk.HashMap(.read_write).init(cursor.*); - var size = if (try root.getCursor(key("size"))) |s| try s.readUint() else 0; + const size = if (try root.getCursor(key("size"))) |s| try s.readUint() else 0; if (size <= ctx.cap) return; const texts = try Disk.HashMap(.read_write).init(try root.putCursor(key("blobs"))); const seqs = try Disk.HashMap(.read_write).init(try root.putCursor(key("seqs"))); const seen = try Disk.SortedMap(.read_write).init(try root.putCursor(key("seen"))); - // The oldest numbers, read before any goes. - var gone: std.ArrayList(struct { seq: [8]u8, hash: [32]u8 }) = .empty; - defer gone.deinit(std.heap.c_allocator); - var left = size; + // Every text, the least recently used first, read before any goes. + const Text = struct { seq: [8]u8, hash: [32]u8, len: ?u64, kept: bool = false }; + var all: std.ArrayList(Text) = .empty; + defer all.deinit(std.heap.c_allocator); var it = try seen.iterator(); - while (left > ctx.cap) { - const kv_at = (try it.next()) orelse break; + while (try it.next()) |kv_at| { const kv = try kv_at.readKeyValuePair(); - var g: @TypeOf(gone.items[0]) = undefined; - if ((try kv.key_cursor.readBytes(&g.seq)).len != 8 or (try kv.value_cursor.readBytes(&g.hash)).len != 32) continue; - if (try texts.getCursor(blobKey(&g.hash))) |at| left -|= try at.count(); - try gone.append(std.heap.c_allocator, g); + var t: Text = .{ .seq = undefined, .hash = undefined, .len = null }; + if ((try kv.key_cursor.readBytes(&t.seq)).len != 8 or (try kv.value_cursor.readBytes(&t.hash)).len != 32) continue; + t.len = if (try texts.getCursor(blobKey(&t.hash))) |at| try at.count() else null; + try all.append(std.heap.c_allocator, t); } - for (gone.items) |*g| { - if (try texts.getCursor(blobKey(&g.hash))) |at| size -|= try at.count(); - _ = try texts.remove(blobKey(&g.hash)); - _ = try seqs.remove(blobKey(&g.hash)); - _ = try seen.remove(&g.seq); + var room = ctx.cap; + var i = all.items.len; + while (i > 0) { + i -= 1; + const t = &all.items[i]; + t.kept = if (t.len) |len| len <= room else false; + if (t.kept) room -= t.len.?; } - try root.put(key("size"), .{ .uint = size }); - ctx.went.* = gone.items.len > 0; + for (all.items) |*t| if (!t.kept) { + _ = try texts.remove(blobKey(&t.hash)); + _ = try seqs.remove(blobKey(&t.hash)); + _ = try seen.remove(&t.seq); + ctx.went.* = true; + }; + try root.put(key("size"), .{ .uint = ctx.cap - room }); } }{ .cap = c.cap, .went = &went }) catch return; if (!went) return; -- cgit v1.3