summaryrefslogtreecommitdiff
path: root/src/Store.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-05 14:11:56 -0300
committerGabriel Schneider <[email protected]>2026-11-05 19:16:19 -0300
commit4e096f6dc6db043caa630514fa75bd18438cb15b (patch)
tree2628101ed3a7e432030918b019935e47240b1c64 /src/Store.zig
parent38d21b0cb34ef1f555e5bda8155150bda9bfbaee (diff)
downloadpardes-4e096f6dc6db043caa630514fa75bd18438cb15b.tar.gz
pardes-4e096f6dc6db043caa630514fa75bd18438cb15b.zip
store: Recent is a value of the user's state file, `pardes.xitdb` in the DumpDir (src/Db.zig: the xitdb database the architecture's §14 puts everything pardes keeps on disk in), which every session shares: read at a session's start (now after the startup config, whose DumpDir says where the file is) and again on use when the file's committed size has moved, written at the end of a step that changed it in one transaction under the file's lock, taken without waiting (busy, the next step tries again), another session's commit read and merged under this one's changes first; not fsynced, as the `recent` file was not (a xitdb commit costs 20 us without fsync and 5.2 ms with its two on this btrfs); a file there that is no database of pardes's is kept aside as `pardes.xitdb.broken` and a new one started; Store.zig keeps the closed panes alone (its file half goes: the path and its inode/size/mtime stamp, load, refresh, read, flush and write, and W4's one-time read of the old `store`, which comes back with the migration), Options.recent_path becomes Options.state (off, disk, memory), main.recentPath goes, a Restore's new core takes the old one's database, and the test host's sessions keep theirs in a `.memory` database, the same code; measured (Debug, 200 files listed): flush after an open 133-135 us (W4's file: 124), refresh after another session's write 239-247 us (198), refresh with nothing new 3 us (1), a session started on it 1144-1194 us (1214), and a Recent change adds 12,325 bytes to the file (its 12,089 bytes of lines and 236 of xitdb's); tests: Store.zig's four Recent tests become Db.zig's three (two windows sharing Recent through a busy lock, the newest 200, a file that is no database kept aside; the old store's migration test goes with that read), the closed-pane test keeps its second window on a state file in its DumpDir, the behaviour suite gains 'two sessions on one state file share Recent', and the process tests and the canary look for the state file where they looked for `recent`
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/Store.zig')
-rw-r--r--src/Store.zig304
1 files changed, 14 insertions, 290 deletions
diff --git a/src/Store.zig b/src/Store.zig
index b1e8abd9..9267dc3b 100644
--- a/src/Store.zig
+++ b/src/Store.zig
@@ -1,21 +1,8 @@
-//! What outlives a pane: Recent's file, which every session of the user's
-//! shares, and the panes this session closed, which Panes brings back.
-//!
-//! Recent's file (Options.recent_path, $XDG_STATE_HOME/pardes/recent) is
-//! the list, a line a file, the newest first: `<line> <col> <aline> <acol>
-//! <path>` (recent.zig). A session reads it at start, and again on use
-//! (Recent, /recent) when a stat says another session wrote it since. A
-//! step that changed the list writes it at its end: `flock` on the file
-//! (held by another session, the next step tries again: a frame never
-//! waits for one), the file read again under this session's changes
-//! (`recent.List.merge`) when another session wrote it since, and the
-//! whole list written over it in place. A line that is no entry is passed
-//! over, so a missing, unreadable or foreign file is an empty list (and a
-//! line a crash tore, one entry fewer): pardes always starts.
-//!
-//! The closed panes are this session's alone, and in memory: each a
-//! one-pane Dump (dump.savePane) and where it was, the newest 64 within
-//! 64 MiB. Restore keeps them (`clone`); the session's end drops them.
+//! The panes this session closed, which Panes brings back: this session's
+//! alone, and in memory. Each is a one-pane Dump (dump.savePane) and where
+//! it was, the newest 64 within 64 MiB. Restore keeps them (`clone`); the
+//! session's end drops them. Recent, which every session shares, is in the
+//! state file (Db.zig).
const Store = @This();
const std = @import("std");
@@ -23,7 +10,6 @@ const pardes = @import("pardes.zig");
const dump = @import("dump.zig");
const layout = @import("layout.zig");
const exec = @import("exec.zig");
-const recent = @import("recent.zig");
const Pardes = pardes.Pardes;
/// This session keeps this many closed panes, the newest.
@@ -33,8 +19,6 @@ pub const max_closed = 64;
pub const max_dump: usize = 8 << 20;
/// The closed panes' dumps together are never longer: the oldest go.
pub const max_bytes: usize = 64 << 20;
-/// Recent's file is read this far at most (200 lines of the longest paths).
-const max_file = 1 << 20;
/// A pane closed in this session, as Panes lists it and brings it back.
pub const Closed = struct {
@@ -54,36 +38,12 @@ pub const Closed = struct {
dump: []u8,
};
-/// Recent's file; null, nothing is kept past the session.
-path: ?[]const u8 = null,
-/// The file as this session last read or wrote it: a stat that says
-/// otherwise is another session's write.
-inode: std.Io.File.INode = 0,
-size: u64 = 0,
-mtime: i96 = 0,
-/// Read once at start (or there was none): only then is it written, so a
-/// session never writes over a list it did not read.
-loaded: bool = false,
/// This session's closed panes, oldest first (recentGpa's).
closed: std.ArrayList(Closed) = .empty,
/// The panes closing now went to another window (Handover.finish): open
/// there, they are no closed panes of this session, so none is kept.
away: bool = false,
-fn ioOf() std.Io {
- return std.Io.Threaded.global_single_threaded.io();
-}
-
-/// One host file call, `f(args)`, made out of the turn (pardes.turn): a
-/// file under a mount of this session's own tree is answered by this
-/// session. Only the call: while a step is out nothing may change the core,
-/// so Recent changes between these, never inside one.
-const host = pardes.tape.out;
-
-const cwd = std.Io.Dir.cwd;
-const File = std.Io.File;
-const Dir = std.Io.Dir;
-
pub fn deinit(s: *Store, gpa: std.mem.Allocator) void {
for (s.closed.items) |c| freeClosed(gpa, c);
s.closed.deinit(gpa);
@@ -116,112 +76,6 @@ pub fn clone(s: *const Store, gpa: std.mem.Allocator) Store {
return copy;
}
-// ---- Recent's file ----
-
-/// Recent read at start: the file, and the store it replaced.
-pub fn load(s: *Store, p: *Pardes) void {
- const path = s.path orelse return;
- s.loaded = true;
- s.refresh(p);
- // ponytail: the store this file replaced (until 2026-11-05), a log
- // whose `@r` records' bodies are this file's lines, oldest first: read
- // once more, over the file's list, while it is the newer of the two
- // (an older pardes still running writes it, so it is left there).
- const io = ioOf();
- var old_buf: [4096 + 8]u8 = undefined;
- const old = std.fmt.bufPrint(&old_buf, "{s}/store", .{std.fs.path.dirname(path) orelse return}) catch return;
- const st = host(Dir.statFile, .{ cwd(), io, old, Dir.StatFileOptions{} }) catch return;
- if (st.mtime.nanoseconds <= s.mtime) return;
- const gpa = p.recentGpa();
- const bytes = host(Dir.readFileAlloc, .{ cwd(), io, old, gpa, std.Io.Limit.limited(128 << 20) }) catch return;
- defer gpa.free(bytes);
- var lines = std.mem.splitScalar(u8, bytes, '\n');
- while (lines.next()) |line| {
- const e = recent.List.parseLine(line) orelse continue;
- const open = if (p.recent.get(e.path)) |here| here.open else false;
- p.recent.opened(gpa, e.path);
- p.recent.closed(gpa, e.path, e.nums[0], e.nums[1], e.nums[2], e.nums[3]);
- // Open here, it stays open: the cap never drops it.
- if (open) p.recent.opened(gpa, e.path);
- }
-}
-
-/// On use: what other sessions wrote since this one last read or wrote
-/// the file, read. One stat when nothing changed.
-pub fn refresh(s: *Store, p: *Pardes) void {
- const path = s.path orelse return;
- if (!s.loaded) return;
- const io = ioOf();
- const st = host(Dir.statFile, .{ cwd(), io, path, Dir.StatFileOptions{} }) catch return;
- if (st.inode == s.inode and st.size == s.size and st.mtime.nanoseconds == s.mtime) return;
- const file = host(Dir.openFile, .{ cwd(), io, path, Dir.OpenFileOptions{} }) catch return;
- defer host(File.close, .{ file, io });
- s.read(p, file);
-}
-
-/// `file` read into Recent, under this session's changes, when it is not
-/// as this session last read or wrote it; then noted as read.
-fn read(s: *Store, p: *Pardes, file: File) void {
- const io = ioOf();
- const st = host(File.stat, .{ file, io }) catch return;
- if (st.inode == s.inode and st.size == s.size and st.mtime.nanoseconds == s.mtime) return;
- const gpa = p.recentGpa();
- const buf = gpa.alloc(u8, @min(st.size, max_file)) catch return;
- defer gpa.free(buf);
- const n = host(File.readPositionalAll, .{ file, io, buf, 0 }) catch return;
- p.recent.merge(gpa, buf[0..n]);
- s.inode = st.inode;
- s.size = st.size;
- s.mtime = st.mtime.nanoseconds;
-}
-
-/// Recent written out when it changed, at the end of a step and at the
-/// end (`wait`: the lock is waited for there, never in a step).
-pub fn flush(s: *Store, p: *Pardes, wait: bool) void {
- if (!p.recent.dirty) return;
- if (s.path) |path| {
- if (!s.loaded) return;
- // Another session's lock: the next step tries again.
- if (!s.write(p, path, wait)) return;
- }
- // Written, or nowhere to write it (or the disk refused it: kept for
- // no one, as before).
- for (p.recent.entries.items) |*e| e.changed = false;
- p.recent.dirty = false;
-}
-
-/// Under the file's lock: the file read again when another session wrote
-/// it since, this session's changes over it, the whole list written in
-/// place. False when another session holds the lock (and `wait` is not
-/// set).
-fn write(s: *Store, p: *Pardes, path: []const u8, wait: bool) bool {
- const io = ioOf();
- const file = host(Dir.createFile, .{ cwd(), io, path, Dir.CreateFileOptions{ .read = true, .truncate = false } }) catch return true;
- // Closing it lets go of the lock.
- defer host(File.close, .{ file, io });
- if (wait) {
- host(File.lock, .{ file, io, File.Lock.exclusive }) catch return true;
- } else if (!(host(File.tryLock, .{ file, io, File.Lock.exclusive }) catch return true)) return false;
- s.read(p, file);
- p.recent.changedFirst();
- const gpa = p.recentGpa();
- var text: std.Io.Writer.Allocating = .init(gpa);
- defer text.deinit();
- const entries = p.recent.entries.items;
- for (entries[0..@min(entries.len, recent.cap)]) |e| recent.List.writeEntry(e, &text.writer) catch return true;
- // In place: a reader that meets it half written reads it again once
- // its stat moves on, and a line a crash tore is passed over. Out of
- // disk, it is kept for no one, as before.
- host(File.writePositionalAll, .{ file, io, text.written(), 0 }) catch return true;
- host(File.setLength, .{ file, io, text.written().len }) catch return true;
- // This session's own write is not read back at the next refresh.
- const st = host(File.stat, .{ file, io }) catch return true;
- s.inode = st.inode;
- s.size = st.size;
- s.mtime = st.mtime.nanoseconds;
- return true;
-}
-
// ---- the closed panes ----
/// Pane `id`, about to close, kept: what it was (a one-pane Dump) and
@@ -402,140 +256,10 @@ pub fn closedAddress(word: []const u8) ?u32 {
const testing = std.testing;
-/// Recent's file of a test, in its own directory (never the user's).
-fn testPath(tmp: *std.testing.TmpDir) ![]u8 {
- var base_buf: [std.fs.max_path_bytes]u8 = undefined;
- const dir = base_buf[0..try tmp.dir.realPath(testing.io, &base_buf)];
- return std.fs.path.join(testing.allocator, &.{ dir, "recent" });
-}
-
-fn testWindow(path: []const u8) !*Pardes {
- return Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48, .recent_path = path });
-}
-
-fn fileSize(path: []const u8) u64 {
- const st = std.Io.Dir.cwd().statFile(testing.io, path, .{}) catch return 0;
- return st.size;
-}
-
-test "store: two windows share Recent, each reading the other's writes on use; a busy lock delays, never loses" {
- var tmp = testing.tmpDir(.{});
- defer tmp.cleanup();
- const path = try testPath(&tmp);
- defer testing.allocator.free(path);
- const a = try testWindow(path);
- defer a.deinit();
- const b = try testWindow(path);
- defer b.deinit();
- a.recent.opened(a.recentGpa(), "/w/a.zig");
- a.saveRecent();
- b.recent.opened(b.recentGpa(), "/w/b.zig");
- b.saveRecent();
- a.recent.closed(a.recentGpa(), "/w/a.zig", 7, 3, 0, 0);
- a.saveRecent();
- // Each has both, in the order they were written: a closed last, then b.
- a.store.refresh(a);
- b.store.refresh(b);
- for ([_]*Pardes{ a, b }) |w| {
- try testing.expectEqual(@as(usize, 2), w.recent.entries.items.len);
- try testing.expectEqualStrings("/w/a.zig", w.recent.entries.items[0].path);
- try testing.expectEqualStrings("/w/b.zig", w.recent.entries.items[1].path);
- try testing.expectEqual(@as(u32, 7), w.recent.get("/w/a.zig").?.line);
- }
- // Another window holding the file's lock: b's change waits in memory...
- const lock = try std.Io.Dir.cwd().openFile(testing.io, path, .{ .mode = .read_write });
- try lock.lock(testing.io, .exclusive);
- b.recent.opened(b.recentGpa(), "/w/c.zig");
- const size = fileSize(path);
- b.saveRecent();
- try testing.expectEqual(size, fileSize(path));
- try testing.expect(b.recent.dirty);
- // ...and is written at the next step after it is let go.
- lock.close(testing.io);
- b.saveRecent();
- try testing.expect(fileSize(path) > size);
- a.store.refresh(a);
- try testing.expectEqualStrings("/w/c.zig", a.recent.entries.items[0].path);
- // A window started now reads the same list.
- const c = try testWindow(path);
- defer c.deinit();
- try testing.expectEqual(@as(usize, 3), c.recent.entries.items.len);
- for (a.recent.entries.items, c.recent.entries.items) |x, y| try testing.expectEqualStrings(x.path, y.path);
-}
-
-test "store: Recent's file holds the newest 200, the newest first, and a window started on it reads them so" {
- var tmp = testing.tmpDir(.{});
- defer tmp.cleanup();
- const path = try testPath(&tmp);
- defer testing.allocator.free(path);
- const a = try testWindow(path);
- defer a.deinit();
- var name: [32]u8 = undefined;
- for (0..recent.cap + 100) |i| {
- const file = std.fmt.bufPrint(&name, "/w/file{d}.zig", .{i}) catch unreachable;
- a.recent.opened(a.recentGpa(), file);
- a.recent.closed(a.recentGpa(), file, 1, 1, 0, 0);
- a.saveRecent();
- }
- try testing.expectEqual(@as(usize, recent.cap), a.recent.entries.items.len);
- const text = try std.Io.Dir.cwd().readFileAlloc(testing.io, path, testing.allocator, .limited(1 << 20));
- defer testing.allocator.free(text);
- try testing.expectEqual(@as(usize, recent.cap), std.mem.count(u8, text, "\n"));
- try testing.expect(std.mem.startsWith(u8, text, "1 1 0 0 /w/file299.zig\n"));
- const b = try testWindow(path);
- defer b.deinit();
- try testing.expectEqual(@as(usize, recent.cap), b.recent.entries.items.len);
- try testing.expectEqualStrings("/w/file299.zig", b.recent.entries.items[0].path);
- try testing.expectEqualStrings("/w/file100.zig", b.recent.entries.items[recent.cap - 1].path);
-}
-
-test "store: a foreign file never stops pardes: its lines that are Recent's are read, the next write mends it" {
- var tmp = testing.tmpDir(.{});
- defer tmp.cleanup();
- const path = try testPath(&tmp);
- defer testing.allocator.free(path);
- try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = path, .data = "\x00\xffnot a list at all\n7 3 0 0 /w/ok.zig\n@r 9 zz\n1 2 /w/short.zig\n" });
- const a = try testWindow(path);
- defer a.deinit();
- try testing.expectEqual(@as(usize, 1), a.recent.entries.items.len);
- try testing.expectEqual(@as(u32, 7), a.recent.get("/w/ok.zig").?.line);
- a.recent.opened(a.recentGpa(), "/w/a.zig");
- a.saveRecent();
- const text = try std.Io.Dir.cwd().readFileAlloc(testing.io, path, testing.allocator, .limited(1 << 20));
- defer testing.allocator.free(text);
- try testing.expectEqualStrings("0 0 0 0 /w/a.zig\n7 3 0 0 /w/ok.zig\n", text);
-}
-
-test "store: the store Recent's file replaced is read once, its newest records first, over the file's list" {
- var tmp = testing.tmpDir(.{});
- defer tmp.cleanup();
- const path = try testPath(&tmp);
- defer testing.allocator.free(path);
- // An older list, then the store written after it: its records oldest
- // first, a closed pane's dump between them.
- try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = path, .data = "5 5 0 0 /w/listed.zig\n" });
- const store = try std.fmt.allocPrint(testing.allocator, "{s}/store", .{std.fs.path.dirname(path).?});
- defer testing.allocator.free(store);
- try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = store, .data = "@h 31 3c6ce28a\npardes-store 1 ca2a9583be7d1ba3\n@r 20 0\n1 1 0 0 /w/old.zig\n@c 12 0\n0 1 7 0 0 0 0 9\t/w/x\n.{}\n@r 21 0\n2 2 0 0 /w/both.zig\n@r 20 0\n9 9 0 0 /w/old.zig\n" });
- {
- // Made newer than the list, as on a disk where both were written.
- const f = try std.Io.Dir.cwd().openFile(testing.io, store, .{ .mode = .read_write });
- defer f.close(testing.io);
- const st = try f.stat(testing.io);
- try f.setTimestamps(testing.io, .{ .modify_timestamp = .{ .new = .{ .nanoseconds = st.mtime.nanoseconds + std.time.ns_per_s } } });
- }
- const a = try testWindow(path);
- defer a.deinit();
- try testing.expectEqual(@as(usize, 3), a.recent.entries.items.len);
- try testing.expectEqualStrings("/w/old.zig", a.recent.entries.items[0].path);
- try testing.expectEqual(@as(u32, 9), a.recent.entries.items[0].line);
- try testing.expectEqualStrings("/w/both.zig", a.recent.entries.items[1].path);
- try testing.expectEqualStrings("/w/listed.zig", a.recent.entries.items[2].path);
- // Written to the file at the step's end; read from it alone after.
- a.saveRecent();
- const b = try testWindow(path);
- defer b.deinit();
- for (a.recent.entries.items, b.recent.entries.items) |x, y| try testing.expectEqualStrings(x.path, y.path);
+/// A window keeping its state in `dir` (never the user's DumpDir).
+fn testWindow(dir: []const u8) !*Pardes {
+ var config: [4200]u8 = undefined;
+ return Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48, .state = .disk, .startup_config = try std.fmt.bufPrint(&config, "DumpDir {s}\n", .{dir}) });
}
test "store: this session keeps its newest 64 closed panes" {
@@ -561,9 +285,9 @@ test "store: this session keeps its newest 64 closed panes" {
test "store: a closed pane comes back where it was, its rows from the pane that took them, nothing else moving; Panes lists it, marked" {
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
- const path = try testPath(&tmp);
- defer testing.allocator.free(path);
- const p = try testWindow(path);
+ var dir_buf: [std.fs.max_path_bytes]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)];
+ const p = try testWindow(dir);
defer p.deinit();
_ = try p.setTestFile("alpha\nbeta\n");
p.newScratchBelow(0);
@@ -615,8 +339,8 @@ test "store: a closed pane comes back where it was, its rows from the pane that
} else false;
try testing.expect(found);
try testing.expect(p.store.closedBySerial(again) == null);
- // Another session on the same file lists none of this one's.
- const w = try testWindow(path);
+ // Another session on the same state file lists none of this one's.
+ const w = try testWindow(dir);
defer w.deinit();
try testing.expectEqual(@as(usize, 0), w.store.closed.items.len);
}