summaryrefslogtreecommitdiff
path: root/src/host/Sessions.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/Sessions.zig')
-rw-r--r--src/host/Sessions.zig42
1 files changed, 34 insertions, 8 deletions
diff --git a/src/host/Sessions.zig b/src/host/Sessions.zig
index c2af557..ae649bf 100644
--- a/src/host/Sessions.zig
+++ b/src/host/Sessions.zig
@@ -1,7 +1,8 @@
-//! Every run of a sketch, recorded: one xitdb database per sketch, beside
-//! its state file, holding an append-only log of the frames live `update`
-//! was fed, and the State at the moments that matter (keyframes). Nothing
-//! in it is ever overwritten: marks and clips are names pointing into it.
+//! A sketch's save: one xitdb database holding every run of it, an
+//! append-only log of the frames live `update` was fed, and the State at
+//! the moments that matter (keyframes). The newest keyframe and the frames
+//! after it are the present a run starts from (`present`). Nothing in it
+//! is ever overwritten: marks and clips are names pointing into it.
//!
//! root = map {
//! version 1
@@ -14,7 +15,7 @@
//! started wall-clock ns at open ended ns at a clean exit, else 0
//! from "" or where it came from ("import <file>")
//! stretches list of { frame, build, source, layout }: one a load (start, reloads);
-//! `layout` is the state file's header (memory.readLayout reads it)
+//! `layout` is State's header (memory.State.header, read by memory.layoutOf)
//! batches list of bytes: a little-endian u64, the first frame's index, then
//! whole frames of raw `abi.Event`s, each frame ending with its tick
//! keys list of { frame, batch, stretch, why, time, size, pages, written, line }:
@@ -43,9 +44,8 @@ const abi = @import("abi");
pub const Disk = xitdb.Database(.buffered_file, u64);
pub const Memory = xitdb.Database(.memory, u64);
-/// `file` for interactive runs of a build that records, `memory` for a
-/// scripted run from fresh state (time travel, nothing on disk), `off`
-/// otherwise (an export, a release build).
+/// `file` for every run but two: `memory` for a scripted run from fresh
+/// state (time travel, nothing on disk), `off` for an export.
pub const Mode = enum { off, memory, file };
pub const Why = enum(u64) { start, periodic, jump, before_reload, after_reload, mark, exit, import };
@@ -691,6 +691,32 @@ pub fn load(s: *Sessions, session: u64, k: u64) !Loaded {
}
}
+/// The State a run starts from, the present: the newest keyframe of the
+/// sessions before this one, with the frames logged after it appended to
+/// `out` (replayed, they bring it to where that run stopped). Null in a
+/// new database. This run's first keyframe starts from its pages.
+pub fn present(s: *Sessions, out: *std.ArrayList(abi.Event)) !?Loaded {
+ if (s.db == .off) return null;
+ var i = s.session;
+ while (i > 0) {
+ i -= 1;
+ const sum = try s.summary(i);
+ if (sum.keys == 0) continue;
+ const newest = try s.load(i, sum.keys - 1);
+ errdefer newest.free(s.gpa);
+ try s.events(i, newest.batch, newest.frame, sum.frames, out);
+ if (newest.paged) {
+ const hashes = try s.gpa.alloc(u64, std.math.divCeil(usize, newest.state.len, page) catch unreachable);
+ for (hashes, 0..) |*h, j| h.* = std.hash.Wyhash.hash(0, newest.state[j * page .. @min(newest.state.len, (j + 1) * page)]);
+ s.gpa.free(s.hashes);
+ s.hashes = hashes;
+ s.last = .{ .session = i, .key = sum.keys - 1 };
+ }
+ return newest;
+ }
+ return null;
+}
+
/// Keyframe `k`'s frame, time and why, without its bytes.
pub fn keyAt(s: *Sessions, session: u64, k: u64) !struct { frame: u64, time: u64, why: Why } {
switch (s.db) {