- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/pathindex.ts?draft=login-timeout&format=json
196 linesFirst import 100%0 versions changed it
1// Each version's files and shape, and each path's history in a space (#41; #46, disk-first 1). Built the first
2// time it's asked for and kept up as versions land; a version's files are also kept in the shapes table, so a
3// load needn't lay them out again.
5import { type AtomId, type Graph, type LineId, View } from "../../src/engine/index.ts";
6import {
7 differs,
8 type LastChange,
9 type PathLife,
10 type ProjectOptions,
11 shapeStamp,
12 type VersionShape,
13} from "./project.ts";
14import { last, SHARED, type Space } from "./spaces.ts";
16/** What the path index needs from a project. */
17export interface PathIndexDeps {
18 readonly graph: Graph;
19 at(space: Space, version: number): ReadonlySet<AtomId>;
20 viewAt(space: Space, version: number): View;
21 touchedFiles(ids: Iterable<AtomId>, views: readonly View[]): Set<string>;
22 readonly shapes: ProjectOptions["shapes"];
23}
25export class PathIndex {
26 readonly #deps: PathIndexDeps;
28 constructor(deps: PathIndexDeps) {
29 this.#deps = deps;
30 }
32 /** Whether a version's files are already worked out (a file's log skips versions that can't name it, #43). */
33 knowsFiles(space: Space, version: number): boolean {
34 return this.#filesCache.get(space)?.has(version) ?? false;
35 }
37 /** Each version's changed files (#43): a published version never changes, so they're worked out once. */
38 readonly #filesCache = new WeakMap<Space, Map<number, string[]>>();
40 /** Each version's files made and taken out, and moves (#41), worked out with its files. */
41 readonly #shapeCache = new WeakMap<Space, Map<number, VersionShape>>();
43 /** Each space's path index (#41): built the first time it's asked for, then kept up as versions land. */
44 readonly #pathIndex = new WeakMap<Space, { upTo: number; paths: Map<string, PathLife> }>();
46 /** 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[] {
48 let cache = this.#filesCache.get(space);
49 if (!cache) this.#filesCache.set(space, (cache = new Map()));
50 const known = cache.get(version);
51 if (known) return known;
52 const kept = this.#keptFiles(space, version);
53 if (kept) return kept;
54 const shapes = this.#shapeCache.get(space)!;
55 const p = space.versions[version]!;
56 const viewAt = (v: number): View => {
57 let view = views.get(v);
58 if (!view) views.set(v, (view = v < 0 ? new View(this.#deps.graph, []) : this.#deps.viewAt(space, v)));
59 return view;
60 };
61 const before = viewAt(version - 1);
62 const after = viewAt(version);
63 // Net changes only, as show has them: a file made and removed again before the publish isn't one (#37).
64 const touched = this.#deps.touchedFiles([...p.atoms, ...(p.dropped ?? [])], [before, after]);
65 const files = [...touched].filter((path) => differs(before, after, path)).sort();
66 cache.set(version, files);
67 // Files made and taken out, and which of those are one file moved (#41): an atom that moves lines
68 // 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));
71 const moves: [string, string][] = [];
72 if (made.length > 0 && gone.length > 0) {
73 const movers = new Set(this.#deps.graph.movers());
74 const moving = p.atoms.filter((id) => movers.has(id)).map((id) => this.#deps.graph.filesOf(id));
75 const taken = new Set<string>();
76 for (const to of made) {
77 const blob = after.attrs(to).blob;
78 const from = gone.find(
79 (g) =>
80 !taken.has(g) &&
81 (moving.some((fs) => fs.includes(g) && fs.includes(to)) ||
82 (blob !== null && before.attrs(g).blob === blob)),
83 );
84 if (from !== undefined) taken.add(from), moves.push([from, to]);
85 }
86 }
87 shapes.set(version, { made, gone, moves });
88 this.#deps.shapes?.put(space.name, version, JSON.stringify({ stamp: shapeStamp(p), files, made, gone, moves }));
89 return files;
90 }
92 /** A version's files kept from an earlier load (#41), when the row describes this very version; into the caches. */
93 #keptFiles(space: Space, version: number): string[] | undefined {
94 let cache = this.#filesCache.get(space);
95 if (!cache) this.#filesCache.set(space, (cache = new Map()));
96 let shapes = this.#shapeCache.get(space);
97 if (!shapes) this.#shapeCache.set(space, (shapes = new Map()));
98 const known = cache.get(version);
99 if (known) return known;
100 const row = this.#deps.shapes?.get(space.name, version);
101 if (row === undefined) return undefined;
102 const kept = JSON.parse(row) as { stamp: string; files: string[] } & VersionShape;
103 if (kept.stamp !== shapeStamp(space.versions[version]!)) return undefined;
104 cache.set(version, kept.files);
105 shapes.set(version, { made: kept.made, gone: kept.gone, moves: kept.moves });
106 return kept.files;
107 }
109 /** A version's files made and taken out, and moves (#41). */
110 versionShape(space: Space, version: number, views?: Map<number, View>): VersionShape {
111 const known = this.#shapeCache.get(space)?.get(version);
112 if (known) return known;
113 this.#filesCache.get(space)?.delete(version);
114 this.versionFiles(space, version, views);
115 return this.#shapeCache.get(space)!.get(version)!;
116 }
118 /**
119 * The space's path index (#41): for each path ever in it, the versions that
120 * changed it, and the versions that made it and took it out. The first ask
121 * after a restart walks every version, as a cold log over all of them does;
122 * later asks only walk the versions since.
123 */
124 pathsOf(space: Space): Map<string, PathLife> {
125 let index = this.#pathIndex.get(space);
126 if (!index) this.#pathIndex.set(space, (index = { upTo: -1, paths: new Map() }));
127 const views = new Map<number, View>();
128 // Shared's sets grow version by version, so walk them forward rather than build each from the
129 // start; the views stay out of the cache, which they would only push other views from. A version
130 // whose files are kept (#41) needs no views at all.
131 let running: Set<AtomId> | undefined;
132 let runningAt = -1;
133 const setAt = (u: number): Set<AtomId> => {
134 if (!running || runningAt > u) (running = new Set(u < 0 ? [] : this.#deps.at(space, u))), (runningAt = u);
135 while (runningAt < u) {
136 const q = space.versions[++runningAt]!;
137 for (const id of q.atoms) running.add(id);
138 for (const id of q.dropped ?? []) running.delete(id);
139 }
140 return running;
141 };
142 for (let v = index.upTo + 1; v <= last(space); v++) {
143 if (space.name === SHARED && !this.#keptFiles(space, v)) {
144 if (v > 0 && !views.has(v - 1)) views.set(v - 1, new View(this.#deps.graph, new Set(setAt(v - 1))));
145 views.set(v, new View(this.#deps.graph, new Set(setAt(v))));
146 }
147 const files = this.versionFiles(space, v, views);
148 const shape = this.versionShape(space, v, views);
149 views.delete(v - 1);
150 const life = (path: string): PathLife => {
151 let l = index!.paths.get(path);
152 if (!l) index!.paths.set(path, (l = { changed: [], events: [] }));
153 return l;
154 };
155 for (const f of files) life(f).changed.push(v);
156 const from = new Map(shape.moves.map(([a, b]) => [b, a]));
157 const to = new Map(shape.moves);
158 for (const f of shape.made)
159 life(f).events.push({ version: v, made: true, ...(from.has(f) ? { other: from.get(f)! } : {}) });
160 for (const f of shape.gone)
161 life(f).events.push({ version: v, made: false, ...(to.has(f) ? { other: to.get(f)! } : {}) });
162 index.upTo = v;
163 }
164 return index.paths;
165 }
167 lastChange(space: Space, version: number): LastChange {
168 const p = space.versions[version]!;
169 return { version, agent: p.agent, time: p.time, message: p.message };
170 }
172 /** The newest version in a sorted list no later than `version`, or undefined. */
173 static atOrBefore(list: readonly number[], version: number): number | undefined {
174 let lo = 0;
175 let hi = list.length;
176 while (lo < hi) {
177 const mid = (lo + hi) >> 1;
178 if (list[mid]! <= version) lo = mid + 1;
179 else hi = mid;
180 }
181 return lo > 0 ? list[lo - 1] : undefined;
182 }
184 /** The newest version in (from, to] that took a line out: one deleting it, or a drop of the change that wrote it. */
185 removedIn(space: Space, from: number, to: number, line: LineId): { removed?: number } {
186 const g = this.#deps.graph;
187 const deleters = new Set(g.deletersOf(line));
188 const writer = g.line(line)?.atom;
189 for (let v = to; v > from; v--) {
190 const p = space.versions[v]!;
191 if (p.atoms.some((id) => deleters.has(id)) || (writer !== undefined && p.dropped?.includes(writer)))
192 return { removed: v };
193 }
194 return {};
195 }
196}