jukebox
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
- a4bce6d1Records: ticket push subscriptionmre
- 14abde3bjukebox: records: Atlas's records moved to worldapi-conductor/archivemre
- 0433e9a8jukebox: records: gitoria remote added and pushedmre
- 1722d707jukebox: records: repo creation handed to Gitoriamre
- b42e7dfbjukebox: conductor records (CONDUCTOR.md, STATUS.md), old STATUS archivedmre
- 1b0d0bc1jukebox: a startup error prints its message before exit 1 (hybriel#247)mre
- b19c478bjukebox JB18 (jukebox#17): onto Hybriel set-day31 (b287cd82, contains the SIGPIPE fix 6d2ca454): plugins/core and plugins/web re-vendored, bin/ from the set; a client that closes /stream while it is written no longer kills the server (90 aborted streams survived, the old bin died with exit 141 on the first)mre
- 73867a2ejukebox#2: Style blocks in the language's nested form (rules named after the View's own elements, pseudo-classes nested, merged duplicates); the same CSS rules, the pages look the samemre
- 06c548d6jukebox JB15: report, log linemre
- 980859cbjukebox JB15 (jukebox#15): the now-playing overlay on a short screen (tablet or phone on its side) gave the queue 12 to 100 px because the big cover took the room; the cover now shrinks with the screen height and goes away under 41rem, the queue keeps 3+ rows; gate tests/jb15.mjs at 8 sizesmre
- 9efc40ffjukebox#12 #14: Grok session 1793246574527-181160mre
- 7d58b36ejukebox JB13 (jukebox#13): Next and Previous ask for the songs after / before the current song's id in the current order, not for place + 1; gate tests/jb13.mjs checks Next with the queue down to one song after the order changedmre
- 51358794jukebox JB14 (jukebox#14): the Songs screen opens sorted the way it was left (kept on the server in storage/ui.json), the sort control shows the sort the rows really have (selected options, autocomplete off); gate tests/jb14.mjsmre
- cc68a063jukebox JB13 (jukebox#13): a tapped song plays the song that was tapped, found by its id in the list as it is now (not by its row number, which counted plays move); the queue goes on from its place there, the rows refresh on the tap; gate tests/jb13.mjs (songs by plays, favourites)mre
- 88d3d5c1jukebox JB12b (jukebox#12): the scroll handle is big (2.75rem x 4rem, accent colour, grip) and drawn on every device, no media query; native bar hidden; gate tests/touch.mjs checks the drawn width with and without touchmre
- b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre
- 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
- 0f2157a9jukebox JV1: onto Hybriel set-day27b: readBytes and stat for /stream and the mp3 tags (no dd, head or tail), startup fault exits 1, fs and mpackdb plugins re-vendored; songs with a comma in the title are indexed againmre
- 63d0f2eejukebox ST1 (jukebox#2): styles of main, songs and search moved into their own Style blocks (styles.hl 81 to 8 rules, pixel-identical at 390 and 1280); artist and album wait for an hl:web fixmre
- c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre