/** * Persistent storage implementation using SQLite + Drizzle ORM (LibSQL WASM-compatible) * Now supports chunk-level indexing for better search granularity */ import { type DbConfig } from './db/client.js'; import type { CodebaseFile, Storage } from './storage.js'; /** * Chunk data for storage */ export interface ChunkData { content: string; type: string; startLine: number; endLine: number; metadata?: Record; } /** * Stored chunk with ID */ export interface StoredChunk extends ChunkData { id: number; fileId: number; filePath: string; } export declare class PersistentStorage implements Storage { private dbInstance; private initPromise; constructor(config?: DbConfig); private initialize; /** * Ensure database is initialized before operations */ private ensureInit; /** * Get the LibSQL client for raw SQL operations */ private get client(); /** * Store a file */ storeFile(file: CodebaseFile): Promise; /** * Store multiple files in a single transaction (batch operation) * Much faster than storing one by one for large datasets */ storeFiles(files: CodebaseFile[]): Promise; /** * Get a file by path */ getFile(path: string): Promise; /** * Get all files */ getAllFiles(): Promise; /** * Delete a file */ deleteFile(path: string): Promise; /** * Clear all files */ clear(): Promise; /** * Store chunks for a file (replaces existing chunks) */ storeChunks(filePath: string, chunks: ChunkData[]): Promise; /** * Store chunks for multiple files in batch */ storeManyChunks(fileChunks: Array<{ filePath: string; chunks: ChunkData[]; }>): Promise>; /** * Get chunks for a file */ getChunksForFile(filePath: string): Promise; /** * Get total chunk count */ getChunkCount(): Promise; /** * Get file count */ count(): Promise; /** * Check if file exists */ exists(path: string): Promise; /** * Store document vectors (TF-IDF) for a CHUNK */ storeChunkVectors(chunkId: number, terms: Map, tokenCount?: number): Promise; /** * Store document vectors for multiple CHUNKS in a single transaction (batch operation) * Much faster than storing one by one for large datasets */ storeManyChunkVectors(chunkVectors: Array<{ chunkId: number; terms: Map; tokenCount?: number; }>): Promise; /** * Store IDF scores */ storeIdfScores(idf: Map, docFreq: Map): Promise; /** * Get IDF scores */ getIdfScores(): Promise>; /** * Get document vectors for a chunk */ getChunkVectors(chunkId: number): Promise | null>; /** * Get all chunk vectors in a single batch query (CPU + Memory optimization) * Avoids N+1 query pattern when loading index from storage * Returns Map> */ getAllChunkVectors(): Promise>>; /** * Search chunks by terms using SQL (Memory optimization) * Returns matching chunks with their content for direct display * Uses pre-computed magnitude from chunks table */ searchByTerms(queryTerms: string[], options?: { limit?: number; }): Promise; magnitude: number; tokenCount: number; }>>; /** * Get IDF scores for specific terms only (Memory optimization) */ getIdfScoresForTerms(terms: string[]): Promise>; /** * Get total chunk count (for IDF calculation) * BM25/TF-IDF now operates at chunk level, not file level */ getTotalDocuments(): Promise; /** * Get all file metadata (path, mtime, hash) without content * Used for incremental diff detection */ getAllFileMetadata(): Promise>; /** * Delete multiple files in a single transaction (batch operation) */ deleteFiles(paths: string[]): Promise; /** * Store metadata */ setMetadata(key: string, value: string): Promise; /** * Get metadata */ getMetadata(key: string): Promise; /** * Get average chunk length (token count) for BM25 scoring * Returns cached value from metadata if available, otherwise calculates from chunks table */ getAverageDocLength(): Promise; /** * Update average chunk length in metadata (call after indexing) */ updateAverageDocLength(): Promise; /** * Rebuild IDF scores from document vectors using SQL (Memory optimization) * Calculates document frequency for each term across CHUNKS and computes IDF */ rebuildIdfScoresFromVectors(): Promise; /** * Recalculate TF-IDF scores for all documents using current IDF values (Memory optimization) * Updates document_vectors.tfidf = document_vectors.tf * idf_scores.idf */ recalculateTfidfScores(): Promise; /** * Update pre-computed magnitude for all chunks (Memory optimization for search) * magnitude = sqrt(sum(tfidf^2)) for each chunk * Called after TF-IDF recalculation to keep magnitude in sync */ updateChunkMagnitudes(): Promise; /** * Get terms for chunks of files (for tracking affected terms during incremental updates) * When files are deleted, we need to know which terms were affected */ getTermsForFiles(paths: string[]): Promise>; /** * Get all chunks with their file paths (for bulk operations) */ getAllChunks(): Promise; /** * Close database connection */ close(): void; } //# sourceMappingURL=storage-persistent.d.ts.map