Versions 32 to 318
new file, 150 lines
1+import type { GraphStateStorage } from "../state-source.ts"; 3+type Page = number | null; 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); 9+function check(root: Page, key: string): void { 10+ if ((root !== null && !safe(root)) || !/^[0-9a-f]{57}$/.test(key)) throw Error("invalid priority lookup"); 12+/** Immutable compressed priority paths. Reads legacy depth pages; writes format 2. */ 13+export class ProjectionPriority<T> { 14+ readonly #storage: GraphStateStorage; 15+ readonly #collection: string; 16+ readonly #decode: (value: unknown) => T; 17+ constructor(storage: GraphStateStorage, namespace: string, decode: (value: unknown) => T) { 18+ if (!namespace || namespace.length > 4096) throw Error("invalid priority namespace"); 19+ this.#storage = storage; 20+ this.#collection = `experimental-priority:${JSON.stringify(namespace)}`; 21+ this.#decode = decode; 23+ #read(id: number, expected: string): Node { 24+ if (!safe(id) || expected.length > 57) throw Error("invalid priority reference"); 25+ const row = this.#storage.read(this.#collection, String(id)); 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 }; 40+ if (row.kind !== "branch" || prefix.length >= 57 || !object(row.children)) throw Error("invalid priority branch"); 41+ const entries = Object.entries(row.children); 42+ if (entries.length < (legacy ? 1 : 2) || entries.length > 16) throw Error("invalid priority fanout"); 43+ for (const [digit, child] of entries) 44+ if (!/^[0-9a-f]$/.test(digit) || !safe(child) || child === id) throw Error("invalid priority child"); 45+ return { format: 2, kind: "branch", prefix, children: row.children as Record<string, number> }; 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); 70+ insert(root: Page, key: string, value: T): number { 72+ const checked = structuredClone(this.#decode(structuredClone(value))); 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 } }); 96+ return visit(root, ""); 99+ remove(root: Page, key: string): Page { 101+ return this.#allocate((write) => { 102+ const visit = (id: Page, expected: string): Page => { 103+ if (id === null) throw Error("missing priority key"); 104+ const node = this.#read(id, expected); 105+ if (!key.startsWith(node.prefix)) throw Error("missing priority key"); 106+ if (node.kind === "leaf") return null; 107+ const digit = key[node.prefix.length]!, 108+ child = visit(node.children[digit] ?? null, node.prefix + digit), 109+ children = { ...node.children }; 110+ if (child === null) delete children[digit]; 111+ else children[digit] = child; 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 }); 120+ return visit(root, ""); 123+ before(root: Page, key: string): T | undefined { 125+ const maximum = (id: number, expected: string): T => { 126+ const node = this.#read(id, expected); 127+ if (node.kind === "leaf") return this.#decode(structuredClone(node.value)); 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, "");