From c0b2e5d9194ce35b9fbb58d8d6e18de66cb209a8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 10:46:54 -0300 Subject: store: Recent is a file of 200 lines under flock, written whole in place at the end of a step that changed it and read back under a session's own changes when another session wrote it since, and the panes a session closes are kept in its memory, the newest 64 within 64 MiB; the append-only CRC log with its resync and compaction, and the closed panes' self-describing dumps in it (only the session that closed one ever listed it), go; Options.store_path is recent_path ($XDG_STATE_HOME/pardes/recent), and the old store is read once while it is the newer of the two; tests: the store's four rewritten through the same windows (the 200 newest, a foreign file, a closed pane's way back from memory), the old store read once, 64 closed panes kept, recent.zig's merge; the process cases read Recent's file; docs/next-store.md is the decision Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/recent.zig | 114 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 83 insertions(+), 31 deletions(-) (limited to 'src/recent.zig') diff --git a/src/recent.zig b/src/recent.zig index b8e275f5..4f744750 100644 --- a/src/recent.zig +++ b/src/recent.zig @@ -1,11 +1,11 @@ //! The files opened lately, most recent first, those since closed too, as //! `Recent` lists them and a jump to a closed pane reopens them: a path //! and where the dot was when it closed. Kept across sessions, and shared -//! by every window, in the store (Store.zig) when the host gives one: each -//! change is a record there, and the records other windows wrote are -//! `note`d here as they are read. acme has nothing like it (its dump and -//! Load are the nearest): a file closed by accident, its path forgotten, -//! is found here again. +//! by every session of the user's, in Recent's file (Store.zig) when the +//! host gives one: written whole at the end of a step that changed it, and +//! read back under this session's changes (`merge`). acme has nothing like +//! it (its dump and Load are the nearest): a file closed by accident, its +//! path forgotten, is found here again. const std = @import("std"); @@ -24,8 +24,9 @@ pub const Entry = struct { /// cap drops. ponytail: a pane renamed away leaves its old path marked /// open until the session ends; kept rather than tracked by pane. open: bool = false, - /// Changed here since the store last heard of it: written out as a - /// record at the end of the step (Store.flush). + /// Changed here since the file was last written: it goes over what the + /// file says when it is read again, and is written at the end of the + /// step (Store.flush). changed: bool = false, }; @@ -90,29 +91,53 @@ pub const List = struct { }; } - /// A record read from the store (another window's, or this one's read - /// again): `path` was opened or closed there last, at that place. It - /// goes first, with that place; whether it is open HERE stays as it - /// was, and nothing is changed for the store to hear again. - pub fn note(l: *List, gpa: std.mem.Allocator, path: []const u8, line: u32, col: u32, aline: u32, acol: u32) void { - if (path.len == 0) return; - var entry: Entry = if (l.find(path)) |i| l.entries.orderedRemove(i) else blk: { - l.dropOldest(gpa); - break :blk .{ .path = gpa.dupe(u8, path) catch return }; - }; - entry.line = line; - entry.col = col; - entry.aline = aline; - entry.acol = acol; - l.entries.insert(gpa, 0, entry) catch { - gpa.free(entry.path); - entry = undefined; - }; + /// The list again from its file's `text`, which other sessions write + /// too: this session's changes not written yet first (they are the + /// newest), then the file's lines in their order, then the files open + /// here that the file has lost (another session's cap dropped them). + /// Whether a file is open HERE stays as it was. A line is matched only + /// against those two, a few: the file holds each path once. + pub fn merge(l: *List, gpa: std.mem.Allocator, text: []const u8) void { + var old = l.entries; + l.entries = .empty; + var opens: std.ArrayList(Entry) = .empty; + defer { + for (opens.items) |e| gpa.free(e.path); + opens.deinit(gpa); + } + for (old.items) |e| { + if (e.changed) { + l.entries.append(gpa, e) catch gpa.free(e.path); + } else if (e.open) { + opens.append(gpa, e) catch gpa.free(e.path); + } else gpa.free(e.path); + } + old.deinit(gpa); + const changed = l.entries.items.len; + var lines = std.mem.splitScalar(u8, text, '\n'); + next: while (lines.next()) |line| { + if (l.entries.items.len >= cap) break; + const e = parseLine(line) orelse continue; + for (l.entries.items[0..changed]) |c| if (std.mem.eql(u8, c.path, e.path)) continue :next; + const at = for (opens.items, 0..) |o, k| { + if (std.mem.eql(u8, o.path, e.path)) break k; + } else null; + var entry: Entry = if (at) |k| opens.orderedRemove(k) else .{ .path = gpa.dupe(u8, e.path) catch break }; + entry.line = e.nums[0]; + entry.col = e.nums[1]; + entry.aline = e.nums[2]; + entry.acol = e.nums[3]; + l.entries.append(gpa, entry) catch { + gpa.free(entry.path); + break; + }; + } + l.entries.appendSlice(gpa, opens.items) catch return; + opens.clearRetainingCapacity(); } - /// This window's changes first again, in their order, after others' - /// records were noted over them: they are written after those, so - /// that is where every window's fold puts them. + /// This session's changes first, in their order: written so, they are + /// where a read of the file puts them too (`merge`), above the rest. pub fn changedFirst(l: *List) void { var w: usize = 0; for (0..l.entries.items.len) |i| if (l.entries.items[i].changed) { @@ -144,14 +169,14 @@ pub const List = struct { return l.entries.items[l.find(path) orelse return null]; } - /// A store record's body, ` `, a line. + /// An entry as a line of the file, ` `. pub fn writeEntry(e: Entry, w: *std.Io.Writer) !void { // A name with a newline in it would read back as two lines: not kept. if (std.mem.indexOfScalar(u8, e.path, '\n') == null) try w.print("{d} {d} {d} {d} {s}\n", .{ e.line, e.col, e.aline, e.acol, e.path }); } - /// One record's body read back: ` `. + /// A line of the file read back: ` `. pub fn parseLine(line: []const u8) ?struct { nums: [4]u32, path: []const u8 } { var nums: [4]u32 = @splat(0); var rest = line; @@ -180,7 +205,7 @@ test "the list keeps a path once, most recent first, its dot from closing, cappe var w = std.Io.Writer.fixed(&buf); for (l.entries.items) |e| try List.writeEntry(e, &w); try std.testing.expectEqualStrings("7 3 0 0 /a\n0 0 0 0 /b\n", w.buffered()); - // Each line a store record's body, read back. + // Each a line of the file, read back. const back = List.parseLine("7 3 0 0 /a").?; try std.testing.expectEqual(@as(u32, 7), back.nums[0]); try std.testing.expectEqualStrings("/a", back.path); @@ -210,3 +235,30 @@ test "the cap drops the oldest closed file, never an open one" { try std.testing.expect(l.get("/open") != null); try std.testing.expect(l.get("/shut") == null); } + +test "a merge puts this session's changes over the file's list, keeps what is open here, and drops what the file lost" { + const gpa = std.testing.allocator; + var l: List = .{}; + defer l.deinit(gpa); + l.opened(gpa, "/mine"); + l.opened(gpa, "/kept"); + l.closed(gpa, "/kept", 4, 2, 0, 0); + l.opened(gpa, "/gone"); + l.closed(gpa, "/gone", 1, 1, 0, 0); + // Written: nothing changed since. + for (l.entries.items) |*e| e.changed = false; + // Another session closed /kept at 9:1 and opened /theirs; its cap lost + // /gone and /mine, which is open here. + l.closed(gpa, "/new", 3, 3, 0, 0); + l.merge(gpa, "0 0 0 0 /theirs\n9 1 0 0 /kept\nnot a line\n"); + var order: [4][]const u8 = undefined; + for (l.entries.items, 0..) |e, i| order[i] = e.path; + try std.testing.expectEqual(@as(usize, 4), l.entries.items.len); + try std.testing.expectEqualStrings("/new", order[0]); + try std.testing.expectEqualStrings("/theirs", order[1]); + try std.testing.expectEqualStrings("/kept", order[2]); + try std.testing.expectEqualStrings("/mine", order[3]); + try std.testing.expectEqual(@as(u32, 9), l.get("/kept").?.line); + try std.testing.expect(l.get("/mine").?.open); + try std.testing.expect(!l.get("/theirs").?.open); +} -- cgit v1.3