pub struct MemoryDatabase { /* private fields */ }Expand description
Database connection manager
Implementations§
Source§impl MemoryDatabase
impl MemoryDatabase
Sourcepub async fn new(db_path: &Path) -> MemoryResult<Self>
pub async fn new(db_path: &Path) -> MemoryResult<Self>
Initialize or open the memory database
Sourcepub async fn validate_vector_tables(&self) -> MemoryResult<()>
pub async fn validate_vector_tables(&self) -> MemoryResult<()>
Validate that sqlite-vec tables are readable. This catches legacy/corrupted vector blobs early so startup can recover.
Sourcepub async fn ensure_vector_tables_healthy(&self) -> MemoryResult<bool>
pub async fn ensure_vector_tables_healthy(&self) -> MemoryResult<bool>
Ensure vector tables are readable and recreate them if corruption is detected. Returns true when a repair was performed.
Sourcepub async fn reset_all_memory_tables(&self) -> MemoryResult<()>
pub async fn reset_all_memory_tables(&self) -> MemoryResult<()>
Last-resort runtime repair for malformed DB states: drop user memory tables and recreate the schema in-place so new writes can proceed. This intentionally clears memory content for the active DB file.
Sourcepub async fn try_repair_after_error(
&self,
err: &MemoryError,
) -> MemoryResult<bool>
pub async fn try_repair_after_error( &self, err: &MemoryError, ) -> MemoryResult<bool>
Attempt an immediate vector-table repair when a concrete DB error indicates sqlite-vec internals are failing at statement/rowid level.
Sourcepub async fn store_chunk(
&self,
chunk: &MemoryChunk,
embedding: &[f32],
) -> MemoryResult<()>
pub async fn store_chunk( &self, chunk: &MemoryChunk, embedding: &[f32], ) -> MemoryResult<()>
Store a chunk with its embedding
Sourcepub async fn search_similar(
&self,
query_embedding: &[f32],
tier: MemoryTier,
project_id: Option<&str>,
session_id: Option<&str>,
limit: i64,
) -> MemoryResult<Vec<(MemoryChunk, f64)>>
pub async fn search_similar( &self, query_embedding: &[f32], tier: MemoryTier, project_id: Option<&str>, session_id: Option<&str>, limit: i64, ) -> MemoryResult<Vec<(MemoryChunk, f64)>>
Search for similar chunks
Sourcepub async fn get_session_chunks(
&self,
session_id: &str,
) -> MemoryResult<Vec<MemoryChunk>>
pub async fn get_session_chunks( &self, session_id: &str, ) -> MemoryResult<Vec<MemoryChunk>>
Get chunks by session ID
Sourcepub async fn get_project_chunks(
&self,
project_id: &str,
) -> MemoryResult<Vec<MemoryChunk>>
pub async fn get_project_chunks( &self, project_id: &str, ) -> MemoryResult<Vec<MemoryChunk>>
Get chunks by project ID
Sourcepub async fn get_global_chunks(
&self,
limit: i64,
) -> MemoryResult<Vec<MemoryChunk>>
pub async fn get_global_chunks( &self, limit: i64, ) -> MemoryResult<Vec<MemoryChunk>>
Get global chunks
Sourcepub async fn clear_session_memory(&self, session_id: &str) -> MemoryResult<u64>
pub async fn clear_session_memory(&self, session_id: &str) -> MemoryResult<u64>
Clear session memory
Sourcepub async fn clear_project_memory(&self, project_id: &str) -> MemoryResult<u64>
pub async fn clear_project_memory(&self, project_id: &str) -> MemoryResult<u64>
Clear project memory
Sourcepub async fn cleanup_old_sessions(
&self,
retention_days: i64,
) -> MemoryResult<u64>
pub async fn cleanup_old_sessions( &self, retention_days: i64, ) -> MemoryResult<u64>
Clear old session memory based on retention policy
Sourcepub async fn get_or_create_config(
&self,
project_id: &str,
) -> MemoryResult<MemoryConfig>
pub async fn get_or_create_config( &self, project_id: &str, ) -> MemoryResult<MemoryConfig>
Get or create memory config for a project
Sourcepub async fn update_config(
&self,
project_id: &str,
config: &MemoryConfig,
) -> MemoryResult<()>
pub async fn update_config( &self, project_id: &str, config: &MemoryConfig, ) -> MemoryResult<()>
Update memory config for a project
Sourcepub async fn get_stats(&self) -> MemoryResult<MemoryStats>
pub async fn get_stats(&self) -> MemoryResult<MemoryStats>
Get memory statistics
Sourcepub async fn log_cleanup(
&self,
cleanup_type: &str,
tier: MemoryTier,
project_id: Option<&str>,
session_id: Option<&str>,
chunks_deleted: i64,
bytes_reclaimed: i64,
) -> MemoryResult<()>
pub async fn log_cleanup( &self, cleanup_type: &str, tier: MemoryTier, project_id: Option<&str>, session_id: Option<&str>, chunks_deleted: i64, bytes_reclaimed: i64, ) -> MemoryResult<()>
Log cleanup operation
Sourcepub async fn vacuum(&self) -> MemoryResult<()>
pub async fn vacuum(&self) -> MemoryResult<()>
Vacuum the database to reclaim space
pub async fn project_file_index_count( &self, project_id: &str, ) -> MemoryResult<i64>
pub async fn project_has_file_chunks( &self, project_id: &str, ) -> MemoryResult<bool>
pub async fn get_file_index_entry( &self, project_id: &str, path: &str, ) -> MemoryResult<Option<(i64, i64, String)>>
pub async fn upsert_file_index_entry( &self, project_id: &str, path: &str, mtime: i64, size: i64, hash: &str, ) -> MemoryResult<()>
pub async fn delete_file_index_entry( &self, project_id: &str, path: &str, ) -> MemoryResult<()>
pub async fn list_file_index_paths( &self, project_id: &str, ) -> MemoryResult<Vec<String>>
pub async fn delete_project_file_chunks_by_path( &self, project_id: &str, source_path: &str, ) -> MemoryResult<(i64, i64)>
pub async fn upsert_project_index_status( &self, project_id: &str, total_files: i64, processed_files: i64, indexed_files: i64, skipped_files: i64, errors: i64, ) -> MemoryResult<()>
pub async fn get_project_stats( &self, project_id: &str, ) -> MemoryResult<ProjectMemoryStats>
pub async fn clear_project_file_index( &self, project_id: &str, vacuum: bool, ) -> MemoryResult<ClearFileIndexResult>
Sourcepub async fn prune_old_session_chunks(
&self,
retention_days: u32,
) -> MemoryResult<u64>
pub async fn prune_old_session_chunks( &self, retention_days: u32, ) -> MemoryResult<u64>
Delete session memory chunks older than retention_days days.
Also removes orphaned vector entries for the deleted chunks so the sqlite-vec virtual table stays consistent.
Returns the number of chunk rows deleted.
If retention_days is 0 hygiene is disabled and this returns Ok(0).
Sourcepub async fn run_hygiene(&self, env_override_days: u32) -> MemoryResult<u64>
pub async fn run_hygiene(&self, env_override_days: u32) -> MemoryResult<u64>
Run scheduled hygiene: read session_retention_days from memory_config
(falling back to env_override if provided) and prune stale session chunks.
Returns Ok(chunks_deleted). This method is intentionally best-effort —
callers should log errors and continue.