* Index all files in the project * * Uses a mutex to prevent concurrent indexing operations.
(options: IndexOptions = {})
| 434 | * Uses a mutex to prevent concurrent indexing operations. |
| 435 | */ |
| 436 | async indexAll(options: IndexOptions = {}): Promise<IndexResult> { |
| 437 | return this.indexMutex.withLock(async () => { |
| 438 | try { |
| 439 | this.fileLock.acquire(); |
| 440 | } catch { |
| 441 | 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 }; |
| 442 | } |
| 443 | // Defer WAL auto-checkpointing for the whole bulk run (#1231): the |
| 444 | // default 1000-page interval re-writes hot pages into the main DB file |
| 445 | // over and over — ~95% of all disk I/O during a bulk index, and a |
| 446 | // 19+min → 45s difference on HDD-class storage. The valve bounds WAL |
| 447 | // growth by backfilling PASSIVEly on a worker thread (never blocking |
| 448 | // the writer or the #850 watchdog heartbeat); runMaintenance below does |
| 449 | // the final fold-up before the interval is restored in the finally. |
| 450 | // Kill switch: CODEGRAPH_NO_WAL_DEFER=1. Non-WAL journal modes (some |
| 451 | // network filesystems) have no WAL to defer — skip. |
| 452 | // Fast-init: on a COMPLETELY fresh DB, trade crash-durability for speed |
| 453 | // during the bulk build (journal in memory, no fsync). Safe because the |
| 454 | // DB is disposable until the index completes — index_state stays |
| 455 | // 'indexing' and a crashed init is re-run from scratch; existing DBs |
| 456 | // (re-index/sync) never take this path. Kill switch: |
| 457 | // CODEGRAPH_NO_FAST_INIT=1 (same pattern as CODEGRAPH_NO_WAL_DEFER). |
| 458 | const freshDb = this.queries.getNodeAndEdgeCount().nodes === 0; |
| 459 | const fastInit = process.env.CODEGRAPH_NO_FAST_INIT !== '1' && freshDb; |
| 460 | if (fastInit) { |
| 461 | try { |
| 462 | this.db.getDb().pragma('journal_mode = MEMORY'); |
| 463 | this.db.getDb().pragma('synchronous = OFF'); |
| 464 | } catch { /* keep WAL */ } |
| 465 | } |
| 466 | const deferWal = !fastInit && process.env.CODEGRAPH_NO_WAL_DEFER !== '1' && this.db.getJournalMode() === 'wal'; |
| 467 | let walValve: WalCheckpointValve | null = null; |
| 468 | let priorAutocheckpoint = 1000; |
| 469 | // Set when the fastInit+pool path below defers autocheckpointing, so the |
| 470 | // finally knows to restore the interval on that path too. |
| 471 | let restoreAutocheckpoint = false; |
| 472 | if (deferWal) { |
| 473 | priorAutocheckpoint = this.db.getWalAutocheckpoint(); |
| 474 | this.db.setWalAutocheckpoint(0); |
| 475 | walValve = new WalCheckpointValve( |
| 476 | this.db, |
| 477 | resolveWalValveMb(process.env.CODEGRAPH_WAL_VALVE_MB, this.db.getDbFileSizeBytes()), |
| 478 | undefined, |
| 479 | options.verbose ? (m) => console.log(`[wal-valve] ${m}`) : undefined |
| 480 | ); |
| 481 | walValve.start(); |
| 482 | } |
| 483 | try { |
| 484 | const before = this.queries.getNodeAndEdgeCount(); |
| 485 | // Mark the index as in-flight BEFORE any writes: a run killed |
| 486 | // mid-index (OOM, SIGKILL, the #850 liveness watchdog) leaves this |
| 487 | // marker behind, so `codegraph status` can tell a truncated index |
| 488 | // from a completed one instead of silently serving partial results. |
| 489 | try { this.queries.setMetadata('index_state', 'indexing'); } catch { /* metadata is advisory */ } |
| 490 | // Segment vocabulary starts empty and is repopulated by the node write |
| 491 | // path as every file (re-)indexes below — so a full index is also the |
| 492 | // orphan-cleanup pass for names deleted since the last one. |
| 493 | try { this.queries.clearNameSegmentVocab(); } catch { /* vocab is advisory — never fail an index over it */ } |