import type { CodeGraph, CodeMapState, CodeMapSummary } from "../contract/views.ts"; /** Raise this when the indexer changes what it finds. Maps built by an older version are built again on first use. */ export const CODE_MAP_VERSION = 1; /** A build that has not finished in this time is taken over by the next request. */ export const BUILD_TIMEOUT_SECONDS = 10 * 60; /** A failed build is tried again after this time, so a passing error does not hide the map for good. */ export const RETRY_FAILED_SECONDS = 30 * 60; const MAX_STORED_BYTES = 1_900_000; type Status = "building" | "ready" | "empty" | "too_large" | "failed"; interface Row { status: Status; version: number; graph: ArrayBuffer | number[] | null; started_at: number; } /** The stored map of a commit, or null when there is none to use and a build should start. */ export async function readCodeMap(db: D1Database, repoId: string, commit: string, at: number): Promise { const row = await db.prepare("SELECT status, version, graph, started_at FROM code_maps WHERE repo_id = ? AND commit_sha = ?").bind(repoId, commit).first(); if (!row || row.version < CODE_MAP_VERSION) return null; if (row.status === "building") return row.started_at > at - BUILD_TIMEOUT_SECONDS ? { kind: "indexing", commit } : null; if (row.status === "failed") return row.started_at > at - RETRY_FAILED_SECONDS ? { kind: "unavailable", commit, reason: "failed" } : null; if (row.status === "ready" && row.graph) return { kind: "ready", graph: await inflate(row.graph) }; return { kind: "unavailable", commit, reason: row.status === "too_large" ? "too_large" : "empty" }; } /** Like `readCodeMap`, without reading the map itself, so a page can show its state cheaply. */ export async function readCodeMapSummary(db: D1Database, repoId: string, commit: string, at: number): Promise { 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 & { files: number; symbols: number }>(); if (!row || row.version < CODE_MAP_VERSION) return null; if (row.status === "ready") return { kind: "ready", commit, files: row.files, symbols: row.symbols }; if (row.status === "building") return row.started_at > at - BUILD_TIMEOUT_SECONDS ? { kind: "indexing", commit } : null; if (row.status === "failed") return row.started_at > at - RETRY_FAILED_SECONDS ? { kind: "unavailable", commit, reason: "failed" } : null; return { kind: "unavailable", commit, reason: row.status === "too_large" ? "too_large" : "empty" }; } /** Marks a build as started. Returns false when another build of the same commit holds it, so each commit is built once. */ export async function claimBuild(db: D1Database, repoId: string, commit: string, at: number): Promise { const result = await db .prepare( `INSERT INTO code_maps (repo_id, commit_sha, version, status, started_at) VALUES (?, ?, ?, 'building', ?) ON CONFLICT (repo_id, commit_sha) DO UPDATE SET version = excluded.version, status = 'building', graph = NULL, started_at = excluded.started_at, built_at = NULL WHERE code_maps.version < excluded.version OR (code_maps.status = 'building' AND code_maps.started_at <= ?) OR (code_maps.status = 'failed' AND code_maps.started_at <= ?)`, ) .bind(repoId, commit, CODE_MAP_VERSION, at, at - BUILD_TIMEOUT_SECONDS, at - RETRY_FAILED_SECONDS) .run(); return result.meta.changes > 0; } export async function saveCodeMap(db: D1Database, repoId: string, graph: CodeGraph, at: number): Promise { const packed = graph.files.length ? await deflate(graph) : null; const status: Status = !packed ? "empty" : packed.byteLength > MAX_STORED_BYTES ? "too_large" : "ready"; await db .prepare("UPDATE code_maps SET status = ?, graph = ?, files = ?, symbols = ?, built_at = ? WHERE repo_id = ? AND commit_sha = ? AND version = ?") .bind(status, status === "ready" && packed ? new Uint8Array(packed) : null,graph.files.length, graph.symbols.length, at, repoId, graph.commit, CODE_MAP_VERSION) .run(); } export async function failCodeMap(db: D1Database, repoId: string, commit: string, at: number): Promise { 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(); } async function deflate(graph: CodeGraph): Promise { return new Response(new Blob([JSON.stringify(graph)]).stream().pipeThrough(new CompressionStream("gzip"))).arrayBuffer(); } async function inflate(stored: ArrayBuffer | number[]): Promise { const bytes = Array.isArray(stored) ? Uint8Array.from(stored) : new Uint8Array(stored); return JSON.parse(await new Response(new Blob([bytes]).stream().pipeThrough(new DecompressionStream("gzip"))).text()); }