* Index all files in the project * * Uses a mutex to prevent concurrent indexing operations.
(options: IndexOptions = {})
| 510 | * Uses a mutex to prevent concurrent indexing operations. |
| 511 | */ |
| 512 | async indexAll(options: IndexOptions = {}): Promise<IndexResult> { |
| 513 | return this.indexMutex.withLock(async () => { |
| 514 | try { |
| 515 | this.fileLock.acquire(); |
| 516 | } catch { |
| 517 | 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 }; |
| 518 | } |
| 519 | // Defer WAL auto-checkpointing for the whole bulk run (#1231): the |
| 520 | // default 1000-page interval re-writes hot pages into the main DB file |
| 521 | // over and over — ~95% of all disk I/O during a bulk index, and a |
| 522 | // 19+min → 45s difference on HDD-class storage. The valve bounds WAL |
| 523 | // growth by backfilling PASSIVEly on a worker thread (never blocking |
| 524 | // the writer or the #850 watchdog heartbeat); runMaintenance below does |
| 525 | // the final fold-up before the interval is restored in the finally. |
| 526 | // Kill switch: CODEGRAPH_NO_WAL_DEFER=1. Non-WAL journal modes (some |
| 527 | // network filesystems) have no WAL to defer — skip. |
| 528 | // Fast-init: on a COMPLETELY fresh DB, trade crash-durability for speed |
| 529 | // during the bulk build (journal in memory, no fsync). Safe because the |
| 530 | // DB is disposable until the index completes — index_state stays |
| 531 | // 'indexing' and a crashed init is re-run from scratch; existing DBs |
| 532 | // (re-index/sync) never take this path. Kill switch: |
| 533 | // CODEGRAPH_NO_FAST_INIT=1 (same pattern as CODEGRAPH_NO_WAL_DEFER). |
| 534 | const freshDb = this.queries.getNodeAndEdgeCount().nodes === 0; |
| 535 | const fastInit = process.env.CODEGRAPH_NO_FAST_INIT !== '1' && freshDb; |
| 536 | if (fastInit) { |
| 537 | try { |
| 538 | this.db.getDb().pragma('journal_mode = MEMORY'); |
| 539 | this.db.getDb().pragma('synchronous = OFF'); |
| 540 | } catch { /* keep WAL */ } |
| 541 | } |
| 542 | const deferWal = !fastInit && process.env.CODEGRAPH_NO_WAL_DEFER !== '1' && this.db.getJournalMode() === 'wal'; |
| 543 | let walValve: WalCheckpointValve | null = null; |
| 544 | let priorAutocheckpoint = 1000; |
| 545 | // Set when the fastInit+pool path below defers autocheckpointing, so the |
| 546 | // finally knows to restore the interval on that path too. |
| 547 | let restoreAutocheckpoint = false; |
| 548 | if (deferWal) { |
| 549 | priorAutocheckpoint = this.db.getWalAutocheckpoint(); |
| 550 | this.db.setWalAutocheckpoint(0); |
| 551 | walValve = new WalCheckpointValve( |
| 552 | this.db, |
| 553 | resolveWalValveMb(process.env.CODEGRAPH_WAL_VALVE_MB, this.db.getDbFileSizeBytes()), |
| 554 | undefined, |
| 555 | options.verbose ? (m) => console.log(`[wal-valve] ${m}`) : undefined |
| 556 | ); |
| 557 | walValve.start(); |
| 558 | } |
| 559 | try { |
| 560 | const gitState = this.orchestrator.beginGitIndexState(true); |
| 561 | const before = this.queries.getNodeAndEdgeCount(); |
| 562 | // Mark the index as in-flight BEFORE any writes: a run killed |
| 563 | // mid-index (OOM, SIGKILL, the #850 liveness watchdog) leaves this |
| 564 | // marker behind, so `codegraph status` can tell a truncated index |
| 565 | // from a completed one instead of silently serving partial results. |
| 566 | try { this.queries.setMetadata('index_state', 'indexing'); } catch { /* metadata is advisory */ } |
| 567 | // Segment vocabulary starts empty and is repopulated by the node write |
| 568 | // path as every file (re-)indexes below — so a full index is also the |
| 569 | // orphan-cleanup pass for names deleted since the last one. |