Lines 1–9 at 318 (were 1–8 at 2)
1+import { View } from "../../src/engine/index.ts"; 12// Each version's files and shape, and each path's history in a space (#41; #46, disk-first 1). Built the first 23// time it's asked for and kept up as versions land; a version's files are also kept in the shapes table, so a 34// load needn't lay them out again. 5−import { type AtomId, type Graph, type LineId, View } from "../../src/engine/index.ts";
6+import type { AtomId, Graph, LineId, ReadView } from "../../src/engine/index.ts"; Lines 18–31 at 318 (were 17–27 at 2)
1718export interface PathIndexDeps { 1819 readonly graph: Graph; 1920 at(space: Space, version: number): ReadonlySet<AtomId>; 20− viewAt(space: Space, version: number): View;
21− touchedFiles(ids: Iterable<AtomId>, views: readonly View[]): Set<string>;
21+ viewAt(space: Space, version: number): ReadView; 22+ touchedFiles(ids: Iterable<AtomId>, views: readonly ReadView[]): Set<string>; 2223 readonly shapes: ProjectOptions["shapes"]; 26+/** Older versions' file lists kept per space (a few agents' bases): about 1,000 paths each on abso. */ 27+const OLDER_PATH_SETS = 8; 2529export class PathIndex { 2630 readonly #deps: PathIndexDeps; Lines 44–134 at 318 (were 40–50 at 2)
4044 /** Each version's files made and taken out, and moves (#41), worked out with its files. */ 4145 readonly #shapeCache = new WeakMap<Space, Map<number, VersionShape>>(); 47+ /** The files at the newest version folded so far from kept shapes, per space (keptPaths). */ 48+ readonly #keptPaths = new WeakMap<Space, { version: number; files: Set<string> }>(); 49+ /** The files at a few older versions, worked back from the newest (agents' bases, mostly), most recent last. */ 50+ readonly #olderPaths = new WeakMap<Space, Map<number, Set<string>>>(); 53+ * The files at a version older than the newest folded (abso Capacity): the newest's files with each version since 54+ * undone, its made files taken out and its gone ones put back. Folding from version 0 again for an agent's base 55+ * read every version's kept row on each call. 59+ fold: { version: number; files: Set<string> }, 61+ ): ReadonlySet<string> | undefined { 62+ let older = this.#olderPaths.get(space); 63+ if (!older) this.#olderPaths.set(space, (older = new Map())); 64+ const known = older.get(version); 66+ older.delete(version); 67+ older.set(version, known); 70+ const files = new Set(fold.files); 71+ for (let v = fold.version; v > version; v--) { 72+ if (!this.#keptFiles(space, v)) return undefined; 73+ const shape = this.#shapeCache.get(space)?.get(v); 74+ if (!shape) return undefined; 75+ for (const f of shape.made) files.delete(f); 76+ for (const f of shape.gone) files.add(f); 78+ older.set(version, files); 79+ if (older.size > OLDER_PATH_SETS) older.delete(older.keys().next().value!); 84+ * The files a space has at a version, from the kept rows of every version up to it: each one's files made and 85+ * taken out, folded from version 0 (abso Capacity). Undefined when a version has no kept row that describes it; 86+ * then the caller lays the version out. 88+ keptPaths(space: Space, version: number): string[] | undefined { 89+ const files = this.keptPathSet(space, version); 90+ return files && [...files].sort(); 94+ * The files the versions after `from`, up to `to`, changed, from each version's kept row (abso 738): undefined when one 95+ * has none, and the caller works them out from the atoms. Every file whose text differs between the two versions is 96+ * in it, since some version between changed it. 98+ keptChangesSince(space: Space, from: number, to: number): ReadonlySet<string> | undefined { 99+ const out = new Set<string>(); 100+ for (let v = from + 1; v <= to; v++) { 101+ const files = this.#keptFiles(space, v); 102+ if (!files) return undefined; 103+ for (const f of files) out.add(f); 108+ /** keptPaths as a set, kept for the newest version asked: never changed once made, so a view can hold it. */ 109+ keptPathSet(space: Space, version: number): ReadonlySet<string> | undefined { 110+ let fold = this.#keptPaths.get(space); 111+ if (fold?.version === version) return fold.files; 112+ if (fold && fold.version > version) return this.#olderPathSet(space, fold, version); 113+ if (!fold) fold = { version: -1, files: new Set() }; 114+ const files = new Set(fold.files); 115+ for (let v = fold.version + 1; v <= version; v++) { 116+ if (!this.#keptFiles(space, v)) return undefined; 117+ // Not yet when the version is still being kept (keep asks its views which files exist). 118+ const shape = this.#shapeCache.get(space)?.get(v); 119+ if (!shape) return undefined; 120+ for (const f of shape.gone) files.delete(f); 121+ for (const f of shape.made) files.add(f); 123+ this.#keptPaths.set(space, { version, files }); 43127 /** Each space's path index (#41): built the first time it's asked for, then kept up as versions land. */ 44128 readonly #pathIndex = new WeakMap<Space, { upTo: number; paths: Map<string, PathLife> }>(); 46130 /** The files a version changed. `views` holds views by version, so a run of versions builds each once. */ 47− versionFiles(space: Space, version: number, views = new Map<number, View>()): string[] {
131+ versionFiles(space: Space, version: number, views = new Map<number, ReadView>()): string[] { 48132 let cache = this.#filesCache.get(space); 49133 if (!cache) this.#filesCache.set(space, (cache = new Map())); 50134 const known = cache.get(version); Lines 137–143 at 318 (were 53–59 at 2)
53137 if (kept) return kept; 54138 const shapes = this.#shapeCache.get(space)!; 55139 const p = space.versions[version]!; 56− const viewAt = (v: number): View => {
140+ const viewAt = (v: number): ReadView => { 57141 let view = views.get(v); 58142 if (!view) views.set(v, (view = v < 0 ? new View(this.#deps.graph, []) : this.#deps.viewAt(space, v))); Lines 147–180 at 318 (were 63–73 at 2)
63147 // Net changes only, as show has them: a file made and removed again before the publish isn't one (#37). 64148 const touched = this.#deps.touchedFiles([...p.atoms, ...(p.dropped ?? [])], [before, after]); 65149 const files = [...touched].filter((path) => differs(before, after, path)).sort(); 150+ this.keep(space, version, files, before, after); 155+ * Keep a version's changed files, with its files made and taken out and its moves: worked out here, or as the 156+ * version landed (version-files.ts, abso Capacity), which says which files existed before and after. 164+ exists?: ReadonlyMap<string, readonly [boolean, boolean]>, 166+ let cache = this.#filesCache.get(space); 167+ if (!cache) this.#filesCache.set(space, (cache = new Map())); 168+ let shapes = this.#shapeCache.get(space); 169+ if (!shapes) this.#shapeCache.set(space, (shapes = new Map())); 170+ const p = space.versions[version]!; 66171 cache.set(version, files); 67172 // Files made and taken out, and which of those are one file moved (#41): an atom that moves lines 68173 // names both files, and a binary or empty file moved keeps its blob. 69− const made = files.filter((f) => !before.hasFile(f) && after.hasFile(f));
70− const gone = files.filter((f) => before.hasFile(f) && !after.hasFile(f));
174+ const had = (f: string) => exists?.get(f)?.[0] ?? before.hasFile(f); 175+ const has = (f: string) => exists?.get(f)?.[1] ?? after.hasFile(f); 176+ const made = files.filter((f) => !had(f) && has(f)); 177+ const gone = files.filter((f) => had(f) && !has(f)); 71178 const moves: [string, string][] = []; 72179 if (made.length > 0 && gone.length > 0) { 73180 const movers = new Set(this.#deps.graph.movers()); Lines 193–198 at 318 (were 86–92 at 2)
87194 shapes.set(version, { made, gone, moves }); 88195 this.#deps.shapes?.put(space.name, version, JSON.stringify({ stamp: shapeStamp(p), files, made, gone, moves })); 89− return files;
92198 /** A version's files kept from an earlier load (#41), when the row describes this very version; into the caches. */ Lines 213–219 at 318 (were 107–113 at 2)
109215 /** A version's files made and taken out, and moves (#41). */ 110− versionShape(space: Space, version: number, views?: Map<number, View>): VersionShape {
216+ versionShape(space: Space, version: number, views?: Map<number, ReadView>): VersionShape { 111217 const known = this.#shapeCache.get(space)?.get(version); 112218 if (known) return known; 113219 this.#filesCache.get(space)?.delete(version); Lines 230–236 at 318 (were 124–130 at 2)
124230 pathsOf(space: Space): Map<string, PathLife> { 125231 let index = this.#pathIndex.get(space); 126232 if (!index) this.#pathIndex.set(space, (index = { upTo: -1, paths: new Map() })); 127− const views = new Map<number, View>();
233+ const views = new Map<number, ReadView>(); 128234 // Shared's sets grow version by version, so walk them forward rather than build each from the 129235 // start; the views stay out of the cache, which they would only push other views from. A version 130236 // whose files are kept (#41) needs no views at all. Lines 246–254 at 318 (were 140–146 at 2)
142248 for (let v = index.upTo + 1; v <= last(space); v++) { 143− if (space.name === SHARED && !this.#keptFiles(space, v)) {
249+ // Persistent graphs use the indexed version reader; a raw View would 250+ // rebuild move scratch state and copy historical membership during a read. 251+ if (space.name === SHARED && !this.#deps.graph.hasOrderedChildren && !this.#keptFiles(space, v)) { 144252 if (v > 0 && !views.has(v - 1)) views.set(v - 1, new View(this.#deps.graph, new Set(setAt(v - 1)))); 145253 views.set(v, new View(this.#deps.graph, new Set(setAt(v))));