Page per-file atom indexes and stream current-view attribute scans
of shared·codex-capacity-chair··Not checked
Version 118 · Files at version 119
4 files, 109 lines added, 7 removed.
--- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -15,8 +15,15 @@
18−import { type GraphStateStorage, type StateMap, storedArray, storedMap, storedNumbers } from "./state-source.ts";
19+ type GraphStateStorage, 25+} from "./state-source.ts"; @@ -314,7 +321,7 @@
314321 this.#dependents = storedMap(storage, "dependents"); 315322 this.#resolvers = storedMap(storage, "resolvers"); 316323 this.#together = storedMap(storage, "together"); 317− this.#fileAtoms = storedMap(storage, "file-atoms");
324+ this.#fileAtomPages = storedLists<AtomId>(storage, "file-atoms"); 318325 this.#runStarts = storedMap(storage, "run-starts"); 319326 this.#relocations = storedMap(storage, "relocations"); 320327 this.#relocatedTo = storedMap(storage, "relocated-to"); @@ -420,6 +427,7 @@
420427 #resolvers: StateMap<AtomId, AtomId | AtomId[]> = new Map(); 421428 #together: StateMap<AtomId, TogetherLink[]> = new Map(); 422429 #fileAtoms: StateMap<string, AtomId[]> = new Map(); 430+ #fileAtomPages: ReturnType<typeof storedLists<AtomId>> | undefined; 423431 /** First lines of runs (their n), by the node they were written after: a line's n, or -1 - a path's index. */ 424432 #runStarts: StateMap<number, number | number[]> = new Map(); 425433 /** 1 for the first line of a run whose atom deleted the line it hangs off: the run replaces it. */ @@ -702,12 +710,20 @@
703711 /** Every atom that inserts or deletes a line of the file, in seq order. */ 704712 atomsIn(file: string): readonly AtomId[] { 713+ if (this.#fileAtomPages) return [...this.#fileAtomPages.values(file)]; 705714 return this.#fileAtoms.get(file) ?? EMPTY; 716+ *eachAtomIn(file: string): IterableIterator<AtomId> { 717+ if (this.#fileAtomPages) yield* this.#fileAtomPages.values(file); 718+ else yield* this.#fileAtoms.get(file) ?? EMPTY; 720+ hasAtomsIn(file: string): boolean { 721+ return this.#fileAtomPages ? this.#fileAtomPages.length(file) > 0 : this.#fileAtoms.has(file); 708724 /** Unique historical file paths, streamed without collecting their names. */ 709725 *knownFiles(): IterableIterator<string> { 710− yield* this.#fileAtoms.keys();
726+ yield* this.#fileAtomPages ? this.#fileAtomPages.keys() : this.#fileAtoms.keys(); @@ -981,7 +997,10 @@
981997 files.add(isStartNode(m.parent) ? m.parent.slice(1) : this.lineFile(m.parent)!); 983999 this.#atomFiles.push(Object.freeze([...files].sort())); 984− for (const file of files) pushIndex(this.#fileAtoms, file, frozen.id);
1000+ for (const file of files) { 1001+ if (this.#fileAtomPages) this.#fileAtomPages.append(file, frozen.id); 1002+ else pushIndex(this.#fileAtoms, file, frozen.id); 9851004 for (const r of frozen.reasons) { 9861005 if (r.kind !== "together" || r.to === frozen.id) continue; 9871006 pushIndex(this.#together, frozen.id, { other: r.to, text: r.text, from: frozen.id }); --- a/src/engine/state-source.ts
+++ b/src/engine/state-source.ts
@@ -259,6 +259,56 @@
259259 values(): IterableIterator<V>; 262+/** Append-only per-key lists, retaining at most one fixed-size page per iterator. */ 263+export function storedLists<V>(storage: GraphStateStorage, name: string) { 264+ const pageSize = 256; 265+ // The previous experimental layout stored one whole historical list per key. 266+ const length = (key: string) => { 267+ const stored = storage.read(`${name}:lengths`, key) as number | undefined; 268+ if (stored === undefined && storage.read(name, JSON.stringify(["string", key])) !== undefined) 269+ throw new Error(`writer list index ${name} requires canonical-log adoption`); 270+ const count = stored ?? 0; 271+ if (!Number.isSafeInteger(count) || count < 0) throw new Error(`invalid writer list count in ${name}`); 274+ const pageKey = (key: string, page: number) => JSON.stringify([key, page]); 275+ const page = (key: string, index: number): V[] => { 276+ const values = storage.read(`${name}:pages`, pageKey(key, index)) as V[] | undefined; 277+ if (!Array.isArray(values) || values.length < 1 || values.length > pageSize) 278+ throw new Error(`missing or invalid writer list page in ${name}`); 283+ *keys(): IterableIterator<string> { 284+ for (const entry of storage.entries(name)) { 286+ throw new Error(`writer list index ${name} requires canonical-log adoption`); 288+ for (const [key] of storage.entries(`${name}:lengths`)) yield key; 290+ *values(key: string): IterableIterator<V> { 291+ const count = length(key); 292+ for (let start = 0; start < count; start += pageSize) { 293+ const values = page(key, start / pageSize); 294+ if (values.length !== Math.min(pageSize, count - start)) 295+ throw new Error(`incomplete writer list page in ${name}`); 299+ append(key: string, value: V): void { 300+ const count = length(key); 301+ const offset = count % pageSize; 302+ const index = Math.floor(count / pageSize); 303+ const values = offset === 0 ? [] : page(key, index); 304+ if (values.length !== offset) throw new Error(`invalid writer list length in ${name}`); 306+ storage.write(`${name}:pages`, pageKey(key, index), values); 307+ storage.write(`${name}:lengths`, key, count + 1); 262312/** Keys are tagged so numeric graph codes cannot collide with string keys. */ 263313export function storedMap<K extends string | number, V>(storage: GraphStateStorage, name: string): StateMap<K, V> { 264314 const keyOf = (key: K) => JSON.stringify([typeof key, key]); --- a/src/engine/test/state-source.test.ts
+++ b/src/engine/test/state-source.test.ts
@@ -1,10 +1,43 @@
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";
4+import { CachedStateStorage, storedLists, storedNumbers } from "../state-source.ts"; 55import { lines, Repo } from "./helpers.ts"; 66import { SqlState } from "./storage-helper.ts"; 8+test("per-file list pages append and reopen without reading the full historical list", () => { 9+ const source = new SqlState(); 11+ const storage = new CachedStateStorage(source); 12+ const lists = storedLists<string>(storage, "files"); 13+ for (let i = 0; i < 1025; i++) storage.transaction(() => lists.append("a.txt", `a${i}`)); 14+ assert.equal(lists.length("a.txt"), 1025); 15+ assert.deepEqual([...lists.keys()], ["a.txt"]); 17+ const before = source.reads; 18+ storage.transaction(() => lists.append("a.txt", "a1025")); 19+ assert.ok(source.reads - before <= 2, "append reads only one page and its length"); 22+ storage.transaction(() => { 23+ lists.append("a.txt", "rolled-back"); 24+ throw new Error("rollback"); 28+ const reopened = storedLists<string>(new CachedStateStorage(source), "files"); 30+ [...reopened.values("a.txt")], 31+ Array.from({ length: 1026 }, (_, i) => `a${i}`), 33+ for (const [, page] of source.entries("files:pages")) assert.ok((page as string[]).length <= 256); 34+ source.write("legacy", "a.txt", ["a0"]); 35+ assert.throws(() => [...storedLists(source, "legacy").keys()], /canonical-log adoption/); 841test("numeric pages cross boundaries, reopen, mutate and roll back without retaining history", () => { 942 const source = new SqlState(); 1043 const storage = new CachedStateStorage(source); --- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -330,7 +330,7 @@
330330 const graph = this.graph; 331331 // For each attribute, the highest-priority setter in the set wins. 332332 const setters = new Map<string, { id: AtomId; value: string | null }>(); 333− for (const id of graph.atomsIn(path)) {
333+ for (const id of graph.eachAtomIn(path)) { 334334 if (!this.#has(graph.atom(id).seq)) continue; 335335 for (const a of graph.atom(id).attrs ?? []) { 336336 if (a.file !== path) continue; @@ -553,7 +553,7 @@
553553 #file(path: string, complete = false): FileState { 554554 let state = this.#files.get(path); 555555 if (!state || (complete && !state.complete)) { 556− state = this.graph.atomsIn(path).length === 0 ? NO_FILE : this.#layout(path, complete);
556+ state = this.graph.hasAtomsIn(path) ? this.#layout(path, complete) : NO_FILE; 557557 this.#files.set(path, state);