Allow transactional bulk cleanup of temporary projection pages (#16)
of shared·codex-capacity-chair··Not checked
Version 143 · Files at version 144
2 files, 43 lines added, 2 removed.
--- a/src/engine/experimental/projection-overlay-storage.ts
+++ b/src/engine/experimental/projection-overlay-storage.ts
@@ -6,11 +6,13 @@
66export class ProjectionOverlayStorage implements GraphStateStorage { 77 readonly #base: GraphStateStorage; 88 readonly #collection: string; 9+ readonly #clearCollection?: (collection: string) => void; 10− constructor(base: GraphStateStorage, namespace: string) {
11+ constructor(base: GraphStateStorage, namespace: string, clearCollection?: (collection: string) => void) { 1112 if (!namespace) throw Error("missing projection overlay namespace"); 1314 this.#collection = `projection:temporary:${namespace}`; 15+ this.#clearCollection = clearCollection; 1416 for (const _ of base.entries(this.#collection)) throw Error("projection overlay namespace is occupied"); @@ -64,7 +66,13 @@
6567 if (!this.#open) return; 6668 this.#base.transaction(() => { 67− for (const [key] of this.#base.entries(this.#collection)) this.#base.remove(this.#collection, key);
69+ if (this.#clearCollection) { 70+ const result: unknown = this.#clearCollection(this.#collection); 71+ if (result && typeof result === "object" && "then" in result) 72+ throw Error("overlay cleanup must be synchronous"); 74+ for (const [key] of this.#base.entries(this.#collection)) this.#base.remove(this.#collection, key); --- a/src/engine/test/projection-overlay-storage.test.ts
+++ b/src/engine/test/projection-overlay-storage.test.ts
@@ -3,6 +3,39 @@
33import { ProjectionOverlayStorage } from "../experimental/projection-overlay-storage.ts"; 44import { SqlState } from "./storage-helper.ts"; 6+test("host cleanup deletes only its overlay and a failed deletion rolls back without scanning", (t) => { 7+ const storage = new SqlState(); 10+ const overlay = new ProjectionOverlayStorage(storage, "bulk", (collection) => { 11+ assert.equal(collection, "projection:temporary:bulk"); 12+ storage.db.prepare("DELETE FROM state WHERE collection = ?").run(collection); 13+ if (fail) throw Error("cleanup failed after deletion"); 15+ storage.write("pages", "durable", 1); 16+ storage.write("projection:temporary:other", "other", 2); 17+ for (let i = 0; i < 100; i++) overlay.write("pages", String(i), i); 18+ const before = storage.db.prepare("SELECT * FROM state ORDER BY collection,key").all(); 19+ t.mock.method(storage, "entries", () => { 20+ throw Error("cleanup must not enumerate rows"); 22+ assert.throws(() => overlay.close(), /cleanup failed/); 23+ assert.deepEqual(storage.db.prepare("SELECT * FROM state ORDER BY collection,key").all(), before); 24+ assert.equal(overlay.read("pages", "99"), 99); 27+ assert.equal(storage.read("pages", "durable"), 1); 28+ assert.equal(storage.read("projection:temporary:other", "other"), 2); 30+ storage.db.prepare("SELECT COUNT(*) AS n FROM state WHERE collection = ?").get("projection:temporary:bulk")!.n, 33+ assert.throws(() => overlay.read("pages", "99"), /closed/); 639test("temporary projection pages shadow committed pages and cleanup preserves durable writes", () => { 740 const storage = new SqlState();