Page disk-first numeric graph indexes under bounded storage cache
of shared·codex-capacity-chair··Not checked
Version 112 · Files at version 113
3 files, 99 lines added, 16 removed.
--- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -15,7 +15,7 @@
18−import { type GraphStateStorage, type StateMap, storedArray, storedMap } from "./state-source.ts";
18+import { type GraphStateStorage, type StateMap, storedArray, storedMap, storedNumbers } from "./state-source.ts"; @@ -286,21 +286,7 @@
286286 this.#storage = options.storage; 287287 if (options.storage) { 288288 const storage = options.storage; 289− const numbers = (name: string): NumberIndex => {
290− const values = storedArray<number>(storage, name);
291− return {
292− get length() {
293− return values.length;
294− },
295− push: (value) => {
296− values.push(value);
297− },
298− at: (index) => values[index]!,
299− set: (index, value) => {
300− values[index] = value;
301− },
302− };
303− };
289+ const numbers = (name: string): NumberIndex => storedNumbers(storage, name); 304290 this.#atoms = storedArray<Atom>(storage, "atoms", (atom) => --- a/src/engine/state-source.ts
+++ b/src/engine/state-source.ts
@@ -160,6 +160,56 @@
160160const indexKey = (key: PropertyKey): key is string => 161161 typeof key === "string" && /^(0|[1-9][0-9]*)$/.test(key) && Number.isSafeInteger(Number(key)); 163+/** Fixed-size numeric pages; callers retain no pages outside the bounded storage cache. */ 164+export function storedNumbers(storage: GraphStateStorage, name: string) { 165+ const pageSize = 256; 166+ const meta = `${name}:meta`; 167+ const length = () => (storage.read(meta, "length") as number | undefined) ?? 0; 168+ // Older experimental writer indexes must be rebuilt from their canonical log. 169+ // Never interpret per-element rows as pages or silently lose existing history. 170+ const page = (index: number): number[] => { 171+ if (storage.read(meta, "format") !== 2) 172+ throw new Error(`writer numeric index ${name} requires canonical-log adoption`); 173+ const values = storage.read(`${name}:pages`, String(Math.floor(index / pageSize))) as number[] | undefined; 174+ if (!values) throw new Error(`missing writer numeric page in ${name}`); 181+ at(index: number): number { 182+ if (!Number.isSafeInteger(index) || index < 0 || index >= length()) 183+ throw new Error(`writer numeric index out of bounds in ${name}`); 184+ const value = page(index)[index % pageSize]; 185+ if (value === undefined) throw new Error(`incomplete writer numeric page in ${name}`); 188+ push(value: number): void { 189+ if (!Number.isSafeInteger(value)) throw new Error("writer numeric indexes require safe integers"); 190+ const index = length(); 191+ if (index > 0 && storage.read(meta, "format") !== 2) 192+ throw new Error(`writer numeric index ${name} requires canonical-log adoption`); 193+ const offset = index % pageSize; 194+ const values = offset === 0 ? [] : page(index); 195+ if (values.length !== offset) throw new Error(`invalid writer numeric page length in ${name}`); 197+ storage.write(`${name}:pages`, String(Math.floor(index / pageSize)), values); 198+ if (index === 0) storage.write(meta, "format", 2); 199+ storage.write(meta, "length", index + 1); 201+ set(index: number, value: number): void { 202+ if (!Number.isSafeInteger(value)) throw new Error("writer numeric indexes require safe integers"); 203+ if (!Number.isSafeInteger(index) || index < 0 || index >= length()) 204+ throw new Error(`writer numeric index out of bounds in ${name}`); 205+ const values = page(index); 206+ if (index % pageSize >= values.length) throw new Error(`incomplete writer numeric page in ${name}`); 207+ values[index % pageSize] = value; 208+ storage.write(`${name}:pages`, String(Math.floor(index / pageSize)), values); 163213/** Array-shaped access without retaining its historical values in memory. */ 164214export function storedArray<T>(storage: GraphStateStorage, name: string, decode?: (value: T) => T): T[] { 165215 const length = () => (storage.read(`${name}:meta`, "length") as number | undefined) ?? 0; --- a/src/engine/test/state-source.test.ts
+++ b/src/engine/test/state-source.test.ts
@@ -1,9 +1,56 @@
11import assert from "node:assert/strict"; 22import { test } from "node:test"; 33import { checkDraft, Graph, View } from "../index.ts"; 4+import { CachedStateStorage, storedNumbers } from "../state-source.ts"; 45import { lines, Repo } from "./helpers.ts"; 56import { SqlState } from "./storage-helper.ts"; 8+test("numeric pages cross boundaries, reopen, mutate and roll back without retaining history", () => { 9+ const source = new SqlState(); 10+ const storage = new CachedStateStorage(source); 11+ const numbers = storedNumbers(storage, "numbers"); 12+ storage.transaction(() => { 13+ for (let i = 0; i < 1025; i++) numbers.push(i - 1); 16+ const reads = source.reads; 17+ for (let i = 0; i < 1025; i++) assert.equal(numbers.at(i), i - 1); 18+ assert.ok(source.reads - reads <= 7, "one backing read per page plus metadata"); 19+ assert.equal([...source.entries("numbers:pages")].length, 5); 22+ storage.transaction(() => { 23+ numbers.set(255, -99); 24+ numbers.set(256, -98); 26+ throw new Error("rollback"); 30+ const reopened = storedNumbers(new CachedStateStorage(source), "numbers"); 31+ assert.equal(reopened.length, 1025); 32+ assert.equal(reopened.at(255), 254); 33+ assert.equal(reopened.at(256), 255); 34+ assert.equal(numbers.at(255), 254); 35+ reopened.set(256, -42); 36+ assert.equal(reopened.at(256), -42); 37+ assert.throws(() => reopened.at(1025), /out of bounds/); 38+ assert.throws(() => reopened.set(-1, 2), /out of bounds/); 39+ assert.throws(() => reopened.push(NaN), /safe integers/); 42+test("numeric pages reject legacy or missing pages instead of fabricating values", () => { 43+ const storage = new SqlState(); 44+ storage.write("legacy:meta", "length", 256); 45+ const legacy = storedNumbers(storage, "legacy"); 46+ assert.throws(() => legacy.at(0), /canonical-log adoption/); 47+ assert.throws(() => legacy.push(1), /canonical-log adoption/); 48+ const numbers = storedNumbers(storage, "new"); 50+ storage.remove("new:pages", "0"); 51+ assert.throws(() => numbers.at(0), /missing writer numeric page/); 754test("stored graph reopens without replay and preserves moves, conflicts and new edits", () => { 855 const oracle = Repo.init({ "a.txt": lines("one", "two", "three"), "b.txt": "" }); 956 oracle.edit({ edits: [{ file: "a.txt", old: "two", new: "TWO" }] });