75 lines · 4.8 KBRaw
| 1 | import type { CodeGraph, CodeMapState, CodeMapSummary } from "../contract/views.ts"; |
| 2 | |
| 3 | /** Raise this when the indexer changes what it finds. Maps built by an older version are built again on first use. */ |
| 4 | export const CODE_MAP_VERSION = 1; |
| 5 | /** A build that has not finished in this time is taken over by the next request. */ |
| 6 | export const BUILD_TIMEOUT_SECONDS = 10 * 60; |
| 7 | /** A failed build is tried again after this time, so a passing error does not hide the map for good. */ |
| 8 | export const RETRY_FAILED_SECONDS = 30 * 60; |
| 9 | const MAX_STORED_BYTES = 1_900_000; |
| 10 | |
| 11 | type Status = "building" | "ready" | "empty" | "too_large" | "failed"; |
| 12 | |
| 13 | interface Row { |
| 14 | status: Status; |
| 15 | version: number; |
| 16 | graph: ArrayBuffer | number[] | null; |
| 17 | started_at: number; |
| 18 | } |
| 19 | |
| 20 | /** The stored map of a commit, or null when there is none to use and a build should start. */ |
| 21 | export async function readCodeMap(db: D1Database, repoId: string, commit: string, at: number): Promise<CodeMapState | null> { |
| 22 | const row = await db.prepare("SELECT status, version, graph, started_at FROM code_maps WHERE repo_id = ? AND commit_sha = ?").bind(repoId, commit).first<Row>(); |
| 23 | if (!row || row.version < CODE_MAP_VERSION) return null; |
| 24 | if (row.status === "building") return row.started_at > at - BUILD_TIMEOUT_SECONDS ? { kind: "indexing", commit } : null; |
| 25 | if (row.status === "failed") return row.started_at > at - RETRY_FAILED_SECONDS ? { kind: "unavailable", commit, reason: "failed" } : null; |
| 26 | if (row.status === "ready" && row.graph) return { kind: "ready", graph: await inflate(row.graph) }; |
| 27 | return { kind: "unavailable", commit, reason: row.status === "too_large" ? "too_large" : "empty" }; |
| 28 | } |
| 29 | |
| 30 | /** Like `readCodeMap`, without reading the map itself, so a page can show its state cheaply. */ |
| 31 | export async function readCodeMapSummary(db: D1Database, repoId: string, commit: string, at: number): Promise<CodeMapSummary | null> { |
| 32 | const row = await db.prepare("SELECT status, version, started_at, files, symbols FROM code_maps WHERE repo_id = ? AND commit_sha = ?").bind(repoId, commit).first<Omit<Row, "graph"> & { files: number; symbols: number }>(); |
| 33 | if (!row || row.version < CODE_MAP_VERSION) return null; |
| 34 | if (row.status === "ready") return { kind: "ready", commit, files: row.files, symbols: row.symbols }; |
| 35 | if (row.status === "building") return row.started_at > at - BUILD_TIMEOUT_SECONDS ? { kind: "indexing", commit } : null; |
| 36 | if (row.status === "failed") return row.started_at > at - RETRY_FAILED_SECONDS ? { kind: "unavailable", commit, reason: "failed" } : null; |
| 37 | return { kind: "unavailable", commit, reason: row.status === "too_large" ? "too_large" : "empty" }; |
| 38 | } |
| 39 | |
| 40 | /** Marks a build as started. Returns false when another build of the same commit holds it, so each commit is built once. */ |
| 41 | export async function claimBuild(db: D1Database, repoId: string, commit: string, at: number): Promise<boolean> { |
| 42 | const result = await db |
| 43 | .prepare( |
| 44 | `INSERT INTO code_maps (repo_id, commit_sha, version, status, started_at) VALUES (?, ?, ?, 'building', ?) |
| 45 | ON CONFLICT (repo_id, commit_sha) DO UPDATE SET version = excluded.version, status = 'building', graph = NULL, started_at = excluded.started_at, built_at = NULL |
| 46 | WHERE code_maps.version < excluded.version |
| 47 | OR (code_maps.status = 'building' AND code_maps.started_at <= ?) |
| 48 | OR (code_maps.status = 'failed' AND code_maps.started_at <= ?)`, |
| 49 | ) |
| 50 | .bind(repoId, commit, CODE_MAP_VERSION, at, at - BUILD_TIMEOUT_SECONDS, at - RETRY_FAILED_SECONDS) |
| 51 | .run(); |
| 52 | return result.meta.changes > 0; |
| 53 | } |
| 54 | |
| 55 | export async function saveCodeMap(db: D1Database, repoId: string, graph: CodeGraph, at: number): Promise<void> { |
| 56 | const packed = graph.files.length ? await deflate(graph) : null; |
| 57 | const status: Status = !packed ? "empty" : packed.byteLength > MAX_STORED_BYTES ? "too_large" : "ready"; |
| 58 | await db |
| 59 | .prepare("UPDATE code_maps SET status = ?, graph = ?, files = ?, symbols = ?, built_at = ? WHERE repo_id = ? AND commit_sha = ? AND version = ?") |
| 60 | .bind(status, status === "ready" && packed ? new Uint8Array(packed) : null,graph.files.length, graph.symbols.length, at, repoId, graph.commit, CODE_MAP_VERSION) |
| 61 | .run(); |
| 62 | } |
| 63 | |
| 64 | export async function failCodeMap(db: D1Database, repoId: string, commit: string, at: number): Promise<void> { |
| 65 | await db.prepare("UPDATE code_maps SET status = 'failed', built_at = ? WHERE repo_id = ? AND commit_sha = ? AND status = 'building'").bind(at, repoId, commit).run(); |
| 66 | } |
| 67 | |
| 68 | async function deflate(graph: CodeGraph): Promise<ArrayBuffer> { |
| 69 | return new Response(new Blob([JSON.stringify(graph)]).stream().pipeThrough(new CompressionStream("gzip"))).arrayBuffer(); |
| 70 | } |
| 71 | |
| 72 | async function inflate(stored: ArrayBuffer | number[]): Promise<CodeGraph> { |
| 73 | const bytes = Array.isArray(stored) ? Uint8Array.from(stored) : new Uint8Array(stored); |
| 74 | return JSON.parse(await new Response(new Blob([bytes]).stream().pipeThrough(new DecompressionStream("gzip"))).text()); |
| 75 | } |