{"draft":null,"from":174,"to":175,"files":[{"path":"src/engine/graph.ts","added":75,"removed":10,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -3,6 +3,7 @@\n // atoms and keeps indexes so those questions are cheap.\n \n import { ChildCounts } from \"./child-counts.ts\";\n+import { JsonStringChunks, registerJsonChunkValue } from \"./json-value-chunks.ts\";\n import {\n \tatomId,\n \tattrKey,\n@@ -358,6 +359,8 @@\n \t\t\t\t\t\tencoded.textFormat === 1 || encoded.listFormat === 1 || encoded.insertionFormat === 1\n \t\t\t\t\t\t\t? encoded.atom!\n \t\t\t\t\t\t\t: value;\n+\t\t\t\t\tif (encoded.insertionFormat === 1 && (!Array.isArray(atom.inserts) || atom.inserts.length !== 0))\n+\t\t\t\t\t\tthrow Error(\"invalid stored insertion placeholder\");\n \t\t\t\t\tconst text =\n \t\t\t\t\t\tencoded.textFormat === 1\n \t\t\t\t\t\t\t? (start: number, length: number): readonly string[] => {\n@@ -368,7 +371,7 @@\n \t\t\t\t\t\t\t\t\t\t\tif (index >= start && index < start + length) {\n \t\t\t\t\t\t\t\t\t\t\t\tconst reference = raw as unknown as TextReference;\n \t\t\t\t\t\t\t\t\t\t\t\tif (typeof raw === \"string\") result.push(raw);\n-\t\t\t\t\t\t\t\t\t\t\t\telse if (reference && typeof reference.text === \"string\")\n+\t\t\t\t\t\t\t\t\t\t\t\telse if (reference && reference.text === JSON.stringify([String(atom.seq), index]))\n \t\t\t\t\t\t\t\t\t\t\t\t\tresult.push(atomText.read(reference.text));\n \t\t\t\t\t\t\t\t\t\t\t\telse throw Error(\"invalid stored atom text reference\");\n \t\t\t\t\t\t\t\t\t\t\t}\n@@ -377,13 +380,51 @@\n \t\t\t\t\t\t\t\t\tthrow Error(\"stored atom text range missing\");\n \t\t\t\t\t\t\t\t}\n \t\t\t\t\t\t\t: undefined;\n+\t\t\t\t\tconst streamed = (target: Atom): Atom => {\n+\t\t\t\t\t\tregisterJsonChunkValue(target, () => {\n+\t\t\t\t\t\t\tconst line = (raw: unknown, index: number): unknown => {\n+\t\t\t\t\t\t\t\tif (typeof raw === \"string\") return raw;\n+\t\t\t\t\t\t\t\tconst reference = raw as TextReference;\n+\t\t\t\t\t\t\t\tif (!reference || reference.text !== JSON.stringify([String(atom.seq), index]))\n+\t\t\t\t\t\t\t\t\tthrow Error(\"invalid stored atom text reference\");\n+\t\t\t\t\t\t\t\treturn new JsonStringChunks(() => atomText.chunks(reference.text));\n+\t\t\t\t\t\t\t};\n+\t\t\t\t\t\t\tlet inserts: readonly unknown[];\n+\t\t\t\t\t\t\tif (encoded.insertionFormat === 1) {\n+\t\t\t\t\t\t\t\tconst collection = `atom-inserts:${atom.seq}`;\n+\t\t\t\t\t\t\t\tinserts = readonlyStoredArray(storage, collection, (raw, index) => {\n+\t\t\t\t\t\t\t\t\tconst run = raw as { file: string; parent: string; lines: string };\n+\t\t\t\t\t\t\t\t\tconst lines = `${collection}:lines:${index}`;\n+\t\t\t\t\t\t\t\t\tif (run.lines !== lines || typeof run.file !== \"string\" || typeof run.parent !== \"string\")\n+\t\t\t\t\t\t\t\t\t\tthrow Error(\"invalid stored insertion run\");\n+\t\t\t\t\t\t\t\t\tlet start = 0;\n+\t\t\t\t\t\t\t\t\tfor (let prior = 0; prior < index; prior++)\n+\t\t\t\t\t\t\t\t\t\tstart += readonlyStoredArray(storage, `${collection}:lines:${prior}`).length;\n+\t\t\t\t\t\t\t\t\treturn {\n+\t\t\t\t\t\t\t\t\t\tfile: run.file,\n+\t\t\t\t\t\t\t\t\t\tparent: run.parent,\n+\t\t\t\t\t\t\t\t\t\tlines: readonlyStoredArray(storage, lines, (raw, i) => line(raw, start + i)),\n+\t\t\t\t\t\t\t\t\t};\n+\t\t\t\t\t\t\t\t});\n+\t\t\t\t\t\t\t} else {\n+\t\t\t\t\t\t\t\tlet index = 0;\n+\t\t\t\t\t\t\t\tinserts = atom.inserts.map((run) => ({\n+\t\t\t\t\t\t\t\t\tfile: run.file,\n+\t\t\t\t\t\t\t\t\tparent: run.parent,\n+\t\t\t\t\t\t\t\t\tlines: run.lines.map((raw) => line(raw, index++)),\n+\t\t\t\t\t\t\t\t}));\n+\t\t\t\t\t\t\t}\n+\t\t\t\t\t\t\treturn { ...target, inserts };\n+\t\t\t\t\t\t});\n+\t\t\t\t\t\treturn target;\n+\t\t\t\t\t};\n \t\t\t\t\tconst frozen = freezeAtom(\n \t\t\t\t\t\tatom,\n \t\t\t\t\t\t(id) => id,\n \t\t\t\t\t\t(author) => author,\n \t\t\t\t\t\ttext,\n \t\t\t\t\t);\n-\t\t\t\t\tif (encoded.listFormat !== 1 && encoded.insertionFormat !== 1) return frozen;\n+\t\t\t\t\tif (encoded.listFormat !== 1 && encoded.insertionFormat !== 1) return streamed(frozen);\n \t\t\t\t\tconst result = { ...frozen };\n \t\t\t\t\tif (encoded.insertionFormat === 1) {\n \t\t\t\t\t\tconst collection = `atom-inserts:${atom.seq}`;\n@@ -396,10 +437,14 @@\n \t\t\t\t\t\t\treturn Object.freeze({\n \t\t\t\t\t\t\t\tfile: run.file,\n \t\t\t\t\t\t\t\tparent: run.parent,\n-\t\t\t\t\t\t\t\tlines: readonlyStoredArray<string>(storage, lines, (rawLine) => {\n+\t\t\t\t\t\t\t\tlines: readonlyStoredArray<string>(storage, lines, (rawLine, lineIndex) => {\n \t\t\t\t\t\t\t\t\tif (typeof rawLine === \"string\") return rawLine;\n \t\t\t\t\t\t\t\t\tconst ref = rawLine as TextReference;\n-\t\t\t\t\t\t\t\t\tif (!ref || typeof ref.text !== \"string\") throw Error(\"invalid stored insertion text\");\n+\t\t\t\t\t\t\t\t\tlet atomLine = lineIndex;\n+\t\t\t\t\t\t\t\t\tfor (let prior = 0; prior < index; prior++)\n+\t\t\t\t\t\t\t\t\t\tatomLine += readonlyStoredArray(storage, `${collection}:lines:${prior}`).length;\n+\t\t\t\t\t\t\t\t\tif (!ref || ref.text !== JSON.stringify([String(atom.seq), atomLine]))\n+\t\t\t\t\t\t\t\t\t\tthrow Error(\"invalid stored insertion text\");\n \t\t\t\t\t\t\t\t\treturn atomText.read(ref.text);\n \t\t\t\t\t\t\t\t}),\n \t\t\t\t\t\t\t});\n@@ -413,7 +458,7 @@\n \t\t\t\t\t\t\tthrow Error(\"invalid stored atom list reference\");\n \t\t\t\t\t\tObject.defineProperty(result, field, { value: readonlyStoredArray(storage, collection), enumerable: true });\n \t\t\t\t\t}\n-\t\t\t\t\treturn Object.freeze(result);\n+\t\t\t\t\treturn streamed(Object.freeze(result));\n \t\t\t\t},\n \t\t\t\t(input, key) => {\n \t\t\t\t\tconst atom = { ...input };\n@@ -779,6 +824,25 @@\n \t\treturn this.#text(n);\n \t}\n \n+\t/** Historical text in bounded chunks, without decoding a whole stored line. */\n+\t*textChunks(n: number): IterableIterator<string> {\n+\t\tif (!this.#storage) {\n+\t\t\tconst text = this.#text(n);\n+\t\t\tfor (let offset = 0; offset < text.length; offset += StoredText.chunkCharacters)\n+\t\t\t\tyield text.slice(offset, offset + StoredText.chunkCharacters);\n+\t\t\treturn;\n+\t\t}\n+\t\tif (!Number.isSafeInteger(n) || n < 0 || n >= this.#lineText!.length) throw Error(\"invalid line number\");\n+\t\tconst value = this.#storage.read(\"line-text\", String(n));\n+\t\tif (typeof value === \"string\") {\n+\t\t\tyield value;\n+\t\t\treturn;\n+\t\t}\n+\t\tconst reference = value as { format?: number; text?: string } | undefined;\n+\t\tif (reference?.format !== 1 || typeof reference.text !== \"string\") throw Error(\"invalid long line reference\");\n+\t\tyield* new StoredText(this.#storage, \"atom-text:long\").chunks(reference.text);\n+\t}\n+\n \t/** The seq of the atom that wrote line n. */\n \twriterSeqOf(n: number): number {\n \t\treturn this.#lineAtom.at(n);\n@@ -976,12 +1040,13 @@\n \n \t/** The line ids an atom inserted, in k order. */\n \tlinesOf(id: AtomId): LineId[] {\n-\t\tconst atom = this.atom(id);\n-\t\tconst out: LineId[] = [];\n-\t\tconst seq = atom.seq;\n+\t\treturn [...this.eachInsertedLine(id)];\n+\t}\n+\t/** Inserted identities without decoding or allocating the atom's text. */\n+\t*eachInsertedLine(id: AtomId): IterableIterator<LineId> {\n+\t\tconst seq = this.atom(id).seq;\n \t\tconst count = (seq + 1 < this.#lineBase.length ? this.#lineBase[seq + 1]! : this.#lineTotal) - this.#lineBase[seq]!;\n-\t\tfor (let k = 0; k < count; k++) out.push(lineId(id, k));\n-\t\treturn out;\n+\t\tfor (let k = 0; k < count; k++) yield lineId(id, k);\n \t}\n \n \t/** Every atom that deletes this line (across the whole graph). */"}],"unmatched":[]}