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

Method indexAll

src/index.ts:657–973  ·  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

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

Calls 15

stampPhpWebRootsMethod · 0.95
resolveWalValveMbFunction · 0.90
minRefsForPoolFunction · 0.90
getNodeAndEdgeCountMethod · 0.80
getDbMethod · 0.80
getWalAutocheckpointMethod · 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