summaryrefslogtreecommitdiff
path: root/src/ninep/pane.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/ninep/pane.zig')
-rw-r--r--src/ninep/pane.zig141
1 files changed, 135 insertions, 6 deletions
diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig
index 9f85a75f..3c1e181e 100644
--- a/src/ninep/pane.zig
+++ b/src/ninep/pane.zig
@@ -475,6 +475,55 @@ fn joinOnly(pane: *Pane, handle: u32) void {
if (pane.fs.joined_by != 0 and pane.fs.joined_by != handle) pane.fs.joined = null;
}
+/// How long writes in a row may pause before the editor's own step puts
+/// them in: long enough that a stream of 8 KB writes is one edit.
+pub const batch_pause_ns: u64 = 20 * std.time.ns_per_ms;
+
+/// The editor's step: the held writes go in once they have paused.
+pub fn flushPausedBatch(p: *Pardes) void {
+ const b = &p.fs.batch;
+ if (b.bytes.items.len == 0) return;
+ if (p.now_ns != 0 and p.now_ns -| b.last_ns < batch_pause_ns) return;
+ flushBatch(p);
+}
+
+/// Puts the held writes (`fs.batch`) in as one edit: before any other
+/// request, the open's release, or the editor's own step.
+pub fn flushBatch(p: *Pardes) void {
+ const b = &p.fs.batch;
+ if (b.bytes.items.len == 0) return;
+ defer b.bytes.clearRetainingCapacity();
+ const id = p.paneBySerial(b.serial) orelse return;
+ const pane = p.panes[id].?;
+ if (fileOf(pane) == null) return;
+ const before = dotOf(pane);
+ joinOnly(pane, b.handle);
+ const take = spliceBody(p, pane, b.at, b.at, b.bytes.items) orelse return;
+ pane.fs.joined = fileOf(pane).?.revision;
+ pane.fs.joined_by = b.handle;
+ setDot(pane, .{
+ .q0 = shiftOne(before.q0, clip(b.at), 0, clip(take)),
+ .q1 = shiftOne(before.q1, clip(b.at), 0, clip(take)),
+ });
+ if (!pane.fs.noscroll) showOffset(pane, b.at + take);
+}
+
+/// The held bytes a write at `at` on this open can go on from: its own
+/// batch, when it ends right at `at`; any other is put in first.
+fn batchFor(p: *Pardes, req: Req, pane: *Pane, at: usize) ?*@TypeOf(p.fs.batch) {
+ const b = &p.fs.batch;
+ const mine = b.serial == pane.serial and b.node == req.node and b.handle == req.handle;
+ if (b.bytes.items.len > 0 and !(mine and b.at + b.bytes.items.len == at)) flushBatch(p);
+ // Only an open's writes are held: one with no record (a test's, handle
+ // 0) is put in at once. A hold past 64 MiB is put in first.
+ if (req.handle == 0 or b.bytes.items.len > 64 << 20) {
+ flushBatch(p);
+ return null;
+ }
+ if (b.bytes.items.len == 0) b.* = .{ .serial = pane.serial, .node = req.node, .handle = req.handle, .at = at, .bytes = b.bytes };
+ return b;
+}
+
fn writeBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
if (req.data.len == 0) return .{ .tag = req.tag, .written = 0 };
if (pane.file == null) {
@@ -484,7 +533,14 @@ fn writeBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
p.emitWrite(id, req.data[0..take]);
return .{ .tag = req.tag, .written = @intCast(take) };
}
- const at = bodyOf(pane).len;
+ const held = if (p.fs.batch.serial == pane.serial and p.fs.batch.node == req.node and p.fs.batch.handle == req.handle) p.fs.batch.bytes.items.len else 0;
+ const at = bodyOf(pane).len + held;
+ if (batchFor(p, req, pane, at)) |b| {
+ const take = wholeUtf8(req.data);
+ b.bytes.appendSlice(p.gpa, req.data[0..take]) catch return Reply.fail(req.tag, E.NOMEM);
+ b.last_ns = p.now_ns;
+ return .{ .tag = req.tag, .written = @intCast(take) };
+ }
joinOnly(pane, req.handle);
const take = spliceBody(p, pane, at, at, req.data) orelse
return Reply.fail(req.tag, E.NOMEM);
@@ -514,8 +570,9 @@ fn writeTag(req: Req, pane: *Pane) Reply {
const pf = &pane.fs;
const had = tagline.curTail(pane).len;
const room = limits.max_tag_tail -| had -| @intFromBool(pf.tag_held_newline);
- if (room == 0) return Reply.fail(req.tag, E.NOSPC);
- const take = wholeUtf8(req.data[0..@min(req.data.len, room)]);
+ // Whole or not at all: a write that would pass the limit changes nothing.
+ if (req.data.len > room) return tree.failText(req.tag, E.NOSPC, std.fmt.comptimePrint("tag: no space: over {d} bytes", .{limits.max_tag_tail}));
+ const take = wholeUtf8(req.data);
// A truncating write drops ONE trailing newline, its whole text's: a
// newline held from a write before goes in once more text follows it.
if (pf.tag_held_newline) {
@@ -538,9 +595,19 @@ fn writeData(p: *Pardes, req: Req, pane: *Pane) Reply {
if (fileOf(pane) == null) return tree.failText(req.tag, E.INVAL, e_no_text);
const pf = &pane.fs;
if (pf.addr_failed) return tree.failText(req.tag, E.INVAL, e_addr_failed);
- clampAddr(pf, bodyOf(pane).len);
+ const held = if (p.fs.batch.serial == pane.serial and p.fs.batch.node == req.node and p.fs.batch.handle == req.handle) p.fs.batch.bytes.items.len else 0;
+ clampAddr(pf, bodyOf(pane).len + held);
const q0: usize = pf.addr.q0;
const q1: usize = @max(q0, @as(usize, pf.addr.q1));
+ // An insert (an empty address) goes on from the open's held bytes.
+ if (q1 == q0) if (batchFor(p, req, pane, q0)) |b| {
+ const take = wholeUtf8(req.data);
+ b.bytes.appendSlice(p.gpa, req.data[0..take]) catch return Reply.fail(req.tag, E.NOMEM);
+ b.last_ns = p.now_ns;
+ pf.addr = .{ .q0 = clip(q0 + take), .q1 = clip(q0 + take) };
+ return .{ .tag = req.tag, .written = @intCast(take) };
+ };
+ flushBatch(p);
const before = dotOf(pane);
joinOnly(pane, req.handle);
const take = spliceBody(p, pane, q0, q1, req.data) orelse
@@ -1097,6 +1164,62 @@ test "the writes of one open are one undo step, as bash's line-buffered printf m
}
}
+test "an open's writes in a row are held and go in as one edit, seen by the next request" {
+ const p = try withFile(testing.allocator, "one\n");
+ defer p.deinit();
+ const serial = serialOf(p);
+ const body = Node.of(serial, .body);
+ const h = call(p, .{ .tag = 1, .op = .open, .node = body, .omode = 1 }).reply.handle;
+ const revision = p.panes[0].?.file.?.revision;
+ for (0..100) |_| _ = call(p, .{ .tag = 2, .op = .write, .node = body, .handle = h, .data = "0123456789\n" });
+ // Held: nothing edited yet.
+ try testing.expectEqual(revision, p.panes[0].?.file.?.revision);
+ // Any other request puts them in, once.
+ try testing.expectEqual(@as(usize, 4 + 1100), rd(p, body, 0, 4096).bytes.len);
+ try testing.expectEqual(revision + 1, p.panes[0].?.file.?.revision);
+ _ = call(p, .{ .tag = 3, .op = .release, .node = body, .handle = h, .opened = true });
+ try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Undo").reply.status);
+ try testing.expectEqualStrings("one\n", rd(p, body, 0, 64).bytes);
+ // data inserts at its address go on the same way.
+ const data = Node.of(serial, .data);
+ _ = wr(p, Node.of(serial, .addr), "#0");
+ const d = call(p, .{ .tag = 4, .op = .open, .node = data, .omode = 1 }).reply.handle;
+ _ = call(p, .{ .tag = 5, .op = .write, .node = data, .handle = d, .data = "a" });
+ _ = call(p, .{ .tag = 5, .op = .write, .node = data, .handle = d, .data = "b" });
+ _ = call(p, .{ .tag = 6, .op = .release, .node = data, .handle = d, .opened = true });
+ try testing.expectEqualStrings("abone\n", rd(p, body, 0, 64).bytes);
+}
+
+test "writes held before the shell's clock is first seen go in at its first step" {
+ const p = try withFile(testing.allocator, "one\n");
+ defer p.deinit();
+ const body = Node.of(serialOf(p), .body);
+ try testing.expect(!p.clock_started);
+ const h = call(p, .{ .tag = 1, .op = .open, .node = body, .omode = 1 }).reply.handle;
+ const revision = p.panes[0].?.file.?.revision;
+ _ = call(p, .{ .tag = 2, .op = .write, .node = body, .handle = h, .data = "x\n" });
+ try testing.expectEqual(@as(u64, 0), p.fs.batch.last_ns);
+ // A detached host's first step: its clock is days into the machine's.
+ p.advance(700_000 * std.time.ns_per_s);
+ flushPausedBatch(p);
+ try testing.expectEqual(revision + 1, p.panes[0].?.file.?.revision);
+}
+
+test "a tag write past the limit is refused whole, naming the limit" {
+ const p = try withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ const tag = Node.of(serialOf(p), .tag);
+ const before = try testing.allocator.dupe(u8, rd(p, tag, 0, 1 << 16).bytes);
+ defer testing.allocator.free(before);
+ const big = "w" ** (limits.max_tag_tail + 10);
+ const r = wr(p, tag, big);
+ try testing.expectEqual(E.NOSPC, r.errno());
+ // `no space` is what 9ns maps to ENOSPC, so a shell through a mount
+ // sees the errno the tree gives.
+ try testing.expect(std.mem.startsWith(u8, r.reply.ename, "tag: no space: over "));
+ try testing.expectEqualStrings(before, rd(p, tag, 0, 1 << 16).bytes);
+}
+
test "two opens writing one body are two undo steps, however their writes interleave" {
const p = try withFile(testing.allocator, "one\n");
defer p.deinit();
@@ -1153,13 +1276,19 @@ test "a name cut across writes is one name, applied once at its newline or its c
const node = Node.of(serial, .name);
const h = call(p, .{ .tag = 1, .op = .open, .node = node, .omode = 1 }).reply.handle;
try testing.expect(h != 0);
+ // A piece that fills its Twrite goes on in the next.
+ p.fs.write_room = "/tmp/pardes-na".len;
_ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "/tmp/pardes-na" });
+ p.fs.write_room = 0;
try testing.expectEqualStrings("/test.txt", nameOf(p.panes[p.paneBySerial(serial).?].?));
_ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "me.txt\n" });
try testing.expectEqualStrings("/tmp/pardes-name.txt", nameOf(p.panes[p.paneBySerial(serial).?].?));
- // No newline: the close applies it.
- _ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "/tmp/pardes-closed.txt" });
_ = call(p, .{ .tag = 3, .op = .release, .node = node, .handle = h, .opened = true });
+ // No newline, the whole write: applied at once, on its own open.
+ const h3 = call(p, .{ .tag = 1, .op = .open, .node = node, .omode = 1 }).reply.handle;
+ _ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h3, .data = "/tmp/pardes-closed.txt" });
+ try testing.expectEqualStrings("/tmp/pardes-closed.txt", nameOf(p.panes[p.paneBySerial(serial).?].?));
+ _ = call(p, .{ .tag = 3, .op = .release, .node = node, .handle = h3, .opened = true });
try testing.expectEqualStrings("/tmp/pardes-closed.txt", nameOf(p.panes[p.paneBySerial(serial).?].?));
}