Page persistent relocation indexes and stream projection reads
of shared·codex-capacity-chair··Not checked
Version 126 · Files at version 127
3 files, 71 lines added, 12 removed.
--- a/src/engine/graph.ts
+++ b/src/engine/graph.ts
@@ -323,9 +323,11 @@
323323 this.#together = storedMap(storage, "together"); 324324 this.#fileAtomPages = storedLists<AtomId>(storage, "file-atoms"); 325325 this.#runStarts = storedMap(storage, "run-starts"); 326− this.#relocations = storedMap(storage, "relocations");
327− this.#relocatedTo = storedMap(storage, "relocated-to");
328− this.#relocatedToCode = storedMap(storage, "relocated-to-code");
326+ this.#relocationPages = storedLists<RelocationInfo>(storage, "relocations"); 327+ this.#relocatedToPages = storedLists<RelocationInfo>(storage, "relocated-to"); 328+ this.#relocatedToCodePages = storedLists<RelocationInfo>(storage, "relocated-to-code", (key) => 329+ JSON.stringify(["number", Number(key)]), 329331 this.#labels = storedMap(storage, "labels"); @@ -438,9 +440,12 @@
438440 /** 1 for the first line of a run whose atom deleted the line it hangs off: the run replaces it. */ 439441 #replacing: NumberIndex = new Ints(true); 440442 #relocations: StateMap<LineId, RelocationInfo[]> = new Map(); 443+ #relocationPages: ReturnType<typeof storedLists<RelocationInfo>> | undefined; 441444 #relocatedTo: StateMap<NodeId, RelocationInfo[]> = new Map(); 445+ #relocatedToPages: ReturnType<typeof storedLists<RelocationInfo>> | undefined; 442446 /** The same relocations by node code (a line's n, or -1 - a path's index), for views laid out by number. */ 443447 #relocatedToCode: StateMap<number, RelocationInfo[]> = new Map(); 448+ #relocatedToCodePages: ReturnType<typeof storedLists<RelocationInfo>> | undefined; 444449 #movers: AtomId[] = []; 445450 #atomFiles: (readonly string[])[] = []; 446451 /** Each atom's deleted lines by n, packed (#46), or undefined when it deletes none. */ @@ -631,8 +636,13 @@
632637 /** Every relocation that puts a line right after the node with this code. */ 633638 relocatedToCode(code: number): readonly RelocationInfo[] { 639+ if (this.#relocatedToCodePages) return [...this.eachRelocatedToCode(code)]; 634640 return this.#relocatedToCode.get(code) ?? EMPTY; 642+ *eachRelocatedToCode(code: number): IterableIterator<RelocationInfo> { 643+ if (this.#relocatedToCodePages) yield* this.#relocatedToCodePages.values(String(code)); 644+ else yield* this.#relocatedToCode.get(code) ?? EMPTY; 637647 /** How many lines every atom together has written. */ 638648 get lineCount(): number { @@ -782,11 +792,17 @@
783793 /** Every relocation of a line, in seq order. */ 784794 relocationsOf(line: LineId): readonly RelocationInfo[] { 795+ if (this.#relocationPages) return [...this.eachRelocationOf(line)]; 785796 return this.#relocations.get(line) ?? EMPTY; 798+ *eachRelocationOf(line: LineId): IterableIterator<RelocationInfo> { 799+ if (this.#relocationPages) yield* this.#relocationPages.values(line); 800+ else yield* this.#relocations.get(line) ?? EMPTY; 788803 /** Every relocation that puts a line right after this node, in seq order. */ 789804 relocatedTo(node: NodeId): readonly RelocationInfo[] { 805+ if (this.#relocatedToPages) return [...this.#relocatedToPages.values(node)]; 790806 return this.#relocatedTo.get(node) ?? EMPTY; @@ -998,13 +1014,13 @@
9991015 (frozen.moves ?? []).forEach((m, i) => { 10001016 const info: RelocationInfo = Object.freeze({ atom: frozen.id, line: m.line, parent: m.parent, k: i }); 1001− pushIndex(this.#relocations, m.line, info);
1002− pushIndex(this.#relocatedTo, m.parent, info);
1003− pushIndex(
1004− this.#relocatedToCode,
1005− isStartNode(m.parent) ? -1 - this.#pathOf(m.parent.slice(1)) : this.#lineN(m.parent),
1006− info,
1007− );
1017+ if (this.#relocationPages) this.#relocationPages.append(m.line, info); 1018+ else pushIndex(this.#relocations, m.line, info); 1019+ if (this.#relocatedToPages) this.#relocatedToPages.append(m.parent, info); 1020+ else pushIndex(this.#relocatedTo, m.parent, info); 1021+ const parentCode = isStartNode(m.parent) ? -1 - this.#pathOf(m.parent.slice(1)) : this.#lineN(m.parent); 1022+ if (this.#relocatedToCodePages) this.#relocatedToCodePages.append(String(parentCode), info); 1023+ else pushIndex(this.#relocatedToCode, parentCode, info); 10091025 if (frozen.moves) this.#movers.push(frozen.id); 10101026 for (const n of deletedN) { --- a/src/engine/test/state-source.test.ts
+++ b/src/engine/test/state-source.test.ts
@@ -5,6 +5,49 @@
55import { lines, Repo } from "./helpers.ts"; 66import { SqlState } from "./storage-helper.ts"; 8+test("relocation indexes stream one fixed page before yielding and preserve order", () => { 9+ class ObservedStorage extends SqlState { 12+ override read(collection: string, key: string): unknown { 13+ if (collection.startsWith("relocat") && collection.endsWith(":pages")) this.pageReads++; 14+ return super.read(collection, key); 16+ override write(collection: string, key: string, value: unknown): void { 17+ if (collection.startsWith("relocat") && collection.endsWith(":pages")) { 18+ assert.ok(Array.isArray(value)); 19+ this.largestPage = Math.max(this.largestPage, value.length); 21+ super.write(collection, key, value); 24+ const storage = new ObservedStorage(); 26+ const oracle = Repo.init({ "a.txt": "one\n", "b.txt": "" }); 27+ const move = oracle.draft({ move: { file: "a.txt", start: 0, end: 1, to: "b.txt", at: 0 } }); 28+ const graph = new Graph({ storage }); 29+ graph.add(oracle.graph.atom("a0")); 30+ for (let seq = 1; seq <= 513; seq++) { 31+ const atom = { ...move, id: `a${seq}`, seq }; 33+ oracle.graph.add(atom); 35+ const reopened = new Graph({ storage }); 36+ storage.pageReads = 0; 37+ const iterator = reopened.eachRelocationOf("a0.0"); 38+ assert.deepEqual(iterator.next().value, oracle.graph.relocationsOf("a0.0")[0]); 39+ assert.equal(storage.pageReads, 1, "first result must decode only one page"); 40+ assert.deepEqual(reopened.relocationsOf("a0.0"), oracle.graph.relocationsOf("a0.0")); 41+ const parent = move.moves![0]!.parent; 42+ const code = reopened.codeOf(parent)!; 43+ assert.deepEqual(reopened.relocatedTo(parent), oracle.graph.relocatedTo(parent)); 44+ assert.deepEqual([...reopened.eachRelocatedToCode(code)], oracle.graph.relocatedToCode(code)); 45+ assert.equal(storage.largestPage, 256); 851test("hot claim indexes stream bounded pages after reopening", (t) => { 952 const oracle = Repo.init({ "a.txt": lines("one", "two") }); 1053 const draft = oracle.draft({ edits: [{ file: "a.txt", old: "one", new: "ONE" }] }); --- a/src/engine/view.ts
+++ b/src/engine/view.ts
@@ -204,7 +204,7 @@
204204 const ignored = new Set<string>(); 205205 const pick = (line: LineId): RelocationInfo | undefined => { 206206 let best: RelocationInfo | undefined; 207− for (const r of graph.relocationsOf(line)) {
207+ for (const r of graph.eachRelocationOf(line)) { 208208 if (this.#has(graph.atom(r.atom).seq) && !ignored.has(identity(r)) && (!best || stronger(r, best))) best = r; @@ -328,7 +328,7 @@
328328 out.push({ n, replaces: graph.replacesN(n) ? 1 : 0, lamport: graph.lamportOfSeq(seq), seq, k: graph.kOf(n) }); 331− for (const r of graph.relocatedToCode(code)) {
331+ for (const r of graph.eachRelocatedToCode(code)) { 332332 const n = graph.lineN(r.line); 333333 const selected = relocs.get(n)?.r; 334334 if (!selected || selected.atom !== r.atom || selected.k !== r.k) continue;