diff options
Diffstat (limited to 'src/Db.zig')
| -rw-r--r-- | src/Db.zig | 58 |
1 files changed, 33 insertions, 25 deletions
@@ -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 `<path>.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 `<path>.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; |
