{"draft":null,"from":108,"to":109,"files":[{"path":"src/engine/graph.ts","added":113,"removed":32,"diff":"--- a/src/engine/graph.ts\n+++ b/src/engine/graph.ts\n@@ -15,6 +15,7 @@\n \tseqOf,\n \tstartNode,\n } from \"./keys.ts\";\n+import { type GraphStateStorage, type StateMap, storedArray, storedMap } from \"./state-source.ts\";\n import {\n \tATTR_NAMES,\n \ttype Atom,\n@@ -76,21 +77,25 @@\n  * are two (#46): most keys name one atom, and a list for each costs more than\n  * the value.\n  */\n-function pushOne<K, V>(map: Map<K, V | V[]>, key: K, value: V): void {\n+function pushOne<K, V>(map: StateMap<K, V | V[]>, key: K, value: V): void {\n \tconst had = map.get(key);\n \tif (had === undefined) map.set(key, value);\n-\telse if (Array.isArray(had)) had.push(value);\n-\telse map.set(key, [had, value]);\n+\telse if (Array.isArray(had)) {\n+\t\thad.push(value);\n+\t\tmap.set(key, had);\n+\t} else map.set(key, [had, value]);\n }\n \n function listOf<V>(had: V | V[] | undefined): readonly V[] {\n \treturn had === undefined ? EMPTY : Array.isArray(had) ? had : [had];\n }\n \n-function pushIndex<K, V>(map: Map<K, V[]>, key: K, value: V): void {\n+function pushIndex<K, V>(map: StateMap<K, V[]>, key: K, value: V): void {\n \tconst list = map.get(key);\n-\tif (list) list.push(value);\n-\telse map.set(key, [value]);\n+\tif (list) {\n+\t\tlist.push(value);\n+\t\tmap.set(key, list);\n+\t} else map.set(key, [value]);\n }\n \n const EMPTY: readonly never[] = Object.freeze([]);\n@@ -176,7 +181,14 @@\n }\n \n /** A growable array of 32-bit integers: a line's fields cost 4 bytes each, not an object and its strings (#46). */\n-class Ints {\n+interface NumberIndex {\n+\treadonly length: number;\n+\tpush(n: number): void;\n+\tat(i: number): number;\n+\tset(i: number, n: number): void;\n+}\n+\n+class Ints implements NumberIndex {\n \t#buf: Int32Array | Uint8Array;\n \t/** bytes: values 0-255 only, one byte each. */\n \tconstructor(bytes = false) {\n@@ -232,6 +244,7 @@\n }\n \n export class Graph {\n+\treadonly #storage?: GraphStateStorage;\n \t#atoms: Atom[] = [];\n \t#lamport: number[] = [];\n \t// Lines, packed (#46): line n is the k-th line atom seq wrote, n = #lineBase[seq] + k. Each holds its\n@@ -239,12 +252,14 @@\n \t// path's index for a file's start). Its text stays in the atom. Ids and LineInfo objects are made only\n \t// when asked for, so a line costs about 16 bytes here instead of a record, its id and its parent's id.\n \t#lineBase: number[] = [];\n-\t#lineAtom = new Ints();\n-\t#lineFile = new Ints();\n-\t#lineParent = new Ints();\n+\t#lineAtom: NumberIndex = new Ints();\n+\t#lineFile: NumberIndex = new Ints();\n+\t#lineParent: NumberIndex = new Ints();\n \t/** Lines' text, when the graph keeps it; without it, it comes from `texts` (#46, text on disk). */\n \t#lineText: string[] | undefined = [];\n-\t#lineTotal = 0;\n+\tget #lineTotal(): number {\n+\t\treturn this.#lineAtom.length;\n+\t}\n \t/** Where an atom's lines' text is when the graph doesn't keep it: each run's lines, by the atom's seq. */\n \t#texts: ((seq: number) => readonly (readonly string[])[]) | undefined;\n \t/** Recently read atoms' text, flattened, most recent last, up to TEXT_CACHE characters. */\n@@ -257,13 +272,69 @@\n \t * atom's text when a line of it is asked for, keeping the most recent.\n \t */\n \tconstructor(\n-\t\toptions: { texts?: (seq: number) => readonly (readonly string[])[]; lists?: (seq: number) => AtomLists } = {},\n+\t\toptions: {\n+\t\t\ttexts?: (seq: number) => readonly (readonly string[])[];\n+\t\t\tlists?: (seq: number) => AtomLists;\n+\t\t\tstorage?: GraphStateStorage;\n+\t\t} = {},\n \t) {\n \t\tif (options.texts) {\n \t\t\tthis.#texts = options.texts;\n \t\t\tthis.#lineText = undefined;\n \t\t}\n \t\tif (options.lists) this.#lists = options.lists;\n+\t\tthis.#storage = options.storage;\n+\t\tif (options.storage) {\n+\t\t\tconst storage = options.storage;\n+\t\t\tconst numbers = (name: string): NumberIndex => {\n+\t\t\t\tconst values = storedArray<number>(storage, name);\n+\t\t\t\treturn {\n+\t\t\t\t\tget length() {\n+\t\t\t\t\t\treturn values.length;\n+\t\t\t\t\t},\n+\t\t\t\t\tpush: (value) => {\n+\t\t\t\t\t\tvalues.push(value);\n+\t\t\t\t\t},\n+\t\t\t\t\tat: (index) => values[index]!,\n+\t\t\t\t\tset: (index, value) => {\n+\t\t\t\t\t\tvalues[index] = value;\n+\t\t\t\t\t},\n+\t\t\t\t};\n+\t\t\t};\n+\t\t\tthis.#atoms = storedArray<Atom>(storage, \"atoms\", (atom) =>\n+\t\t\t\tfreezeAtom(\n+\t\t\t\t\tatom,\n+\t\t\t\t\t(id) => id,\n+\t\t\t\t\t(author) => author,\n+\t\t\t\t),\n+\t\t\t);\n+\t\t\tthis.#lamport = storedArray(storage, \"lamport\");\n+\t\t\tthis.#lineBase = storedArray(storage, \"line-base\");\n+\t\t\tthis.#paths = storedArray(storage, \"paths\");\n+\t\t\tthis.#movers = storedArray(storage, \"movers\");\n+\t\t\tthis.#atomFiles = storedArray(storage, \"atom-files\");\n+\t\t\tthis.#atomDeletes = storedArray(storage, \"atom-deletes\");\n+\t\t\tthis.#lineAtom = numbers(\"line-atom\");\n+\t\t\tthis.#lineFile = numbers(\"line-file\");\n+\t\t\tthis.#lineParent = numbers(\"line-parent\");\n+\t\t\tthis.#deleter = numbers(\"deleter\");\n+\t\t\tthis.#replacing = numbers(\"replacing\");\n+\t\t\tthis.#lineText = storedArray(storage, \"line-text\");\n+\t\t\tthis.#pathIndex = storedMap(storage, \"path-index\");\n+\t\t\tthis.#moreDeleters = storedMap(storage, \"more-deleters\");\n+\t\t\tthis.#writers = storedMap(storage, \"writers\");\n+\t\t\tthis.#readers = storedMap(storage, \"readers\");\n+\t\t\tthis.#holders = storedMap(storage, \"holders\");\n+\t\t\tthis.#dependents = storedMap(storage, \"dependents\");\n+\t\t\tthis.#resolvers = storedMap(storage, \"resolvers\");\n+\t\t\tthis.#together = storedMap(storage, \"together\");\n+\t\t\tthis.#fileAtoms = storedMap(storage, \"file-atoms\");\n+\t\t\tthis.#runStarts = storedMap(storage, \"run-starts\");\n+\t\t\tthis.#relocations = storedMap(storage, \"relocations\");\n+\t\t\tthis.#relocatedTo = storedMap(storage, \"relocated-to\");\n+\t\t\tthis.#relocatedToCode = storedMap(storage, \"relocated-to-code\");\n+\t\t\tthis.#labels = storedMap(storage, \"labels\");\n+\t\t}\n \t}\n \n \t/** Where an atom's key and delete lists are when the graph doesn't keep them (#46): the stored atom. */\n@@ -352,30 +423,30 @@\n \t\treturn this.#textOf(seq)[n - this.#lineBase[seq]!]!;\n \t}\n \t#paths: string[] = [];\n-\t#pathIndex = new Map<string, number>();\n+\t#pathIndex: StateMap<string, number> = new Map();\n \t/** The first atom that deletes each line (its seq), or -1; a line deleted by more than one has the rest in #moreDeleters. */\n-\t#deleter = new Ints();\n-\t#moreDeleters = new Map<number, number[]>();\n-\t#writers = new Map<Key | number, AtomId | AtomId[]>();\n-\t#readers = new Map<Key | number, AtomId | AtomId[]>();\n-\t#holders = new Map<Key | number, AtomId | AtomId[]>();\n-\t#dependents = new Map<AtomId, AtomId | AtomId[]>();\n-\t#resolvers = new Map<AtomId, AtomId | AtomId[]>();\n-\t#together = new Map<AtomId, TogetherLink[]>();\n-\t#fileAtoms = new Map<string, AtomId[]>();\n+\t#deleter: NumberIndex = new Ints();\n+\t#moreDeleters: StateMap<number, number[]> = new Map();\n+\t#writers: StateMap<Key | number, AtomId | AtomId[]> = new Map();\n+\t#readers: StateMap<Key | number, AtomId | AtomId[]> = new Map();\n+\t#holders: StateMap<Key | number, AtomId | AtomId[]> = new Map();\n+\t#dependents: StateMap<AtomId, AtomId | AtomId[]> = new Map();\n+\t#resolvers: StateMap<AtomId, AtomId | AtomId[]> = new Map();\n+\t#together: StateMap<AtomId, TogetherLink[]> = new Map();\n+\t#fileAtoms: StateMap<string, AtomId[]> = new Map();\n \t/** First lines of runs (their n), by the node they were written after: a line's n, or -1 - a path's index. */\n-\t#runStarts = new Map<number, number | number[]>();\n+\t#runStarts: StateMap<number, number | number[]> = new Map();\n \t/** 1 for the first line of a run whose atom deleted the line it hangs off: the run replaces it. */\n-\t#replacing = new Ints(true);\n-\t#relocations = new Map<LineId, RelocationInfo[]>();\n-\t#relocatedTo = new Map<NodeId, RelocationInfo[]>();\n+\t#replacing: NumberIndex = new Ints(true);\n+\t#relocations: StateMap<LineId, RelocationInfo[]> = new Map();\n+\t#relocatedTo: StateMap<NodeId, RelocationInfo[]> = new Map();\n \t/** The same relocations by node code (a line's n, or -1 - a path's index), for views laid out by number. */\n-\t#relocatedToCode = new Map<number, RelocationInfo[]>();\n+\t#relocatedToCode: StateMap<number, RelocationInfo[]> = new Map();\n \t#movers: AtomId[] = [];\n \t#atomFiles: (readonly string[])[] = [];\n \t/** Each atom's deleted lines by n, packed (#46), or undefined when it deletes none. */\n-\t#atomDeletes: (Int32Array | undefined)[] = [];\n-\t#labels = new Map<string, Label>();\n+\t#atomDeletes: (readonly number[] | Int32Array | undefined)[] = [];\n+\t#labels: StateMap<string, Label> = new Map();\n \t/** Each author's name kept once (#46). */\n \t#authors = new Map<string, string>();\n \n@@ -776,6 +847,11 @@\n \t * The atom is frozen. Adding to the graph does not add it to any set.\n \t */\n \tadd(atom: Atom): void {\n+\t\tif (this.#storage) this.#storage.transaction(() => this.#add(atom));\n+\t\telse this.#add(atom);\n+\t}\n+\n+\t#add(atom: Atom): void {\n \t\tvalidateShape(atom);\n \t\tconst seq = this.#atoms.length;\n \t\tconst bad = (why: string): never => {\n@@ -850,6 +926,7 @@\n \t\t\tatom,\n \t\t\t(id) => this.#atoms[seqOf(id)]!.id,\n \t\t\t(author) => {\n+\t\t\t\tif (this.#storage) return author;\n \t\t\t\tlet a = this.#authors.get(author);\n \t\t\t\tif (a === undefined) this.#authors.set(author, (a = author));\n \t\t\t\treturn a;\n@@ -857,7 +934,7 @@\n \t\t\tthis.#texts ? this.#reader(seq) : undefined,\n \t\t\tthis.#lists ? this.#listReader(seq) : undefined,\n \t\t);\n-\t\tthis.#atoms.push(frozen);\n+\t\tthis.#atoms.push(this.#storage ? atom : frozen);\n \t\tlet lam = 0;\n \t\tfor (const d of deps) lam = Math.max(lam, this.lamport(d) + 1);\n \t\tthis.#lamport.push(lam);\n@@ -866,7 +943,7 @@\n \t\t// The atom as given: with lists kept elsewhere, the frozen one reads them back from the log.\n \t\tconst deletes = atom.deletes.length > 0 ? new Set(atom.deletes) : undefined;\n \t\tconst deletedN = atom.deletes.map((line) => this.#lineN(line));\n-\t\tthis.#atomDeletes.push(deletedN.length > 0 ? Int32Array.from(deletedN) : undefined);\n+\t\tthis.#atomDeletes.push(deletedN.length > 0 ? (this.#storage ? deletedN : Int32Array.from(deletedN)) : undefined);\n \t\tthis.#lineBase.push(this.#lineTotal);\n \t\tfor (const run of atom.inserts) {\n \t\t\tconst file = this.#pathOf(run.file);\n@@ -874,7 +951,7 @@\n \t\t\tconst first = this.#lineTotal;\n \t\t\tpushOne(this.#runStarts, parent, first);\n \t\t\tfor (let i = 0; i < run.lines.length; i++) {\n-\t\t\t\tconst n = this.#lineTotal++;\n+\t\t\t\tconst n = this.#lineTotal;\n \t\t\t\tthis.#lineAtom.push(seq);\n \t\t\t\tthis.#lineFile.push(file);\n \t\t\t\tthis.#lineParent.push(parent);\n@@ -974,6 +1051,10 @@\n \t * their base instead, so their contents don't change.\n \t */\n \tremoveLabel(name: string): boolean {\n+\t\treturn this.#storage ? this.#storage.transaction(() => this.#removeLabel(name)) : this.#removeLabel(name);\n+\t}\n+\n+\t#removeLabel(name: string): boolean {\n \t\tconst gone = this.#labels.get(name);\n \t\tif (!gone) return false;\n \t\tthis.#labels.delete(name);"}],"unmatched":[]}