mirror of
https://github.com/catlog22/Claude-Code-Workflow.git
synced 2026-02-10 02:24:35 +08:00
perf(codex-lens): optimize search performance with vectorized operations
Performance Optimizations: - VectorStore: NumPy vectorized cosine similarity (100x+ faster) - Cached embedding matrix with pre-computed norms - Lazy content loading for top-k results only - Thread-safe cache invalidation - SQLite: Added PRAGMA mmap_size=30GB for memory-mapped I/O - FTS5: unicode61 tokenizer with tokenchars='_' for code identifiers - ChainSearch: files_only fast path skipping snippet generation - ThreadPoolExecutor: shared pool across searches New Components: - DirIndexStore: single-directory index with FTS5 and symbols - RegistryStore: global project registry with path mappings - PathMapper: source-to-index path conversion utility - IndexTreeBuilder: hierarchical index tree construction - ChainSearchEngine: parallel recursive directory search Test Coverage: - 36 comprehensive search functionality tests - 14 performance benchmark tests - 296 total tests passing (100% pass rate) Benchmark Results: - FTS5 search: 0.23-0.26ms avg (3900-4300 ops/sec) - Vector search: 1.05-1.54ms avg (650-955 ops/sec) - Full semantic: 4.56-6.38ms avg per query 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,27 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from .sqlite_store import SQLiteStore
|
||||
from .path_mapper import PathMapper
|
||||
from .registry import RegistryStore, ProjectInfo, DirMapping
|
||||
from .dir_index import DirIndexStore, SubdirLink, FileEntry
|
||||
from .index_tree import IndexTreeBuilder, BuildResult, DirBuildResult
|
||||
|
||||
__all__ = ["SQLiteStore"]
|
||||
__all__ = [
|
||||
# Legacy (workspace-local)
|
||||
"SQLiteStore",
|
||||
# Path mapping
|
||||
"PathMapper",
|
||||
# Global registry
|
||||
"RegistryStore",
|
||||
"ProjectInfo",
|
||||
"DirMapping",
|
||||
# Directory index
|
||||
"DirIndexStore",
|
||||
"SubdirLink",
|
||||
"FileEntry",
|
||||
# Tree builder
|
||||
"IndexTreeBuilder",
|
||||
"BuildResult",
|
||||
"DirBuildResult",
|
||||
]
|
||||
|
||||
|
||||
797
codex-lens/src/codexlens/storage/dir_index.py
Normal file
797
codex-lens/src/codexlens/storage/dir_index.py
Normal file
@@ -0,0 +1,797 @@
|
||||
"""Single-directory index storage with hierarchical linking.
|
||||
|
||||
Each directory maintains its own _index.db with:
|
||||
- Files in the current directory
|
||||
- Links to subdirectory indexes
|
||||
- Full-text search via FTS5
|
||||
- Symbol table for code navigation
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
import threading
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
from codexlens.entities import SearchResult, Symbol
|
||||
from codexlens.errors import StorageError
|
||||
|
||||
|
||||
@dataclass
|
||||
class SubdirLink:
|
||||
"""Link to a subdirectory's index database."""
|
||||
|
||||
id: int
|
||||
name: str
|
||||
index_path: Path
|
||||
files_count: int
|
||||
direct_files: int
|
||||
last_updated: float
|
||||
|
||||
|
||||
@dataclass
|
||||
class FileEntry:
|
||||
"""Metadata for an indexed file in current directory."""
|
||||
|
||||
id: int
|
||||
name: str
|
||||
full_path: Path
|
||||
language: str
|
||||
mtime: float
|
||||
line_count: int
|
||||
|
||||
|
||||
class DirIndexStore:
|
||||
"""Single-directory index storage with hierarchical subdirectory linking.
|
||||
|
||||
Each directory has an independent _index.db containing:
|
||||
- Files table: Files in this directory only
|
||||
- Subdirs table: Links to child directory indexes
|
||||
- Symbols table: Code symbols from files
|
||||
- FTS5 index: Full-text search on file content
|
||||
|
||||
Thread-safe operations with WAL mode enabled.
|
||||
"""
|
||||
|
||||
def __init__(self, db_path: str | Path) -> None:
|
||||
"""Initialize directory index store.
|
||||
|
||||
Args:
|
||||
db_path: Path to _index.db file for this directory
|
||||
"""
|
||||
self.db_path = Path(db_path).resolve()
|
||||
self._lock = threading.RLock()
|
||||
self._conn: Optional[sqlite3.Connection] = None
|
||||
|
||||
def initialize(self) -> None:
|
||||
"""Create database and schema if not exists."""
|
||||
with self._lock:
|
||||
self.db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
conn = self._get_connection()
|
||||
self._create_schema(conn)
|
||||
self._create_fts_triggers(conn)
|
||||
conn.commit()
|
||||
|
||||
def close(self) -> None:
|
||||
"""Close database connection."""
|
||||
with self._lock:
|
||||
if self._conn is not None:
|
||||
try:
|
||||
self._conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
self._conn = None
|
||||
|
||||
def __enter__(self) -> DirIndexStore:
|
||||
"""Context manager entry."""
|
||||
self.initialize()
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type: object, exc: object, tb: object) -> None:
|
||||
"""Context manager exit."""
|
||||
self.close()
|
||||
|
||||
# === File Operations ===
|
||||
|
||||
def add_file(
|
||||
self,
|
||||
name: str,
|
||||
full_path: str | Path,
|
||||
content: str,
|
||||
language: str,
|
||||
symbols: Optional[List[Symbol]] = None,
|
||||
) -> int:
|
||||
"""Add or update a file in the current directory index.
|
||||
|
||||
Args:
|
||||
name: Filename without path
|
||||
full_path: Complete source file path
|
||||
content: File content for indexing
|
||||
language: Programming language identifier
|
||||
symbols: List of Symbol objects from the file
|
||||
|
||||
Returns:
|
||||
Database file_id
|
||||
|
||||
Raises:
|
||||
StorageError: If database operations fail
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
full_path_str = str(Path(full_path).resolve())
|
||||
mtime = Path(full_path_str).stat().st_mtime if Path(full_path_str).exists() else None
|
||||
line_count = content.count('\n') + 1
|
||||
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO files(name, full_path, language, content, mtime, line_count)
|
||||
VALUES(?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(full_path) DO UPDATE SET
|
||||
name=excluded.name,
|
||||
language=excluded.language,
|
||||
content=excluded.content,
|
||||
mtime=excluded.mtime,
|
||||
line_count=excluded.line_count
|
||||
""",
|
||||
(name, full_path_str, language, content, mtime, line_count),
|
||||
)
|
||||
|
||||
row = conn.execute("SELECT id FROM files WHERE full_path=?", (full_path_str,)).fetchone()
|
||||
if not row:
|
||||
raise StorageError(f"Failed to retrieve file_id for {full_path_str}")
|
||||
|
||||
file_id = int(row["id"])
|
||||
|
||||
# Replace symbols
|
||||
conn.execute("DELETE FROM symbols WHERE file_id=?", (file_id,))
|
||||
if symbols:
|
||||
conn.executemany(
|
||||
"""
|
||||
INSERT INTO symbols(file_id, name, kind, start_line, end_line)
|
||||
VALUES(?, ?, ?, ?, ?)
|
||||
""",
|
||||
[
|
||||
(file_id, s.name, s.kind, s.range[0], s.range[1])
|
||||
for s in symbols
|
||||
],
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
return file_id
|
||||
|
||||
except sqlite3.DatabaseError as exc:
|
||||
conn.rollback()
|
||||
raise StorageError(f"Failed to add file {name}: {exc}") from exc
|
||||
|
||||
def add_files_batch(
|
||||
self, files: List[Tuple[str, Path, str, str, Optional[List[Symbol]]]]
|
||||
) -> int:
|
||||
"""Add multiple files in a single transaction.
|
||||
|
||||
Args:
|
||||
files: List of (name, full_path, content, language, symbols) tuples
|
||||
|
||||
Returns:
|
||||
Number of files added
|
||||
|
||||
Raises:
|
||||
StorageError: If batch operation fails
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
count = 0
|
||||
|
||||
try:
|
||||
conn.execute("BEGIN")
|
||||
|
||||
for name, full_path, content, language, symbols in files:
|
||||
full_path_str = str(Path(full_path).resolve())
|
||||
mtime = Path(full_path_str).stat().st_mtime if Path(full_path_str).exists() else None
|
||||
line_count = content.count('\n') + 1
|
||||
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO files(name, full_path, language, content, mtime, line_count)
|
||||
VALUES(?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(full_path) DO UPDATE SET
|
||||
name=excluded.name,
|
||||
language=excluded.language,
|
||||
content=excluded.content,
|
||||
mtime=excluded.mtime,
|
||||
line_count=excluded.line_count
|
||||
""",
|
||||
(name, full_path_str, language, content, mtime, line_count),
|
||||
)
|
||||
|
||||
row = conn.execute("SELECT id FROM files WHERE full_path=?", (full_path_str,)).fetchone()
|
||||
if not row:
|
||||
raise StorageError(f"Failed to retrieve file_id for {full_path_str}")
|
||||
|
||||
file_id = int(row["id"])
|
||||
count += 1
|
||||
|
||||
conn.execute("DELETE FROM symbols WHERE file_id=?", (file_id,))
|
||||
if symbols:
|
||||
conn.executemany(
|
||||
"""
|
||||
INSERT INTO symbols(file_id, name, kind, start_line, end_line)
|
||||
VALUES(?, ?, ?, ?, ?)
|
||||
""",
|
||||
[
|
||||
(file_id, s.name, s.kind, s.range[0], s.range[1])
|
||||
for s in symbols
|
||||
],
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
return count
|
||||
|
||||
except sqlite3.DatabaseError as exc:
|
||||
conn.rollback()
|
||||
raise StorageError(f"Batch insert failed: {exc}") from exc
|
||||
|
||||
def remove_file(self, full_path: str | Path) -> bool:
|
||||
"""Remove a file from the index.
|
||||
|
||||
Args:
|
||||
full_path: Complete source file path
|
||||
|
||||
Returns:
|
||||
True if file was removed, False if not found
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
full_path_str = str(Path(full_path).resolve())
|
||||
|
||||
row = conn.execute("SELECT id FROM files WHERE full_path=?", (full_path_str,)).fetchone()
|
||||
if not row:
|
||||
return False
|
||||
|
||||
file_id = int(row["id"])
|
||||
conn.execute("DELETE FROM files WHERE id=?", (file_id,))
|
||||
conn.commit()
|
||||
return True
|
||||
|
||||
def get_file(self, full_path: str | Path) -> Optional[FileEntry]:
|
||||
"""Get file metadata.
|
||||
|
||||
Args:
|
||||
full_path: Complete source file path
|
||||
|
||||
Returns:
|
||||
FileEntry if found, None otherwise
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
full_path_str = str(Path(full_path).resolve())
|
||||
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT id, name, full_path, language, mtime, line_count
|
||||
FROM files WHERE full_path=?
|
||||
""",
|
||||
(full_path_str,),
|
||||
).fetchone()
|
||||
|
||||
if not row:
|
||||
return None
|
||||
|
||||
return FileEntry(
|
||||
id=int(row["id"]),
|
||||
name=row["name"],
|
||||
full_path=Path(row["full_path"]),
|
||||
language=row["language"],
|
||||
mtime=float(row["mtime"]) if row["mtime"] else 0.0,
|
||||
line_count=int(row["line_count"]) if row["line_count"] else 0,
|
||||
)
|
||||
|
||||
def get_file_mtime(self, full_path: str | Path) -> Optional[float]:
|
||||
"""Get stored modification time for a file.
|
||||
|
||||
Args:
|
||||
full_path: Complete source file path
|
||||
|
||||
Returns:
|
||||
Modification time as float, or None if not found
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
full_path_str = str(Path(full_path).resolve())
|
||||
|
||||
row = conn.execute(
|
||||
"SELECT mtime FROM files WHERE full_path=?", (full_path_str,)
|
||||
).fetchone()
|
||||
|
||||
return float(row["mtime"]) if row and row["mtime"] else None
|
||||
|
||||
def list_files(self) -> List[FileEntry]:
|
||||
"""List all files in current directory.
|
||||
|
||||
Returns:
|
||||
List of FileEntry objects
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, name, full_path, language, mtime, line_count
|
||||
FROM files
|
||||
ORDER BY name
|
||||
"""
|
||||
).fetchall()
|
||||
|
||||
return [
|
||||
FileEntry(
|
||||
id=int(row["id"]),
|
||||
name=row["name"],
|
||||
full_path=Path(row["full_path"]),
|
||||
language=row["language"],
|
||||
mtime=float(row["mtime"]) if row["mtime"] else 0.0,
|
||||
line_count=int(row["line_count"]) if row["line_count"] else 0,
|
||||
)
|
||||
for row in rows
|
||||
]
|
||||
|
||||
def file_count(self) -> int:
|
||||
"""Get number of files in current directory.
|
||||
|
||||
Returns:
|
||||
File count
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
row = conn.execute("SELECT COUNT(*) AS c FROM files").fetchone()
|
||||
return int(row["c"]) if row else 0
|
||||
|
||||
# === Subdirectory Links ===
|
||||
|
||||
def register_subdir(
|
||||
self,
|
||||
name: str,
|
||||
index_path: str | Path,
|
||||
files_count: int = 0,
|
||||
direct_files: int = 0,
|
||||
) -> None:
|
||||
"""Register or update a subdirectory link.
|
||||
|
||||
Args:
|
||||
name: Subdirectory name
|
||||
index_path: Path to subdirectory's _index.db
|
||||
files_count: Total files recursively
|
||||
direct_files: Files directly in subdirectory
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
index_path_str = str(Path(index_path).resolve())
|
||||
|
||||
import time
|
||||
last_updated = time.time()
|
||||
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO subdirs(name, index_path, files_count, direct_files, last_updated)
|
||||
VALUES(?, ?, ?, ?, ?)
|
||||
ON CONFLICT(name) DO UPDATE SET
|
||||
index_path=excluded.index_path,
|
||||
files_count=excluded.files_count,
|
||||
direct_files=excluded.direct_files,
|
||||
last_updated=excluded.last_updated
|
||||
""",
|
||||
(name, index_path_str, files_count, direct_files, last_updated),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
def unregister_subdir(self, name: str) -> bool:
|
||||
"""Remove a subdirectory link.
|
||||
|
||||
Args:
|
||||
name: Subdirectory name
|
||||
|
||||
Returns:
|
||||
True if removed, False if not found
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
row = conn.execute("SELECT id FROM subdirs WHERE name=?", (name,)).fetchone()
|
||||
if not row:
|
||||
return False
|
||||
|
||||
conn.execute("DELETE FROM subdirs WHERE name=?", (name,))
|
||||
conn.commit()
|
||||
return True
|
||||
|
||||
def get_subdirs(self) -> List[SubdirLink]:
|
||||
"""Get all subdirectory links.
|
||||
|
||||
Returns:
|
||||
List of SubdirLink objects
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, name, index_path, files_count, direct_files, last_updated
|
||||
FROM subdirs
|
||||
ORDER BY name
|
||||
"""
|
||||
).fetchall()
|
||||
|
||||
return [
|
||||
SubdirLink(
|
||||
id=int(row["id"]),
|
||||
name=row["name"],
|
||||
index_path=Path(row["index_path"]),
|
||||
files_count=int(row["files_count"]) if row["files_count"] else 0,
|
||||
direct_files=int(row["direct_files"]) if row["direct_files"] else 0,
|
||||
last_updated=float(row["last_updated"]) if row["last_updated"] else 0.0,
|
||||
)
|
||||
for row in rows
|
||||
]
|
||||
|
||||
def get_subdir(self, name: str) -> Optional[SubdirLink]:
|
||||
"""Get a specific subdirectory link.
|
||||
|
||||
Args:
|
||||
name: Subdirectory name
|
||||
|
||||
Returns:
|
||||
SubdirLink if found, None otherwise
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT id, name, index_path, files_count, direct_files, last_updated
|
||||
FROM subdirs WHERE name=?
|
||||
""",
|
||||
(name,),
|
||||
).fetchone()
|
||||
|
||||
if not row:
|
||||
return None
|
||||
|
||||
return SubdirLink(
|
||||
id=int(row["id"]),
|
||||
name=row["name"],
|
||||
index_path=Path(row["index_path"]),
|
||||
files_count=int(row["files_count"]) if row["files_count"] else 0,
|
||||
direct_files=int(row["direct_files"]) if row["direct_files"] else 0,
|
||||
last_updated=float(row["last_updated"]) if row["last_updated"] else 0.0,
|
||||
)
|
||||
|
||||
def update_subdir_stats(
|
||||
self, name: str, files_count: int, direct_files: Optional[int] = None
|
||||
) -> None:
|
||||
"""Update subdirectory statistics.
|
||||
|
||||
Args:
|
||||
name: Subdirectory name
|
||||
files_count: Total files recursively
|
||||
direct_files: Files directly in subdirectory (optional)
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
import time
|
||||
last_updated = time.time()
|
||||
|
||||
if direct_files is not None:
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE subdirs
|
||||
SET files_count=?, direct_files=?, last_updated=?
|
||||
WHERE name=?
|
||||
""",
|
||||
(files_count, direct_files, last_updated, name),
|
||||
)
|
||||
else:
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE subdirs
|
||||
SET files_count=?, last_updated=?
|
||||
WHERE name=?
|
||||
""",
|
||||
(files_count, last_updated, name),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
# === Search ===
|
||||
|
||||
def search_fts(self, query: str, limit: int = 20) -> List[SearchResult]:
|
||||
"""Full-text search in current directory files.
|
||||
|
||||
Args:
|
||||
query: FTS5 query string
|
||||
limit: Maximum results to return
|
||||
|
||||
Returns:
|
||||
List of SearchResult objects sorted by relevance
|
||||
|
||||
Raises:
|
||||
StorageError: If FTS search fails
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
try:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT rowid, full_path, bm25(files_fts) AS rank,
|
||||
snippet(files_fts, 2, '[bold red]', '[/bold red]', '...', 20) AS excerpt
|
||||
FROM files_fts
|
||||
WHERE files_fts MATCH ?
|
||||
ORDER BY rank
|
||||
LIMIT ?
|
||||
""",
|
||||
(query, limit),
|
||||
).fetchall()
|
||||
except sqlite3.DatabaseError as exc:
|
||||
raise StorageError(f"FTS search failed: {exc}") from exc
|
||||
|
||||
results: List[SearchResult] = []
|
||||
for row in rows:
|
||||
rank = float(row["rank"]) if row["rank"] is not None else 0.0
|
||||
score = abs(rank) if rank < 0 else 0.0
|
||||
results.append(
|
||||
SearchResult(
|
||||
path=row["full_path"],
|
||||
score=score,
|
||||
excerpt=row["excerpt"],
|
||||
)
|
||||
)
|
||||
return results
|
||||
|
||||
def search_files_only(self, query: str, limit: int = 20) -> List[str]:
|
||||
"""Fast FTS search returning only file paths (no snippet generation).
|
||||
|
||||
Optimized for when only file paths are needed, skipping expensive
|
||||
snippet() function call.
|
||||
|
||||
Args:
|
||||
query: FTS5 query string
|
||||
limit: Maximum results to return
|
||||
|
||||
Returns:
|
||||
List of file paths as strings
|
||||
|
||||
Raises:
|
||||
StorageError: If FTS search fails
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
try:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT full_path
|
||||
FROM files_fts
|
||||
WHERE files_fts MATCH ?
|
||||
ORDER BY bm25(files_fts)
|
||||
LIMIT ?
|
||||
""",
|
||||
(query, limit),
|
||||
).fetchall()
|
||||
except sqlite3.DatabaseError as exc:
|
||||
raise StorageError(f"FTS search failed: {exc}") from exc
|
||||
|
||||
return [row["full_path"] for row in rows]
|
||||
|
||||
def search_symbols(
|
||||
self, name: str, kind: Optional[str] = None, limit: int = 50
|
||||
) -> List[Symbol]:
|
||||
"""Search symbols by name pattern.
|
||||
|
||||
Args:
|
||||
name: Symbol name pattern (LIKE query)
|
||||
kind: Optional symbol kind filter
|
||||
limit: Maximum results to return
|
||||
|
||||
Returns:
|
||||
List of Symbol objects
|
||||
"""
|
||||
pattern = f"%{name}%"
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
if kind:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT name, kind, start_line, end_line
|
||||
FROM symbols
|
||||
WHERE name LIKE ? AND kind=?
|
||||
ORDER BY name
|
||||
LIMIT ?
|
||||
""",
|
||||
(pattern, kind, limit),
|
||||
).fetchall()
|
||||
else:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT name, kind, start_line, end_line
|
||||
FROM symbols
|
||||
WHERE name LIKE ?
|
||||
ORDER BY name
|
||||
LIMIT ?
|
||||
""",
|
||||
(pattern, limit),
|
||||
).fetchall()
|
||||
|
||||
return [
|
||||
Symbol(
|
||||
name=row["name"],
|
||||
kind=row["kind"],
|
||||
range=(row["start_line"], row["end_line"]),
|
||||
)
|
||||
for row in rows
|
||||
]
|
||||
|
||||
# === Statistics ===
|
||||
|
||||
def stats(self) -> Dict[str, Any]:
|
||||
"""Get current directory statistics.
|
||||
|
||||
Returns:
|
||||
Dictionary containing:
|
||||
- files: Number of files in this directory
|
||||
- symbols: Number of symbols
|
||||
- subdirs: Number of subdirectories
|
||||
- total_files: Total files including subdirectories
|
||||
- languages: Dictionary of language counts
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
|
||||
file_count = conn.execute("SELECT COUNT(*) AS c FROM files").fetchone()["c"]
|
||||
symbol_count = conn.execute("SELECT COUNT(*) AS c FROM symbols").fetchone()["c"]
|
||||
subdir_count = conn.execute("SELECT COUNT(*) AS c FROM subdirs").fetchone()["c"]
|
||||
|
||||
total_files_row = conn.execute(
|
||||
"SELECT COALESCE(SUM(files_count), 0) AS total FROM subdirs"
|
||||
).fetchone()
|
||||
total_files = int(file_count) + int(total_files_row["total"] if total_files_row else 0)
|
||||
|
||||
lang_rows = conn.execute(
|
||||
"SELECT language, COUNT(*) AS c FROM files GROUP BY language ORDER BY c DESC"
|
||||
).fetchall()
|
||||
languages = {row["language"]: int(row["c"]) for row in lang_rows}
|
||||
|
||||
return {
|
||||
"files": int(file_count),
|
||||
"symbols": int(symbol_count),
|
||||
"subdirs": int(subdir_count),
|
||||
"total_files": total_files,
|
||||
"languages": languages,
|
||||
}
|
||||
|
||||
# === Internal Methods ===
|
||||
|
||||
def _get_connection(self) -> sqlite3.Connection:
|
||||
"""Get or create database connection with proper configuration.
|
||||
|
||||
Returns:
|
||||
sqlite3.Connection with WAL mode and foreign keys enabled
|
||||
"""
|
||||
if self._conn is None:
|
||||
self._conn = sqlite3.connect(str(self.db_path), check_same_thread=False)
|
||||
self._conn.row_factory = sqlite3.Row
|
||||
self._conn.execute("PRAGMA journal_mode=WAL")
|
||||
self._conn.execute("PRAGMA synchronous=NORMAL")
|
||||
self._conn.execute("PRAGMA foreign_keys=ON")
|
||||
# Memory-mapped I/O for faster reads (30GB limit)
|
||||
self._conn.execute("PRAGMA mmap_size=30000000000")
|
||||
return self._conn
|
||||
|
||||
def _create_schema(self, conn: sqlite3.Connection) -> None:
|
||||
"""Create database schema.
|
||||
|
||||
Args:
|
||||
conn: Database connection
|
||||
|
||||
Raises:
|
||||
StorageError: If schema creation fails
|
||||
"""
|
||||
try:
|
||||
# Files table
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS files (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
full_path TEXT UNIQUE NOT NULL,
|
||||
language TEXT,
|
||||
content TEXT,
|
||||
mtime REAL,
|
||||
line_count INTEGER
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
# Subdirectories table
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS subdirs (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
index_path TEXT NOT NULL,
|
||||
files_count INTEGER DEFAULT 0,
|
||||
direct_files INTEGER DEFAULT 0,
|
||||
last_updated REAL
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
# Symbols table
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS symbols (
|
||||
id INTEGER PRIMARY KEY,
|
||||
file_id INTEGER REFERENCES files(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
start_line INTEGER,
|
||||
end_line INTEGER
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
# FTS5 external content table with code-friendly tokenizer
|
||||
# unicode61 tokenchars keeps underscores as part of tokens
|
||||
# so 'user_id' is indexed as one token, not 'user' and 'id'
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE VIRTUAL TABLE IF NOT EXISTS files_fts USING fts5(
|
||||
name, full_path UNINDEXED, content,
|
||||
content='files',
|
||||
content_rowid='id',
|
||||
tokenize="unicode61 tokenchars '_'"
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
# Indexes
|
||||
conn.execute("CREATE INDEX IF NOT EXISTS idx_files_name ON files(name)")
|
||||
conn.execute("CREATE INDEX IF NOT EXISTS idx_files_path ON files(full_path)")
|
||||
conn.execute("CREATE INDEX IF NOT EXISTS idx_subdirs_name ON subdirs(name)")
|
||||
conn.execute("CREATE INDEX IF NOT EXISTS idx_symbols_name ON symbols(name)")
|
||||
conn.execute("CREATE INDEX IF NOT EXISTS idx_symbols_file ON symbols(file_id)")
|
||||
|
||||
except sqlite3.DatabaseError as exc:
|
||||
raise StorageError(f"Failed to create schema: {exc}") from exc
|
||||
|
||||
def _create_fts_triggers(self, conn: sqlite3.Connection) -> None:
|
||||
"""Create FTS5 external content triggers.
|
||||
|
||||
Args:
|
||||
conn: Database connection
|
||||
"""
|
||||
# Insert trigger
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TRIGGER IF NOT EXISTS files_ai AFTER INSERT ON files BEGIN
|
||||
INSERT INTO files_fts(rowid, name, full_path, content)
|
||||
VALUES(new.id, new.name, new.full_path, new.content);
|
||||
END
|
||||
"""
|
||||
)
|
||||
|
||||
# Delete trigger
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TRIGGER IF NOT EXISTS files_ad AFTER DELETE ON files BEGIN
|
||||
INSERT INTO files_fts(files_fts, rowid, name, full_path, content)
|
||||
VALUES('delete', old.id, old.name, old.full_path, old.content);
|
||||
END
|
||||
"""
|
||||
)
|
||||
|
||||
# Update trigger
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TRIGGER IF NOT EXISTS files_au AFTER UPDATE ON files BEGIN
|
||||
INSERT INTO files_fts(files_fts, rowid, name, full_path, content)
|
||||
VALUES('delete', old.id, old.name, old.full_path, old.content);
|
||||
INSERT INTO files_fts(rowid, name, full_path, content)
|
||||
VALUES(new.id, new.name, new.full_path, new.content);
|
||||
END
|
||||
"""
|
||||
)
|
||||
698
codex-lens/src/codexlens/storage/index_tree.py
Normal file
698
codex-lens/src/codexlens/storage/index_tree.py
Normal file
@@ -0,0 +1,698 @@
|
||||
"""Hierarchical index tree builder for CodexLens.
|
||||
|
||||
Constructs a bottom-up directory index tree with parallel processing support.
|
||||
Each directory maintains its own _index.db with files and subdirectory links.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
from concurrent.futures import ProcessPoolExecutor, as_completed
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Optional, Set
|
||||
|
||||
from codexlens.config import Config
|
||||
from codexlens.parsers.factory import ParserFactory
|
||||
from codexlens.storage.dir_index import DirIndexStore
|
||||
from codexlens.storage.path_mapper import PathMapper
|
||||
from codexlens.storage.registry import ProjectInfo, RegistryStore
|
||||
|
||||
|
||||
@dataclass
|
||||
class BuildResult:
|
||||
"""Complete build operation result."""
|
||||
|
||||
project_id: int
|
||||
source_root: Path
|
||||
index_root: Path
|
||||
total_files: int
|
||||
total_dirs: int
|
||||
errors: List[str]
|
||||
|
||||
|
||||
@dataclass
|
||||
class DirBuildResult:
|
||||
"""Single directory build result."""
|
||||
|
||||
source_path: Path
|
||||
index_path: Path
|
||||
files_count: int
|
||||
symbols_count: int
|
||||
subdirs: List[str] # Subdirectory names
|
||||
error: Optional[str] = None
|
||||
|
||||
|
||||
class IndexTreeBuilder:
|
||||
"""Hierarchical index tree builder with parallel processing.
|
||||
|
||||
Builds directory indexes bottom-up to enable proper subdirectory linking.
|
||||
Each directory gets its own _index.db containing:
|
||||
- Files in that directory
|
||||
- Links to child directory indexes
|
||||
- Symbols and FTS5 search
|
||||
|
||||
Attributes:
|
||||
registry: Global project registry
|
||||
mapper: Path mapping between source and index
|
||||
config: CodexLens configuration
|
||||
parser_factory: Parser factory for symbol extraction
|
||||
logger: Logger instance
|
||||
IGNORE_DIRS: Set of directory names to skip during indexing
|
||||
"""
|
||||
|
||||
# Directories to skip during indexing
|
||||
IGNORE_DIRS: Set[str] = {
|
||||
".git",
|
||||
".venv",
|
||||
"venv",
|
||||
"node_modules",
|
||||
"__pycache__",
|
||||
".codexlens",
|
||||
".idea",
|
||||
".vscode",
|
||||
}
|
||||
|
||||
def __init__(
|
||||
self, registry: RegistryStore, mapper: PathMapper, config: Config = None
|
||||
):
|
||||
"""Initialize the index tree builder.
|
||||
|
||||
Args:
|
||||
registry: Global registry store for project tracking
|
||||
mapper: Path mapper for source to index conversions
|
||||
config: CodexLens configuration (uses defaults if None)
|
||||
"""
|
||||
self.registry = registry
|
||||
self.mapper = mapper
|
||||
self.config = config or Config()
|
||||
self.parser_factory = ParserFactory(self.config)
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
def build(
|
||||
self,
|
||||
source_root: Path,
|
||||
languages: List[str] = None,
|
||||
workers: int = 4,
|
||||
) -> BuildResult:
|
||||
"""Build complete index tree for a project.
|
||||
|
||||
Process:
|
||||
1. Register project in registry
|
||||
2. Collect all directories grouped by depth
|
||||
3. Build indexes bottom-up (deepest first)
|
||||
4. Link subdirectories to parents
|
||||
5. Update project statistics
|
||||
|
||||
Args:
|
||||
source_root: Project root directory to index
|
||||
languages: Optional list of language IDs to limit indexing
|
||||
workers: Number of parallel worker processes
|
||||
|
||||
Returns:
|
||||
BuildResult with statistics and errors
|
||||
|
||||
Raises:
|
||||
ValueError: If source_root doesn't exist
|
||||
"""
|
||||
source_root = source_root.resolve()
|
||||
if not source_root.exists():
|
||||
raise ValueError(f"Source root does not exist: {source_root}")
|
||||
|
||||
self.logger.info("Building index tree for %s", source_root)
|
||||
|
||||
# Register project
|
||||
index_root = self.mapper.source_to_index_dir(source_root)
|
||||
project_info = self.registry.register_project(source_root, index_root)
|
||||
|
||||
# Collect directories by depth
|
||||
dirs_by_depth = self._collect_dirs_by_depth(source_root, languages)
|
||||
|
||||
if not dirs_by_depth:
|
||||
self.logger.warning("No indexable directories found in %s", source_root)
|
||||
return BuildResult(
|
||||
project_id=project_info.id,
|
||||
source_root=source_root,
|
||||
index_root=index_root,
|
||||
total_files=0,
|
||||
total_dirs=0,
|
||||
errors=["No indexable directories found"],
|
||||
)
|
||||
|
||||
total_files = 0
|
||||
total_dirs = 0
|
||||
all_errors: List[str] = []
|
||||
all_results: List[DirBuildResult] = [] # Store all results for subdir linking
|
||||
|
||||
# Build bottom-up (highest depth first)
|
||||
max_depth = max(dirs_by_depth.keys())
|
||||
for depth in range(max_depth, -1, -1):
|
||||
if depth not in dirs_by_depth:
|
||||
continue
|
||||
|
||||
dirs = dirs_by_depth[depth]
|
||||
self.logger.info("Building %d directories at depth %d", len(dirs), depth)
|
||||
|
||||
# Build directories at this level in parallel
|
||||
results = self._build_level_parallel(dirs, languages, workers)
|
||||
all_results.extend(results)
|
||||
|
||||
# Process results
|
||||
for result in results:
|
||||
if result.error:
|
||||
all_errors.append(f"{result.source_path}: {result.error}")
|
||||
continue
|
||||
|
||||
total_files += result.files_count
|
||||
total_dirs += 1
|
||||
|
||||
# Register directory in registry
|
||||
self.registry.register_dir(
|
||||
project_id=project_info.id,
|
||||
source_path=result.source_path,
|
||||
index_path=result.index_path,
|
||||
depth=self.mapper.get_relative_depth(result.source_path, source_root),
|
||||
files_count=result.files_count,
|
||||
)
|
||||
|
||||
# After building all directories, link subdirectories to parents
|
||||
# This needs to happen after all indexes exist
|
||||
for result in all_results:
|
||||
if result.error:
|
||||
continue
|
||||
# Link children to this directory
|
||||
self._link_children_to_parent(result.source_path, all_results)
|
||||
|
||||
# Update project statistics
|
||||
self.registry.update_project_stats(source_root, total_files, total_dirs)
|
||||
|
||||
self.logger.info(
|
||||
"Index build complete: %d files, %d directories, %d errors",
|
||||
total_files,
|
||||
total_dirs,
|
||||
len(all_errors),
|
||||
)
|
||||
|
||||
return BuildResult(
|
||||
project_id=project_info.id,
|
||||
source_root=source_root,
|
||||
index_root=index_root,
|
||||
total_files=total_files,
|
||||
total_dirs=total_dirs,
|
||||
errors=all_errors,
|
||||
)
|
||||
|
||||
def update_subtree(
|
||||
self,
|
||||
source_path: Path,
|
||||
languages: List[str] = None,
|
||||
workers: int = 4,
|
||||
) -> BuildResult:
|
||||
"""Incrementally update a subtree.
|
||||
|
||||
Rebuilds indexes for the specified directory and all subdirectories.
|
||||
Useful for incremental updates when only part of the tree changed.
|
||||
|
||||
Args:
|
||||
source_path: Root of subtree to update
|
||||
languages: Optional list of language IDs to limit indexing
|
||||
workers: Number of parallel worker processes
|
||||
|
||||
Returns:
|
||||
BuildResult for the subtree
|
||||
|
||||
Raises:
|
||||
ValueError: If source_path is not indexed
|
||||
"""
|
||||
source_path = source_path.resolve()
|
||||
project_root = self.mapper.get_project_root(source_path)
|
||||
|
||||
# Get project info
|
||||
project_info = self.registry.get_project(project_root)
|
||||
if not project_info:
|
||||
raise ValueError(f"Directory not indexed: {source_path}")
|
||||
|
||||
self.logger.info("Updating subtree at %s", source_path)
|
||||
|
||||
# Use build logic but start from source_path
|
||||
return self.build(source_path, languages, workers)
|
||||
|
||||
def rebuild_dir(self, source_path: Path) -> DirBuildResult:
|
||||
"""Rebuild index for a single directory.
|
||||
|
||||
Only rebuilds the specified directory, does not touch subdirectories.
|
||||
Useful for updating a single directory after file changes.
|
||||
|
||||
Args:
|
||||
source_path: Directory to rebuild
|
||||
|
||||
Returns:
|
||||
DirBuildResult for the directory
|
||||
"""
|
||||
source_path = source_path.resolve()
|
||||
self.logger.info("Rebuilding directory %s", source_path)
|
||||
return self._build_single_dir(source_path)
|
||||
|
||||
# === Internal Methods ===
|
||||
|
||||
def _collect_dirs_by_depth(
|
||||
self, source_root: Path, languages: List[str] = None
|
||||
) -> Dict[int, List[Path]]:
|
||||
"""Collect all indexable directories grouped by depth.
|
||||
|
||||
Walks the directory tree and groups directories by their depth
|
||||
relative to source_root. Depth 0 is the root itself.
|
||||
|
||||
Args:
|
||||
source_root: Root directory to start from
|
||||
languages: Optional language filter
|
||||
|
||||
Returns:
|
||||
Dictionary mapping depth to list of directory paths
|
||||
Example: {0: [root], 1: [src, tests], 2: [src/api, src/utils]}
|
||||
"""
|
||||
source_root = source_root.resolve()
|
||||
dirs_by_depth: Dict[int, List[Path]] = {}
|
||||
|
||||
# Always include the root directory at depth 0 for chain search entry point
|
||||
dirs_by_depth[0] = [source_root]
|
||||
|
||||
for root, dirnames, _ in os.walk(source_root):
|
||||
# Filter out ignored directories
|
||||
dirnames[:] = [
|
||||
d
|
||||
for d in dirnames
|
||||
if d not in self.IGNORE_DIRS and not d.startswith(".")
|
||||
]
|
||||
|
||||
root_path = Path(root)
|
||||
|
||||
# Skip root (already added)
|
||||
if root_path == source_root:
|
||||
continue
|
||||
|
||||
# Check if this directory should be indexed
|
||||
if not self._should_index_dir(root_path, languages):
|
||||
continue
|
||||
|
||||
# Calculate depth relative to source_root
|
||||
try:
|
||||
depth = len(root_path.relative_to(source_root).parts)
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
if depth not in dirs_by_depth:
|
||||
dirs_by_depth[depth] = []
|
||||
|
||||
dirs_by_depth[depth].append(root_path)
|
||||
|
||||
return dirs_by_depth
|
||||
|
||||
def _should_index_dir(self, dir_path: Path, languages: List[str] = None) -> bool:
|
||||
"""Check if directory should be indexed.
|
||||
|
||||
A directory is indexed if:
|
||||
1. It's not in IGNORE_DIRS
|
||||
2. It doesn't start with '.'
|
||||
3. It contains at least one supported language file
|
||||
|
||||
Args:
|
||||
dir_path: Directory to check
|
||||
languages: Optional language filter
|
||||
|
||||
Returns:
|
||||
True if directory should be indexed
|
||||
"""
|
||||
# Check directory name
|
||||
if dir_path.name in self.IGNORE_DIRS or dir_path.name.startswith("."):
|
||||
return False
|
||||
|
||||
# Check for supported files in this directory
|
||||
source_files = self._iter_source_files(dir_path, languages)
|
||||
return len(source_files) > 0
|
||||
|
||||
def _build_level_parallel(
|
||||
self, dirs: List[Path], languages: List[str], workers: int
|
||||
) -> List[DirBuildResult]:
|
||||
"""Build multiple directories in parallel.
|
||||
|
||||
Uses ProcessPoolExecutor to build directories concurrently.
|
||||
All directories at the same level are independent and can be
|
||||
processed in parallel.
|
||||
|
||||
Args:
|
||||
dirs: List of directories to build
|
||||
languages: Language filter
|
||||
workers: Number of worker processes
|
||||
|
||||
Returns:
|
||||
List of DirBuildResult objects
|
||||
"""
|
||||
results: List[DirBuildResult] = []
|
||||
|
||||
if not dirs:
|
||||
return results
|
||||
|
||||
# For single directory, avoid overhead of process pool
|
||||
if len(dirs) == 1:
|
||||
result = self._build_single_dir(dirs[0], languages)
|
||||
return [result]
|
||||
|
||||
# Prepare arguments for worker processes
|
||||
config_dict = {
|
||||
"data_dir": str(self.config.data_dir),
|
||||
"supported_languages": self.config.supported_languages,
|
||||
"parsing_rules": self.config.parsing_rules,
|
||||
}
|
||||
|
||||
worker_args = [
|
||||
(
|
||||
dir_path,
|
||||
self.mapper.source_to_index_db(dir_path),
|
||||
languages,
|
||||
config_dict,
|
||||
)
|
||||
for dir_path in dirs
|
||||
]
|
||||
|
||||
# Execute in parallel
|
||||
with ProcessPoolExecutor(max_workers=workers) as executor:
|
||||
futures = {
|
||||
executor.submit(_build_dir_worker, args): args[0]
|
||||
for args in worker_args
|
||||
}
|
||||
|
||||
for future in as_completed(futures):
|
||||
try:
|
||||
result = future.result()
|
||||
results.append(result)
|
||||
except Exception as exc:
|
||||
dir_path = futures[future]
|
||||
self.logger.error("Failed to build %s: %s", dir_path, exc)
|
||||
results.append(
|
||||
DirBuildResult(
|
||||
source_path=dir_path,
|
||||
index_path=self.mapper.source_to_index_db(dir_path),
|
||||
files_count=0,
|
||||
symbols_count=0,
|
||||
subdirs=[],
|
||||
error=str(exc),
|
||||
)
|
||||
)
|
||||
|
||||
return results
|
||||
|
||||
def _build_single_dir(
|
||||
self, dir_path: Path, languages: List[str] = None
|
||||
) -> DirBuildResult:
|
||||
"""Build index for a single directory.
|
||||
|
||||
Creates _index.db and indexes all files in the directory.
|
||||
Does not recurse into subdirectories.
|
||||
|
||||
Args:
|
||||
dir_path: Directory to index
|
||||
languages: Optional language filter
|
||||
|
||||
Returns:
|
||||
DirBuildResult with statistics and subdirectory list
|
||||
"""
|
||||
dir_path = dir_path.resolve()
|
||||
index_db_path = self.mapper.source_to_index_db(dir_path)
|
||||
|
||||
try:
|
||||
# Ensure index directory exists
|
||||
index_db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Create directory index
|
||||
store = DirIndexStore(index_db_path)
|
||||
store.initialize()
|
||||
|
||||
# Get source files in this directory only
|
||||
source_files = self._iter_source_files(dir_path, languages)
|
||||
|
||||
files_count = 0
|
||||
symbols_count = 0
|
||||
|
||||
for file_path in source_files:
|
||||
try:
|
||||
# Read and parse file
|
||||
text = file_path.read_text(encoding="utf-8", errors="ignore")
|
||||
language_id = self.config.language_for_path(file_path)
|
||||
if not language_id:
|
||||
continue
|
||||
|
||||
parser = self.parser_factory.get_parser(language_id)
|
||||
indexed_file = parser.parse(text, file_path)
|
||||
|
||||
# Add to directory index
|
||||
store.add_file(
|
||||
name=file_path.name,
|
||||
full_path=file_path,
|
||||
content=text,
|
||||
language=language_id,
|
||||
symbols=indexed_file.symbols,
|
||||
)
|
||||
|
||||
files_count += 1
|
||||
symbols_count += len(indexed_file.symbols)
|
||||
|
||||
except Exception as exc:
|
||||
self.logger.debug("Failed to index %s: %s", file_path, exc)
|
||||
continue
|
||||
|
||||
# Get list of subdirectories
|
||||
subdirs = [
|
||||
d.name
|
||||
for d in dir_path.iterdir()
|
||||
if d.is_dir()
|
||||
and d.name not in self.IGNORE_DIRS
|
||||
and not d.name.startswith(".")
|
||||
]
|
||||
|
||||
store.close()
|
||||
|
||||
self.logger.debug(
|
||||
"Built %s: %d files, %d symbols, %d subdirs",
|
||||
dir_path,
|
||||
files_count,
|
||||
symbols_count,
|
||||
len(subdirs),
|
||||
)
|
||||
|
||||
return DirBuildResult(
|
||||
source_path=dir_path,
|
||||
index_path=index_db_path,
|
||||
files_count=files_count,
|
||||
symbols_count=symbols_count,
|
||||
subdirs=subdirs,
|
||||
)
|
||||
|
||||
except Exception as exc:
|
||||
self.logger.error("Failed to build directory %s: %s", dir_path, exc)
|
||||
return DirBuildResult(
|
||||
source_path=dir_path,
|
||||
index_path=index_db_path,
|
||||
files_count=0,
|
||||
symbols_count=0,
|
||||
subdirs=[],
|
||||
error=str(exc),
|
||||
)
|
||||
|
||||
def _link_children_to_parent(
|
||||
self, parent_path: Path, all_results: List[DirBuildResult]
|
||||
) -> None:
|
||||
"""Link child directory indexes to parent's subdirs table.
|
||||
|
||||
Finds all direct children of parent_path in all_results and
|
||||
registers them as subdirectories in the parent's index.
|
||||
|
||||
Args:
|
||||
parent_path: Parent directory path
|
||||
all_results: List of all build results
|
||||
"""
|
||||
parent_index_db = self.mapper.source_to_index_db(parent_path)
|
||||
|
||||
try:
|
||||
store = DirIndexStore(parent_index_db)
|
||||
store.initialize()
|
||||
|
||||
for result in all_results:
|
||||
# Only register direct children (parent is one level up)
|
||||
if result.source_path.parent != parent_path:
|
||||
continue
|
||||
|
||||
if result.error:
|
||||
continue
|
||||
|
||||
# Register subdirectory link
|
||||
store.register_subdir(
|
||||
name=result.source_path.name,
|
||||
index_path=result.index_path,
|
||||
files_count=result.files_count,
|
||||
direct_files=result.files_count,
|
||||
)
|
||||
self.logger.debug(
|
||||
"Linked %s to parent %s",
|
||||
result.source_path.name,
|
||||
parent_path,
|
||||
)
|
||||
|
||||
store.close()
|
||||
|
||||
except Exception as exc:
|
||||
self.logger.error(
|
||||
"Failed to link children to %s: %s", parent_path, exc
|
||||
)
|
||||
|
||||
def _iter_source_files(
|
||||
self, dir_path: Path, languages: List[str] = None
|
||||
) -> List[Path]:
|
||||
"""Iterate source files in directory (non-recursive).
|
||||
|
||||
Returns files in the specified directory that match language filters.
|
||||
Does not recurse into subdirectories.
|
||||
|
||||
Args:
|
||||
dir_path: Directory to scan
|
||||
languages: Optional language filter
|
||||
|
||||
Returns:
|
||||
List of source file paths
|
||||
"""
|
||||
files: List[Path] = []
|
||||
|
||||
if not dir_path.is_dir():
|
||||
return files
|
||||
|
||||
for item in dir_path.iterdir():
|
||||
if not item.is_file():
|
||||
continue
|
||||
|
||||
if item.name.startswith("."):
|
||||
continue
|
||||
|
||||
# Check language support
|
||||
language_id = self.config.language_for_path(item)
|
||||
if not language_id:
|
||||
continue
|
||||
|
||||
# Apply language filter
|
||||
if languages and language_id not in languages:
|
||||
continue
|
||||
|
||||
files.append(item)
|
||||
|
||||
return files
|
||||
|
||||
|
||||
# === Worker Function for ProcessPoolExecutor ===
|
||||
|
||||
|
||||
def _build_dir_worker(args: tuple) -> DirBuildResult:
|
||||
"""Worker function for parallel directory building.
|
||||
|
||||
Must be at module level for ProcessPoolExecutor pickling.
|
||||
Reconstructs necessary objects from serializable arguments.
|
||||
|
||||
Args:
|
||||
args: Tuple of (dir_path, index_db_path, languages, config_dict)
|
||||
|
||||
Returns:
|
||||
DirBuildResult for the directory
|
||||
"""
|
||||
dir_path, index_db_path, languages, config_dict = args
|
||||
|
||||
# Reconstruct config
|
||||
config = Config(
|
||||
data_dir=Path(config_dict["data_dir"]),
|
||||
supported_languages=config_dict["supported_languages"],
|
||||
parsing_rules=config_dict["parsing_rules"],
|
||||
)
|
||||
|
||||
parser_factory = ParserFactory(config)
|
||||
|
||||
try:
|
||||
# Ensure index directory exists
|
||||
index_db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Create directory index
|
||||
store = DirIndexStore(index_db_path)
|
||||
store.initialize()
|
||||
|
||||
files_count = 0
|
||||
symbols_count = 0
|
||||
|
||||
# Index files in this directory
|
||||
for item in dir_path.iterdir():
|
||||
if not item.is_file():
|
||||
continue
|
||||
|
||||
if item.name.startswith("."):
|
||||
continue
|
||||
|
||||
language_id = config.language_for_path(item)
|
||||
if not language_id:
|
||||
continue
|
||||
|
||||
if languages and language_id not in languages:
|
||||
continue
|
||||
|
||||
try:
|
||||
text = item.read_text(encoding="utf-8", errors="ignore")
|
||||
parser = parser_factory.get_parser(language_id)
|
||||
indexed_file = parser.parse(text, item)
|
||||
|
||||
store.add_file(
|
||||
name=item.name,
|
||||
full_path=item,
|
||||
content=text,
|
||||
language=language_id,
|
||||
symbols=indexed_file.symbols,
|
||||
)
|
||||
|
||||
files_count += 1
|
||||
symbols_count += len(indexed_file.symbols)
|
||||
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
# Get subdirectories
|
||||
ignore_dirs = {
|
||||
".git",
|
||||
".venv",
|
||||
"venv",
|
||||
"node_modules",
|
||||
"__pycache__",
|
||||
".codexlens",
|
||||
".idea",
|
||||
".vscode",
|
||||
}
|
||||
|
||||
subdirs = [
|
||||
d.name
|
||||
for d in dir_path.iterdir()
|
||||
if d.is_dir() and d.name not in ignore_dirs and not d.name.startswith(".")
|
||||
]
|
||||
|
||||
store.close()
|
||||
|
||||
return DirBuildResult(
|
||||
source_path=dir_path,
|
||||
index_path=index_db_path,
|
||||
files_count=files_count,
|
||||
symbols_count=symbols_count,
|
||||
subdirs=subdirs,
|
||||
)
|
||||
|
||||
except Exception as exc:
|
||||
return DirBuildResult(
|
||||
source_path=dir_path,
|
||||
index_path=index_db_path,
|
||||
files_count=0,
|
||||
symbols_count=0,
|
||||
subdirs=[],
|
||||
error=str(exc),
|
||||
)
|
||||
274
codex-lens/src/codexlens/storage/path_mapper.py
Normal file
274
codex-lens/src/codexlens/storage/path_mapper.py
Normal file
@@ -0,0 +1,274 @@
|
||||
"""Path mapping utilities for source paths and index paths.
|
||||
|
||||
This module provides bidirectional mapping between source code directories
|
||||
and their corresponding index storage locations.
|
||||
|
||||
Storage Structure:
|
||||
~/.codexlens/
|
||||
├── registry.db # Global mapping table
|
||||
└── indexes/
|
||||
└── D/
|
||||
└── Claude_dms3/
|
||||
├── _index.db # Root directory index
|
||||
└── src/
|
||||
└── _index.db # src/ directory index
|
||||
"""
|
||||
|
||||
import platform
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class PathMapper:
|
||||
"""Bidirectional mapping tool for source paths ↔ index paths.
|
||||
|
||||
Handles cross-platform path normalization and conversion between
|
||||
source code directories and their index storage locations.
|
||||
|
||||
Attributes:
|
||||
DEFAULT_INDEX_ROOT: Default root directory for all indexes
|
||||
INDEX_DB_NAME: Standard name for index database files
|
||||
index_root: Configured index root directory
|
||||
"""
|
||||
|
||||
DEFAULT_INDEX_ROOT = Path.home() / ".codexlens" / "indexes"
|
||||
INDEX_DB_NAME = "_index.db"
|
||||
|
||||
def __init__(self, index_root: Optional[Path] = None):
|
||||
"""Initialize PathMapper with optional custom index root.
|
||||
|
||||
Args:
|
||||
index_root: Custom index root directory. If None, uses DEFAULT_INDEX_ROOT.
|
||||
"""
|
||||
self.index_root = (index_root or self.DEFAULT_INDEX_ROOT).resolve()
|
||||
|
||||
def source_to_index_dir(self, source_path: Path) -> Path:
|
||||
"""Convert source directory to its index directory path.
|
||||
|
||||
Maps a source code directory to where its index data should be stored.
|
||||
The mapping preserves the directory structure but normalizes paths
|
||||
for cross-platform compatibility.
|
||||
|
||||
Args:
|
||||
source_path: Source directory path to map
|
||||
|
||||
Returns:
|
||||
Index directory path under index_root
|
||||
|
||||
Examples:
|
||||
>>> mapper = PathMapper()
|
||||
>>> mapper.source_to_index_dir(Path("D:/Claude_dms3/src"))
|
||||
PosixPath('/home/user/.codexlens/indexes/D/Claude_dms3/src')
|
||||
|
||||
>>> mapper.source_to_index_dir(Path("/home/user/project"))
|
||||
PosixPath('/home/user/.codexlens/indexes/home/user/project')
|
||||
"""
|
||||
source_path = source_path.resolve()
|
||||
normalized = self.normalize_path(source_path)
|
||||
return self.index_root / normalized
|
||||
|
||||
def source_to_index_db(self, source_path: Path) -> Path:
|
||||
"""Convert source directory to its index database file path.
|
||||
|
||||
Maps a source directory to the full path of its index database file,
|
||||
including the standard INDEX_DB_NAME.
|
||||
|
||||
Args:
|
||||
source_path: Source directory path to map
|
||||
|
||||
Returns:
|
||||
Full path to the index database file
|
||||
|
||||
Examples:
|
||||
>>> mapper = PathMapper()
|
||||
>>> mapper.source_to_index_db(Path("D:/Claude_dms3/src"))
|
||||
PosixPath('/home/user/.codexlens/indexes/D/Claude_dms3/src/_index.db')
|
||||
"""
|
||||
index_dir = self.source_to_index_dir(source_path)
|
||||
return index_dir / self.INDEX_DB_NAME
|
||||
|
||||
def index_to_source(self, index_path: Path) -> Path:
|
||||
"""Convert index path back to original source path.
|
||||
|
||||
Performs reverse mapping from an index storage location to the
|
||||
original source directory. Handles both directory paths and
|
||||
database file paths.
|
||||
|
||||
Args:
|
||||
index_path: Index directory or database file path
|
||||
|
||||
Returns:
|
||||
Original source directory path
|
||||
|
||||
Raises:
|
||||
ValueError: If index_path is not under index_root
|
||||
|
||||
Examples:
|
||||
>>> mapper = PathMapper()
|
||||
>>> mapper.index_to_source(
|
||||
... Path("~/.codexlens/indexes/D/Claude_dms3/src/_index.db")
|
||||
... )
|
||||
WindowsPath('D:/Claude_dms3/src')
|
||||
|
||||
>>> mapper.index_to_source(
|
||||
... Path("~/.codexlens/indexes/D/Claude_dms3/src")
|
||||
... )
|
||||
WindowsPath('D:/Claude_dms3/src')
|
||||
"""
|
||||
index_path = index_path.resolve()
|
||||
|
||||
# Remove _index.db if present
|
||||
if index_path.name == self.INDEX_DB_NAME:
|
||||
index_path = index_path.parent
|
||||
|
||||
# Verify path is under index_root
|
||||
try:
|
||||
relative = index_path.relative_to(self.index_root)
|
||||
except ValueError:
|
||||
raise ValueError(
|
||||
f"Index path {index_path} is not under index root {self.index_root}"
|
||||
)
|
||||
|
||||
# Convert normalized path back to source path
|
||||
normalized_str = str(relative).replace("\\", "/")
|
||||
return self.denormalize_path(normalized_str)
|
||||
|
||||
def get_project_root(self, source_path: Path) -> Path:
|
||||
"""Find the project root directory (topmost indexed directory).
|
||||
|
||||
Walks up the directory tree to find the highest-level directory
|
||||
that has an index database.
|
||||
|
||||
Args:
|
||||
source_path: Source directory to start from
|
||||
|
||||
Returns:
|
||||
Project root directory path. Returns source_path itself if
|
||||
no parent index is found.
|
||||
|
||||
Examples:
|
||||
>>> mapper = PathMapper()
|
||||
>>> mapper.get_project_root(Path("D:/Claude_dms3/src/codexlens"))
|
||||
WindowsPath('D:/Claude_dms3')
|
||||
"""
|
||||
source_path = source_path.resolve()
|
||||
current = source_path
|
||||
project_root = source_path
|
||||
|
||||
# Walk up the tree
|
||||
while current.parent != current: # Stop at filesystem root
|
||||
parent_index_db = self.source_to_index_db(current.parent)
|
||||
if parent_index_db.exists():
|
||||
project_root = current.parent
|
||||
current = current.parent
|
||||
else:
|
||||
break
|
||||
|
||||
return project_root
|
||||
|
||||
def get_relative_depth(self, source_path: Path, project_root: Path) -> int:
|
||||
"""Calculate directory depth relative to project root.
|
||||
|
||||
Args:
|
||||
source_path: Target directory path
|
||||
project_root: Project root directory path
|
||||
|
||||
Returns:
|
||||
Number of directory levels from project_root to source_path
|
||||
|
||||
Raises:
|
||||
ValueError: If source_path is not under project_root
|
||||
|
||||
Examples:
|
||||
>>> mapper = PathMapper()
|
||||
>>> mapper.get_relative_depth(
|
||||
... Path("D:/Claude_dms3/src/codexlens"),
|
||||
... Path("D:/Claude_dms3")
|
||||
... )
|
||||
2
|
||||
"""
|
||||
source_path = source_path.resolve()
|
||||
project_root = project_root.resolve()
|
||||
|
||||
try:
|
||||
relative = source_path.relative_to(project_root)
|
||||
# Count path components
|
||||
return len(relative.parts)
|
||||
except ValueError:
|
||||
raise ValueError(
|
||||
f"Source path {source_path} is not under project root {project_root}"
|
||||
)
|
||||
|
||||
def normalize_path(self, path: Path) -> str:
|
||||
"""Normalize path to cross-platform storage format.
|
||||
|
||||
Converts OS-specific paths to a standardized format for storage:
|
||||
- Windows: Removes drive colons (D: → D)
|
||||
- Unix: Removes leading slash
|
||||
- Uses forward slashes throughout
|
||||
|
||||
Args:
|
||||
path: Path to normalize
|
||||
|
||||
Returns:
|
||||
Normalized path string
|
||||
|
||||
Examples:
|
||||
>>> mapper = PathMapper()
|
||||
>>> mapper.normalize_path(Path("D:/path/to/dir"))
|
||||
'D/path/to/dir'
|
||||
|
||||
>>> mapper.normalize_path(Path("/home/user/path"))
|
||||
'home/user/path'
|
||||
"""
|
||||
path = path.resolve()
|
||||
path_str = str(path)
|
||||
|
||||
# Handle Windows paths with drive letters
|
||||
if platform.system() == "Windows" and len(path.parts) > 0:
|
||||
# Convert D:\path\to\dir → D/path/to/dir
|
||||
drive = path.parts[0].replace(":", "") # D: → D
|
||||
rest = Path(*path.parts[1:]) if len(path.parts) > 1 else Path()
|
||||
normalized = f"{drive}/{rest}".replace("\\", "/")
|
||||
return normalized.rstrip("/")
|
||||
|
||||
# Handle Unix paths
|
||||
# /home/user/path → home/user/path
|
||||
return path_str.lstrip("/").replace("\\", "/")
|
||||
|
||||
def denormalize_path(self, normalized: str) -> Path:
|
||||
"""Convert normalized path back to OS-specific path.
|
||||
|
||||
Reverses the normalization process to restore OS-native path format:
|
||||
- Windows: Adds drive colons (D → D:)
|
||||
- Unix: Adds leading slash
|
||||
|
||||
Args:
|
||||
normalized: Normalized path string
|
||||
|
||||
Returns:
|
||||
OS-specific Path object
|
||||
|
||||
Examples:
|
||||
>>> mapper = PathMapper()
|
||||
>>> mapper.denormalize_path("D/path/to/dir") # On Windows
|
||||
WindowsPath('D:/path/to/dir')
|
||||
|
||||
>>> mapper.denormalize_path("home/user/path") # On Unix
|
||||
PosixPath('/home/user/path')
|
||||
"""
|
||||
parts = normalized.split("/")
|
||||
|
||||
# Handle Windows paths
|
||||
if platform.system() == "Windows" and len(parts) > 0:
|
||||
# Check if first part is a drive letter
|
||||
if len(parts[0]) == 1 and parts[0].isalpha():
|
||||
# D/path/to/dir → D:/path/to/dir
|
||||
drive = f"{parts[0]}:"
|
||||
if len(parts) > 1:
|
||||
return Path(drive) / Path(*parts[1:])
|
||||
return Path(drive)
|
||||
|
||||
# Handle Unix paths or relative paths
|
||||
# home/user/path → /home/user/path
|
||||
return Path("/") / Path(*parts)
|
||||
600
codex-lens/src/codexlens/storage/registry.py
Normal file
600
codex-lens/src/codexlens/storage/registry.py
Normal file
@@ -0,0 +1,600 @@
|
||||
"""Global project registry for CodexLens - SQLite storage."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from codexlens.errors import StorageError
|
||||
|
||||
|
||||
@dataclass
|
||||
class ProjectInfo:
|
||||
"""Registered project information."""
|
||||
|
||||
id: int
|
||||
source_root: Path
|
||||
index_root: Path
|
||||
created_at: float
|
||||
last_indexed: float
|
||||
total_files: int
|
||||
total_dirs: int
|
||||
status: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class DirMapping:
|
||||
"""Directory to index path mapping."""
|
||||
|
||||
id: int
|
||||
project_id: int
|
||||
source_path: Path
|
||||
index_path: Path
|
||||
depth: int
|
||||
files_count: int
|
||||
last_updated: float
|
||||
|
||||
|
||||
class RegistryStore:
|
||||
"""Global project registry - SQLite storage.
|
||||
|
||||
Manages indexed projects and directory-to-index path mappings.
|
||||
Thread-safe with connection pooling.
|
||||
"""
|
||||
|
||||
DEFAULT_DB_PATH = Path.home() / ".codexlens" / "registry.db"
|
||||
|
||||
def __init__(self, db_path: Path | None = None) -> None:
|
||||
self.db_path = (db_path or self.DEFAULT_DB_PATH).resolve()
|
||||
self._lock = threading.RLock()
|
||||
self._local = threading.local()
|
||||
self._pool_lock = threading.Lock()
|
||||
self._pool: Dict[int, sqlite3.Connection] = {}
|
||||
self._pool_generation = 0
|
||||
|
||||
def _get_connection(self) -> sqlite3.Connection:
|
||||
"""Get or create a thread-local database connection."""
|
||||
thread_id = threading.get_ident()
|
||||
if getattr(self._local, "generation", None) == self._pool_generation:
|
||||
conn = getattr(self._local, "conn", None)
|
||||
if conn is not None:
|
||||
return conn
|
||||
|
||||
with self._pool_lock:
|
||||
conn = self._pool.get(thread_id)
|
||||
if conn is None:
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
conn.row_factory = sqlite3.Row
|
||||
conn.execute("PRAGMA journal_mode=WAL")
|
||||
conn.execute("PRAGMA synchronous=NORMAL")
|
||||
conn.execute("PRAGMA foreign_keys=ON")
|
||||
self._pool[thread_id] = conn
|
||||
|
||||
self._local.conn = conn
|
||||
self._local.generation = self._pool_generation
|
||||
return conn
|
||||
|
||||
def close(self) -> None:
|
||||
"""Close all pooled connections."""
|
||||
with self._lock:
|
||||
with self._pool_lock:
|
||||
for conn in self._pool.values():
|
||||
conn.close()
|
||||
self._pool.clear()
|
||||
self._pool_generation += 1
|
||||
|
||||
if hasattr(self._local, "conn"):
|
||||
self._local.conn = None
|
||||
if hasattr(self._local, "generation"):
|
||||
self._local.generation = self._pool_generation
|
||||
|
||||
def __enter__(self) -> RegistryStore:
|
||||
self.initialize()
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type: object, exc: object, tb: object) -> None:
|
||||
self.close()
|
||||
|
||||
def initialize(self) -> None:
|
||||
"""Create database and schema."""
|
||||
with self._lock:
|
||||
self.db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
conn = self._get_connection()
|
||||
self._create_schema(conn)
|
||||
|
||||
def _create_schema(self, conn: sqlite3.Connection) -> None:
|
||||
"""Create database schema."""
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS projects (
|
||||
id INTEGER PRIMARY KEY,
|
||||
source_root TEXT UNIQUE NOT NULL,
|
||||
index_root TEXT NOT NULL,
|
||||
created_at REAL,
|
||||
last_indexed REAL,
|
||||
total_files INTEGER DEFAULT 0,
|
||||
total_dirs INTEGER DEFAULT 0,
|
||||
status TEXT DEFAULT 'active'
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS dir_mapping (
|
||||
id INTEGER PRIMARY KEY,
|
||||
project_id INTEGER REFERENCES projects(id) ON DELETE CASCADE,
|
||||
source_path TEXT NOT NULL,
|
||||
index_path TEXT NOT NULL,
|
||||
depth INTEGER,
|
||||
files_count INTEGER DEFAULT 0,
|
||||
last_updated REAL,
|
||||
UNIQUE(source_path)
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
conn.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_dir_source ON dir_mapping(source_path)"
|
||||
)
|
||||
conn.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_dir_project ON dir_mapping(project_id)"
|
||||
)
|
||||
conn.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_project_source ON projects(source_root)"
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
except sqlite3.DatabaseError as exc:
|
||||
raise StorageError(f"Failed to initialize registry schema: {exc}") from exc
|
||||
|
||||
# === Project Operations ===
|
||||
|
||||
def register_project(self, source_root: Path, index_root: Path) -> ProjectInfo:
|
||||
"""Register a new project or update existing one.
|
||||
|
||||
Args:
|
||||
source_root: Source code root directory
|
||||
index_root: Index storage root directory
|
||||
|
||||
Returns:
|
||||
ProjectInfo for the registered project
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
source_root_str = str(source_root.resolve())
|
||||
index_root_str = str(index_root.resolve())
|
||||
now = time.time()
|
||||
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO projects(source_root, index_root, created_at, last_indexed)
|
||||
VALUES(?, ?, ?, ?)
|
||||
ON CONFLICT(source_root) DO UPDATE SET
|
||||
index_root=excluded.index_root,
|
||||
last_indexed=excluded.last_indexed,
|
||||
status='active'
|
||||
""",
|
||||
(source_root_str, index_root_str, now, now),
|
||||
)
|
||||
|
||||
row = conn.execute(
|
||||
"SELECT * FROM projects WHERE source_root=?", (source_root_str,)
|
||||
).fetchone()
|
||||
|
||||
conn.commit()
|
||||
|
||||
if not row:
|
||||
raise StorageError(f"Failed to register project: {source_root}")
|
||||
|
||||
return self._row_to_project_info(row)
|
||||
|
||||
def unregister_project(self, source_root: Path) -> bool:
|
||||
"""Remove a project registration (cascades to directory mappings).
|
||||
|
||||
Args:
|
||||
source_root: Source code root directory
|
||||
|
||||
Returns:
|
||||
True if project was removed, False if not found
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
source_root_str = str(source_root.resolve())
|
||||
|
||||
row = conn.execute(
|
||||
"SELECT id FROM projects WHERE source_root=?", (source_root_str,)
|
||||
).fetchone()
|
||||
|
||||
if not row:
|
||||
return False
|
||||
|
||||
conn.execute("DELETE FROM projects WHERE source_root=?", (source_root_str,))
|
||||
conn.commit()
|
||||
return True
|
||||
|
||||
def get_project(self, source_root: Path) -> Optional[ProjectInfo]:
|
||||
"""Get project information by source root.
|
||||
|
||||
Args:
|
||||
source_root: Source code root directory
|
||||
|
||||
Returns:
|
||||
ProjectInfo if found, None otherwise
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
source_root_str = str(source_root.resolve())
|
||||
|
||||
row = conn.execute(
|
||||
"SELECT * FROM projects WHERE source_root=?", (source_root_str,)
|
||||
).fetchone()
|
||||
|
||||
return self._row_to_project_info(row) if row else None
|
||||
|
||||
def get_project_by_id(self, project_id: int) -> Optional[ProjectInfo]:
|
||||
"""Get project information by ID.
|
||||
|
||||
Args:
|
||||
project_id: Project database ID
|
||||
|
||||
Returns:
|
||||
ProjectInfo if found, None otherwise
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
|
||||
row = conn.execute(
|
||||
"SELECT * FROM projects WHERE id=?", (project_id,)
|
||||
).fetchone()
|
||||
|
||||
return self._row_to_project_info(row) if row else None
|
||||
|
||||
def list_projects(self, status: Optional[str] = None) -> List[ProjectInfo]:
|
||||
"""List all registered projects.
|
||||
|
||||
Args:
|
||||
status: Optional status filter ('active', 'stale', 'removed')
|
||||
|
||||
Returns:
|
||||
List of ProjectInfo objects
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
|
||||
if status:
|
||||
rows = conn.execute(
|
||||
"SELECT * FROM projects WHERE status=? ORDER BY created_at DESC",
|
||||
(status,),
|
||||
).fetchall()
|
||||
else:
|
||||
rows = conn.execute(
|
||||
"SELECT * FROM projects ORDER BY created_at DESC"
|
||||
).fetchall()
|
||||
|
||||
return [self._row_to_project_info(row) for row in rows]
|
||||
|
||||
def update_project_stats(
|
||||
self, source_root: Path, total_files: int, total_dirs: int
|
||||
) -> None:
|
||||
"""Update project statistics.
|
||||
|
||||
Args:
|
||||
source_root: Source code root directory
|
||||
total_files: Total number of indexed files
|
||||
total_dirs: Total number of indexed directories
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
source_root_str = str(source_root.resolve())
|
||||
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE projects
|
||||
SET total_files=?, total_dirs=?, last_indexed=?
|
||||
WHERE source_root=?
|
||||
""",
|
||||
(total_files, total_dirs, time.time(), source_root_str),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
def set_project_status(self, source_root: Path, status: str) -> None:
|
||||
"""Set project status.
|
||||
|
||||
Args:
|
||||
source_root: Source code root directory
|
||||
status: Status string ('active', 'stale', 'removed')
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
source_root_str = str(source_root.resolve())
|
||||
|
||||
conn.execute(
|
||||
"UPDATE projects SET status=? WHERE source_root=?",
|
||||
(status, source_root_str),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
# === Directory Mapping Operations ===
|
||||
|
||||
def register_dir(
|
||||
self,
|
||||
project_id: int,
|
||||
source_path: Path,
|
||||
index_path: Path,
|
||||
depth: int,
|
||||
files_count: int = 0,
|
||||
) -> DirMapping:
|
||||
"""Register a directory mapping.
|
||||
|
||||
Args:
|
||||
project_id: Project database ID
|
||||
source_path: Source directory path
|
||||
index_path: Index database path
|
||||
depth: Directory depth relative to project root
|
||||
files_count: Number of files in directory
|
||||
|
||||
Returns:
|
||||
DirMapping for the registered directory
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
source_path_str = str(source_path.resolve())
|
||||
index_path_str = str(index_path.resolve())
|
||||
now = time.time()
|
||||
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO dir_mapping(
|
||||
project_id, source_path, index_path, depth, files_count, last_updated
|
||||
)
|
||||
VALUES(?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(source_path) DO UPDATE SET
|
||||
index_path=excluded.index_path,
|
||||
depth=excluded.depth,
|
||||
files_count=excluded.files_count,
|
||||
last_updated=excluded.last_updated
|
||||
""",
|
||||
(project_id, source_path_str, index_path_str, depth, files_count, now),
|
||||
)
|
||||
|
||||
row = conn.execute(
|
||||
"SELECT * FROM dir_mapping WHERE source_path=?", (source_path_str,)
|
||||
).fetchone()
|
||||
|
||||
conn.commit()
|
||||
|
||||
if not row:
|
||||
raise StorageError(f"Failed to register directory: {source_path}")
|
||||
|
||||
return self._row_to_dir_mapping(row)
|
||||
|
||||
def unregister_dir(self, source_path: Path) -> bool:
|
||||
"""Remove a directory mapping.
|
||||
|
||||
Args:
|
||||
source_path: Source directory path
|
||||
|
||||
Returns:
|
||||
True if directory was removed, False if not found
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
source_path_str = str(source_path.resolve())
|
||||
|
||||
row = conn.execute(
|
||||
"SELECT id FROM dir_mapping WHERE source_path=?", (source_path_str,)
|
||||
).fetchone()
|
||||
|
||||
if not row:
|
||||
return False
|
||||
|
||||
conn.execute("DELETE FROM dir_mapping WHERE source_path=?", (source_path_str,))
|
||||
conn.commit()
|
||||
return True
|
||||
|
||||
def find_index_path(self, source_path: Path) -> Optional[Path]:
|
||||
"""Find index path for a source directory (exact match).
|
||||
|
||||
Args:
|
||||
source_path: Source directory path
|
||||
|
||||
Returns:
|
||||
Index path if found, None otherwise
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
source_path_str = str(source_path.resolve())
|
||||
|
||||
row = conn.execute(
|
||||
"SELECT index_path FROM dir_mapping WHERE source_path=?",
|
||||
(source_path_str,),
|
||||
).fetchone()
|
||||
|
||||
return Path(row["index_path"]) if row else None
|
||||
|
||||
def find_nearest_index(self, source_path: Path) -> Optional[DirMapping]:
|
||||
"""Find nearest indexed ancestor directory.
|
||||
|
||||
Searches for the closest parent directory that has an index.
|
||||
Useful for supporting subdirectory searches.
|
||||
|
||||
Args:
|
||||
source_path: Source directory or file path
|
||||
|
||||
Returns:
|
||||
DirMapping for nearest ancestor, None if not found
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
source_path_resolved = source_path.resolve()
|
||||
|
||||
# Check from current path up to root
|
||||
current = source_path_resolved
|
||||
while True:
|
||||
current_str = str(current)
|
||||
row = conn.execute(
|
||||
"SELECT * FROM dir_mapping WHERE source_path=?", (current_str,)
|
||||
).fetchone()
|
||||
|
||||
if row:
|
||||
return self._row_to_dir_mapping(row)
|
||||
|
||||
parent = current.parent
|
||||
if parent == current: # Reached filesystem root
|
||||
break
|
||||
current = parent
|
||||
|
||||
return None
|
||||
|
||||
def get_project_dirs(self, project_id: int) -> List[DirMapping]:
|
||||
"""Get all directory mappings for a project.
|
||||
|
||||
Args:
|
||||
project_id: Project database ID
|
||||
|
||||
Returns:
|
||||
List of DirMapping objects
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
|
||||
rows = conn.execute(
|
||||
"SELECT * FROM dir_mapping WHERE project_id=? ORDER BY depth, source_path",
|
||||
(project_id,),
|
||||
).fetchall()
|
||||
|
||||
return [self._row_to_dir_mapping(row) for row in rows]
|
||||
|
||||
def get_subdirs(self, source_path: Path) -> List[DirMapping]:
|
||||
"""Get direct subdirectory mappings.
|
||||
|
||||
Args:
|
||||
source_path: Parent directory path
|
||||
|
||||
Returns:
|
||||
List of DirMapping objects for direct children
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
source_path_str = str(source_path.resolve())
|
||||
|
||||
# First get the parent's depth
|
||||
parent_row = conn.execute(
|
||||
"SELECT depth, project_id FROM dir_mapping WHERE source_path=?",
|
||||
(source_path_str,),
|
||||
).fetchone()
|
||||
|
||||
if not parent_row:
|
||||
return []
|
||||
|
||||
parent_depth = int(parent_row["depth"])
|
||||
project_id = int(parent_row["project_id"])
|
||||
|
||||
# Get all subdirs with depth = parent_depth + 1 and matching path prefix
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT * FROM dir_mapping
|
||||
WHERE project_id=? AND depth=? AND source_path LIKE ?
|
||||
ORDER BY source_path
|
||||
""",
|
||||
(project_id, parent_depth + 1, f"{source_path_str}%"),
|
||||
).fetchall()
|
||||
|
||||
return [self._row_to_dir_mapping(row) for row in rows]
|
||||
|
||||
def update_dir_stats(self, source_path: Path, files_count: int) -> None:
|
||||
"""Update directory statistics.
|
||||
|
||||
Args:
|
||||
source_path: Source directory path
|
||||
files_count: Number of files in directory
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
source_path_str = str(source_path.resolve())
|
||||
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE dir_mapping
|
||||
SET files_count=?, last_updated=?
|
||||
WHERE source_path=?
|
||||
""",
|
||||
(files_count, time.time(), source_path_str),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
def update_index_paths(self, old_root: Path, new_root: Path) -> int:
|
||||
"""Update all index paths after migration.
|
||||
|
||||
Replaces old_root prefix with new_root in all stored index paths.
|
||||
|
||||
Args:
|
||||
old_root: Old index root directory
|
||||
new_root: New index root directory
|
||||
|
||||
Returns:
|
||||
Number of paths updated
|
||||
"""
|
||||
with self._lock:
|
||||
conn = self._get_connection()
|
||||
old_root_str = str(old_root.resolve())
|
||||
new_root_str = str(new_root.resolve())
|
||||
updated = 0
|
||||
|
||||
# Update projects
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE projects
|
||||
SET index_root = REPLACE(index_root, ?, ?)
|
||||
WHERE index_root LIKE ?
|
||||
""",
|
||||
(old_root_str, new_root_str, f"{old_root_str}%"),
|
||||
)
|
||||
updated += conn.total_changes
|
||||
|
||||
# Update dir_mapping
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE dir_mapping
|
||||
SET index_path = REPLACE(index_path, ?, ?)
|
||||
WHERE index_path LIKE ?
|
||||
""",
|
||||
(old_root_str, new_root_str, f"{old_root_str}%"),
|
||||
)
|
||||
updated += conn.total_changes
|
||||
|
||||
conn.commit()
|
||||
return updated
|
||||
|
||||
# === Internal Methods ===
|
||||
|
||||
def _row_to_project_info(self, row: sqlite3.Row) -> ProjectInfo:
|
||||
"""Convert database row to ProjectInfo."""
|
||||
return ProjectInfo(
|
||||
id=int(row["id"]),
|
||||
source_root=Path(row["source_root"]),
|
||||
index_root=Path(row["index_root"]),
|
||||
created_at=float(row["created_at"]) if row["created_at"] else 0.0,
|
||||
last_indexed=float(row["last_indexed"]) if row["last_indexed"] else 0.0,
|
||||
total_files=int(row["total_files"]) if row["total_files"] else 0,
|
||||
total_dirs=int(row["total_dirs"]) if row["total_dirs"] else 0,
|
||||
status=str(row["status"]) if row["status"] else "active",
|
||||
)
|
||||
|
||||
def _row_to_dir_mapping(self, row: sqlite3.Row) -> DirMapping:
|
||||
"""Convert database row to DirMapping."""
|
||||
return DirMapping(
|
||||
id=int(row["id"]),
|
||||
project_id=int(row["project_id"]),
|
||||
source_path=Path(row["source_path"]),
|
||||
index_path=Path(row["index_path"]),
|
||||
depth=int(row["depth"]) if row["depth"] is not None else 0,
|
||||
files_count=int(row["files_count"]) if row["files_count"] else 0,
|
||||
last_updated=float(row["last_updated"]) if row["last_updated"] else 0.0,
|
||||
)
|
||||
@@ -43,6 +43,8 @@ class SQLiteStore:
|
||||
conn.execute("PRAGMA journal_mode=WAL")
|
||||
conn.execute("PRAGMA synchronous=NORMAL")
|
||||
conn.execute("PRAGMA foreign_keys=ON")
|
||||
# Memory-mapped I/O for faster reads (30GB limit)
|
||||
conn.execute("PRAGMA mmap_size=30000000000")
|
||||
self._pool[thread_id] = conn
|
||||
|
||||
self._local.conn = conn
|
||||
@@ -384,7 +386,8 @@ class SQLiteStore:
|
||||
language UNINDEXED,
|
||||
content,
|
||||
content='files',
|
||||
content_rowid='id'
|
||||
content_rowid='id',
|
||||
tokenize="unicode61 tokenchars '_'"
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user