- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/experimental/projection-overlay-storage.ts?format=json
79 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 144, 2 versions changed it
1import type { GraphStateStorage } from "../state-source.ts";
3type OverlayRow = { collection: string; key: string; present: boolean; value?: unknown };
5/** Disk-backed temporary pages over immutable committed projection pages. */
6export class ProjectionOverlayStorage implements GraphStateStorage {
7 readonly #base: GraphStateStorage;
8 readonly #collection: string;
9 readonly #clearCollection?: (collection: string) => void;
10 #open = true;
11 constructor(base: GraphStateStorage, namespace: string, clearCollection?: (collection: string) => void) {
12 if (!namespace) throw Error("missing projection overlay namespace");
13 this.#base = base;
14 this.#collection = `projection:temporary:${namespace}`;
15 this.#clearCollection = clearCollection;
16 for (const _ of base.entries(this.#collection)) throw Error("projection overlay namespace is occupied");
17 }
18 #check(): void {
19 if (!this.#open) throw Error("closed projection overlay");
20 }
21 #key(collection: string, key: string): string {
22 return JSON.stringify([collection, key]);
23 }
24 read(collection: string, key: string): unknown {
25 this.#check();
26 const row = this.#base.read(this.#collection, this.#key(collection, key)) as OverlayRow | undefined;
27 return row ? (row.present ? row.value : undefined) : this.#base.read(collection, key);
28 }
29 write(collection: string, key: string, value: unknown): void {
30 this.#check();
31 this.#base.write(this.#collection, this.#key(collection, key), { collection, key, present: true, value });
32 }
33 remove(collection: string, key: string): void {
34 this.#check();
35 this.#base.write(this.#collection, this.#key(collection, key), { collection, key, present: false });
36 }
37 *entries(collection: string): Iterable<readonly [string, unknown]> {
38 this.#check();
39 for (const [key, value] of this.#rows(collection)) {
40 this.#check();
41 if (!this.#base.read(this.#collection, this.#key(collection, key))) yield [key, value];
42 }
43 for (const [, value] of this.#rows(this.#collection)) {
44 this.#check();
45 const row = value as OverlayRow;
46 if (row.collection === collection && row.present) yield [row.key, row.value];
47 }
48 this.#check();
49 }
50 *#rows(collection: string): Iterable<readonly [string, unknown]> {
51 this.#check();
52 const iterator = this.#base.entries(collection)[Symbol.iterator]();
53 while (true) {
54 this.#check();
55 const next = iterator.next();
56 this.#check();
57 if (next.done) return;
58 yield next.value;
59 }
60 }
61 transaction<T>(body: () => T): T {
62 this.#check();
63 return this.#base.transaction(body);
64 }
65 /** Invoke before the owning outer transaction commits; failed cleanup must abort it. */
66 close(): void {
67 if (!this.#open) return;
68 this.#base.transaction(() => {
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");
73 } else {
74 for (const [key] of this.#base.entries(this.#collection)) this.#base.remove(this.#collection, key);
75 }
76 });
77 this.#open = false;
78 }
79}