Versions 98 to 318
new file, 177 lines
1+import assert from "node:assert/strict"; 2+import { test } from "node:test"; 3+import { ProjectionPriority } from "../experimental/projection-priority.ts"; 4+import { orderedChildKey } from "../ordered-index.ts"; 5+import { SqlState } from "./storage-helper.ts"; 7+const decode = (value: unknown): number => { 8+ if (!Number.isSafeInteger(value)) throw Error("invalid payload"); 9+ return value as number; 11+test("experimental priority retains old roots, removes winners and rejects corrupt pages", () => { 12+ const storage = new SqlState(); 14+ const index = new ProjectionPriority(storage, "test", decode), 15+ oracle: { key: string; value: number }[] = []; 16+ let root: number | null = null; 17+ for (let n = 0; n < 96; n++) { 18+ const key = orderedChildKey({ n, replaces: n % 2, lamport: (n * 71) % 97, seq: n, k: 0 }); 19+ const expected = oracle.filter((entry) => entry.key < key).at(-1)?.value; 20+ assert.equal(index.before(root, key), expected); 21+ const old: number | null = root; 22+ root = index.insert(root, key, n); 23+ assert.equal(index.before(old, key), expected); 24+ oracle.push({ key, value: n }); 25+ oracle.sort((a, b) => a.key.localeCompare(b.key)); 28+ const greatest = oracle.pop()!; 29+ root = index.remove(root, greatest.key); 30+ assert.equal(index.before(root, "f".repeat(57)), oracle.at(-1)!.value); 31+ assert.equal(index.before(full, "f".repeat(57)), greatest.value); 32+ const rows = () => JSON.stringify(storage.db.prepare("SELECT * FROM state ORDER BY collection,key").all()); 33+ const before = rows(); 34+ storage.failWrite = storage.writes + 3; 35+ assert.throws(() => index.insert(root, "e".repeat(57), 100), /injected/); 36+ assert.equal(rows(), before); 37+ storage.failWrite = Infinity; 38+ const collection = 'experimental-priority:"test"', 39+ saved = storage.read(collection, String(root)); 40+ for (const corrupt of [ 41+ { format: 1, kind: "branch", depth: 1, children: { "0": 0 } }, 42+ { format: 1, kind: "branch", depth: 0, children: { f: root } }, 43+ { format: 1, kind: "branch", depth: 0, children: {} }, 45+ storage.write(collection, String(root), corrupt); 46+ assert.throws(() => index.before(root, "f".repeat(57)), /invalid priority/); 48+ storage.write(collection, String(root), saved); 49+ assert.equal(index.before(root, "f".repeat(57)), oracle.at(-1)!.value); 55+test("priority allocation rolls back a failed final counter and retries without collisions", (t) => { 56+ const storage = new SqlState(); 57+ t.after(() => storage.db.close()); 58+ const index = new ProjectionPriority(storage, "counter-retry", decode); 59+ const key = "1".repeat(57); 60+ const root = index.insert(null, key, 1); 61+ const rows = () => storage.db.prepare("SELECT * FROM state ORDER BY collection,key").all(); 62+ const before = rows(); 63+ const original = storage.write.bind(storage); 65+ t.mock.method(storage, "write", (collection: string, key: string, value: unknown) => { 66+ original(collection, key, value); 67+ if (key === "next" && fail) throw Error("final allocation counter failure"); 69+ assert.throws(() => index.insert(root, "2".repeat(57), 2), /final allocation counter/); 70+ assert.deepEqual(rows(), before); 71+ assert.equal(index.before(root, "f".repeat(57)), 1); 73+ const next = index.insert(root, "2".repeat(57), 2); 74+ assert.equal(index.before(next, "f".repeat(57)), 2); 75+ assert.equal(index.before(root, "f".repeat(57)), 1); 76+ const beforeRemove = rows(); 78+ assert.throws(() => index.remove(next, "2".repeat(57)), /final allocation counter/); 79+ assert.deepEqual(rows(), beforeRemove); 81+ const removed = index.remove(next, "2".repeat(57)); 82+ assert.equal(index.before(removed, "f".repeat(57)), 1); 85+test("compressed priority updates legacy depth paths without changing old roots", (t) => { 86+ const storage = new SqlState(); 87+ t.after(() => storage.db.close()); 88+ const collection = 'experimental-priority:"legacy"'; 89+ const key = "1".repeat(57); 90+ storage.write(collection, "57", { format: 1, kind: "leaf", depth: 57, value: 1 }); 91+ for (let depth = 56; depth >= 0; depth--) 92+ storage.write(collection, String(depth), { 96+ children: { [key[depth]!]: depth + 1 }, 98+ storage.write(collection, "next", 58); 99+ const index = new ProjectionPriority(storage, "legacy", decode); 100+ assert.equal(index.before(0, "f".repeat(57)), 1); 101+ const mixed = index.insert(0, "1".repeat(56) + "2", 2); 102+ assert.equal(index.before(mixed, "f".repeat(57)), 2); 103+ assert.equal(index.before(mixed, "1".repeat(56) + "2"), 1); 104+ assert.equal(index.before(0, "f".repeat(57)), 1); 105+ const removed = index.remove(mixed, key); 106+ assert.equal(index.before(removed, "f".repeat(57)), 2); 107+ assert.equal(index.remove(removed, "1".repeat(56) + "2"), null); 108+ assert.equal(index.before(0, "f".repeat(57)), 1); 111+test("compressed priority matches a sorted oracle across varied prefixes and removals", (t) => { 112+ const storage = new SqlState(); 113+ t.after(() => storage.db.close()); 114+ const index = new ProjectionPriority(storage, "varied", decode); 115+ const oracle = new Map<string, number>(); 116+ let root: number | null = null; 118+ const randomKey = () => 119+ Array.from({ length: 57 }, () => { 120+ seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; 121+ return (seed >>> 28).toString(16); 126+ ...Array.from({ length: 128 }, randomKey), 127+ ...Array.from({ length: 32 }, (_, n) => "a".repeat(55) + n.toString(16).padStart(2, "0")), 129+ const verify = () => { 130+ const sorted = [...oracle.keys()].sort(); 131+ for (const key of [...keys, randomKey()]) { 132+ const before = sorted.filter((candidate) => candidate < key).at(-1); 133+ assert.equal(index.before(root, key), before === undefined ? undefined : oracle.get(before)); 136+ for (const [n, key] of keys.entries()) { 137+ root = index.insert(root, key, n); 139+ if (n % 23 === 0) verify(); 142+ for (const [n, key] of keys.entries()) { 143+ root = index.remove(root, key); 145+ if (n % 19 === 0) verify(); 147+ assert.equal(root, null); 151+test("compressed priority rejects malformed prefixes and cyclic child edges", (t) => { 152+ const storage = new SqlState(); 153+ t.after(() => storage.db.close()); 154+ const collection = 'experimental-priority:"corrupt"'; 155+ const index = new ProjectionPriority(storage, "corrupt", decode); 156+ const first = index.insert(null, "1".repeat(57), 1); 157+ const root = index.insert(first, "2".repeat(57), 2); 158+ const saved = storage.read(collection, String(root)); 159+ for (const node of [ 160+ { format: 2, kind: "branch", prefix: "", children: { "1": root, "2": first } }, 161+ { format: 2, kind: "branch", prefix: "x", children: { "1": first, "2": first } }, 162+ { format: 2, kind: "branch", prefix: "", children: { "1": first } }, 163+ { format: 2, kind: "leaf", prefix: "1", value: 1 }, 164+ { format: 3, kind: "leaf", prefix: "1".repeat(57), value: 1 }, 166+ storage.write(collection, String(root), node); 167+ assert.throws(() => index.before(root, "f".repeat(57)), /invalid priority/); 169+ storage.write(collection, String(root), saved); 170+ const branch = saved as { children: Record<string, number> }; 171+ const child = branch.children["2"]!; 172+ const leaf = storage.read(collection, String(child)); 173+ storage.write(collection, String(child), { format: 2, kind: "leaf", prefix: "3".repeat(57), value: 2 }); 174+ assert.throws(() => index.before(root, "f".repeat(57)), /invalid priority/); 175+ storage.write(collection, String(child), leaf); 176+ assert.equal(index.before(root, "f".repeat(57)), 2);