|
| 1 | +/** |
| 2 | + * Incremental hot reload — specs/behaviors/storage.md → "Hot reload". |
| 3 | + * |
| 4 | + * Instead of re-reading every sheet, diff each in-memory sheet between the |
| 5 | + * commit the live state reflects and the new HEAD (gitsheets |
| 6 | + * `Sheet.diffFrom`), then unindex each changed record's old version and |
| 7 | + * index its new one. `unindex*` is the exact inverse of `index*`, so the |
| 8 | + * result equals a full load of the new tree. |
| 9 | + */ |
| 10 | +import { RECORD_PATH_KEY, RECORD_SHEET_KEY, type DiffChange } from 'gitsheets'; |
| 11 | + |
| 12 | +import type { FtsEngine } from '../fts.js'; |
| 13 | +import type { PublicStore } from '../public.js'; |
| 14 | +import { |
| 15 | + indexBlogPost, |
| 16 | + indexHelpWantedInterest, |
| 17 | + indexHelpWantedRole, |
| 18 | + indexMembership, |
| 19 | + indexPerson, |
| 20 | + indexPersonEvaluation, |
| 21 | + indexProject, |
| 22 | + indexProjectBuzz, |
| 23 | + indexProjectUpdate, |
| 24 | + indexSlugHistory, |
| 25 | + indexTag, |
| 26 | + indexTagAssignment, |
| 27 | + unindexBlogPost, |
| 28 | + unindexHelpWantedInterest, |
| 29 | + unindexHelpWantedRole, |
| 30 | + unindexMembership, |
| 31 | + unindexPerson, |
| 32 | + unindexPersonEvaluation, |
| 33 | + unindexProject, |
| 34 | + unindexProjectBuzz, |
| 35 | + unindexProjectUpdate, |
| 36 | + unindexSlugHistory, |
| 37 | + unindexTag, |
| 38 | + unindexTagAssignment, |
| 39 | + type InMemoryState, |
| 40 | +} from './state.js'; |
| 41 | + |
| 42 | +/** More changed records than this and a full rebuild is cheaper and safer. */ |
| 43 | +export const INCREMENTAL_RECORD_LIMIT = 5_000; |
| 44 | + |
| 45 | +type Sheets = PublicStore; |
| 46 | +type RecordOf<K extends keyof Sheets> = Sheets[K] extends { queryAll(): Promise<(infer T)[]> } |
| 47 | + ? T |
| 48 | + : never; |
| 49 | + |
| 50 | +interface SheetHandler<K extends keyof Sheets> { |
| 51 | + readonly sheet: K; |
| 52 | + readonly unindex: (state: InMemoryState, record: RecordOf<K>) => void; |
| 53 | + readonly index: (state: InMemoryState, record: RecordOf<K>) => void; |
| 54 | + readonly ftsRemove?: (fts: FtsEngine, record: RecordOf<K>) => void; |
| 55 | + readonly ftsAdd?: (fts: FtsEngine, record: RecordOf<K>) => void; |
| 56 | +} |
| 57 | + |
| 58 | +function handler<K extends keyof Sheets>(h: SheetHandler<K>): SheetHandler<K> { |
| 59 | + return h; |
| 60 | +} |
| 61 | + |
| 62 | +/** Every sheet the loader reads into InMemoryState, in loader order. */ |
| 63 | +const HANDLERS = [ |
| 64 | + handler({ |
| 65 | + sheet: 'projects', |
| 66 | + unindex: unindexProject, |
| 67 | + index: indexProject, |
| 68 | + ftsRemove: (fts, p) => fts.removeProject(p.slug), |
| 69 | + ftsAdd: (fts, p) => { |
| 70 | + if (!p.deletedAt) fts.upsertProject(p.slug, p.title, p.summary ?? '', p.overview ?? ''); |
| 71 | + }, |
| 72 | + }), |
| 73 | + handler({ |
| 74 | + sheet: 'people', |
| 75 | + unindex: unindexPerson, |
| 76 | + index: indexPerson, |
| 77 | + ftsRemove: (fts, p) => fts.removePerson(p.slug), |
| 78 | + ftsAdd: (fts, p) => { |
| 79 | + if (!p.deletedAt) fts.upsertPerson(p.slug, p.fullName, p.bio ?? ''); |
| 80 | + }, |
| 81 | + }), |
| 82 | + handler({ sheet: 'tags', unindex: unindexTag, index: indexTag }), |
| 83 | + handler({ sheet: 'tag-assignments', unindex: unindexTagAssignment, index: indexTagAssignment }), |
| 84 | + handler({ sheet: 'project-memberships', unindex: unindexMembership, index: indexMembership }), |
| 85 | + handler({ sheet: 'project-updates', unindex: unindexProjectUpdate, index: indexProjectUpdate }), |
| 86 | + handler({ sheet: 'project-buzz', unindex: unindexProjectBuzz, index: indexProjectBuzz }), |
| 87 | + handler({ sheet: 'blog-posts', unindex: unindexBlogPost, index: indexBlogPost }), |
| 88 | + handler({ |
| 89 | + sheet: 'help-wanted-roles', |
| 90 | + unindex: unindexHelpWantedRole, |
| 91 | + index: indexHelpWantedRole, |
| 92 | + ftsRemove: (fts, r) => fts.removeHelpWanted(r.id), |
| 93 | + ftsAdd: (fts, r) => fts.upsertHelpWanted(r.id, r.title, r.description), |
| 94 | + }), |
| 95 | + handler({ |
| 96 | + sheet: 'help-wanted-interest', |
| 97 | + unindex: unindexHelpWantedInterest, |
| 98 | + index: indexHelpWantedInterest, |
| 99 | + }), |
| 100 | + handler({ |
| 101 | + sheet: 'slug-history', |
| 102 | + unindex: unindexSlugHistory, |
| 103 | + index: (state, r) => indexSlugHistory(state, r), |
| 104 | + }), |
| 105 | + handler({ |
| 106 | + sheet: 'person-evaluations', |
| 107 | + unindex: unindexPersonEvaluation, |
| 108 | + index: indexPersonEvaluation, |
| 109 | + }), |
| 110 | +] as const; |
| 111 | + |
| 112 | +interface CollectedChanges { |
| 113 | + readonly handler: SheetHandler<keyof Sheets>; |
| 114 | + readonly changes: readonly DiffChange[]; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Collect every changed record between `fromCommit` and each sheet's current |
| 119 | + * read snapshot. Returns null when more than {@link INCREMENTAL_RECORD_LIMIT} |
| 120 | + * records changed, meaning the caller should do a full rebuild. |
| 121 | + */ |
| 122 | +export async function collectChanges( |
| 123 | + store: PublicStore, |
| 124 | + fromCommit: string, |
| 125 | +): Promise<CollectedChanges[] | null> { |
| 126 | + const collected: CollectedChanges[] = []; |
| 127 | + let total = 0; |
| 128 | + for (const h of HANDLERS) { |
| 129 | + const changes: DiffChange[] = []; |
| 130 | + for await (const change of store[h.sheet].diffFrom(fromCommit, { records: true })) { |
| 131 | + changes.push(change); |
| 132 | + if (++total > INCREMENTAL_RECORD_LIMIT) return null; |
| 133 | + } |
| 134 | + if (changes.length > 0) { |
| 135 | + collected.push({ handler: h as unknown as SheetHandler<keyof Sheets>, changes }); |
| 136 | + } |
| 137 | + } |
| 138 | + return collected; |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Apply collected changes to the live state and FTS index. Synchronous, so |
| 143 | + * no request handler observes a half-applied diff. |
| 144 | + */ |
| 145 | +export function applyChanges( |
| 146 | + state: InMemoryState, |
| 147 | + fts: FtsEngine, |
| 148 | + collected: readonly CollectedChanges[], |
| 149 | +): number { |
| 150 | + let applied = 0; |
| 151 | + for (const { handler: h, changes } of collected) { |
| 152 | + for (const change of changes) { |
| 153 | + const src = change.src as RecordOf<keyof Sheets> | undefined; |
| 154 | + const dst = change.dst as RecordOf<keyof Sheets> | undefined; |
| 155 | + if (src) { |
| 156 | + h.unindex(state, src); |
| 157 | + h.ftsRemove?.(fts, src); |
| 158 | + } |
| 159 | + if (dst) { |
| 160 | + // Annotate like `queryAll()` does: gitsheets locates a record's |
| 161 | + // current path through these on a later upsert/delete. |
| 162 | + Object.assign(dst, { [RECORD_SHEET_KEY]: h.sheet, [RECORD_PATH_KEY]: change.path }); |
| 163 | + h.index(state, dst); |
| 164 | + h.ftsAdd?.(fts, dst); |
| 165 | + } |
| 166 | + applied++; |
| 167 | + } |
| 168 | + } |
| 169 | + return applied; |
| 170 | +} |
0 commit comments