components
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
- 29f63d01md-editor: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
- 869f49c7files-upload: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
- f60dab20month-view: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
- 5b3c349aRe-vendor to Hybriel set day35 (cb9e480d): plugins core and web sources, README vendoring notemre
- 3449b117components: a startup error prints its message before exit 1 (hybriel#247)mre
- 59704ce1Merge branch 'n5b-pipes'mre
- 23b9164aMdEditor: a clipboard whose text is only the names or file:// paths of its files (a file manager copy) is about the files, as before; any other text wins (tickets#49); gate casesmre
- b0f1ec28MdEditor: a clipboard with text always pastes the text, also when a picture is on it too (a spreadsheet or office app), files on or off; a picture alone still goes to the files; real Ctrl+V gate tests/md-paste-files.mjs (tickets#49)mre
- ad4e929bMdEditor: pipe rows without a delimiter row are a table (first row = header, short rows padded, written back without an invented | --- | line) (notes#5)mre
- 4bd11128MdEditor: the Table button's empty table is written as a real pipe table (empty cells), so it survives the source switch, a save and a reload (notes#7)mre
- 7437e32bMdEditor draws its surface at once when claimed from server HTML: 1 ms mount animation, the first editor mounts all editors of the pagemre
- 5de0264cRe-vendor onto Hybriel master acaceaf3 (set-day27b): binary, lib and plugins; exit 1 on a startup fault; .gitignore binmre
- 79c77e7dMerge sb3-standby: components deploys through its standbymre
- c5442043components: no jobs switch needed in the standbymre
- 8d2fb24ccomponents: a standby container (port 45016, profile standby); deploy hands over through standby-swap.sh without a gapmre
- c2834dd3Merge mr-fixes: media player state after a queue pick, Range handling, cover-less audio, accessibility labelsmre
- 354fe3c6Media player: audio without a cover starts and shows its time (the mount tick sits on the seek bar, which every item has); the speed button names the current speed, the seek slider says its time in words; a gate page for the first render on cover-less audio and a fourth demo queue item without a covermre
- ee4aee1eMedia player: a new queue item shows its own speed, mute, play state and time and starts when asked, also for two videos in a row; the media route reads suffix ranges and ignores range lists and garbage instead of answering 500; stronger gate checks (the 10 percent drag, the played bar, the queue tap)mre
- 9a4f9b31Merge mp-player: media served from the committed filesmre
- ab96e686Media route: serve the demo's media from the committed files in components/media-player (storage/ is not in git, the deploy gate found no files)mre