* Index all files in the project * * Uses a mutex to prevent concurrent indexing operations.
(options: IndexOptions = {})
| 675 | * Uses a mutex to prevent concurrent indexing operations. |
| 676 | */ |
| 677 | async indexAll(options: IndexOptions = {}): Promise<IndexResult> { |
| 678 | return this.indexMutex.withLock(async () => { |
| 679 | const startedAt = Date.now(); |
| 680 | try { |
| 681 | if (!this.hasRebuildLock) this.fileLock.acquire(); |
| 682 | } catch { |
| 683 | return { success: false, filesIndexed: 0, filesSkipped: 0, filesErrored: 0, nodesCreated: 0, edgesCreated: 0, errors: [{ message: 'Could not acquire file lock - another process may be indexing', severity: 'error' as const }], durationMs: 0 }; |
| 684 | } |
| 685 | this.hasRebuildLock = false; |
| 686 | // Defer WAL auto-checkpointing for the whole bulk run (#1231): the |
| 687 | // default 1000-page interval re-writes hot pages into the main DB file |
| 688 | // over and over — ~95% of all disk I/O during a bulk index, and a |
| 689 | // 19+min → 45s difference on HDD-class storage. The valve bounds WAL |
| 690 | // growth by backfilling PASSIVEly on a worker thread (never blocking |
| 691 | // the writer or the #850 watchdog heartbeat); runMaintenance below does |
| 692 | // the final fold-up before the interval is restored in the finally. |
| 693 | // Kill switch: CODEGRAPH_NO_WAL_DEFER=1. Non-WAL journal modes (some |
| 694 | // network filesystems) have no WAL to defer — skip. |
| 695 | // Fast-init: on a COMPLETELY fresh DB, trade crash-durability for speed |
| 696 | // during the bulk build (journal in memory, no fsync). Safe because the |
| 697 | // DB is disposable until the index completes — index_state stays |
| 698 | // 'indexing' and a crashed init is re-run from scratch; existing DBs |
| 699 | // (re-index/sync) never take this path. Kill switch: |
| 700 | // CODEGRAPH_NO_FAST_INIT=1 (same pattern as CODEGRAPH_NO_WAL_DEFER). |
| 701 | const freshDb = this.queries.getNodeAndEdgeCount().nodes === 0; |
| 702 | const fastInit = process.env.CODEGRAPH_NO_FAST_INIT !== '1' && freshDb; |
| 703 | if (fastInit) { |
| 704 | try { |
| 705 | this.db.getDb().pragma('journal_mode = MEMORY'); |
| 706 | this.db.getDb().pragma('synchronous = OFF'); |
| 707 | } catch { /* keep WAL */ } |
| 708 | } |
| 709 | const deferWal = !fastInit && process.env.CODEGRAPH_NO_WAL_DEFER !== '1' && this.db.getJournalMode() === 'wal'; |
| 710 | let walValve: WalCheckpointValve | null = null; |
| 711 | let priorAutocheckpoint = 1000; |
| 712 | // Set when the fastInit+pool path below defers autocheckpointing, so the |
| 713 | // finally knows to restore the interval on that path too. |
| 714 | let restoreAutocheckpoint = false; |
| 715 | if (deferWal) { |
| 716 | priorAutocheckpoint = this.db.getWalAutocheckpoint(); |
| 717 | this.db.setWalAutocheckpoint(0); |
| 718 | walValve = new WalCheckpointValve( |
| 719 | this.db, |
| 720 | resolveWalValveMb(process.env.CODEGRAPH_WAL_VALVE_MB, this.db.getDbFileSizeBytes()), |
| 721 | undefined, |
| 722 | options.verbose ? (m) => console.log(`[wal-valve] ${m}`) : undefined |
| 723 | ); |
| 724 | walValve.start(); |
| 725 | } |
| 726 | try { |
| 727 | const gitState = this.orchestrator.beginGitIndexState(true); |
| 728 | const before = this.queries.getNodeAndEdgeCount(); |
| 729 | // Mark the index as in-flight BEFORE any writes: a run killed |
| 730 | // mid-index (OOM, SIGKILL, the #850 liveness watchdog) leaves this |
| 731 | // marker behind, so `codegraph status` can tell a truncated index |
| 732 | // from a completed one instead of silently serving partial results. |
| 733 | try { this.queries.setMetadata('index_state', 'indexing'); } catch { /* metadata is advisory */ } |
| 734 | // Segment vocabulary. On a fresh database it starts empty, and the node |