summaryrefslogtreecommitdiff
path: root/src/Db.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-05 15:39:26 -0300
committerGabriel Schneider <[email protected]>2026-11-05 19:16:37 -0300
commitcd712084c68b9e2bad350066a92a96ce78a9ff35 (patch)
treee7dee186af5e761450f0b8b0f1bce24802e3babf /src/Db.zig
parent1eae252b80ddab0a06574de700b6b2957d3b64fc (diff)
downloadpardes-cd712084c68b9e2bad350066a92a96ce78a9ff35.tar.gz
pardes-cd712084c68b9e2bad350066a92a96ce78a9ff35.zip
migration: a state file that holds no Recent yet reads it once from the files before it, 0.24's $XDG_STATE_HOME/pardes/recent (merged as another session's commit is), else the `store` before that (its `@r` records, the oldest first, as W4's one-time read did), and leaves both where they are (Options.recent_import names their directory, set by main.zig alone, so no test reads the user's); an old `.dump` file, the byte dump, stays readable for one release: `-l <path>` and `Restore <path>` read it, told from an image by its first bytes rather than by its name (the snapshot and process harnesses' dumps are `.dump.zon` files, and a renamed file is the same file), and an Upgrade from the build before, whose record names its `.dump` file and no label, loads it and deletes it as before, so the next Upgrade commits to the state file; tests: Db.zig's 'Recent is read once from the file it replaced, else from the store before that, both left where they are' (in place of W4's 'the store Recent's file replaced is read once, its newest records first, over the file's list'), dump.zig's 'an old dump's file, a byte dump, is read by -l and by a Restore of its path, for one release', and upgrade.zig's 'an upgrade from the build before reads the dump file its record names, and deletes it once loaded'
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/Db.zig')
-rw-r--r--src/Db.zig104
1 files changed, 98 insertions, 6 deletions
diff --git a/src/Db.zig b/src/Db.zig
index 846c8d96..95a4eede 100644
--- a/src/Db.zig
+++ b/src/Db.zig
@@ -264,18 +264,57 @@ fn putRecent(o: *Open, text: []const u8) !void {
/// wrote it, read under this session's changes: at the start, and on use.
pub fn readRecent(db: *Db, p: *Pardes) void {
const o = db.opened(p) orelse return;
+ const first = !db.loaded;
db.loaded = true;
- db.mergeRecent(p, o);
+ const held = db.mergeRecent(p, o);
+ // A state file that holds no Recent yet: the files before it, once.
+ if (first and held != null and !held.?) if (p.opts.recent_import) |dir| importRecent(p, dir);
}
-fn mergeRecent(db: *Db, p: *Pardes, o: *Open) void {
- const size = committed(o) catch return;
- if (size == db.seen) return;
+/// Whether the database holds Recent; null when nothing was read (nothing
+/// committed since the last read, or it could not be).
+fn mergeRecent(db: *Db, p: *Pardes, o: *Open) ?bool {
+ const size = committed(o) catch return null;
+ if (size == db.seen) return null;
const gpa = p.recentGpa();
- const text = recentText(o, gpa) catch return;
+ const text = recentText(o, gpa) catch return null;
defer if (text) |t| gpa.free(t);
p.recent.merge(gpa, text orelse "");
db.seen = size;
+ return text != null;
+}
+
+/// Recent as the files before the state file kept it, in `dir`: 0.24's
+/// `recent` (200 lines, the newest first), else the `store` before it (a
+/// log whose `@r` records are such lines, the oldest first). Both are left
+/// where they are. ponytail: for a release or two, while those files are
+/// about.
+fn importRecent(p: *Pardes, dir: []const u8) void {
+ const gpa = p.recentGpa();
+ const io = ioOf();
+ var path_buf: [4200]u8 = undefined;
+ pardes.turn.yield();
+ const listed = std.Io.Dir.cwd().readFileAlloc(io, std.fmt.bufPrint(&path_buf, "{s}/recent", .{dir}) catch "", gpa, .limited(recent_max)) catch null;
+ const logged = if (listed == null) std.Io.Dir.cwd().readFileAlloc(io, std.fmt.bufPrint(&path_buf, "{s}/store", .{dir}) catch "", gpa, .limited(128 << 20)) catch null else null;
+ pardes.turn.back();
+ if (listed) |text| {
+ defer gpa.free(text);
+ p.recent.merge(gpa, text);
+ // Written to the state file at the next step: read once.
+ for (p.recent.entries.items) |*e| e.changed = true;
+ p.recent.dirty = p.recent.entries.items.len > 0;
+ } else if (logged) |text| {
+ defer gpa.free(text);
+ var lines = std.mem.splitScalar(u8, text, '\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);
+ }
+ }
}
/// Recent written when it changed, at the end of a step and at the end
@@ -290,7 +329,7 @@ pub fn writeRecent(db: *Db, p: *Pardes, wait: bool) void {
error.NoStateFile => break :written,
};
defer unlock(o);
- db.mergeRecent(p, o);
+ _ = db.mergeRecent(p, o);
p.recent.changedFirst();
const gpa = p.recentGpa();
var text: std.Io.Writer.Allocating = .init(gpa);
@@ -1160,3 +1199,56 @@ test "state file: a Dump makes every directory missing on the way to its DumpDir
var label_buf: [64]u8 = undefined;
try testing.expect(p.db.labelled(p, p.last_dump.?, &label_buf) != null);
}
+
+test "state file: Recent is read once from the file it replaced, else from the store before that, both left where they are" {
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var dir_buf: [std.fs.max_path_bytes]u8 = undefined;
+ const dir = try testDir(&tmp, &dir_buf);
+ try tmp.dir.createDirPath(testing.io, "old");
+ try tmp.dir.createDirPath(testing.io, "older");
+ try tmp.dir.createDirPath(testing.io, "a");
+ try tmp.dir.createDirPath(testing.io, "b");
+ // 0.24's Recent: 200 lines, the newest first.
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "old/recent", .data = "7 3 0 0 /w/newest.zig\n1 1 0 0 /w/older.zig\n" });
+ // Before it, the store: its `@r` records' bodies the same lines, oldest
+ // first, a closed pane's record between them.
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "older/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" });
+ var buf: [3][4200]u8 = undefined;
+ const old = try std.fmt.bufPrint(&buf[0], "{s}/old", .{dir});
+ const older = try std.fmt.bufPrint(&buf[1], "{s}/older", .{dir});
+ const window = struct {
+ fn of(state: []const u8, import: []const u8) !*Pardes {
+ var config: [4200]u8 = undefined;
+ return Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48, .state = .disk, .recent_import = import, .startup_config = try std.fmt.bufPrint(&config, "DumpDir {s}\n", .{state}) });
+ }
+ }.of;
+ {
+ const a = try window(try std.fmt.bufPrint(&buf[2], "{s}/a", .{dir}), old);
+ defer a.deinit();
+ try testing.expectEqual(@as(usize, 2), a.recent.entries.items.len);
+ try testing.expectEqualStrings("/w/newest.zig", a.recent.entries.items[0].path);
+ try testing.expectEqual(@as(u32, 7), a.recent.get("/w/newest.zig").?.line);
+ a.saveRecent();
+ }
+ // Read once: what the old file says after is not the state file's.
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "old/recent", .data = "5 5 0 0 /w/later.zig\n" });
+ {
+ const a = try window(try std.fmt.bufPrint(&buf[2], "{s}/a", .{dir}), old);
+ defer a.deinit();
+ try testing.expectEqual(@as(usize, 2), a.recent.entries.items.len);
+ try testing.expect(a.recent.get("/w/later.zig") == null);
+ }
+ // No `recent` there: the store's, its newest records first.
+ {
+ const b = try window(try std.fmt.bufPrint(&buf[2], "{s}/b", .{dir}), older);
+ defer b.deinit();
+ try testing.expectEqual(@as(usize, 2), b.recent.entries.items.len);
+ try testing.expectEqualStrings("/w/old.zig", b.recent.entries.items[0].path);
+ try testing.expectEqual(@as(u32, 9), b.recent.entries.items[0].line);
+ try testing.expectEqualStrings("/w/both.zig", b.recent.entries.items[1].path);
+ }
+ // Both left where they were.
+ _ = try tmp.dir.statFile(testing.io, "old/recent", .{});
+ _ = try tmp.dir.statFile(testing.io, "older/store", .{});
+}