Page persistent claim indexes and stream dependency queries
of shared·codex-capacity-chair··Not checked
Version 119 · Files at version 120
5 files, 87 lines added, 15 removed.
--- a/src/engine/build.ts
+++ b/src/engine/build.ts
@@ -353,10 +353,10 @@
353353 // An attribute builds on whoever set it last in the view, the way an insertion builds on a gap's writers; 354354 // a move builds on the earlier moves of its lines. 355355 if (kind !== "attr" && !isStartNode(node)) deps.add(writerOf(node)); 356− if (kind !== "line") for (const writer of graph.writersOf(key)) if (view.set.has(writer)) deps.add(writer);
356+ if (kind !== "line") for (const writer of graph.eachWriterOf(key)) if (view.set.has(writer)) deps.add(writer); 358358 for (const key of parts.writes) { 359− for (const reader of graph.readersOf(key)) if (view.set.has(reader)) deps.add(reader);
359+ for (const reader of graph.eachReaderOf(key)) if (view.set.has(reader)) deps.add(reader); 361361 for (const id of parts.needs) deps.add(id); 362362 for (const id of parts.resolves) deps.add(id); --- a/src/engine/conflicts.ts
+++ b/src/engine/conflicts.ts
@@ -138,7 +138,7 @@
138138/** Atoms in the set that claim (write, read or hold) a key. */ 139139function claimersIn(graph: Graph, set: AtomSet, key: Key): AtomId[] { 140140 const out: AtomId[] = []; 141− for (const list of [graph.writersOf(key), graph.readersOf(key), graph.holdersOf(key)]) {
141+ for (const list of [graph.eachWriterOf(key), graph.eachReaderOf(key), graph.eachHolderOf(key)]) { 142142 for (const id of list) if (set.has(id)) out.push(id); @@ -302,7 +302,7 @@
302302 const below = graph.closureOf(draft.deps); 303303 const candidates = new Set<AtomId>(); 304304 for (const key of draft.writes) for (const id of claimersIn(graph, set, key)) candidates.add(id); 305− for (const key of claimsOf(draft)) for (const id of graph.writersOf(key)) if (set.has(id)) candidates.add(id);
305+ for (const key of claimsOf(draft)) for (const id of graph.eachWriterOf(key)) if (set.has(id)) candidates.add(id); 306306 const found: Conflict[] = []; 307307 for (const id of [...candidates].sort(bySeq(graph))) { 308308 const other = graph.atom(id); --- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -315,9 +315,9 @@
315315 this.#lineText = storedArray(storage, "line-text"); 316316 this.#pathIndex = storedMap(storage, "path-index"); 317317 this.#moreDeleters = storedMap(storage, "more-deleters"); 318− this.#writers = storedMap(storage, "writers");
319− this.#readers = storedMap(storage, "readers");
320− this.#holders = storedMap(storage, "holders");
318+ this.#writersPages = storedLists<AtomId>(storage, "writers", (key) => key); 319+ this.#readersPages = storedLists<AtomId>(storage, "readers", (key) => key); 320+ this.#holdersPages = storedLists<AtomId>(storage, "holders", (key) => key); 321321 this.#dependents = storedMap(storage, "dependents"); 322322 this.#resolvers = storedMap(storage, "resolvers"); 323323 this.#together = storedMap(storage, "together"); @@ -421,8 +421,11 @@
421421 #deleter: NumberIndex = new Ints(); 422422 #moreDeleters: StateMap<number, number[]> = new Map(); 423423 #writers: StateMap<Key | number, AtomId | AtomId[]> = new Map(); 424+ #writersPages: ReturnType<typeof storedLists<AtomId>> | undefined; 424425 #readers: StateMap<Key | number, AtomId | AtomId[]> = new Map(); 426+ #readersPages: ReturnType<typeof storedLists<AtomId>> | undefined; 425427 #holders: StateMap<Key | number, AtomId | AtomId[]> = new Map(); 428+ #holdersPages: ReturnType<typeof storedLists<AtomId>> | undefined; 426429 #dependents: StateMap<AtomId, AtomId | AtomId[]> = new Map(); 427430 #resolvers: StateMap<AtomId, AtomId | AtomId[]> = new Map(); 428431 #together: StateMap<AtomId, TogetherLink[]> = new Map(); @@ -695,18 +698,33 @@
697700 writersOf(key: Key): readonly AtomId[] { 698− return listOf(this.#writers.get(this.#code(key)));
701+ return [...this.eachWriterOf(key)]; 703+ *eachWriterOf(key: Key): IterableIterator<AtomId> { 704+ const code = this.#code(key); 705+ if (this.#writersPages) yield* this.#writersPages.values(JSON.stringify([typeof code, code])); 706+ else yield* listOf(this.#writers.get(code)); 701709 /** Atoms whose implicit reads include the key. */ 702710 readersOf(key: Key): readonly AtomId[] { 703− return listOf(this.#readers.get(this.#code(key)));
711+ return [...this.eachReaderOf(key)]; 713+ *eachReaderOf(key: Key): IterableIterator<AtomId> { 714+ const code = this.#code(key); 715+ if (this.#readersPages) yield* this.#readersPages.values(JSON.stringify([typeof code, code])); 716+ else yield* listOf(this.#readers.get(code)); 706719 /** Atoms whose explicit holds include the key. */ 707720 holdersOf(key: Key): readonly AtomId[] { 708− return listOf(this.#holders.get(this.#code(key)));
721+ return [...this.eachHolderOf(key)]; 723+ *eachHolderOf(key: Key): IterableIterator<AtomId> { 724+ const code = this.#code(key); 725+ if (this.#holdersPages) yield* this.#holdersPages.values(JSON.stringify([typeof code, code])); 726+ else yield* listOf(this.#holders.get(code)); 711729 /** Every atom that inserts or deletes a line of the file, in seq order. */ 712730 atomsIn(file: string): readonly AtomId[] { @@ -983,9 +1001,21 @@
9831001 if (this.#deleter.at(n) < 0) this.#deleter.set(n, seq); 9841002 else pushIndex(this.#moreDeleters, n, seq); 986− for (const key of new Set(atom.writes)) pushOne(this.#writers, this.#code(key), frozen.id);
987− for (const key of new Set(atom.reads)) pushOne(this.#readers, this.#code(key), frozen.id);
988− for (const key of new Set(atom.holds)) pushOne(this.#holders, this.#code(key), frozen.id);
1004+ for (const key of new Set(atom.writes)) { 1005+ const code = this.#code(key); 1006+ if (this.#writersPages) this.#writersPages.append(JSON.stringify([typeof code, code]), frozen.id); 1007+ else pushOne(this.#writers, code, frozen.id); 1009+ for (const key of new Set(atom.reads)) { 1010+ const code = this.#code(key); 1011+ if (this.#readersPages) this.#readersPages.append(JSON.stringify([typeof code, code]), frozen.id); 1012+ else pushOne(this.#readers, code, frozen.id); 1014+ for (const key of new Set(atom.holds)) { 1015+ const code = this.#code(key); 1016+ if (this.#holdersPages) this.#holdersPages.append(JSON.stringify([typeof code, code]), frozen.id); 1017+ else pushOne(this.#holders, code, frozen.id); 9891019 for (const d of deps) pushOne(this.#dependents, d, frozen.id); 9901020 for (const r of new Set(frozen.resolves)) pushOne(this.#resolvers, r, frozen.id); 9911021 const files = new Set<string>(); --- a/src/engine/state-source.ts
+++ b/src/engine/state-source.ts
@@ -260,12 +260,16 @@
262262/** Append-only per-key lists, retaining at most one fixed-size page per iterator. */ 263−export function storedLists<V>(storage: GraphStateStorage, name: string) {
263+export function storedLists<V>( 264+ storage: GraphStateStorage, 266+ legacyKey = (key: string) => JSON.stringify(["string", key]), 264268 const pageSize = 256; 265269 // The previous experimental layout stored one whole historical list per key. 266270 const length = (key: string) => { 267271 const stored = storage.read(`${name}:lengths`, key) as number | undefined; 268− if (stored === undefined && storage.read(name, JSON.stringify(["string", key])) !== undefined)
272+ if (stored === undefined && storage.read(name, legacyKey(key)) !== undefined) 269273 throw new Error(`writer list index ${name} requires canonical-log adoption`); 270274 const count = stored ?? 0; 271275 if (!Number.isSafeInteger(count) || count < 0) throw new Error(`invalid writer list count in ${name}`); --- a/src/engine/test/state-source.test.ts
+++ b/src/engine/test/state-source.test.ts
@@ -5,6 +5,44 @@
55import { lines, Repo } from "./helpers.ts"; 66import { SqlState } from "./storage-helper.ts"; 8+test("hot claim indexes stream bounded pages after reopening", (t) => { 9+ const oracle = Repo.init({ "a.txt": lines("one", "two") }); 10+ const draft = oracle.draft({ edits: [{ file: "a.txt", old: "one", new: "ONE" }] }); 11+ const source = new SqlState(); 13+ const graph = new Graph({ storage: source }); 14+ graph.add(oracle.graph.atom("a0")); 15+ for (let i = 0; i < 513; i++) { 16+ const atom = { ...draft, id: `a${i + 1}`, seq: i + 1 }; 18+ oracle.graph.add(atom); 20+ const reopened = new Graph({ storage: source }); 21+ const read = source.read.bind(source); 23+ t.mock.method(source, "read", (collection: string, key: string) => { 24+ if (collection === "writers:pages") pagesRead++; 25+ return read(collection, key); 27+ const iterator = reopened.eachWriterOf("L:a0.0"); 28+ assert.equal(iterator.next().value, "a1"); 29+ assert.equal(pagesRead, 1, "first claimant reads one claim page, not the entire hot-key history"); 30+ assert.deepEqual(reopened.writersOf("L:a0.0"), oracle.graph.writersOf("L:a0.0")); 31+ for (const name of ["writers", "readers", "holders"]) { 32+ assert.equal([...source.entries(name)].length, 0, "no whole-list claim rows remain"); 33+ for (const [, page] of source.entries(`${name}:pages`)) assert.ok((page as string[]).length <= 256); 35+ const key = JSON.stringify(["number", 0]); 36+ source.write("legacy-claims", key, ["old"]); 38+ () => [...storedLists(source, "legacy-claims", (value) => value).values(key)], 39+ /canonical-log adoption/, 846test("per-file list pages append and reopen without reading the full historical list", () => { 947 const source = new SqlState();