notes
All repositories: gitoria
8.5 KB
// Cross-compat proof driver (mission 066, acceptance gate for D12):// exercises the Zig mpackdb engine against db files written by the REAL// JS mpackdb v1.0.7 — and writes db files the JS side then verifies.//// Companion JS scripts: testdata/compat/*.mjs (see their headers for setup).//// Usage:// zig run compat_driver.zig -- verify_a <dbfile> # read a JS-written db// zig run compat_driver.zig -- mutate_a <dbfile> # mutate it (JS re-verifies)// zig run compat_driver.zig -- write_b <dbfile> # write a fresh db for JS// zig run compat_driver.zig -- verify_b2 <dbfile> # verify JS-side mutationsconst std = @import("std");const msgpack = @import("msgpack.zig");const engine = @import("engine.zig");const P = std.debug.print;fn keyStr(s: []const u8) engine.Key {return .{ .str = s };}fn keyNum(n: f64) engine.Key {return .{ .num = n };}fn printValue(v: msgpack.Value) void {switch (v) {.nil => P("null", .{}),.undef => P("undefined", .{}),.boolean => |b| P("{}", .{b}),.number => |n| {var buf: [40]u8 = undefined;P("{s}", .{engine.jsNumFmt(&buf, n)});},.str => |s| P("\"{s}\"", .{s}),.bin => |b| P("<bin {d}>", .{b.len}),.array => |items| {P("[", .{});for (items, 0..) |it, i| {if (i > 0) P(",", .{});printValue(it);}P("]", .{});},.map => |entries| {P("{{", .{});for (entries, 0..) |e, i| {if (i > 0) P(",", .{});printValue(e.key);P(":", .{});printValue(e.value);}P("}}", .{});},.ext => |e| {if (v.timestampMs()) |ms| {var buf: [40]u8 = undefined;P("Date({s})", .{engine.jsNumFmt(&buf, ms)});} else {P("<ext {d} len {d}>", .{ e.type, e.data.len });}},}}fn printRecords(label: []const u8, db: *engine.Db, arena: std.mem.Allocator, locs: []const engine.Loc) !void {P("{s}: {d} record(s)\n", .{ label, locs.len });for (locs) |loc| {const rec = try db.readAt(arena, loc);P(" ", .{});printValue(rec);P("\n", .{});}}pub fn main(init: std.process.Init) !void {const gpa = init.gpa;var arena_state = std.heap.ArenaAllocator.init(gpa);defer arena_state.deinit();const arena = arena_state.allocator();var iter = std.process.Args.Iterator.init(init.minimal.args);_ = iter.next();const cmd = iter.next() orelse {P("usage: compat_driver <verify_a|mutate_a|write_b|verify_b2> <dbfile>\n", .{});return;};const dbfile = iter.next() orelse {P("missing dbfile\n", .{});return;};if (std.mem.eql(u8, cmd, "verify_a")) {// Open the JS-written db WITHOUT compaction so we read its files// (data, indexes, idxstate, meta) exactly as JS persisted them.const db = try engine.Db.open(gpa, dbfile, .{.primary_key = "*id",.indexes = &.{ "email", "*age" },.compact = false,});defer db.close();var locs: std.ArrayList(engine.Loc) = .empty;try db.findPkLocs(arena, keyNum(1), &locs);try printRecords("pk id=1", db, arena, locs.items);var locs2: std.ArrayList(engine.Loc) = .empty;try db.findIndexLocs(arena, "email", keyStr("[email protected]"), &locs2);try printRecords("[email protected]", db, arena, locs2.items);// Carol (id=2) was deleted by JS — her email must yield nothingvar locs2b: std.ArrayList(engine.Loc) = .empty;try db.findIndexLocs(arena, "email", keyStr("[email protected]"), &locs2b);P("[email protected] (JS-deleted): {d} record(s)\n", .{locs2b.items.len});var locs3: std.ArrayList(engine.Loc) = .empty;try db.findRangeLocs(arena, "age", keyNum(28), keyNum(40), &locs3);try printRecords("age in [28,40]", db, arena, locs3.items);// deleted record must be invisiblevar locs4: std.ArrayList(engine.Loc) = .empty;try db.findPkLocs(arena, keyNum(2), &locs4);P("pk id=2 (JS-deleted): {d} record(s)\n", .{locs4.items.len});// full scan countvar scanner = engine.Db.Scanner.init(db, arena, true);defer scanner.deinit();var count: usize = 0;while (try scanner.next(arena)) |_| count += 1;P("total live records: {d}\n", .{count});P("nextId: ", .{});var buf: [40]u8 = undefined;P("{s}\n", .{engine.jsNumFmt(&buf, db.next_id)});} else if (std.mem.eql(u8, cmd, "mutate_a")) {const db = try engine.Db.open(gpa, dbfile, .{.primary_key = "*id",.indexes = &.{ "email", "*age" },.compact = false,});defer db.close();// 1) insert a new record — auto id must continue the JS sequencevar rec = engine.Db.MutableRecord{};try rec.set(arena, "name", .{ .str = "Ziggy Stardust" });try rec.set(arena, "email", .{ .str = "[email protected]" });try rec.set(arena, "age", .{ .number = 99 });var tags = [_]msgpack.Value{ .{ .str = "zig" }, .{ .str = "native" } };try rec.set(arena, "tags", .{ .array = &tags });const res = try db.insert(arena, rec.toValue(), false);var buf: [40]u8 = undefined;P("inserted id={s}\n", .{engine.jsNumFmt(&buf, res.pk_num)});// 2) update id=0: bump age to 31, KEEP the Date field byte-identicalvar locs: std.ArrayList(engine.Loc) = .empty;try db.findPkLocs(arena, keyNum(0), &locs);if (locs.items.len != 1) return error.UpdateTargetMissing;const old = try db.readAt(arena, locs.items[0]);var upd = engine.Db.MutableRecord{};for (old.map) |e| try upd.entries.append(arena, e);try upd.set(arena, "age", .{ .number = 31 });const n_upd = try db.updatePk(arena, keyNum(0), upd.toValue());P("updated id=0: {d} record(s), age -> 31\n", .{n_upd});// 3) delete id=3var deleted: std.ArrayList(msgpack.Value) = .empty;const n_del = try db.deletePk(arena, keyNum(3), &deleted);P("deleted id=3: {d} record(s)\n", .{n_del});} else if (std.mem.eql(u8, cmd, "write_b")) {// Fresh Zig-written db with uuid pk + secondary indexes for JS to read.const db = try engine.Db.open(gpa, dbfile, .{.primary_key = "@uuid",.indexes = &.{ "city", "*score" },});defer db.close();const cities = [_][]const u8{ "Klagenfurt", "Helsinki", "Wörgl" };const names = [_][]const u8{ "Ada", "Böb", "Chidi" };const scores = [_]f64{ 12.5, -3, 4294967296 };for (cities, names, scores) |city, name, score| {var rec = engine.Db.MutableRecord{};try rec.set(arena, "name", .{ .str = name });try rec.set(arena, "city", .{ .str = city });try rec.set(arena, "score", .{ .number = score });var nested_entries = [_]msgpack.Entry{.{ .key = .{ .str = "active" }, .value = .{ .boolean = true } },.{ .key = .{ .str = "note" }, .value = .nil },};try rec.set(arena, "meta", .{ .map = &nested_entries });const res = try db.insert(arena, rec.toValue(), false);P("inserted uuid={s} ({s})\n", .{ res.pk_str, name });}} else if (std.mem.eql(u8, cmd, "verify_b2")) {const db = try engine.Db.open(gpa, dbfile, .{.primary_key = "@uuid",.indexes = &.{ "city", "*score" },.compact = false,});defer db.close();// JS inserted a marker record with city "Byrodin"var locs: std.ArrayList(engine.Loc) = .empty;try db.findIndexLocs(arena, "city", keyStr("Byrodin"), &locs);try printRecords("city=Byrodin (JS-inserted)", db, arena, locs.items);// JS deleted "Ada" — city Klagenfurt must be gonevar locs2: std.ArrayList(engine.Loc) = .empty;try db.findIndexLocs(arena, "city", keyStr("Klagenfurt"), &locs2);P("city=Klagenfurt (JS-deleted): {d} record(s)\n", .{locs2.items.len});var scanner = engine.Db.Scanner.init(db, arena, true);defer scanner.deinit();var count: usize = 0;while (try scanner.next(arena)) |_| count += 1;P("total live records: {d}\n", .{count});} else {P("unknown command {s}\n", .{cmd});}}
Branches
Latest commits
- 452458ee|||PR Type the function parameters of notes' own .hl files (HL-STANDARD L10)Atlas
- 23758cd6Re-vendor complete Hybriel plugins and runtime to day44mre
- 5d7cc700Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
- 3e07b1cfVendor complete Hybriel day39 plugin sources and librariesmre
- d7366702Check session cookie flags after login and assert anonymous GET stays cookielessmre
- 16bc8fd8Re-vendor Hybriel day38b with the mpackdb store-open race fixmre
- d860dc2dnotes#12: the markup names its own rules instead of hand-written class strings: the selected row is `aria-current="page"` (the list-first theme rule styles it, no class), the delete question is `Style.pAsk` and follows its own `open` attribute (bound to `asking`), the error line is `Style.pMessage` (body once in lib/looks.hl, rule in notes.hl, main.hl and loginfailed.hl). The CSS declarations are the same, only selectors changed (.ask -> p.ask, .ask.open -> p.ask[open], .message -> p.message); the pages look the same (AE 0 at 4 sizes). main.hl's selectorClass in/out stays (login.js reads it).mre
- 2e7f8664notes#13: the list and back links pass the note key from the loop (emit pick(e, r.key)) instead of writing data-note into the View and reading it back with getAttribute; the two window.history.replaceState calls and the identcode e.target.value read stay (hl:web has no replace-navigation, a bound hidden input is not adopted on a script's change event), see NO13 reportmre
- 4dc98282notes#15: a deleted note made in this tab leaves the list at once (the delete now removes this tab's edit under every key the row has: local id, server id); gate: browser.mjs notes#15 section, red on the old codemre
- e4e7402fnotes#11: plain declarations instead of let for variables never reassigned (components/notes.hl 3, lib/jsoncheck.hl 5, lib/util.hl 2, tools/migrate-short-ids.hl 8)mre
- 27b6a09enotes#8: Style blocks in the nested form (note-list, note-editor, .ask with their children nested, :empty as a nested pseudo-class); the same CSS rules, the pages look the samemre
- 71a9538bnotes: B27W report, log linemre
- 78ba0073notes: an ident access key (Authorization: Bearer idt_…) reads and writes its identity's notes through /api/me and /api/notes; gate tests/ident-access-notes.mjs (ident#27)mre
- 3de46dacnotes: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 73+5 failing → 78 passedmre
- 5909d458notes: MdEditor from components 59704ce (text on the clipboard always pastes, a file manager copy still attaches; pipe rows without a delimiter row; tickets#49, notes#5)mre
- 98bd6a98N5b: pipe rows without a delimiter row are a table (MdEditor ad4e929, plain copy); gate tests/dashboard-table.mjs; reportmre
- dbf93319N5 report: components gate rerun 613 passed, 0 failedmre
- 3a8bc0d2N5: empty Table-button table is written as a real pipe table (MdEditor 4bd1112, plain copy); gate tests/table-button.mjs; reportmre
- c16bd443N5: step 1 report (tables pass locally; his DASHBOARD note has no delimiter row)mre
- 6091320anotes: MdEditor 7437e32 (surface drawn at once when the page is claimed); gate measures claim-to-text on 5 note loads at 1280 and 390mre