MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / indexAll

Method indexAll

src/index.ts:656–978  ·  view source on GitHub ↗

* Index all files in the project * * Uses a mutex to prevent concurrent indexing operations.

(options: IndexOptions = {})

Source from the content-addressed store, hash-verified

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

Calls 15

stampPhpWebRootsMethod · 0.95
resolveWalValveMbFunction · 0.90
getNodeAndEdgeCountMethod · 0.80
getDbMethod · 0.80
getWalAutocheckpointMethod · 0.80
setWalAutocheckpointMethod · 0.80

Tested by 15

loadFunction · 0.36
bridgesFunction · 0.36
buildProjectFunction · 0.36
buildProjectFunction · 0.36
synthEdgesFunction · 0.36
indexProjectFunction · 0.36
indexProjectFunction · 0.36
staleIndexFunction · 0.36
makeIndexedFunction · 0.36
loadFunction · 0.36
indexProjectFunction · 0.36
loadFunction · 0.36