Batch immutable locator writes for sequence updates
of shared·codex-capacity-chair··Not checked
Version 146 · Files at version 147
3 files, 107 lines added, 1 removed.
--- a/src/engine/experimental/projection-locator.ts
+++ b/src/engine/experimental/projection-locator.ts
@@ -63,6 +63,46 @@
6363 this.#storage.write(this.#collection, "next", next + 1); 66+ /** Persist only pages reachable from the final root of a bounded update batch. */ 67+ setMany(root: Page, values: ReadonlyArray<readonly [number, T]>): Page { 69+ if (values.length > 256) throw Error("locator batch limit"); 70+ return this.#storage.transaction(() => { 71+ const staged = new Map<string, unknown>(); 72+ const storage: GraphStateStorage = { 73+ read: (collection, key) => 74+ collection === this.#collection && staged.has(key) ? staged.get(key) : this.#storage.read(collection, key), 75+ write: (collection, key, value) => { 76+ if (collection !== this.#collection) throw Error("unexpected batch collection"); 77+ staged.set(key, value); 80+ throw Error("unexpected batch removal"); 83+ throw Error("unexpected batch scan"); 85+ transaction: (body) => body(), 87+ // The namespace is encoded into the collection at construction. 88+ const namespace = JSON.parse(this.#collection.slice("experimental-locator:".length)) as string; 89+ const index = new ProjectionLocator<T>(storage, namespace, this.#decode); 91+ for (const [id, value] of values) result = index.set(result, id, value); 92+ const retained = new Set<number>(); 93+ const visit = (id: Page): void => { 94+ if (id === null || retained.has(id) || !staged.has(String(id))) return; 96+ const node = staged.get(String(id)) as Node; 97+ if (node.kind === "branch") for (const child of Object.values(node.children)) visit(child); 100+ for (const id of retained) this.#storage.write(this.#collection, String(id), staged.get(String(id))); 101+ if (staged.has("next")) this.#storage.write(this.#collection, "next", staged.get("next")); 66106 get(root: Page, id: number): T | undefined { 68108 const key = keyOf(id); --- a/src/engine/experimental/projection-sequence.ts
+++ b/src/engine/experimental/projection-sequence.ts
@@ -300,7 +300,17 @@
302302 } else throw Error("unknown operation"); 303− for (const [id, node] of changed) map = this.set(map, id, node);
303+ // Keep locator staging bounded even if a valid split/merge changes more 304+ // than one batch. The enclosing transaction commits all batches together. 305+ let batch: Array<readonly [number, SequenceNode]> = []; 306+ for (const entry of changed) { 308+ if (batch.length === 256) { 309+ map = this.#locator.setMany(map, batch); 313+ map = this.#locator.setMany(map, batch); 304314 return { map, root, next }; --- a/src/engine/test/projection-locator.test.ts
+++ b/src/engine/test/projection-locator.test.ts
@@ -47,3 +47,59 @@
51+test("bounded locator batches preserve old roots, discard intermediate pages, and roll back writes", () => { 52+ const db = new SqlState(); 54+ const index = new ProjectionLocator(db, "batch", decode); 55+ const old = index.set(null, 0, 1); 56+ const values = Array.from({ length: 64 }, (_, i) => [i, i + 10] as const); 57+ const before = db.writes; 58+ const root = index.setMany(old, values); 59+ const batchWrites = db.writes - before; 60+ assert.equal(index.get(old, 0), 1); 61+ for (const [id, value] of values) assert.equal(index.get(root, id), value); 62+ const plain = new ProjectionLocator(db, "plain", decode); 63+ let sequential: number | null = plain.set(null, 0, 1); 64+ const baseline = db.writes; 65+ for (const [id, value] of values) sequential = plain.set(sequential, id, value); 66+ assert.ok(batchWrites < (db.writes - baseline) / 2); 67+ const rows = db.db.prepare("SELECT * FROM state ORDER BY collection,key").all(); 68+ // Fail every persisted page and the final allocation-counter write. 69+ for (let offset = 1; offset <= batchWrites; offset++) { 70+ db.failWrite = db.writes + offset; 71+ assert.throws(() => index.setMany(old, values), /injected/); 72+ assert.deepEqual(db.db.prepare("SELECT * FROM state ORDER BY collection,key").all(), rows); 74+ db.failWrite = Infinity; 79+ Array.from({ length: 257 }, () => [0, 1] as const), 83+ assert.equal(index.setMany(root, []), root); 84+ const duplicate = index.setMany(root, [ 89+ assert.equal(index.get(duplicate, 0), 200); 90+ assert.equal(index.get(duplicate, 0xffffffff), 300); 91+ assert.equal(index.get(root, 0), 10); 92+ const stable = db.db.prepare("SELECT * FROM state ORDER BY collection,key").all(); 95+ index.setMany(duplicate, [ 101+ assert.deepEqual(db.db.prepare("SELECT * FROM state ORDER BY collection,key").all(), stable);