Lines 10–105 at 251 (were 10–21 at 250)
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 }; 82+/** A file's totals from its layout in a view, as tree with authors counts them. */ 83+function laidOutTotals(view: ReadView, path: string, authorOf: (id: AtomId) => string): StoredTotals | null { 84+ if (!view.hasFile(path)) return null; 85+ const authors = new Map<string, number>(); 86+ for (const l of view.lines(path)) { 87+ const author = authorOf(l.atom); 88+ authors.set(author, (authors.get(author) ?? 0) + 1); 90+ return normalTotals(authors, authors.size === 0 ? 0 : utf8Bytes(view.text(path))); 93+/** Keep a file's totals at version `v`, when they differ from what it had. */ 94+function keepTotals(storage: GraphStateStorage, v: number, path: string, totals: StoredTotals | null): void { 95+ const was = (storage.read(AUTHORS, path) as StoredTotals | undefined) ?? null; 96+ if (JSON.stringify(was) === JSON.stringify(totals)) return; 97+ if (totals) storage.write(AUTHORS, path, totals); 98+ else storage.remove(AUTHORS, path); 99+ const log = (storage.read(AUTHORS_LOG, path) as [number, StoredTotals | null][] | undefined) ?? []; 100+ storage.write(AUTHORS_LOG, path, [...log, [v, totals]]); 19103/** The shared version the running counts describe, or undefined when this writer keeps none. */ 20104export function countedVersion(storage: GraphStateStorage): number | undefined { 21105 return storage.read(META, "version") as number | undefined; Lines 142–151 at 251 (were 58–63 at 250)
58142 // The same moves as the version before: share them, and where lines sit, instead of resolving them again. 59143 if (fast && before instanceof View && after instanceof View) after.shareMoves(before); 60144 const touched = touchedFiles(graph, atoms, [before, after]); 145+ // Author totals start with the first version too, and are kept only while every version since has kept them. 146+ const authored = authoredVersion(storage); 147+ const authoring = authored === v - 1 || (v === 0 && authored === undefined); 148+ const authorOf = authorsOf(graph); 61149 let files: string[]; 63151 files = [...touched].filter((path) => differs(before, after, path)).sort(); Lines 154–165 at 251 (were 66–71 at 250)
66154 for (const path of files) storage.write(COUNTS, path, visibleCount(after, path)); 67155 storage.write(META, "version", v); 157+ // Every touched file's totals from its layout: one whose lines are the same texts by someone else isn't among 158+ // the changed files, but its authors changed. 160+ for (const path of touched) keepTotals(storage, v, path, laidOutTotals(after, path, authorOf)); 161+ storage.write(AUTHORS_META, "version", v); 71165 const beforeSet = host.at(v - 1); Lines 170–184 at 251 (were 76–86 at 250)
77171 const fileOf = (view: ReadView, line: string, n: number) => 78172 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 }>();
173+ // Per file: visible lines added minus removed, and the texts' balance; and, while totals are kept, each author's lines 174+ // and the bytes added minus removed (a line and its newline, as joinLines makes the file). 175+ const changes = new Map< 177+ { net: number; texts: Map<string, number>; lines: number; authors: Map<string, number>; bytes: number } 81179 const change = (path: string) => { 82180 let c = changes.get(path); 83− if (!c) changes.set(path, (c = { net: 0, texts: new Map(), lines: 0 }));
181+ if (!c) changes.set(path, (c = { net: 0, texts: new Map(), lines: 0, authors: new Map(), bytes: 0 })); 86184 for (const id of p.atoms) { Lines 190–227 at 251 (were 92–108 at 250)
94192 c.texts.set(text, (c.texts.get(text) ?? 0) + 1); 194+ const author = authorOf(id); 195+ c.authors.set(author, (c.authors.get(author) ?? 0) + 1); 196+ c.bytes += utf8Bytes(text) + 1; 96199 for (const line of graph.deletedLines(id)) { 97200 const n = graph.lineN(line); 98− if (!beforeSet.has(graph.idOfSeq(graph.writerSeqOf(n))) || !visibleIn(beforeSet, n)) continue;
201+ const writer = graph.idOfSeq(graph.writerSeqOf(n)); 202+ if (!beforeSet.has(writer) || !visibleIn(beforeSet, n)) continue; 99203 const c = change(fileOf(before, line, n)); 100204 const text = graph.textOf(n); 103207 c.texts.set(text, (c.texts.get(text) ?? 0) - 1); 209+ const author = authorOf(writer); 210+ c.authors.set(author, (c.authors.get(author) ?? 0) - 1); 211+ c.bytes -= utf8Bytes(text) + 1; 216+ for (const [path, c] of changes) { 217+ if (c.lines === 0) continue; 218+ const was = storage.read(AUTHORS, path) as StoredTotals | undefined; 219+ const authors = new Map(was?.a ?? []); 220+ for (const [author, d] of c.authors) authors.set(author, (authors.get(author) ?? 0) + d); 221+ keepTotals(storage, v, path, normalTotals(authors, (was?.b ?? 0) + c.bytes)); 223+ storage.write(AUTHORS_META, "version", v); 107226 const exists = new Map<string, readonly [boolean, boolean]>(); 108227 for (const path of touched) {