summaryrefslogtreecommitdiff
path: root/src/host/Sessions.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-16 23:46:16 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit5dc2c61f1df30d2dd84fbba6d0e8895a62c3b2b5 (patch)
treeca8907b582fb2c1fda197aa1011e5ea5f2725a46 /src/host/Sessions.zig
parentca602943e3fc9faad24e51497eeafb0ae7856800 (diff)
downloadsketchbook-5dc2c61f1df30d2dd84fbba6d0e8895a62c3b2b5.tar.gz
sketchbook-5dc2c61f1df30d2dd84fbba6d0e8895a62c3b2b5.zip
Export a session to a file of its own, and import one
export-session [S] FILE writes session S (this run's by default, committed first), with the marks and clips that point into it, as session 0 of a new database: written beside FILE, synced, renamed over it. import FILE appends each session of such a file to this database, from "import <file name>", its marks and clips renamed NAME@<file name> (cut to 32 bytes). The copy is value by value, the same few lines for both, over whatever a session holds; sessions lists where one came from. test-import records test/past.script's session, exports it, imports it into another database and replays it there: the mark it carries is the State half a second in exactly, and two seconds in is frame 120's. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/host/Sessions.zig')
-rw-r--r--src/host/Sessions.zig149
1 files changed, 147 insertions, 2 deletions
diff --git a/src/host/Sessions.zig b/src/host/Sessions.zig
index 4399c63..c5fc185 100644
--- a/src/host/Sessions.zig
+++ b/src/host/Sessions.zig
@@ -550,7 +550,20 @@ pub fn events(s: *Sessions, session: u64, from: u64, start: u64, end: u64, out:
}
/// Session `i`'s summary.
-pub const Summary = struct { started: u64, ended: u64, frames: u64, stretches: u64, keys: u64 };
+pub const Summary = struct {
+ started: u64,
+ ended: u64,
+ frames: u64,
+ stretches: u64,
+ keys: u64,
+ /// Where it came from ("import <file name>"), cut short.
+ from_bytes: [48]u8 = undefined,
+ from_len: usize = 0,
+
+ pub fn from(m: *const Summary) []const u8 {
+ return m.from_bytes[0..m.from_len];
+ }
+};
pub fn summary(s: *Sessions, i: u64) !Summary {
switch (s.db) {
@@ -558,14 +571,146 @@ pub fn summary(s: *Sessions, i: u64) !Summary {
inline .memory, .file => |*db| {
const R = Read(@TypeOf(db.*));
const ses = try R.session(db, i);
- return .{
+ var m: Summary = .{
.started = try R.uint(ses, .started),
.ended = try R.uint(ses, .ended),
.frames = try R.uint(ses, .frames),
.stretches = if (try R.list(ses, .stretches)) |l| try l.count() else 0,
.keys = if (try R.list(ses, .keys)) |l| try l.count() else 0,
};
+ const from = try R.bytes(ses, .from, s.gpa);
+ defer s.gpa.free(from);
+ m.from_len = @min(from.len, m.from_bytes.len);
+ @memcpy(m.from_bytes[0..m.from_len], from[0..m.from_len]);
+ return m;
+ },
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Export and import
+
+/// Writes session `i`, with the marks and clips in it (now of session 0), to
+/// a new database at `path` (written beside it, synced, then renamed): a
+/// playtest's sessions to send, which `import` reads.
+pub fn exportSession(s: *Sessions, i: u64, path: []const u8) !void {
+ if (s.db == .off) return error.NotRecording;
+ if (i == s.session) try s.commit(.none);
+ if (i >= try s.count(.sessions)) return error.NoSuchSession;
+ const cwd = std.Io.Dir.cwd();
+ var buf: [std.fs.max_path_bytes + 8]u8 = undefined;
+ const tmp = try std.fmt.bufPrint(&buf, "{s}.tmp", .{path});
+ const file = try cwd.createFile(s.io, tmp, .{ .read = true, .truncate = true });
+ errdefer cwd.deleteFile(s.io, tmp) catch {};
+ defer file.close(s.io);
+ var buffer: std.Io.Writer.Allocating = .init(s.gpa);
+ defer buffer.deinit();
+ var out = try Disk.init(s.io, .{ .file = file, .buffer = &buffer, .fsync = true });
+ switch (s.db) {
+ .off => unreachable,
+ inline .memory, .file => |*db| try out.runContext(Transfer(@TypeOf(db.*), Disk){ .gpa = s.gpa, .from = db, .only = i, .label = null }),
+ }
+ try cwd.rename(tmp, cwd, path, s.io);
+}
+
+/// Appends the sessions of the database at `path` (an `exportSession`'s) to
+/// this one, their `from` "import <file name>", with their marks and clips,
+/// named `<name>@<file name>` (cut to `max_name`). Returns the first one's
+/// index.
+pub fn import(s: *Sessions, path: []const u8) !u64 {
+ if (s.db == .off) return error.NotRecording;
+ const file = try std.Io.Dir.cwd().openFile(s.io, path, .{});
+ defer file.close(s.io);
+ if (try file.length(s.io) == 0) return error.NotASessionsDatabase;
+ var buffer: std.Io.Writer.Allocating = .init(s.gpa);
+ defer buffer.deinit();
+ var in = try Disk.init(s.io, .{ .file = file, .buffer = &buffer, .fsync = false });
+ const first = try s.count(.sessions);
+ switch (s.db) {
+ .off => unreachable,
+ inline .memory, .file => |*db| try db.runContext(Transfer(Disk, @TypeOf(db.*)){ .gpa = s.gpa, .from = &in, .only = null, .label = std.fs.path.basename(path) }),
+ }
+ return first;
+}
+
+/// A transaction appending database `from`'s sessions (`only` one, else
+/// all) to the one it runs in, with the marks and clips pointing into them;
+/// `label`, an import's file name, goes in their `from` and names.
+fn Transfer(comptime From: type, comptime To: type) type {
+ return struct {
+ gpa: std.mem.Allocator,
+ from: *From,
+ only: ?u64,
+ label: ?[]const u8,
+
+ const R = Read(From);
+ const Map = To.HashMap(.read_write);
+ const List = To.ArrayList(.read_write);
+
+ pub fn run(ctx: @This(), cursor: *To.Cursor(.read_write)) !void {
+ const src = try R.root(ctx.from);
+ if (try R.uint(src, .version) != 1) return error.NotASessionsDatabase;
+ const all = try R.list(src, .sessions) orelse return error.NotASessionsDatabase;
+ const root = try Map.init(cursor.*);
+ try root.put(key(.version), .{ .uint = 1 });
+ const sessions = try List.init(try root.putCursor(key(.sessions)));
+ const base = try sessions.count();
+ var buf: [std.fs.max_path_bytes + 8]u8 = undefined;
+ for (0..try all.count()) |i| {
+ if (ctx.only) |o| if (o != i) continue;
+ var to = try sessions.appendCursor();
+ try copy(From, To, ctx.gpa, try all.getCursor(@intCast(i)) orelse return error.CorruptSession, &to);
+ if (ctx.label) |l| try (try Map.init(to)).put(key(.from), .{ .bytes = try std.fmt.bufPrint(&buf, "import {s}", .{l}) });
+ }
+ for ([_]K{ .marks, .clips }) |which| {
+ const entries = try R.list(src, which) orelse continue;
+ const list = try List.init(try root.putCursor(key(which)));
+ var it = try entries.iterator();
+ while (try it.next()) |e| {
+ const session = try R.uint(try R.Map.init(e), .session);
+ if (ctx.only) |o| if (o != session) continue;
+ var to = try list.appendCursor();
+ try copy(From, To, ctx.gpa, e, &to);
+ const item = try Map.init(to);
+ try item.put(key(.session), .{ .uint = base + if (ctx.only == null) session else 0 });
+ const l = ctx.label orelse continue;
+ var name: [max_name]u8 = undefined;
+ const old = (try R.field(try R.Map.init(e), .name)).readBytes(&name) catch return error.CorruptSession;
+ const new = std.fmt.bufPrint(&buf, "{s}@{s}", .{ old, l }) catch unreachable;
+ try item.put(key(.name), .{ .bytes = new[0..@min(new.len, max_name)] });
+ }
+ }
+ }
+ };
+}
+
+/// Copies the value at `from` (a uint, bytes, or a list or map of them) to `to`.
+fn copy(comptime From: type, comptime To: type, gpa: std.mem.Allocator, from: From.Cursor(.read_only), to: *To.Cursor(.read_write)) anyerror!void {
+ switch ((try from.slot()).tag) {
+ .uint => try to.write(.{ .uint = try from.readUint() }),
+ .bytes, .short_bytes => {
+ const bytes = try from.readBytesAlloc(gpa, null);
+ defer gpa.free(bytes);
+ try to.write(.{ .bytes = bytes });
+ },
+ .array_list => {
+ const list = try To.ArrayList(.read_write).init(to.*);
+ var it = try from.iterator();
+ while (try it.next()) |c| {
+ var next = try list.appendCursor();
+ try copy(From, To, gpa, c, &next);
+ }
+ },
+ .hash_map => {
+ const map = try To.HashMap(.read_write).init(to.*);
+ var it = try from.iterator();
+ while (try it.next()) |c| {
+ const kv = try c.readKeyValuePair();
+ var next = try map.putCursor(kv.hash);
+ try copy(From, To, gpa, kv.value_cursor, &next);
+ }
},
+ else => return error.CorruptSession,
}
}