Lines 1–15 at 162 (were 1–26 at 161)
11import type { GraphStateStorage } from "../state-source.ts"; 33type Page = number | null; 4−interface Leaf {
5− format: 1;
6− kind: "leaf";
7− depth: 57;
8− value: unknown;
9−}
10−interface Branch {
11− format: 1;
12− kind: "branch";
13− depth: number;
14− children: Record<string, number>;
15−}
16−type Node = Leaf | Branch;
17−const safe = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0;
18−const object = (value: unknown): value is Record<string, unknown> =>
19− value !== null && typeof value === "object" && !Array.isArray(value);
5+ | { format: 2; kind: "leaf"; prefix: string; value: unknown } 6+ | { format: 2; kind: "branch"; prefix: string; children: Record<string, number> }; 7+const safe = (v: unknown): v is number => Number.isSafeInteger(v) && Number(v) >= 0; 8+const object = (v: unknown): v is Record<string, unknown> => v !== null && typeof v === "object" && !Array.isArray(v); 209function check(root: Page, key: string): void { 2110 if ((root !== null && !safe(root)) || !/^[0-9a-f]{57}$/.test(key)) throw Error("invalid priority lookup"); 23−/** Internal immutable priority experiment. At most 57 levels and 16 children. */
12+/** Immutable compressed priority paths. Reads legacy depth pages; writes format 2. */ 2413export class ProjectionPriority<T> { 2514 readonly #storage: GraphStateStorage; 2615 readonly #collection: string; Lines 20–150 at 162 (were 31–115 at 161)
3120 this.#collection = `experimental-priority:${JSON.stringify(namespace)}`; 3221 this.#decode = decode; 34− #read(id: number, depth: number): Node {
35− if (!safe(id) || !safe(depth) || depth > 57) throw Error("invalid priority reference");
23+ #read(id: number, expected: string): Node { 24+ if (!safe(id) || expected.length > 57) throw Error("invalid priority reference"); 3625 const row = this.#storage.read(this.#collection, String(id)); 37− if (!object(row) || row.format !== 1 || row.depth !== depth || Object.keys(row).length !== 4)
38− throw Error("invalid priority node");
39− if (depth === 57) {
40− if (row.kind !== "leaf" || !("value" in row)) throw Error("invalid priority leaf");
41− return row as unknown as Leaf;
26+ if (!object(row) || Object.keys(row).length !== 4) throw Error("invalid priority node"); 27+ const legacy = row.format === 1; 28+ const prefix = legacy ? expected : row.prefix; 30+ (legacy ? row.depth !== expected.length : row.format !== 2) || 31+ typeof prefix !== "string" || 32+ !/^[0-9a-f]{0,57}$/.test(prefix) || 33+ !prefix.startsWith(expected) 35+ throw Error("invalid priority prefix"); 36+ if (row.kind === "leaf") { 37+ if (prefix.length !== 57 || !("value" in row)) throw Error("invalid priority leaf"); 38+ return { format: 2, kind: "leaf", prefix, value: row.value }; 43− if (row.kind !== "branch" || !object(row.children)) throw Error("invalid priority branch");
40+ if (row.kind !== "branch" || prefix.length >= 57 || !object(row.children)) throw Error("invalid priority branch"); 4441 const entries = Object.entries(row.children); 45− if (!entries.length || entries.length > 16) throw Error("invalid priority fanout");
42+ if (entries.length < (legacy ? 1 : 2) || entries.length > 16) throw Error("invalid priority fanout"); 4643 for (const [digit, child] of entries) 4744 if (!/^[0-9a-f]$/.test(digit) || !safe(child) || child === id) throw Error("invalid priority child"); 48− return row as unknown as Branch;
45+ return { format: 2, kind: "branch", prefix, children: row.children as Record<string, number> }; 50− #write(node: Node): number {
51− const next = this.#storage.read(this.#collection, "next") ?? 0;
52− if (!safe(next) || !Number.isSafeInteger(next + 1)) throw Error("invalid priority allocation");
53− if (this.#storage.read(this.#collection, String(next)) !== undefined) throw Error("priority allocation collision");
54− this.#storage.write(this.#collection, String(next), node);
55− this.#storage.write(this.#collection, "next", next + 1);
56− return next;
47+ #allocate<R>(body: (write: (node: Node) => number) => R): R { 48+ return this.#storage.transaction(() => { 49+ let next = this.#storage.read(this.#collection, "next") ?? 0; 50+ if (!safe(next)) throw Error("invalid priority allocation"); 51+ const initial = next; 52+ const result = body((node) => { 53+ // Collapse legacy singleton paths before storing a compressed page. 54+ while (node.kind === "branch" && Object.keys(node.children).length === 1) { 55+ const [digit, child] = Object.entries(node.children)[0]!; 56+ node = this.#read(child, node.prefix + digit); 58+ if (!safe(next) || !Number.isSafeInteger(next + 1)) throw Error("invalid priority allocation"); 59+ if (this.#storage.read(this.#collection, String(next)) !== undefined) 60+ throw Error("priority allocation collision"); 62+ this.#storage.write(this.#collection, String(id), node); 65+ if (next !== initial) this.#storage.write(this.#collection, "next", next); 5870 insert(root: Page, key: string, value: T): number { 6072 const checked = structuredClone(this.#decode(structuredClone(value))); 61− return this.#storage.transaction(() => {
62− const visit = (id: Page, depth: number): number => {
63− const node = id === null ? undefined : this.#read(id, depth);
64− if (depth === 57) return this.#write({ format: 1, kind: "leaf", depth: 57, value: checked });
65− if (node?.kind === "leaf") throw Error("invalid priority depth");
66− const children = node?.children ?? {},
67− digit = key[depth]!;
68− const child = visit(children[digit] ?? null, depth + 1);
69− return this.#write({ format: 1, kind: "branch", depth, children: { ...children, [digit]: child } });
73+ return this.#allocate((write) => { 74+ const leaf = () => write({ format: 2, kind: "leaf", prefix: key, value: checked }); 75+ const visit = (id: Page, expected: string): number => { 76+ if (id === null) return leaf(); 77+ const node = this.#read(id, expected); 79+ while (common < node.prefix.length && node.prefix[common] === key[common]) common++; 80+ if (common < node.prefix.length) { 81+ // Legacy pages need their inferred prefix persisted when moved below a new branch. 82+ const old = write(node), 87+ prefix: key.slice(0, common), 88+ children: { [node.prefix[common]!]: old, [key[common]!]: added }, 91+ if (node.kind === "leaf") return leaf(); 92+ const digit = key[node.prefix.length]!; 93+ const child = visit(node.children[digit] ?? null, node.prefix + digit); 94+ return write({ ...node, children: { ...node.children, [digit]: child } }); 71− return visit(root, 0);
96+ return visit(root, ""); 7499 remove(root: Page, key: string): Page { 76− return this.#storage.transaction(() => {
77− const visit = (id: Page, depth: number): Page => {
101+ return this.#allocate((write) => { 102+ const visit = (id: Page, expected: string): Page => { 78103 if (id === null) throw Error("missing priority key"); 79− const node = this.#read(id, depth);
104+ const node = this.#read(id, expected); 105+ if (!key.startsWith(node.prefix)) throw Error("missing priority key"); 80106 if (node.kind === "leaf") return null; 81− const digit = key[depth]!,
82− child = visit(node.children[digit] ?? null, depth + 1),
107+ const digit = key[node.prefix.length]!, 108+ child = visit(node.children[digit] ?? null, node.prefix + digit), 83109 children = { ...node.children }; 84110 if (child === null) delete children[digit]; 85111 else children[digit] = child; 86− return Object.keys(children).length ? this.#write({ ...node, children }) : null;
112+ const entries = Object.entries(children); 113+ if (!entries.length) return null; 114+ if (entries.length === 1) { 115+ const [d, c] = entries[0]!; 116+ return write(this.#read(c, node.prefix + d)); 118+ return write({ ...node, children }); 88− return visit(root, 0);
120+ return visit(root, ""); 91123 before(root: Page, key: string): T | undefined { 93− let current = root;
94− let candidate: { id: number; depth: number } | undefined;
95− for (let depth = 0; current !== null && depth < 57; depth++) {
96− const node = this.#read(current, depth);
97− if (node.kind !== "branch") throw Error("invalid priority depth");
98− const digit = key[depth]!,
99− lower = Object.keys(node.children)
100− .filter((item) => item < digit)
101− .sort()
102− .at(-1);
103− if (lower !== undefined) candidate = { id: node.children[lower]!, depth: depth + 1 };
104− current = node.children[digit] ?? null;
105− }
106− if (!candidate) return undefined;
107− let { id, depth } = candidate;
108− for (; depth <= 57; depth++) {
109− const node = this.#read(id, depth);
125+ const maximum = (id: number, expected: string): T => { 126+ const node = this.#read(id, expected); 110127 if (node.kind === "leaf") return this.#decode(structuredClone(node.value)); 111− id = node.children[Object.keys(node.children).sort().at(-1)!]!;
112− }
113− throw Error("invalid priority depth");
128+ const digit = Object.keys(node.children).sort().at(-1)!; 129+ return maximum(node.children[digit]!, node.prefix + digit); 131+ const visit = (id: Page, expected: string): T | undefined => { 132+ if (id === null) return undefined; 133+ const node = this.#read(id, expected); 134+ if (!key.startsWith(node.prefix)) return node.prefix < key ? maximum(id, expected) : undefined; 135+ if (node.kind === "leaf") return undefined; 136+ const digit = key[node.prefix.length]!; 137+ const same = node.children[digit]; 138+ if (same !== undefined) { 139+ const found = visit(same, node.prefix + digit); 140+ if (found !== undefined) return found; 142+ const lower = Object.keys(node.children) 143+ .filter((d) => d < digit) 146+ return lower === undefined ? undefined : maximum(node.children[lower]!, node.prefix + lower); 148+ return visit(root, "");