Lines 15–21 at 109 (were 15–20 at 108)
18+import { type GraphStateStorage, type StateMap, storedArray, storedMap } from "./state-source.ts"; Lines 77–101 at 109 (were 76–96 at 108)
7677 * are two (#46): most keys name one atom, and a list for each costs more than 79−function pushOne<K, V>(map: Map<K, V | V[]>, key: K, value: V): void {
80+function pushOne<K, V>(map: StateMap<K, V | V[]>, key: K, value: V): void { 8081 const had = map.get(key); 8182 if (had === undefined) map.set(key, value); 82− else if (Array.isArray(had)) had.push(value);
83− else map.set(key, [had, value]);
83+ else if (Array.isArray(had)) { 86+ } else map.set(key, [had, value]); 8689function listOf<V>(had: V | V[] | undefined): readonly V[] { 8790 return had === undefined ? EMPTY : Array.isArray(had) ? had : [had]; 90−function pushIndex<K, V>(map: Map<K, V[]>, key: K, value: V): void {
93+function pushIndex<K, V>(map: StateMap<K, V[]>, key: K, value: V): void { 9194 const list = map.get(key); 92− if (list) list.push(value);
93− else map.set(key, [value]);
98+ } else map.set(key, [value]); 96101const EMPTY: readonly never[] = Object.freeze([]); Lines 181–194 at 109 (were 176–182 at 108)
178183/** A growable array of 32-bit integers: a line's fields cost 4 bytes each, not an object and its strings (#46). */ 179−class Ints {
184+interface NumberIndex { 185+ readonly length: number; 186+ push(n: number): void; 187+ at(i: number): number; 188+ set(i: number, n: number): void; 191+class Ints implements NumberIndex { 180192 #buf: Int32Array | Uint8Array; 181193 /** bytes: values 0-255 only, one byte each. */ 182194 constructor(bytes = false) { Lines 244–250 at 109 (were 232–237 at 108)
234246export class Graph { 247+ readonly #storage?: GraphStateStorage; 235248 #atoms: Atom[] = []; 236249 #lamport: number[] = []; 237250 // Lines, packed (#46): line n is the k-th line atom seq wrote, n = #lineBase[seq] + k. Each holds its Lines 252–265 at 109 (were 239–250 at 108)
239252 // path's index for a file's start). Its text stays in the atom. Ids and LineInfo objects are made only 240253 // when asked for, so a line costs about 16 bytes here instead of a record, its id and its parent's id. 241254 #lineBase: number[] = []; 242− #lineAtom = new Ints();
243− #lineFile = new Ints();
244− #lineParent = new Ints();
255+ #lineAtom: NumberIndex = new Ints(); 256+ #lineFile: NumberIndex = new Ints(); 257+ #lineParent: NumberIndex = new Ints(); 245258 /** Lines' text, when the graph keeps it; without it, it comes from `texts` (#46, text on disk). */ 246259 #lineText: string[] | undefined = []; 247− #lineTotal = 0;
260+ get #lineTotal(): number { 261+ return this.#lineAtom.length; 248263 /** Where an atom's lines' text is when the graph doesn't keep it: each run's lines, by the atom's seq. */ 249264 #texts: ((seq: number) => readonly (readonly string[])[]) | undefined; 250265 /** Recently read atoms' text, flattened, most recent last, up to TEXT_CACHE characters. */ Lines 272–340 at 109 (were 257–269 at 108)
257272 * atom's text when a line of it is asked for, keeping the most recent. 260− options: { texts?: (seq: number) => readonly (readonly string[])[]; lists?: (seq: number) => AtomLists } = {},
276+ texts?: (seq: number) => readonly (readonly string[])[]; 277+ lists?: (seq: number) => AtomLists; 278+ storage?: GraphStateStorage; 262281 if (options.texts) { 263282 this.#texts = options.texts; 264283 this.#lineText = undefined; 266285 if (options.lists) this.#lists = options.lists; 286+ this.#storage = options.storage; 287+ if (options.storage) { 288+ const storage = options.storage; 289+ const numbers = (name: string): NumberIndex => { 290+ const values = storedArray<number>(storage, name); 293+ return values.length; 298+ at: (index) => values[index]!, 299+ set: (index, value) => { 300+ values[index] = value; 304+ this.#atoms = storedArray<Atom>(storage, "atoms", (atom) => 311+ this.#lamport = storedArray(storage, "lamport"); 312+ this.#lineBase = storedArray(storage, "line-base"); 313+ this.#paths = storedArray(storage, "paths"); 314+ this.#movers = storedArray(storage, "movers"); 315+ this.#atomFiles = storedArray(storage, "atom-files"); 316+ this.#atomDeletes = storedArray(storage, "atom-deletes"); 317+ this.#lineAtom = numbers("line-atom"); 318+ this.#lineFile = numbers("line-file"); 319+ this.#lineParent = numbers("line-parent"); 320+ this.#deleter = numbers("deleter"); 321+ this.#replacing = numbers("replacing"); 322+ this.#lineText = storedArray(storage, "line-text"); 323+ this.#pathIndex = storedMap(storage, "path-index"); 324+ this.#moreDeleters = storedMap(storage, "more-deleters"); 325+ this.#writers = storedMap(storage, "writers"); 326+ this.#readers = storedMap(storage, "readers"); 327+ this.#holders = storedMap(storage, "holders"); 328+ this.#dependents = storedMap(storage, "dependents"); 329+ this.#resolvers = storedMap(storage, "resolvers"); 330+ this.#together = storedMap(storage, "together"); 331+ this.#fileAtoms = storedMap(storage, "file-atoms"); 332+ this.#runStarts = storedMap(storage, "run-starts"); 333+ this.#relocations = storedMap(storage, "relocations"); 334+ this.#relocatedTo = storedMap(storage, "relocated-to"); 335+ this.#relocatedToCode = storedMap(storage, "relocated-to-code"); 336+ this.#labels = storedMap(storage, "labels"); 269340 /** Where an atom's key and delete lists are when the graph doesn't keep them (#46): the stored atom. */ Lines 423–452 at 109 (were 352–381 at 108)
352423 return this.#textOf(seq)[n - this.#lineBase[seq]!]!; 354425 #paths: string[] = []; 355− #pathIndex = new Map<string, number>();
426+ #pathIndex: StateMap<string, number> = new Map(); 356427 /** The first atom that deletes each line (its seq), or -1; a line deleted by more than one has the rest in #moreDeleters. */ 357− #deleter = new Ints();
358− #moreDeleters = new Map<number, number[]>();
359− #writers = new Map<Key | number, AtomId | AtomId[]>();
360− #readers = new Map<Key | number, AtomId | AtomId[]>();
361− #holders = new Map<Key | number, AtomId | AtomId[]>();
362− #dependents = new Map<AtomId, AtomId | AtomId[]>();
363− #resolvers = new Map<AtomId, AtomId | AtomId[]>();
364− #together = new Map<AtomId, TogetherLink[]>();
365− #fileAtoms = new Map<string, AtomId[]>();
428+ #deleter: NumberIndex = new Ints(); 429+ #moreDeleters: StateMap<number, number[]> = new Map(); 430+ #writers: StateMap<Key | number, AtomId | AtomId[]> = new Map(); 431+ #readers: StateMap<Key | number, AtomId | AtomId[]> = new Map(); 432+ #holders: StateMap<Key | number, AtomId | AtomId[]> = new Map(); 433+ #dependents: StateMap<AtomId, AtomId | AtomId[]> = new Map(); 434+ #resolvers: StateMap<AtomId, AtomId | AtomId[]> = new Map(); 435+ #together: StateMap<AtomId, TogetherLink[]> = new Map(); 436+ #fileAtoms: StateMap<string, AtomId[]> = new Map(); 366437 /** First lines of runs (their n), by the node they were written after: a line's n, or -1 - a path's index. */ 367− #runStarts = new Map<number, number | number[]>();
438+ #runStarts: StateMap<number, number | number[]> = new Map(); 368439 /** 1 for the first line of a run whose atom deleted the line it hangs off: the run replaces it. */ 369− #replacing = new Ints(true);
370− #relocations = new Map<LineId, RelocationInfo[]>();
371− #relocatedTo = new Map<NodeId, RelocationInfo[]>();
440+ #replacing: NumberIndex = new Ints(true); 441+ #relocations: StateMap<LineId, RelocationInfo[]> = new Map(); 442+ #relocatedTo: StateMap<NodeId, RelocationInfo[]> = new Map(); 372443 /** The same relocations by node code (a line's n, or -1 - a path's index), for views laid out by number. */ 373− #relocatedToCode = new Map<number, RelocationInfo[]>();
444+ #relocatedToCode: StateMap<number, RelocationInfo[]> = new Map(); 374445 #movers: AtomId[] = []; 375446 #atomFiles: (readonly string[])[] = []; 376447 /** Each atom's deleted lines by n, packed (#46), or undefined when it deletes none. */ 377− #atomDeletes: (Int32Array | undefined)[] = [];
378− #labels = new Map<string, Label>();
448+ #atomDeletes: (readonly number[] | Int32Array | undefined)[] = []; 449+ #labels: StateMap<string, Label> = new Map(); 379450 /** Each author's name kept once (#46). */ 380451 #authors = new Map<string, string>(); Lines 847–857 at 109 (were 776–781 at 108)
776847 * The atom is frozen. Adding to the graph does not add it to any set. 778849 add(atom: Atom): void { 850+ if (this.#storage) this.#storage.transaction(() => this.#add(atom)); 851+ else this.#add(atom); 854+ #add(atom: Atom): void { 779855 validateShape(atom); 780856 const seq = this.#atoms.length; 781857 const bad = (why: string): never => { Lines 926–932 at 109 (were 850–855 at 108)
851927 (id) => this.#atoms[seqOf(id)]!.id, 929+ if (this.#storage) return author; 853930 let a = this.#authors.get(author); 854931 if (a === undefined) this.#authors.set(author, (a = author)); Lines 934–940 at 109 (were 857–863 at 108)
857934 this.#texts ? this.#reader(seq) : undefined, 858935 this.#lists ? this.#listReader(seq) : undefined, 860− this.#atoms.push(frozen);
937+ this.#atoms.push(this.#storage ? atom : frozen); 862939 for (const d of deps) lam = Math.max(lam, this.lamport(d) + 1); 863940 this.#lamport.push(lam); Lines 943–949 at 109 (were 866–872 at 108)
866943 // The atom as given: with lists kept elsewhere, the frozen one reads them back from the log. 867944 const deletes = atom.deletes.length > 0 ? new Set(atom.deletes) : undefined; 868945 const deletedN = atom.deletes.map((line) => this.#lineN(line)); 869− this.#atomDeletes.push(deletedN.length > 0 ? Int32Array.from(deletedN) : undefined);
946+ this.#atomDeletes.push(deletedN.length > 0 ? (this.#storage ? deletedN : Int32Array.from(deletedN)) : undefined); 870947 this.#lineBase.push(this.#lineTotal); 871948 for (const run of atom.inserts) { 872949 const file = this.#pathOf(run.file); Lines 951–957 at 109 (were 874–880 at 108)
874951 const first = this.#lineTotal; 875952 pushOne(this.#runStarts, parent, first); 876953 for (let i = 0; i < run.lines.length; i++) { 877− const n = this.#lineTotal++;
954+ const n = this.#lineTotal; 878955 this.#lineAtom.push(seq); 879956 this.#lineFile.push(file); 880957 this.#lineParent.push(parent); Lines 1051–1060 at 109 (were 974–979 at 108)
9741051 * their base instead, so their contents don't change. 9761053 removeLabel(name: string): boolean { 1054+ return this.#storage ? this.#storage.transaction(() => this.#removeLabel(name)) : this.#removeLabel(name); 1057+ #removeLabel(name: string): boolean { 9771058 const gone = this.#labels.get(name); 9781059 if (!gone) return false; 9791060 this.#labels.delete(name);