use std::collections::HashMap;
use anyhow::{Context, Result};
use rusqlite::{Connection, OpenFlags, OptionalExtension, params};
use crate::parsing::type_relations::{RawTypeRelation, TypeRelationKind};
use crate::types::{Chunk, Language, SymbolType};
#[derive(Debug, Clone, serde::Serialize)]
pub struct CallerRef {
pub file_path: String,
pub line: u32,
pub caller: Option<String>,
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct CalleeRef {
pub file_path: String,
pub line: u32,
pub callee: String,
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct DeadSymbol {
pub symbol_name: String,
pub file_path: String,
pub line: u32,
pub symbol_type: Option<String>,
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct TypeRelationRef {
pub file_path: String,
pub line: u32,
pub owner: String,
pub target: String,
pub kind: TypeRelationKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FileIndexStatus {
Indexed,
ParseError,
}
impl FileIndexStatus {
fn as_str(self) -> &'static str {
match self {
Self::Indexed => "indexed",
Self::ParseError => "parse_error",
}
}
fn from_db(value: &str) -> std::result::Result<Self, std::io::Error> {
match value {
"indexed" => Ok(Self::Indexed),
"parse_error" => Ok(Self::ParseError),
other => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("unknown file index status: {other}"),
)),
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct FileIndexState {
pub file_path: String,
pub language: String,
pub status: FileIndexStatus,
pub tree_has_error: bool,
pub tier0_fallback: bool,
pub chunk_count: u64,
}
#[derive(Debug, Clone)]
pub struct FileChunkSummary {
pub chunk_count: u64,
pub max_line_end: u32,
pub language: String,
}
#[derive(Debug, Clone, Default, serde::Serialize)]
pub struct IndexHealth {
pub files_indexed: u64,
pub files_with_tree_sitter_errors: u64,
pub files_using_tier0_fallback: u64,
pub files_with_parse_failures: u64,
pub by_language: Vec<LanguageHealthStat>,
}
#[derive(Debug, Clone, Default, serde::Serialize)]
pub struct LanguageHealthStat {
pub language: String,
pub files_indexed: u64,
pub files_with_tree_sitter_errors: u64,
pub files_using_tier0_fallback: u64,
pub files_with_parse_failures: u64,
}
const SQL_CHUNKS_BY_SYMBOL_NAME: &str =
"SELECT id, file_path, line_start, line_end, content, language,
symbol_type, symbol_name
FROM chunks
WHERE lower(symbol_name) = lower(?1)
ORDER BY file_path, line_start";
const SQL_FIND_CALLERS: &str = "SELECT file_path, line, caller FROM [references]
WHERE lower(callee) = lower(?1)
ORDER BY file_path, line";
const SQL_FIND_CALLERS_BY_QUALIFIER: &str = "SELECT file_path, line, caller FROM [references]
WHERE lower(callee) = lower(?1) AND lower(qualifier) = lower(?2)
ORDER BY file_path, line";
const SQL_FIND_CALLEES: &str = "SELECT file_path, line, callee FROM [references]
WHERE lower(caller) = lower(?1)
ORDER BY file_path, line";
const SQL_FIND_TYPE_RELATIONS: &str = "SELECT file_path, line, owner, target, kind
FROM type_relations
WHERE lower(target) = lower(?1)
ORDER BY file_path, line, owner";
const SQL_FIND_DEAD_SYMBOLS: &str = "SELECT c.symbol_name, c.file_path, c.line_start, c.symbol_type
FROM chunks c
WHERE c.symbol_name IS NOT NULL
AND c.symbol_type IN ('function', 'method')
AND lower(c.symbol_name) NOT IN ('main', 'new', 'default', 'drop', 'clone', 'fmt', 'from', 'into', 'deref', 'init', 'setup', 'teardown')
AND NOT EXISTS (
SELECT 1 FROM [references] r
WHERE lower(r.callee) = lower(c.symbol_name)
)
ORDER BY c.file_path, c.line_start";
const REQUIRED_TABLES: &[&str] = &[
"chunks",
"file_hashes",
"file_index_state",
"index_metadata",
"references",
"type_relations",
];
const SQL_PARAMETER_BATCH: usize = 900;
pub struct MetadataStore {
conn: Connection,
}
impl MetadataStore {
pub fn open(db_path: &std::path::Path) -> Result<Self> {
let conn = Connection::open(db_path)
.with_context(|| format!("failed to open metadata db: {}", db_path.display()))?;
let store = Self { conn };
store.init_schema()?;
Ok(store)
}
pub fn open_existing(db_path: &std::path::Path) -> Result<Self> {
if !db_path.is_file() {
anyhow::bail!(
"no index metadata found at: {}\nRun `vera index <path>` first to create an index.",
db_path.display()
);
}
let conn = Connection::open_with_flags(db_path, OpenFlags::SQLITE_OPEN_READ_ONLY)
.with_context(|| format!("failed to open metadata db: {}", db_path.display()))?;
let store = Self { conn };
store.validate_schema()?;
Ok(store)
}
fn validate_schema(&self) -> Result<()> {
let db_path = self.conn.path().unwrap_or_default();
for table in REQUIRED_TABLES {
let present: bool = self
.conn
.query_row(
"SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?1)",
params![table],
|row| row.get(0),
)
.context("failed to inspect metadata schema")?;
anyhow::ensure!(
present,
"metadata db at {} is missing the `{table}` table\nRun `vera index <path>` to rebuild the index.",
db_path
);
}
Ok(())
}
pub fn open_in_memory() -> Result<Self> {
let conn = Connection::open_in_memory().context("failed to open in-memory metadata db")?;
let store = Self { conn };
store.init_schema()?;
Ok(store)
}
fn init_schema(&self) -> Result<()> {
self.conn
.execute_batch(
"PRAGMA journal_mode=WAL;
PRAGMA synchronous=NORMAL;
PRAGMA foreign_keys=ON;",
)
.context("failed to set SQLite pragmas")?;
self.conn
.execute_batch(
"CREATE TABLE IF NOT EXISTS chunks (
id TEXT PRIMARY KEY,
file_path TEXT NOT NULL,
line_start INTEGER NOT NULL,
line_end INTEGER NOT NULL,
content TEXT NOT NULL,
language TEXT NOT NULL,
symbol_type TEXT,
symbol_name TEXT
);
CREATE INDEX IF NOT EXISTS idx_chunks_file_path
ON chunks(file_path);
CREATE INDEX IF NOT EXISTS idx_chunks_language
ON chunks(language);
CREATE INDEX IF NOT EXISTS idx_chunks_symbol_name
ON chunks(symbol_name);
-- `get_chunks_by_symbol_name` matches on `lower(symbol_name)`.
-- An index on the bare column cannot serve a predicate over an
-- expression, so without this the lookup scans the table.
CREATE INDEX IF NOT EXISTS idx_chunks_symbol_name_lower
ON chunks(lower(symbol_name));",
)
.context("failed to create chunks table")?;
self.conn
.execute_batch(
"CREATE TABLE IF NOT EXISTS file_hashes (
file_path TEXT PRIMARY KEY,
content_hash TEXT NOT NULL
);",
)
.context("failed to create file_hashes table")?;
self.conn
.execute_batch(
"CREATE TABLE IF NOT EXISTS file_index_state (
file_path TEXT PRIMARY KEY,
language TEXT NOT NULL,
status TEXT NOT NULL,
tree_has_error INTEGER NOT NULL,
tier0_fallback INTEGER NOT NULL,
chunk_count INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_file_index_state_language
ON file_index_state(language);
CREATE INDEX IF NOT EXISTS idx_file_index_state_status
ON file_index_state(status);",
)
.context("failed to create file_index_state table")?;
self.conn
.execute_batch(
"CREATE TABLE IF NOT EXISTS index_metadata (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
);",
)
.context("failed to create index_metadata table")?;
self.conn
.execute_batch(
"CREATE TABLE IF NOT EXISTS [references] (
file_path TEXT NOT NULL,
line INTEGER NOT NULL,
callee TEXT NOT NULL,
caller TEXT,
qualifier TEXT
);
CREATE INDEX IF NOT EXISTS idx_refs_callee
ON [references](callee);
CREATE INDEX IF NOT EXISTS idx_refs_caller
ON [references](caller);
CREATE INDEX IF NOT EXISTS idx_refs_file_path
ON [references](file_path);
-- `find_callers`, `find_callees` and the `NOT EXISTS` subquery
-- in `find_dead_symbols` all match on `lower(...)`, which an
-- index on the bare column cannot serve.
CREATE INDEX IF NOT EXISTS idx_refs_callee_lower
ON [references](lower(callee));
CREATE INDEX IF NOT EXISTS idx_refs_caller_lower
ON [references](lower(caller));",
)
.context("failed to create references table")?;
if !self.column_exists("references", "qualifier")? {
self.conn
.execute_batch("ALTER TABLE [references] ADD COLUMN qualifier TEXT;")
.context("failed to add qualifier column to references table")?;
}
self.conn
.execute_batch(
"CREATE TABLE IF NOT EXISTS type_relations (
file_path TEXT NOT NULL,
line INTEGER NOT NULL,
owner TEXT NOT NULL,
target TEXT NOT NULL,
kind TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_type_relations_target
ON type_relations(target);
CREATE INDEX IF NOT EXISTS idx_type_relations_owner
ON type_relations(owner);
CREATE INDEX IF NOT EXISTS idx_type_relations_file_path
ON type_relations(file_path);
-- `find_type_relations` matches on `lower(target)`, which an
-- index on the bare column cannot serve.
CREATE INDEX IF NOT EXISTS idx_type_relations_target_lower
ON type_relations(lower(target));",
)
.context("failed to create type_relations table")?;
Ok(())
}
pub fn insert_chunks(&self, chunks: &[Chunk]) -> Result<()> {
let tx = self
.conn
.unchecked_transaction()
.context("failed to begin transaction")?;
{
let mut stmt = self
.conn
.prepare_cached(
"INSERT OR REPLACE INTO chunks
(id, file_path, line_start, line_end, content, language, symbol_type, symbol_name)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
)
.context("failed to prepare insert statement")?;
for chunk in chunks {
let sym_type = chunk.symbol_type.map(|st| st.to_string());
stmt.execute(params![
chunk.id,
chunk.file_path,
chunk.line_start,
chunk.line_end,
chunk.content,
chunk.language.to_string(),
sym_type,
chunk.symbol_name,
])
.with_context(|| format!("failed to insert chunk: {}", chunk.id))?;
}
}
tx.commit().context("failed to commit chunk inserts")?;
Ok(())
}
pub fn get_chunk(&self, id: &str) -> Result<Option<Chunk>> {
let mut stmt = self
.conn
.prepare_cached(
"SELECT id, file_path, line_start, line_end, content, language,
symbol_type, symbol_name
FROM chunks WHERE id = ?1",
)
.context("failed to prepare select statement")?;
let result = stmt
.query_row(params![id], |row| Ok(row_to_chunk(row)))
.optional()
.context("failed to query chunk by id")?;
match result {
Some(chunk) => Ok(Some(chunk?)),
None => Ok(None),
}
}
pub fn get_chunks_by_ids(&self, ids: &[String]) -> Result<HashMap<String, Chunk>> {
if ids.is_empty() {
return Ok(HashMap::new());
}
let placeholders = std::iter::repeat_n("?", ids.len())
.collect::<Vec<_>>()
.join(",");
let sql = format!(
"SELECT id, file_path, line_start, line_end, content, language,
symbol_type, symbol_name
FROM chunks WHERE id IN ({placeholders})"
);
let mut stmt = self
.conn
.prepare(&sql)
.context("failed to prepare batch chunk query")?;
let rows = stmt
.query_map(rusqlite::params_from_iter(ids.iter()), |row| {
Ok(row_to_chunk(row))
})
.context("failed to query chunks by id batch")?;
let chunks: Vec<Chunk> = collect_rows(rows)?
.into_iter()
.collect::<Result<Vec<_>>>()?;
Ok(chunks.into_iter().map(|c| (c.id.clone(), c)).collect())
}
pub fn get_chunks_by_file(&self, file_path: &str) -> Result<Vec<Chunk>> {
let mut stmt = self
.conn
.prepare_cached(
"SELECT id, file_path, line_start, line_end, content, language,
symbol_type, symbol_name
FROM chunks WHERE file_path = ?1
ORDER BY line_start",
)
.context("failed to prepare file chunks query")?;
let rows = stmt
.query_map(params![file_path], |row| Ok(row_to_chunk(row)))
.context("failed to query chunks by file")?;
collect_rows(rows)?.into_iter().collect()
}
pub fn file_chunk_summaries(
&self,
file_paths: &[String],
) -> Result<HashMap<String, FileChunkSummary>> {
let mut summaries = HashMap::with_capacity(file_paths.len());
for batch in file_paths.chunks(SQL_PARAMETER_BATCH) {
let placeholders = std::iter::repeat_n("?", batch.len())
.collect::<Vec<_>>()
.join(",");
let sql = format!(
"SELECT file_path, COUNT(*), MAX(line_end), MIN(language)
FROM chunks WHERE file_path IN ({placeholders})
GROUP BY file_path"
);
let mut stmt = self
.conn
.prepare(&sql)
.context("failed to prepare file chunk summaries query")?;
let rows = stmt
.query_map(rusqlite::params_from_iter(batch.iter()), |row| {
Ok((
row.get::<_, String>(0)?,
FileChunkSummary {
chunk_count: row.get::<_, i64>(1)? as u64,
max_line_end: row.get(2)?,
language: row.get(3)?,
},
))
})
.context("failed to query file chunk summaries")?;
for (file_path, summary) in collect_rows(rows)? {
summaries.insert(file_path, summary);
}
}
Ok(summaries)
}
pub fn symbol_type_counts(&self, file_paths: &[String]) -> Result<Vec<(String, u64)>> {
let mut totals: HashMap<String, u64> = HashMap::new();
for batch in file_paths.chunks(SQL_PARAMETER_BATCH) {
let placeholders = std::iter::repeat_n("?", batch.len())
.collect::<Vec<_>>()
.join(",");
let sql = format!(
"SELECT symbol_type, COUNT(*) FROM chunks
WHERE file_path IN ({placeholders}) AND symbol_type IS NOT NULL
GROUP BY symbol_type"
);
let mut stmt = self
.conn
.prepare(&sql)
.context("failed to prepare symbol type counts query")?;
let rows = stmt
.query_map(rusqlite::params_from_iter(batch.iter()), |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)? as u64))
})
.context("failed to query symbol type counts")?;
for (symbol_type, count) in collect_rows(rows)? {
*totals.entry(symbol_type).or_default() += count;
}
}
Ok(totals.into_iter().collect())
}
pub fn get_chunks_by_symbol_name(&self, symbol_name: &str) -> Result<Vec<Chunk>> {
let mut stmt = self
.conn
.prepare_cached(SQL_CHUNKS_BY_SYMBOL_NAME)
.context("failed to prepare symbol chunks query")?;
let rows = stmt
.query_map(params![symbol_name], |row| Ok(row_to_chunk(row)))
.context("failed to query chunks by symbol name")?;
collect_rows(rows)?.into_iter().collect()
}
pub fn get_chunks_by_symbol_name_case_sensitive(
&self,
symbol_name: &str,
) -> Result<Vec<Chunk>> {
let mut stmt = self
.conn
.prepare_cached(
"SELECT id, file_path, line_start, line_end, content, language,
symbol_type, symbol_name
FROM chunks
WHERE symbol_name = ?1
ORDER BY file_path, line_start",
)
.context("failed to prepare case-sensitive symbol chunks query")?;
let rows = stmt
.query_map(params![symbol_name], |row| Ok(row_to_chunk(row)))
.context("failed to query chunks by symbol name (case-sensitive)")?;
collect_rows(rows)?.into_iter().collect()
}
pub fn chunk_count(&self) -> Result<u64> {
let count: i64 = self
.conn
.query_row("SELECT COUNT(*) FROM chunks", [], |row| row.get(0))
.context("failed to count chunks")?;
Ok(count as u64)
}
pub fn delete_chunks_by_file(&self, file_path: &str) -> Result<u64> {
let deleted = self
.conn
.execute(
"DELETE FROM chunks WHERE file_path = ?1",
params![file_path],
)
.context("failed to delete chunks by file")?;
Ok(deleted as u64)
}
pub fn clear(&self) -> Result<()> {
self.conn
.execute_batch(
"DELETE FROM chunks; DELETE FROM file_hashes; DELETE FROM file_index_state; DELETE FROM index_metadata; DELETE FROM [references]; DELETE FROM type_relations;",
)
.context("failed to clear metadata store")?;
Ok(())
}
pub fn insert_file_states(&self, states: &[FileIndexState]) -> Result<()> {
let tx = self
.conn
.unchecked_transaction()
.context("failed to begin file state transaction")?;
{
let mut stmt = self
.conn
.prepare_cached(
"INSERT OR REPLACE INTO file_index_state
(file_path, language, status, tree_has_error, tier0_fallback, chunk_count)
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
)
.context("failed to prepare file state insert")?;
for state in states {
stmt.execute(params![
state.file_path,
state.language,
state.status.as_str(),
state.tree_has_error,
state.tier0_fallback,
state.chunk_count as i64,
])
.with_context(|| format!("failed to insert file state: {}", state.file_path))?;
}
}
tx.commit().context("failed to commit file state inserts")?;
Ok(())
}
pub fn delete_file_state(&self, file_path: &str) -> Result<()> {
self.conn
.execute(
"DELETE FROM file_index_state WHERE file_path = ?1",
params![file_path],
)
.context("failed to delete file state")?;
Ok(())
}
pub fn set_index_meta(&self, key: &str, value: &str) -> Result<()> {
self.conn
.execute(
"INSERT OR REPLACE INTO index_metadata (key, value) VALUES (?1, ?2)",
params![key, value],
)
.context("failed to set index metadata")?;
Ok(())
}
pub fn get_index_meta(&self, key: &str) -> Result<Option<String>> {
let result: Option<String> = self
.conn
.query_row(
"SELECT value FROM index_metadata WHERE key = ?1",
params![key],
|row| row.get(0),
)
.optional()
.context("failed to get index metadata")?;
Ok(result)
}
pub fn set_file_hash(&self, file_path: &str, hash: &str) -> Result<()> {
self.set_file_hashes_batch_borrowed(&[(file_path, hash)])
}
pub fn get_file_hash(&self, file_path: &str) -> Result<Option<String>> {
let result: Option<String> = self
.conn
.query_row(
"SELECT content_hash FROM file_hashes WHERE file_path = ?1",
params![file_path],
|row| row.get(0),
)
.optional()
.context("failed to get file hash")?;
Ok(result)
}
pub fn delete_file_hash(&self, file_path: &str) -> Result<()> {
self.conn
.execute(
"DELETE FROM file_hashes WHERE file_path = ?1",
params![file_path],
)
.context("failed to delete file hash")?;
Ok(())
}
pub fn tracked_files(&self) -> Result<Vec<String>> {
let mut stmt = self
.conn
.prepare("SELECT file_path FROM file_hashes ORDER BY file_path")
.context("failed to prepare tracked files query")?;
let rows = stmt
.query_map([], |row| row.get(0))
.context("failed to query tracked files")?;
collect_rows(rows)
}
pub fn file_states(&self) -> Result<Vec<FileIndexState>> {
let mut stmt = self
.conn
.prepare(
"SELECT file_path, language, status, tree_has_error, tier0_fallback, chunk_count
FROM file_index_state
ORDER BY file_path",
)
.context("failed to prepare file states query")?;
let rows = stmt
.query_map([], |row| {
let status: String = row.get(2)?;
Ok(FileIndexState {
file_path: row.get(0)?,
language: row.get(1)?,
status: FileIndexStatus::from_db(&status).map_err(|err| {
rusqlite::Error::FromSqlConversionFailure(
2,
rusqlite::types::Type::Text,
Box::new(err),
)
})?,
tree_has_error: row.get(3)?,
tier0_fallback: row.get(4)?,
chunk_count: row.get::<_, i64>(5)? as u64,
})
})
.context("failed to query file states")?;
collect_rows(rows)
}
pub fn set_file_hashes_batch(&self, hashes: &[(String, String)]) -> Result<()> {
if hashes.is_empty() {
return Ok(());
}
let borrowed: Vec<(&str, &str)> = hashes
.iter()
.map(|(file_path, hash)| (file_path.as_str(), hash.as_str()))
.collect();
self.set_file_hashes_batch_borrowed(&borrowed)
}
pub(crate) fn set_file_hashes_batch_borrowed(&self, hashes: &[(&str, &str)]) -> Result<()> {
if hashes.is_empty() {
return Ok(());
}
let tx = self
.conn
.unchecked_transaction()
.context("failed to begin file hash batch transaction")?;
{
let mut stmt = tx
.prepare_cached(
"INSERT OR REPLACE INTO file_hashes (file_path, content_hash)
VALUES (?1, ?2)",
)
.context("failed to prepare batch file hash insert")?;
for (file_path, hash) in hashes {
stmt.execute(params![file_path, hash])
.with_context(|| format!("failed to set file hash: {file_path}"))?;
}
}
tx.commit().context("failed to commit file hash batch")?;
Ok(())
}
pub fn insert_parse_artifacts_batch(
&self,
file_refs: &[(String, Vec<crate::parsing::references::RawReference>)],
file_type_relations: &[(String, Vec<RawTypeRelation>)],
) -> Result<()> {
if file_refs.is_empty() && file_type_relations.is_empty() {
return Ok(());
}
let borrowed_refs: Vec<(&str, &[crate::parsing::references::RawReference])> = file_refs
.iter()
.map(|(file_path, refs)| (file_path.as_str(), refs.as_slice()))
.collect();
let borrowed_relations: Vec<(&str, &[RawTypeRelation])> = file_type_relations
.iter()
.map(|(file_path, relations)| (file_path.as_str(), relations.as_slice()))
.collect();
self.insert_parse_artifacts_batch_borrowed(&borrowed_refs, &borrowed_relations)
}
pub(crate) fn insert_parse_artifacts_batch_borrowed(
&self,
file_refs: &[(&str, &[crate::parsing::references::RawReference])],
file_type_relations: &[(&str, &[RawTypeRelation])],
) -> Result<()> {
if file_refs.is_empty() && file_type_relations.is_empty() {
return Ok(());
}
let tx = self
.conn
.unchecked_transaction()
.context("failed to begin parse artifacts batch transaction")?;
{
let mut ref_stmt = tx
.prepare_cached(
"INSERT INTO [references] (file_path, line, callee, caller, qualifier)
VALUES (?1, ?2, ?3, ?4, ?5)",
)
.context("failed to prepare batch reference insert")?;
for &(file_path, refs) in file_refs {
for r in refs {
ref_stmt
.execute(params![file_path, r.line, r.callee, r.caller, r.qualifier])
.with_context(|| format!("failed to insert reference for {file_path}"))?;
}
}
let mut relation_stmt = tx
.prepare_cached(
"INSERT INTO type_relations (file_path, line, owner, target, kind)
VALUES (?1, ?2, ?3, ?4, ?5)",
)
.context("failed to prepare batch type relation insert")?;
for &(file_path, relations) in file_type_relations {
for relation in relations {
relation_stmt
.execute(params![
file_path,
relation.line,
relation.owner,
relation.target,
relation.kind.as_str(),
])
.with_context(|| {
format!("failed to insert type relation for {file_path}")
})?;
}
}
}
tx.commit()
.context("failed to commit parse artifacts batch")?;
Ok(())
}
pub fn delete_references_by_file(&self, file_path: &str) -> Result<()> {
self.conn
.execute(
"DELETE FROM [references] WHERE file_path = ?1",
params![file_path],
)
.context("failed to delete references by file")?;
Ok(())
}
pub fn delete_type_relations_by_file(&self, file_path: &str) -> Result<()> {
self.conn
.execute(
"DELETE FROM type_relations WHERE file_path = ?1",
params![file_path],
)
.context("failed to delete type relations by file")?;
Ok(())
}
fn column_exists(&self, table: &str, column: &str) -> Result<bool> {
let mut stmt = self
.conn
.prepare(&format!("PRAGMA table_info([{table}])"))
.with_context(|| format!("failed to inspect {table} columns"))?;
let mut rows = stmt.query([])?;
while let Some(row) = rows.next()? {
if row.get::<_, String>(1)? == column {
return Ok(true);
}
}
Ok(false)
}
pub fn find_callers(&self, symbol_name: &str) -> Result<Vec<CallerRef>> {
self.find_callers_through(symbol_name, None)
}
pub fn find_callers_through(
&self,
symbol_name: &str,
qualifier: Option<&str>,
) -> Result<Vec<CallerRef>> {
let read = |row: &rusqlite::Row<'_>| {
Ok(CallerRef {
file_path: row.get(0)?,
line: row.get(1)?,
caller: row.get(2)?,
})
};
match qualifier {
Some(qualifier) => {
let mut stmt = self
.conn
.prepare_cached(SQL_FIND_CALLERS_BY_QUALIFIER)
.context("failed to prepare receiver-filtered callers query")?;
let rows = stmt
.query_map(params![symbol_name, qualifier], read)
.context("failed to query callers")?;
collect_rows(rows)
}
None => {
let mut stmt = self
.conn
.prepare_cached(SQL_FIND_CALLERS)
.context("failed to prepare callers query")?;
let rows = stmt
.query_map(params![symbol_name], read)
.context("failed to query callers")?;
collect_rows(rows)
}
}
}
pub fn caller_qualifiers(&self, symbol_name: &str) -> Result<Vec<(String, usize)>> {
let mut stmt = self
.conn
.prepare_cached(
"SELECT qualifier, COUNT(*) FROM [references]
WHERE lower(callee) = lower(?1) AND qualifier IS NOT NULL
GROUP BY lower(qualifier)
ORDER BY COUNT(*) DESC, qualifier",
)
.context("failed to prepare receiver summary query")?;
let rows = stmt
.query_map(params![symbol_name], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)? as usize))
})
.context("failed to query receivers")?;
collect_rows(rows)
}
pub fn find_type_relations(&self, symbol_name: &str) -> Result<Vec<TypeRelationRef>> {
let mut stmt = self
.conn
.prepare_cached(SQL_FIND_TYPE_RELATIONS)
.context("failed to prepare type relation query")?;
let rows = stmt
.query_map(params![symbol_name], |row| {
Ok(TypeRelationRef {
file_path: row.get(0)?,
line: row.get(1)?,
owner: row.get(2)?,
target: row.get(3)?,
kind: TypeRelationKind::parse(&row.get::<_, String>(4)?).ok_or_else(|| {
rusqlite::Error::InvalidColumnType(
4,
"kind".to_string(),
rusqlite::types::Type::Text,
)
})?,
})
})
.context("failed to query type relations")?;
collect_rows(rows)
}
pub fn find_callees(&self, symbol_name: &str) -> Result<Vec<CalleeRef>> {
let mut stmt = self
.conn
.prepare_cached(SQL_FIND_CALLEES)
.context("failed to prepare callees query")?;
let rows = stmt
.query_map(params![symbol_name], |row| {
Ok(CalleeRef {
file_path: row.get(0)?,
line: row.get(1)?,
callee: row.get(2)?,
})
})
.context("failed to query callees")?;
collect_rows(rows)
}
pub fn find_dead_symbols(&self) -> Result<Vec<DeadSymbol>> {
let mut stmt = self
.conn
.prepare(SQL_FIND_DEAD_SYMBOLS)
.context("failed to prepare dead symbols query")?;
let rows = stmt
.query_map([], |row| {
Ok(DeadSymbol {
symbol_name: row.get(0)?,
file_path: row.get(1)?,
line: row.get(2)?,
symbol_type: row.get(3)?,
})
})
.context("failed to query dead symbols")?;
collect_rows(rows)
}
pub fn indexed_files(&self) -> Result<Vec<String>> {
let mut stmt = self
.conn
.prepare("SELECT DISTINCT file_path FROM chunks ORDER BY file_path")
.context("failed to prepare indexed files query")?;
let rows = stmt
.query_map([], |row| row.get(0))
.context("failed to query indexed files")?;
collect_rows(rows)
}
pub fn file_count(&self) -> Result<u64> {
let count: i64 = self
.conn
.query_row("SELECT COUNT(DISTINCT file_path) FROM chunks", [], |row| {
row.get(0)
})
.context("failed to count files")?;
Ok(count as u64)
}
pub fn language_stats(&self) -> Result<Vec<(String, u64)>> {
let mut stmt = self
.conn
.prepare(
"SELECT language, COUNT(*) FROM chunks
GROUP BY language ORDER BY COUNT(*) DESC",
)
.context("failed to prepare language stats query")?;
let rows = stmt
.query_map([], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?))
})
.context("failed to query language stats")?;
Ok(collect_rows(rows)?
.into_iter()
.map(|(lang, count): (String, i64)| (lang, count as u64))
.collect())
}
pub fn language_file_counts(&self) -> Result<Vec<(String, u64)>> {
let mut stmt = self
.conn
.prepare(
"SELECT language, COUNT(DISTINCT file_path) FROM chunks
GROUP BY language ORDER BY COUNT(DISTINCT file_path) DESC",
)
.context("failed to prepare language file counts query")?;
let rows = stmt
.query_map([], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?))
})
.context("failed to query language file counts")?;
Ok(collect_rows(rows)?
.into_iter()
.map(|(lang, count): (String, i64)| (lang, count as u64))
.collect())
}
pub fn index_health(&self) -> Result<IndexHealth> {
let files_indexed: i64 = self
.conn
.query_row(
"SELECT COUNT(*) FROM file_index_state WHERE status = 'indexed'",
[],
|row| row.get(0),
)
.context("failed to count indexed files for health")?;
let files_with_tree_sitter_errors: i64 = self
.conn
.query_row(
"SELECT COUNT(*) FROM file_index_state WHERE status = 'indexed' AND tree_has_error = 1",
[],
|row| row.get(0),
)
.context("failed to count tree-sitter errors for health")?;
let files_using_tier0_fallback: i64 = self
.conn
.query_row(
"SELECT COUNT(*) FROM file_index_state WHERE status = 'indexed' AND tier0_fallback = 1",
[],
|row| row.get(0),
)
.context("failed to count tier0 fallbacks for health")?;
let files_with_parse_failures: i64 = self
.conn
.query_row(
"SELECT COUNT(*) FROM file_index_state WHERE status = 'parse_error'",
[],
|row| row.get(0),
)
.context("failed to count parse failures for health")?;
let mut stmt = self
.conn
.prepare(
"SELECT
language,
SUM(CASE WHEN status = 'indexed' THEN 1 ELSE 0 END),
SUM(CASE WHEN status = 'indexed' AND tree_has_error = 1 THEN 1 ELSE 0 END),
SUM(CASE WHEN status = 'indexed' AND tier0_fallback = 1 THEN 1 ELSE 0 END),
SUM(CASE WHEN status = 'parse_error' THEN 1 ELSE 0 END)
FROM file_index_state
GROUP BY language
ORDER BY (SUM(CASE WHEN status = 'indexed' THEN 1 ELSE 0 END) +
SUM(CASE WHEN status = 'parse_error' THEN 1 ELSE 0 END)) DESC,
language ASC",
)
.context("failed to prepare index health query")?;
let rows = stmt
.query_map([], |row| {
Ok(LanguageHealthStat {
language: row.get(0)?,
files_indexed: row.get::<_, i64>(1)? as u64,
files_with_tree_sitter_errors: row.get::<_, i64>(2)? as u64,
files_using_tier0_fallback: row.get::<_, i64>(3)? as u64,
files_with_parse_failures: row.get::<_, i64>(4)? as u64,
})
})
.context("failed to execute index health query")?;
let by_language = collect_rows(rows)?;
Ok(IndexHealth {
files_indexed: files_indexed as u64,
files_with_tree_sitter_errors: files_with_tree_sitter_errors as u64,
files_using_tier0_fallback: files_using_tier0_fallback as u64,
files_with_parse_failures: files_with_parse_failures as u64,
by_language,
})
}
pub fn top_directories(&self, limit: usize) -> Result<Vec<(String, u64)>> {
let mut stmt = self
.conn
.prepare(
"SELECT
CASE
WHEN instr(file_path, '/') > 0
THEN substr(file_path, 1, instr(file_path, '/') - 1)
ELSE '.'
END AS dir,
COUNT(DISTINCT file_path)
FROM chunks
GROUP BY dir
ORDER BY COUNT(DISTINCT file_path) DESC
LIMIT ?1",
)
.context("failed to prepare top directories query")?;
let rows = stmt
.query_map(params![limit as i64], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?))
})
.context("failed to query top directories")?;
Ok(collect_rows(rows)?
.into_iter()
.map(|(dir, count): (String, i64)| (dir, count as u64))
.collect())
}
pub fn total_lines(&self) -> Result<u64> {
let count: i64 = self
.conn
.query_row(
"SELECT COALESCE(SUM(line_end - line_start + 1), 0) FROM chunks",
[],
|row| row.get(0),
)
.context("failed to count total lines")?;
Ok(count as u64)
}
pub fn symbol_type_stats(&self) -> Result<Vec<(String, u64)>> {
let mut stmt = self
.conn
.prepare(
"SELECT symbol_type, COUNT(*) FROM chunks
WHERE symbol_type IS NOT NULL
GROUP BY symbol_type ORDER BY COUNT(*) DESC",
)
.context("failed to prepare symbol type stats query")?;
let rows = stmt
.query_map([], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?))
})
.context("failed to query symbol type stats")?;
Ok(collect_rows(rows)?
.into_iter()
.map(|(sym_type, count): (String, i64)| (sym_type, count as u64))
.collect())
}
pub fn hotspot_files(&self, limit: usize) -> Result<Vec<(String, u64)>> {
let mut stmt = self
.conn
.prepare(
"SELECT file_path, COUNT(*) FROM chunks
GROUP BY file_path ORDER BY COUNT(*) DESC LIMIT ?1",
)
.context("failed to prepare hotspot files query")?;
let rows = stmt
.query_map(params![limit as i64], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?))
})
.context("failed to query hotspot files")?;
Ok(collect_rows(rows)?
.into_iter()
.map(|(path, count): (String, i64)| (path, count as u64))
.collect())
}
pub fn entry_points(&self) -> Result<Vec<String>> {
let files = self.indexed_files()?;
Ok(files
.into_iter()
.filter(|file_path| is_entry_point_path(file_path))
.collect())
}
}
fn row_to_chunk(row: &rusqlite::Row<'_>) -> Result<Chunk> {
let id: String = row.get(0).context("missing id")?;
let file_path: String = row.get(1).context("missing file_path")?;
let line_start: u32 = row.get(2).context("missing line_start")?;
let line_end: u32 = row.get(3).context("missing line_end")?;
let content: String = row.get(4).context("missing content")?;
let language_str: String = row.get(5).context("missing language")?;
let symbol_type_str: Option<String> = row.get(6).context("missing symbol_type")?;
let symbol_name: Option<String> = row.get(7).context("missing symbol_name")?;
let language = parse_language(&language_str);
let symbol_type = symbol_type_str.as_deref().map(parse_symbol_type);
Ok(Chunk {
id,
file_path,
line_start,
line_end,
content,
language,
symbol_type,
symbol_name,
})
}
fn parse_language(s: &str) -> Language {
match s.parse::<Language>() {
Ok(language) => language,
Err(_) => {
warn_unknown_enum_value("language", s);
Language::Unknown
}
}
}
fn parse_symbol_type(s: &str) -> SymbolType {
match s {
"function" => SymbolType::Function,
"method" => SymbolType::Method,
"class" => SymbolType::Class,
"struct" => SymbolType::Struct,
"enum" => SymbolType::Enum,
"trait" => SymbolType::Trait,
"interface" => SymbolType::Interface,
"type_alias" => SymbolType::TypeAlias,
"constant" => SymbolType::Constant,
"variable" => SymbolType::Variable,
"module" => SymbolType::Module,
"block" => SymbolType::Block,
other => {
warn_unknown_enum_value("symbol type", other);
SymbolType::Block
}
}
}
fn warn_unknown_enum_value(kind: &'static str, value: &str) {
static WARNED_KINDS: std::sync::OnceLock<std::sync::Mutex<HashMap<&'static str, String>>> =
std::sync::OnceLock::new();
let warned = WARNED_KINDS.get_or_init(|| std::sync::Mutex::new(HashMap::new()));
let Ok(mut warned) = warned.lock() else {
return;
};
if warned.insert(kind, value.to_string()).is_none() {
tracing::warn!(
enum_kind = kind,
value = value,
"unknown persisted enum value; reporting the fallback variant"
);
}
}
pub(crate) fn is_entry_point_path(file_path: &str) -> bool {
let Some(file_name) = std::path::Path::new(file_path)
.file_name()
.and_then(|name| name.to_str())
else {
return false;
};
let Some((stem, _rest)) = file_name.split_once('.') else {
return false;
};
matches!(stem, "main" | "index" | "app" | "lib" | "mod" | "server")
}
fn collect_rows<T>(
rows: impl IntoIterator<Item = std::result::Result<T, rusqlite::Error>>,
) -> Result<Vec<T>> {
rows.into_iter()
.map(|row| row.context("failed to read row"))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_chunks() -> Vec<Chunk> {
vec![
Chunk {
id: "src/main.rs:0".to_string(),
file_path: "src/main.rs".to_string(),
line_start: 1,
line_end: 5,
content: "fn main() {\n println!(\"hello\");\n}".to_string(),
language: Language::Rust,
symbol_type: Some(SymbolType::Function),
symbol_name: Some("main".to_string()),
},
Chunk {
id: "src/main.rs:1".to_string(),
file_path: "src/main.rs".to_string(),
line_start: 7,
line_end: 12,
content: "struct Config {\n name: String,\n}".to_string(),
language: Language::Rust,
symbol_type: Some(SymbolType::Struct),
symbol_name: Some("Config".to_string()),
},
Chunk {
id: "src/lib.py:0".to_string(),
file_path: "src/lib.py".to_string(),
line_start: 1,
line_end: 3,
content: "def hello():\n pass".to_string(),
language: Language::Python,
symbol_type: Some(SymbolType::Function),
symbol_name: Some("hello".to_string()),
},
]
}
#[test]
fn insert_and_count() {
let store = MetadataStore::open_in_memory().unwrap();
let chunks = sample_chunks();
store.insert_chunks(&chunks).unwrap();
assert_eq!(store.chunk_count().unwrap(), 3);
}
#[test]
fn case_insensitive_lookups_use_an_index_rather_than_scanning() {
let store = MetadataStore::open_in_memory().unwrap();
let plan_for = |sql: &str| -> String {
let mut stmt = store
.conn
.prepare(&format!("EXPLAIN QUERY PLAN {sql}"))
.unwrap();
let rows: Vec<String> = stmt
.query_map(params!["needle"], |row| row.get::<_, String>(3))
.unwrap()
.map(|r| r.unwrap())
.collect();
rows.join(" | ")
};
for (label, sql, expected_index) in [
("find_callers", SQL_FIND_CALLERS, "idx_refs_callee_lower"),
("find_callees", SQL_FIND_CALLEES, "idx_refs_caller_lower"),
(
"find_type_relations",
SQL_FIND_TYPE_RELATIONS,
"idx_type_relations_target_lower",
),
(
"get_chunks_by_symbol_name",
SQL_CHUNKS_BY_SYMBOL_NAME,
"idx_chunks_symbol_name_lower",
),
] {
let plan = plan_for(sql);
assert!(
plan.contains(&format!("USING INDEX {expected_index}")),
"{label} must use {expected_index}, but the plan was: {plan}"
);
assert!(
plan.split(" | ").all(|detail| {
detail
.split_whitespace()
.next()
.is_none_or(|operation| operation != "SCAN")
}),
"{label} still scans: {plan}"
);
}
}
#[test]
fn dead_symbol_lookup_seeks_the_reference_index() {
let store = MetadataStore::open_in_memory().unwrap();
let mut stmt = store
.conn
.prepare(&format!("EXPLAIN QUERY PLAN {SQL_FIND_DEAD_SYMBOLS}"))
.unwrap();
let plan: Vec<String> = stmt
.query_map([], |row| row.get::<_, String>(3))
.unwrap()
.map(|r| r.unwrap())
.collect();
let plan = plan.join(" | ");
assert!(
plan.contains("SEARCH r"),
"the reference subquery must seek, not scan: {plan}"
);
}
#[test]
fn set_file_hashes_batch_persists_every_row_and_replaces_on_repeat() {
let store = MetadataStore::open_in_memory().unwrap();
store
.set_file_hashes_batch(&[
("src/main.rs".to_string(), "hash-a".to_string()),
("src/lib.py".to_string(), "hash-b".to_string()),
])
.unwrap();
assert_eq!(
store.get_file_hash("src/main.rs").unwrap().as_deref(),
Some("hash-a")
);
assert_eq!(
store.get_file_hash("src/lib.py").unwrap().as_deref(),
Some("hash-b")
);
store
.set_file_hashes_batch(&[("src/main.rs".to_string(), "hash-c".to_string())])
.unwrap();
assert_eq!(
store.get_file_hash("src/main.rs").unwrap().as_deref(),
Some("hash-c")
);
assert_eq!(
store.get_file_hash("src/lib.py").unwrap().as_deref(),
Some("hash-b")
);
}
#[test]
fn batch_writes_accept_empty_input() {
let store = MetadataStore::open_in_memory().unwrap();
store.set_file_hashes_batch(&[]).unwrap();
store.insert_parse_artifacts_batch(&[], &[]).unwrap();
assert!(store.get_chunks_by_ids(&[]).unwrap().is_empty());
}
#[test]
fn insert_parse_artifacts_batch_persists_both_kinds_across_files() {
let store = MetadataStore::open_in_memory().unwrap();
let refs = vec![
(
"src/main.rs".to_string(),
vec![crate::parsing::references::RawReference {
callee: "helper".to_string(),
caller: Some("main".to_string()),
qualifier: None,
line: 3,
}],
),
(
"src/lib.py".to_string(),
vec![crate::parsing::references::RawReference {
callee: "helper".to_string(),
caller: None,
qualifier: None,
line: 9,
}],
),
];
let relations = vec![(
"src/main.rs".to_string(),
vec![RawTypeRelation {
owner: "Config".to_string(),
target: "Display".to_string(),
line: 12,
kind: TypeRelationKind::Conforms,
}],
)];
store
.insert_parse_artifacts_batch(&refs, &relations)
.unwrap();
let mut caller_paths: Vec<String> = store
.find_callers("helper")
.unwrap()
.into_iter()
.map(|caller_ref| caller_ref.file_path)
.collect();
caller_paths.sort();
assert_eq!(caller_paths, vec!["src/lib.py", "src/main.rs"]);
assert_eq!(store.find_type_relations("Display").unwrap().len(), 1);
}
#[test]
fn get_chunks_by_ids_returns_only_requested_rows() {
let store = MetadataStore::open_in_memory().unwrap();
store.insert_chunks(&sample_chunks()).unwrap();
let found = store
.get_chunks_by_ids(&["src/main.rs:0".to_string(), "src/lib.py:0".to_string()])
.unwrap();
assert_eq!(found.len(), 2);
assert_eq!(found["src/main.rs:0"].symbol_name.as_deref(), Some("main"));
assert!(!found.contains_key("src/main.rs:1"));
let partial = store
.get_chunks_by_ids(&["src/main.rs:0".to_string(), "does/not/exist:9".to_string()])
.unwrap();
assert_eq!(partial.len(), 1);
}
#[test]
fn get_chunk_by_id() {
let store = MetadataStore::open_in_memory().unwrap();
store.insert_chunks(&sample_chunks()).unwrap();
let chunk = store.get_chunk("src/main.rs:0").unwrap().unwrap();
assert_eq!(chunk.file_path, "src/main.rs");
assert_eq!(chunk.symbol_name, Some("main".to_string()));
assert_eq!(chunk.language, Language::Rust);
assert_eq!(chunk.symbol_type, Some(SymbolType::Function));
assert_eq!(chunk.line_start, 1);
assert_eq!(chunk.line_end, 5);
}
#[test]
fn get_nonexistent_chunk() {
let store = MetadataStore::open_in_memory().unwrap();
assert!(store.get_chunk("nonexistent").unwrap().is_none());
}
#[test]
fn get_chunks_by_file() {
let store = MetadataStore::open_in_memory().unwrap();
store.insert_chunks(&sample_chunks()).unwrap();
let chunks = store.get_chunks_by_file("src/main.rs").unwrap();
assert_eq!(chunks.len(), 2);
assert_eq!(chunks[0].id, "src/main.rs:0");
assert_eq!(chunks[1].id, "src/main.rs:1");
}
#[test]
fn get_chunks_by_symbol_name() {
let store = MetadataStore::open_in_memory().unwrap();
store.insert_chunks(&sample_chunks()).unwrap();
let chunks = store.get_chunks_by_symbol_name("config").unwrap();
assert_eq!(chunks.len(), 1);
assert_eq!(chunks[0].id, "src/main.rs:1");
}
#[test]
fn get_chunks_by_symbol_name_case_sensitive() {
let store = MetadataStore::open_in_memory().unwrap();
store.insert_chunks(&sample_chunks()).unwrap();
let chunks = store
.get_chunks_by_symbol_name_case_sensitive("Config")
.unwrap();
assert_eq!(chunks.len(), 1);
assert_eq!(chunks[0].id, "src/main.rs:1");
let lower = store
.get_chunks_by_symbol_name_case_sensitive("config")
.unwrap();
assert!(lower.is_empty());
}
#[test]
fn delete_chunks_by_file() {
let store = MetadataStore::open_in_memory().unwrap();
store.insert_chunks(&sample_chunks()).unwrap();
assert_eq!(store.chunk_count().unwrap(), 3);
let deleted = store.delete_chunks_by_file("src/main.rs").unwrap();
assert_eq!(deleted, 2);
assert_eq!(store.chunk_count().unwrap(), 1);
}
#[test]
fn clear_store() {
let store = MetadataStore::open_in_memory().unwrap();
store.insert_chunks(&sample_chunks()).unwrap();
store.set_file_hash("src/main.rs", "abc123").unwrap();
store.clear().unwrap();
assert_eq!(store.chunk_count().unwrap(), 0);
assert!(store.get_file_hash("src/main.rs").unwrap().is_none());
}
#[test]
fn file_hash_operations() {
let store = MetadataStore::open_in_memory().unwrap();
assert!(store.get_file_hash("src/main.rs").unwrap().is_none());
store.set_file_hash("src/main.rs", "hash1").unwrap();
assert_eq!(
store.get_file_hash("src/main.rs").unwrap().unwrap(),
"hash1"
);
store.set_file_hash("src/main.rs", "hash2").unwrap();
assert_eq!(
store.get_file_hash("src/main.rs").unwrap().unwrap(),
"hash2"
);
store.delete_file_hash("src/main.rs").unwrap();
assert!(store.get_file_hash("src/main.rs").unwrap().is_none());
}
#[test]
fn indexed_files() {
let store = MetadataStore::open_in_memory().unwrap();
store.insert_chunks(&sample_chunks()).unwrap();
let files = store.indexed_files().unwrap();
assert_eq!(files, vec!["src/lib.py", "src/main.rs"]);
}
#[test]
fn language_stats() {
let store = MetadataStore::open_in_memory().unwrap();
store.insert_chunks(&sample_chunks()).unwrap();
let stats = store.language_stats().unwrap();
assert_eq!(stats.len(), 2);
assert_eq!(stats[0], ("rust".to_string(), 2));
assert_eq!(stats[1], ("python".to_string(), 1));
}
#[test]
fn type_relation_operations() {
let store = MetadataStore::open_in_memory().unwrap();
let relation = RawTypeRelation {
owner: "Repo".to_string(),
target: "Loader".to_string(),
line: 2,
kind: TypeRelationKind::Conforms,
};
store
.insert_parse_artifacts_batch_borrowed(
&[],
&[("src/types.ts", std::slice::from_ref(&relation))],
)
.unwrap();
let relations = store.find_type_relations("loader").unwrap();
assert_eq!(relations.len(), 1);
assert_eq!(relations[0].owner, "Repo");
assert_eq!(relations[0].target, "Loader");
assert_eq!(relations[0].kind, TypeRelationKind::Conforms);
store.delete_type_relations_by_file("src/types.ts").unwrap();
assert!(store.find_type_relations("Loader").unwrap().is_empty());
}
#[test]
fn insert_replaces_existing() {
let store = MetadataStore::open_in_memory().unwrap();
let mut chunks = sample_chunks();
store.insert_chunks(&chunks).unwrap();
chunks[0].content = "fn main() { /* updated */ }".to_string();
store.insert_chunks(&chunks[..1]).unwrap();
let chunk = store.get_chunk("src/main.rs:0").unwrap().unwrap();
assert!(chunk.content.contains("updated"));
assert_eq!(store.chunk_count().unwrap(), 3);
}
#[test]
fn parse_language_roundtrip() {
let all_langs = vec![
Language::Rust,
Language::TypeScript,
Language::JavaScript,
Language::Python,
Language::Go,
Language::Java,
Language::C,
Language::Cpp,
Language::Ruby,
Language::Swift,
Language::Kotlin,
Language::Scala,
Language::Zig,
Language::Lua,
Language::Bash,
Language::CSharp,
Language::Php,
Language::Haskell,
Language::Elixir,
Language::Dart,
Language::Sql,
Language::Hcl,
Language::Protobuf,
Language::Html,
Language::Css,
Language::Scss,
Language::Vue,
Language::GraphQl,
Language::CMake,
Language::Dockerfile,
Language::Xml,
Language::ObjectiveC,
Language::Perl,
Language::Julia,
Language::Nix,
Language::OCaml,
Language::Groovy,
Language::Clojure,
Language::CommonLisp,
Language::Erlang,
Language::FSharp,
Language::Fortran,
Language::PowerShell,
Language::R,
Language::Matlab,
Language::DLang,
Language::Fish,
Language::Zsh,
Language::Luau,
Language::Scheme,
Language::Racket,
Language::Elm,
Language::Glsl,
Language::Hlsl,
Language::Svelte,
Language::Astro,
Language::Makefile,
Language::Ini,
Language::Nginx,
Language::Prisma,
Language::Rst,
Language::Toml,
Language::Yaml,
Language::Json,
Language::Markdown,
Language::Unknown,
];
for lang in &all_langs {
let s = lang.to_string();
assert_eq!(parse_language(&s), *lang, "Failed roundtrip for {s}");
let json = serde_json::to_string(lang).unwrap();
assert_eq!(
json,
format!("\"{s}\""),
"serde name for {lang:?} diverges from Display"
);
assert_eq!(
serde_json::from_str::<Language>(&json).unwrap(),
*lang,
"Failed serde roundtrip for {s}"
);
}
}
#[test]
fn symbol_type_serde_name_matches_display() {
let all_types = vec![
SymbolType::Function,
SymbolType::Method,
SymbolType::Class,
SymbolType::Struct,
SymbolType::Enum,
SymbolType::Trait,
SymbolType::Interface,
SymbolType::TypeAlias,
SymbolType::Constant,
SymbolType::Variable,
SymbolType::Module,
SymbolType::Block,
];
for symbol_type in &all_types {
let s = symbol_type.to_string();
assert_eq!(
serde_json::to_string(symbol_type).unwrap(),
format!("\"{s}\""),
"serde name for {symbol_type:?} diverges from Display"
);
assert_eq!(
parse_symbol_type(&s),
*symbol_type,
"Failed roundtrip for {s}"
);
}
}
#[test]
fn parse_language_unknown_input() {
assert_eq!(parse_language("nonexistent"), Language::Unknown);
assert_eq!(parse_language(""), Language::Unknown);
}
#[test]
fn open_existing_errors_on_missing_db_without_creating_it() {
let dir = tempfile::tempdir().unwrap();
let db_path = dir.path().join("metadata.db");
let error = match MetadataStore::open_existing(&db_path) {
Ok(_) => panic!("open_existing must fail on a missing database"),
Err(error) => error,
};
assert!(
error.to_string().contains("no index metadata found"),
"error was: {error:#}"
);
assert!(
!db_path.exists(),
"a failed read-side open must not create the database"
);
assert!(MetadataStore::open(&db_path).is_ok());
assert!(MetadataStore::open_existing(&db_path).is_ok());
}
#[test]
fn open_existing_rejects_partial_schemas_instead_of_serving_empty_results() {
let dir = tempfile::tempdir().unwrap();
let db_path = dir.path().join("metadata.db");
drop(MetadataStore::open(&db_path).unwrap());
Connection::open(&db_path)
.unwrap()
.execute_batch("DROP TABLE chunks;")
.unwrap();
let error = match MetadataStore::open_existing(&db_path) {
Ok(_) => panic!("open_existing must reject a partial schema"),
Err(error) => error,
};
assert!(
error.to_string().contains("`chunks`"),
"error must name the missing table, was: {error:#}"
);
}
#[test]
fn file_summaries_and_symbol_counts_aggregate_in_sql_without_content() {
let store = MetadataStore::open_in_memory().unwrap();
store.insert_chunks(&sample_chunks()).unwrap();
let files = vec!["src/main.rs".to_string(), "src/lib.py".to_string()];
let summaries = store.file_chunk_summaries(&files).unwrap();
let main_rs = &summaries["src/main.rs"];
assert_eq!(main_rs.chunk_count, 2);
assert_eq!(main_rs.max_line_end, 12);
assert_eq!(main_rs.language, "rust");
assert_eq!(summaries["src/lib.py"].chunk_count, 1);
assert_eq!(summaries.len(), 2);
let mut symbol_types = store.symbol_type_counts(&files).unwrap();
symbol_types.sort();
assert_eq!(
symbol_types,
vec![("function".to_string(), 2), ("struct".to_string(), 1),]
);
assert!(store.file_chunk_summaries(&[]).unwrap().is_empty());
let missing = vec!["gone.rs".to_string()];
assert!(store.file_chunk_summaries(&missing).unwrap().is_empty());
}
#[test]
fn file_summaries_cover_batches_beyond_the_parameter_limit() {
let store = MetadataStore::open_in_memory().unwrap();
let total = SQL_PARAMETER_BATCH + 50;
let chunks: Vec<Chunk> = (0..total)
.map(|index| Chunk {
id: format!("f{index}:0"),
file_path: format!("f{index}.rs"),
line_start: 1,
line_end: 3,
content: String::new(),
language: Language::Rust,
symbol_type: None,
symbol_name: None,
})
.collect();
store.insert_chunks(&chunks).unwrap();
let files: Vec<String> = (0..total).map(|index| format!("f{index}.rs")).collect();
let summaries = store.file_chunk_summaries(&files).unwrap();
assert_eq!(summaries.len(), total);
assert!(summaries.values().all(|summary| summary.chunk_count == 1));
assert!(store.symbol_type_counts(&files).unwrap().is_empty());
}
#[test]
fn entry_points_come_from_distinct_files_without_a_full_chunk_scan() {
let store = MetadataStore::open_in_memory().unwrap();
let chunk = |id: &str, path: &str| Chunk {
id: id.to_string(),
file_path: path.to_string(),
line_start: 1,
line_end: 2,
content: String::new(),
language: Language::Rust,
symbol_type: None,
symbol_name: None,
};
let chunks = vec![
chunk("a", "src/main.rs"),
chunk("b", "src/main.rs"),
chunk("c", "server.ts"),
chunk("d", "src/domain.rs"),
];
store.insert_chunks(&chunks).unwrap();
assert_eq!(
store.entry_points().unwrap(),
vec!["server.ts".to_string(), "src/main.rs".to_string()]
);
}
#[test]
fn unknown_persisted_enum_values_fall_back_per_kind_while_valid_values_survive() {
assert_eq!(
parse_symbol_type("invented_by_a_newer_binary"),
SymbolType::Block
);
assert_eq!(
parse_language("invented_by_a_newer_binary"),
Language::Unknown
);
assert_eq!(parse_symbol_type("block"), SymbolType::Block);
assert_eq!(parse_symbol_type("type_alias"), SymbolType::TypeAlias);
assert_eq!(parse_language("rust"), Language::Rust);
}
}