Lines 10–129 at 318 (were 10–21 at 207)
1010import { type AtomId, type Graph, type ReadView, View } from "../../src/engine/index.ts"; 1111import type { GraphStateStorage } from "../../src/engine/state-source.ts"; 1212import { touchedFiles } from "./collisions.ts"; 13+import { utf8Bytes } from "./project-helpers.ts"; 1314import type { Published } from "./spaces.ts"; 1415import { differs } from "./view-changes.ts"; 1617const COUNTS = "head-visible"; 1718const META = "head-visible:meta"; 20+// Author totals (docs/disk-first-2.md, abso Capacity, issue 664): each shared file's lines by author and its text's size 21+// in UTF-8 bytes, as tree with authors counts them, so it reads one value per file instead of laying every file out. 22+// AUTHORS holds each file's totals at the newest version kept; AUTHORS_LOG each file's totals at every version that 23+// changed them, oldest first. Shared only, never a draft (Security on ask 2096). They live in the writer's state, so 24+// whatever clears or replaces that state (a new adoption, a restore, a drop of the project) takes them with it. 25+const AUTHORS = "head-authors"; 26+const AUTHORS_LOG = "author-totals"; 27+const AUTHORS_META = "head-authors:meta"; 29+/** A file's lines by author and its text's size, stored: authors by name, none at zero; null when it has no lines. */ 30+type StoredTotals = { a: [string, number][]; b: number }; 32+/** A shared file's lines by author and its text's size in UTF-8 bytes. */ 33+export interface FileTotals { 34+ readonly authors: ReadonlyMap<string, number>; 35+ readonly bytes: number; 38+const NO_TOTALS: FileTotals = { authors: new Map(), bytes: 0 }; 40+/** The newest shared version whose author totals are kept, or undefined when this writer keeps none. */ 41+export function authoredVersion(storage: GraphStateStorage): number | undefined { 42+ return storage.read(AUTHORS_META, "version") as number | undefined; 45+/** A shared file's totals at version `v`, or undefined when they aren't kept that far: lay the file out instead. */ 46+export function authorTotalsAt(storage: GraphStateStorage, path: string, v: number): FileTotals | undefined { 47+ const upTo = authoredVersion(storage); 48+ if (upTo === undefined || v > upTo) return undefined; 49+ const log = storage.read(AUTHORS_LOG, path) as [number, StoredTotals | null][] | undefined; 50+ if (!log) return NO_TOTALS; 51+ // The newest entry at or before v. 53+ let hi = log.length - 1; 56+ const mid = (lo + hi) >> 1; 57+ if (log[mid]![0] <= v) { 62+ const totals = found < 0 ? null : log[found]![1]; 63+ return totals ? { authors: new Map(totals.a), bytes: totals.b } : NO_TOTALS; 66+/** Each atom's author, read once. */ 67+function authorsOf(graph: Graph): (id: AtomId) => string { 68+ const known = new Map<AtomId, string>(); 70+ let author = known.get(id); 71+ if (author === undefined) known.set(id, (author = graph.atom(id).author)); 76+/** Totals in stored form: authors sorted, none at zero; null for a file with no lines. */ 77+function normalTotals(authors: ReadonlyMap<string, number>, bytes: number): StoredTotals | null { 78+ const a = [...authors].filter(([, n]) => n !== 0).sort(([x], [y]) => (x < y ? -1 : x > y ? 1 : 0)); 79+ return a.length === 0 && bytes === 0 ? null : { a, b: bytes }; 83+ * A laid-out file's lines by author, from its visible lines' writers (abso Capacity): each writer's author once, instead 84+ * of making an atom id and a line id for every line (an agent's tree spent 0.4 s on that for its draft's files). 86+export function authorsInView( 90+ authorOf: (id: AtomId) => string, 91+): Map<string, number> { 92+ const out = new Map<string, number>(); 93+ const writers = (view as { visibleWriters?(path: string): readonly number[] }).visibleWriters?.call(view, path); 95+ for (const l of view.lines(path)) { 96+ const author = authorOf(l.atom); 97+ out.set(author, (out.get(author) ?? 0) + 1); 101+ const bySeq = new Map<number, number>(); 102+ for (const seq of writers) bySeq.set(seq, (bySeq.get(seq) ?? 0) + 1); 103+ for (const [seq, n] of bySeq) { 104+ const author = authorOf(graph.idOfSeq(seq)); 105+ out.set(author, (out.get(author) ?? 0) + n); 110+/** A file's totals from its layout in a view, as tree with authors counts them. */ 111+function laidOutTotals(view: ReadView, path: string, authorOf: (id: AtomId) => string): StoredTotals | null { 112+ if (!view.hasFile(path)) return null; 113+ const authors = authorsInView(view, path, view.graph, authorOf); 114+ return normalTotals(authors, authors.size === 0 ? 0 : utf8Bytes(view.text(path))); 117+/** Keep a file's totals at version `v`, when they differ from what it had. */ 118+function keepTotals(storage: GraphStateStorage, v: number, path: string, totals: StoredTotals | null): void { 119+ const was = (storage.read(AUTHORS, path) as StoredTotals | undefined) ?? null; 120+ if (JSON.stringify(was) === JSON.stringify(totals)) return; 121+ if (totals) storage.write(AUTHORS, path, totals); 122+ else storage.remove(AUTHORS, path); 123+ const log = (storage.read(AUTHORS_LOG, path) as [number, StoredTotals | null][] | undefined) ?? []; 124+ storage.write(AUTHORS_LOG, path, [...log, [v, totals]]); 19127/** The shared version the running counts describe, or undefined when this writer keeps none. */ 20128export function countedVersion(storage: GraphStateStorage): number | undefined { 21129 return storage.read(META, "version") as number | undefined; Lines 166–175 at 318 (were 58–63 at 207)
58166 // The same moves as the version before: share them, and where lines sit, instead of resolving them again. 59167 if (fast && before instanceof View && after instanceof View) after.shareMoves(before); 60168 const touched = touchedFiles(graph, atoms, [before, after]); 169+ // Author totals start with the first version too, and are kept only while every version since has kept them. 170+ const authored = authoredVersion(storage); 171+ const authoring = authored === v - 1 || (v === 0 && authored === undefined); 172+ const authorOf = authorsOf(graph); 61173 let files: string[]; 63175 files = [...touched].filter((path) => differs(before, after, path)).sort(); Lines 178–189 at 318 (were 66–71 at 207)
66178 for (const path of files) storage.write(COUNTS, path, visibleCount(after, path)); 67179 storage.write(META, "version", v); 181+ // Every touched file's totals from its layout: one whose lines are the same texts by someone else isn't among 182+ // the changed files, but its authors changed. 184+ for (const path of touched) keepTotals(storage, v, path, laidOutTotals(after, path, authorOf)); 185+ storage.write(AUTHORS_META, "version", v); 71189 const beforeSet = host.at(v - 1); Lines 194–208 at 318 (were 76–86 at 207)
77195 const fileOf = (view: ReadView, line: string, n: number) => 78196 view.hasMoves() ? view.fileOf(line) : graph.writtenIn(n); 79− // Per file: visible lines added minus removed, and the texts' balance.
80− const changes = new Map<string, { net: number; texts: Map<string, number>; lines: number }>();
197+ // Per file: visible lines added minus removed, and the texts' balance; and, while totals are kept, each author's lines 198+ // and the bytes added minus removed (a line and its newline, as joinLines makes the file). 199+ const changes = new Map< 201+ { net: number; texts: Map<string, number>; lines: number; authors: Map<string, number>; bytes: number } 81203 const change = (path: string) => { 82204 let c = changes.get(path); 83− if (!c) changes.set(path, (c = { net: 0, texts: new Map(), lines: 0 }));
205+ if (!c) changes.set(path, (c = { net: 0, texts: new Map(), lines: 0, authors: new Map(), bytes: 0 })); 86208 for (const id of p.atoms) { Lines 214–251 at 318 (were 92–108 at 207)
94216 c.texts.set(text, (c.texts.get(text) ?? 0) + 1); 218+ const author = authorOf(id); 219+ c.authors.set(author, (c.authors.get(author) ?? 0) + 1); 220+ c.bytes += utf8Bytes(text) + 1; 96223 for (const line of graph.deletedLines(id)) { 97224 const n = graph.lineN(line); 98− if (!beforeSet.has(graph.idOfSeq(graph.writerSeqOf(n))) || !visibleIn(beforeSet, n)) continue;
225+ const writer = graph.idOfSeq(graph.writerSeqOf(n)); 226+ if (!beforeSet.has(writer) || !visibleIn(beforeSet, n)) continue; 99227 const c = change(fileOf(before, line, n)); 100228 const text = graph.textOf(n); 103231 c.texts.set(text, (c.texts.get(text) ?? 0) - 1); 233+ const author = authorOf(writer); 234+ c.authors.set(author, (c.authors.get(author) ?? 0) - 1); 235+ c.bytes -= utf8Bytes(text) + 1; 240+ for (const [path, c] of changes) { 241+ if (c.lines === 0) continue; 242+ const was = storage.read(AUTHORS, path) as StoredTotals | undefined; 243+ const authors = new Map(was?.a ?? []); 244+ for (const [author, d] of c.authors) authors.set(author, (authors.get(author) ?? 0) + d); 245+ keepTotals(storage, v, path, normalTotals(authors, (was?.b ?? 0) + c.bytes)); 247+ storage.write(AUTHORS_META, "version", v); 107250 const exists = new Map<string, readonly [boolean, boolean]>(); 108251 for (const path of touched) {