{"draft":null,"from":142,"to":143,"files":[{"path":"src/engine/experimental/projection-priority.ts","added":115,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/experimental/projection-priority.ts\n@@ -0,0 +1,115 @@\n+import type { GraphStateStorage } from \"../state-source.ts\";\n+\n+type Page = number | null;\n+interface Leaf {\n+\tformat: 1;\n+\tkind: \"leaf\";\n+\tdepth: 57;\n+\tvalue: unknown;\n+}\n+interface Branch {\n+\tformat: 1;\n+\tkind: \"branch\";\n+\tdepth: number;\n+\tchildren: Record<string, number>;\n+}\n+type Node = Leaf | Branch;\n+const safe = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0;\n+const object = (value: unknown): value is Record<string, unknown> =>\n+\tvalue !== null && typeof value === \"object\" && !Array.isArray(value);\n+function check(root: Page, key: string): void {\n+\tif ((root !== null && !safe(root)) || !/^[0-9a-f]{57}$/.test(key)) throw Error(\"invalid priority lookup\");\n+}\n+/** Internal immutable priority experiment. At most 57 levels and 16 children. */\n+export class ProjectionPriority<T> {\n+\treadonly #storage: GraphStateStorage;\n+\treadonly #collection: string;\n+\treadonly #decode: (value: unknown) => T;\n+\tconstructor(storage: GraphStateStorage, namespace: string, decode: (value: unknown) => T) {\n+\t\tif (!namespace || namespace.length > 4096) throw Error(\"invalid priority namespace\");\n+\t\tthis.#storage = storage;\n+\t\tthis.#collection = `experimental-priority:${JSON.stringify(namespace)}`;\n+\t\tthis.#decode = decode;\n+\t}\n+\t#read(id: number, depth: number): Node {\n+\t\tif (!safe(id) || !safe(depth) || depth > 57) throw Error(\"invalid priority reference\");\n+\t\tconst row = this.#storage.read(this.#collection, String(id));\n+\t\tif (!object(row) || row.format !== 1 || row.depth !== depth || Object.keys(row).length !== 4)\n+\t\t\tthrow Error(\"invalid priority node\");\n+\t\tif (depth === 57) {\n+\t\t\tif (row.kind !== \"leaf\" || !(\"value\" in row)) throw Error(\"invalid priority leaf\");\n+\t\t\treturn row as unknown as Leaf;\n+\t\t}\n+\t\tif (row.kind !== \"branch\" || !object(row.children)) throw Error(\"invalid priority branch\");\n+\t\tconst entries = Object.entries(row.children);\n+\t\tif (!entries.length || entries.length > 16) throw Error(\"invalid priority fanout\");\n+\t\tfor (const [digit, child] of entries)\n+\t\t\tif (!/^[0-9a-f]$/.test(digit) || !safe(child) || child === id) throw Error(\"invalid priority child\");\n+\t\treturn row as unknown as Branch;\n+\t}\n+\t#write(node: Node): number {\n+\t\tconst next = this.#storage.read(this.#collection, \"next\") ?? 0;\n+\t\tif (!safe(next) || !Number.isSafeInteger(next + 1)) throw Error(\"invalid priority allocation\");\n+\t\tif (this.#storage.read(this.#collection, String(next)) !== undefined) throw Error(\"priority allocation collision\");\n+\t\tthis.#storage.write(this.#collection, String(next), node);\n+\t\tthis.#storage.write(this.#collection, \"next\", next + 1);\n+\t\treturn next;\n+\t}\n+\tinsert(root: Page, key: string, value: T): number {\n+\t\tcheck(root, key);\n+\t\tconst checked = structuredClone(this.#decode(structuredClone(value)));\n+\t\treturn this.#storage.transaction(() => {\n+\t\t\tconst visit = (id: Page, depth: number): number => {\n+\t\t\t\tconst node = id === null ? undefined : this.#read(id, depth);\n+\t\t\t\tif (depth === 57) return this.#write({ format: 1, kind: \"leaf\", depth: 57, value: checked });\n+\t\t\t\tif (node?.kind === \"leaf\") throw Error(\"invalid priority depth\");\n+\t\t\t\tconst children = node?.children ?? {},\n+\t\t\t\t\tdigit = key[depth]!;\n+\t\t\t\tconst child = visit(children[digit] ?? null, depth + 1);\n+\t\t\t\treturn this.#write({ format: 1, kind: \"branch\", depth, children: { ...children, [digit]: child } });\n+\t\t\t};\n+\t\t\treturn visit(root, 0);\n+\t\t});\n+\t}\n+\tremove(root: Page, key: string): Page {\n+\t\tcheck(root, key);\n+\t\treturn this.#storage.transaction(() => {\n+\t\t\tconst visit = (id: Page, depth: number): Page => {\n+\t\t\t\tif (id === null) throw Error(\"missing priority key\");\n+\t\t\t\tconst node = this.#read(id, depth);\n+\t\t\t\tif (node.kind === \"leaf\") return null;\n+\t\t\t\tconst digit = key[depth]!,\n+\t\t\t\t\tchild = visit(node.children[digit] ?? null, depth + 1),\n+\t\t\t\t\tchildren = { ...node.children };\n+\t\t\t\tif (child === null) delete children[digit];\n+\t\t\t\telse children[digit] = child;\n+\t\t\t\treturn Object.keys(children).length ? this.#write({ ...node, children }) : null;\n+\t\t\t};\n+\t\t\treturn visit(root, 0);\n+\t\t});\n+\t}\n+\tbefore(root: Page, key: string): T | undefined {\n+\t\tcheck(root, key);\n+\t\tlet current = root;\n+\t\tlet candidate: { id: number; depth: number } | undefined;\n+\t\tfor (let depth = 0; current !== null && depth < 57; depth++) {\n+\t\t\tconst node = this.#read(current, depth);\n+\t\t\tif (node.kind !== \"branch\") throw Error(\"invalid priority depth\");\n+\t\t\tconst digit = key[depth]!,\n+\t\t\t\tlower = Object.keys(node.children)\n+\t\t\t\t\t.filter((item) => item < digit)\n+\t\t\t\t\t.sort()\n+\t\t\t\t\t.at(-1);\n+\t\t\tif (lower !== undefined) candidate = { id: node.children[lower]!, depth: depth + 1 };\n+\t\t\tcurrent = node.children[digit] ?? null;\n+\t\t}\n+\t\tif (!candidate) return undefined;\n+\t\tlet { id, depth } = candidate;\n+\t\tfor (; depth <= 57; depth++) {\n+\t\t\tconst node = this.#read(id, depth);\n+\t\t\tif (node.kind === \"leaf\") return this.#decode(structuredClone(node.value));\n+\t\t\tid = node.children[Object.keys(node.children).sort().at(-1)!]!;\n+\t\t}\n+\t\tthrow Error(\"invalid priority depth\");\n+\t}\n+}"}],"unmatched":[]}