Skip to content
File

Blob: src/worker/git/pack/snapshot.ts

typescript86 lines
1import type { CacheContext } from "@/worker/cache";
2import type { Logger } from "@/worker/common/logger";
3import type {
4 OrderedPackSnapshot,
5 OrderedPackSnapshotEntry,
6} from "@/worker/git/operations/fetch/types";
7 
8import { getOidHexAt, loadActivePackCatalog, loadIdxView } from "@/worker/git/object-store";
9 
10export type SnapshotLoadResult =
11 | {
12 type: "Ready";
13 snapshot: OrderedPackSnapshot;
14 }
15 | {
16 type: "RepositoryNotReady";
17 reason: "no-active-packs" | "snapshot-missing-idx";
18 };
19 
20export async function loadOrderedPackSnapshot(
21 env: Env,
22 repoId: string,
23 cacheCtx: CacheContext | undefined,
24 log: Logger
25): Promise<SnapshotLoadResult> {
26 const rows = await loadActivePackCatalog(env, repoId, cacheCtx);
27 if (rows.length === 0) {
28 return {
29 type: "RepositoryNotReady",
30 reason: "no-active-packs",
31 };
32 }
33 
34 let idxMemoHits = 0;
35 let idxLoads = 0;
36 let indexedObjects = 0;
37 const packs: OrderedPackSnapshotEntry[] = [];
38 
39 for (const row of rows) {
40 const memoHit = cacheCtx?.memo?.idxViews?.has(row.packKey) === true;
41 if (memoHit) idxMemoHits++;
42 
43 const idx = await loadIdxView(env, row.packKey, cacheCtx, row.packBytes);
44 idxLoads++;
45 if (!idx) {
46 log.warn("stream:plan:snapshot-missing-idx", { packKey: row.packKey });
47 return {
48 type: "RepositoryNotReady",
49 reason: "snapshot-missing-idx",
50 };
51 }
52 
53 indexedObjects += idx.count;
54 packs.push({
55 packKey: row.packKey,
56 packBytes: row.packBytes,
57 idx,
58 });
59 }
60 
61 log.info("stream:plan:snapshot", {
62 packs: packs.length,
63 idxLoads,
64 idxMemoHits,
65 idxMemoMisses: idxLoads - idxMemoHits,
66 indexedObjects,
67 });
68 
69 return {
70 type: "Ready",
71 snapshot: { packs },
72 };
73}
74 
75export function buildInitialCloneNeeded(snapshot: OrderedPackSnapshot): string[] {
76 const needed = new Set<string>();
77 
78 for (const pack of snapshot.packs) {
79 for (let index = 0; index < pack.idx.count; index++) {
80 needed.add(getOidHexAt(pack.idx, index));
81 }
82 }
83 
84 return Array.from(needed);
85}