Lines 3–9 at 175 (were 3–8 at 174)
33// atoms and keeps indexes so those questions are cheap. 55import { ChildCounts } from "./child-counts.ts"; 6+import { JsonStringChunks, registerJsonChunkValue } from "./json-value-chunks.ts"; Lines 359–366 at 175 (were 358–363 at 174)
358359 encoded.textFormat === 1 || encoded.listFormat === 1 || encoded.insertionFormat === 1 362+ if (encoded.insertionFormat === 1 && (!Array.isArray(atom.inserts) || atom.inserts.length !== 0)) 363+ throw Error("invalid stored insertion placeholder"); 362365 encoded.textFormat === 1 363366 ? (start: number, length: number): readonly string[] => { Lines 371–377 at 175 (were 368–374 at 174)
368371 if (index >= start && index < start + length) { 369372 const reference = raw as unknown as TextReference; 370373 if (typeof raw === "string") result.push(raw); 371− else if (reference && typeof reference.text === "string")
374+ else if (reference && reference.text === JSON.stringify([String(atom.seq), index])) 372375 result.push(atomText.read(reference.text)); 373376 else throw Error("invalid stored atom text reference"); Lines 380–430 at 175 (were 377–389 at 174)
377380 throw Error("stored atom text range missing"); 383+ const streamed = (target: Atom): Atom => { 384+ registerJsonChunkValue(target, () => { 385+ const line = (raw: unknown, index: number): unknown => { 386+ if (typeof raw === "string") return raw; 387+ const reference = raw as TextReference; 388+ if (!reference || reference.text !== JSON.stringify([String(atom.seq), index])) 389+ throw Error("invalid stored atom text reference"); 390+ return new JsonStringChunks(() => atomText.chunks(reference.text)); 392+ let inserts: readonly unknown[]; 393+ if (encoded.insertionFormat === 1) { 394+ const collection = `atom-inserts:${atom.seq}`; 395+ inserts = readonlyStoredArray(storage, collection, (raw, index) => { 396+ const run = raw as { file: string; parent: string; lines: string }; 397+ const lines = `${collection}:lines:${index}`; 398+ if (run.lines !== lines || typeof run.file !== "string" || typeof run.parent !== "string") 399+ throw Error("invalid stored insertion run"); 401+ for (let prior = 0; prior < index; prior++) 402+ start += readonlyStoredArray(storage, `${collection}:lines:${prior}`).length; 406+ lines: readonlyStoredArray(storage, lines, (raw, i) => line(raw, start + i)), 411+ inserts = atom.inserts.map((run) => ({ 414+ lines: run.lines.map((raw) => line(raw, index++)), 417+ return { ...target, inserts }; 380421 const frozen = freezeAtom( 383424 (author) => author, 386− if (encoded.listFormat !== 1 && encoded.insertionFormat !== 1) return frozen;
427+ if (encoded.listFormat !== 1 && encoded.insertionFormat !== 1) return streamed(frozen); 387428 const result = { ...frozen }; 388429 if (encoded.insertionFormat === 1) { 389430 const collection = `atom-inserts:${atom.seq}`; Lines 437–450 at 175 (were 396–405 at 174)
396437 return Object.freeze({ 398439 parent: run.parent, 399− lines: readonlyStoredArray<string>(storage, lines, (rawLine) => {
440+ lines: readonlyStoredArray<string>(storage, lines, (rawLine, lineIndex) => { 400441 if (typeof rawLine === "string") return rawLine; 401442 const ref = rawLine as TextReference; 402− if (!ref || typeof ref.text !== "string") throw Error("invalid stored insertion text");
443+ let atomLine = lineIndex; 444+ for (let prior = 0; prior < index; prior++) 445+ atomLine += readonlyStoredArray(storage, `${collection}:lines:${prior}`).length; 446+ if (!ref || ref.text !== JSON.stringify([String(atom.seq), atomLine])) 447+ throw Error("invalid stored insertion text"); 403448 return atomText.read(ref.text); Lines 458–464 at 175 (were 413–419 at 174)
413458 throw Error("invalid stored atom list reference"); 414459 Object.defineProperty(result, field, { value: readonlyStoredArray(storage, collection), enumerable: true }); 416− return Object.freeze(result);
461+ return streamed(Object.freeze(result)); 419464 const atom = { ...input }; Lines 824–848 at 175 (were 779–784 at 174)
779824 return this.#text(n); 827+ /** Historical text in bounded chunks, without decoding a whole stored line. */ 828+ *textChunks(n: number): IterableIterator<string> { 829+ if (!this.#storage) { 830+ const text = this.#text(n); 831+ for (let offset = 0; offset < text.length; offset += StoredText.chunkCharacters) 832+ yield text.slice(offset, offset + StoredText.chunkCharacters); 835+ if (!Number.isSafeInteger(n) || n < 0 || n >= this.#lineText!.length) throw Error("invalid line number"); 836+ const value = this.#storage.read("line-text", String(n)); 837+ if (typeof value === "string") { 841+ const reference = value as { format?: number; text?: string } | undefined; 842+ if (reference?.format !== 1 || typeof reference.text !== "string") throw Error("invalid long line reference"); 843+ yield* new StoredText(this.#storage, "atom-text:long").chunks(reference.text); 782846 /** The seq of the atom that wrote line n. */ 783847 writerSeqOf(n: number): number { 784848 return this.#lineAtom.at(n); Lines 1040–1052 at 175 (were 976–987 at 174)
9771041 /** The line ids an atom inserted, in k order. */ 9781042 linesOf(id: AtomId): LineId[] { 979− const atom = this.atom(id);
980− const out: LineId[] = [];
981− const seq = atom.seq;
1043+ return [...this.eachInsertedLine(id)]; 1045+ /** Inserted identities without decoding or allocating the atom's text. */ 1046+ *eachInsertedLine(id: AtomId): IterableIterator<LineId> { 1047+ const seq = this.atom(id).seq; 9821048 const count = (seq + 1 < this.#lineBase.length ? this.#lineBase[seq + 1]! : this.#lineTotal) - this.#lineBase[seq]!; 983− for (let k = 0; k < count; k++) out.push(lineId(id, k));
984− return out;
1049+ for (let k = 0; k < count; k++) yield lineId(id, k); 9871052 /** Every atom that deletes this line (across the whole graph). */