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