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