pub struct Database { /* private fields */ }Expand description
Main database handle
Implementations§
Source§impl Database
impl Database
Sourcepub fn add_collection(
&self,
name: &str,
path: &str,
pattern: &str,
provider_type: &str,
provider_config: Option<&str>,
) -> Result<()>
pub fn add_collection( &self, name: &str, path: &str, pattern: &str, provider_type: &str, provider_config: Option<&str>, ) -> Result<()>
Add a new collection
Sourcepub fn remove_collection(&self, name: &str) -> Result<bool>
pub fn remove_collection(&self, name: &str) -> Result<bool>
Remove a collection and its documents
Sourcepub fn rename_collection(&self, old_name: &str, new_name: &str) -> Result<bool>
pub fn rename_collection(&self, old_name: &str, new_name: &str) -> Result<bool>
Rename a collection
Sourcepub fn list_collections(&self) -> Result<Vec<CollectionInfo>>
pub fn list_collections(&self) -> Result<Vec<CollectionInfo>>
List all collections with document counts
Sourcepub fn get_collection(&self, name: &str) -> Result<Option<CollectionInfo>>
pub fn get_collection(&self, name: &str) -> Result<Option<CollectionInfo>>
Get collection by name
Sourcepub fn touch_collection(&self, name: &str) -> Result<()>
pub fn touch_collection(&self, name: &str) -> Result<()>
Update collection’s updated_at timestamp
Sourcepub async fn reindex_collection(&self, name: &str) -> Result<usize>
pub async fn reindex_collection(&self, name: &str) -> Result<usize>
Reindex a collection using the provider system
Sourcepub async fn generate_or_fetch_metadata(
&self,
content_hash: &str,
content: &str,
context: MetadataContext,
generator: Option<&dyn MetadataGenerator>,
) -> Result<Option<DocumentMetadata>>
pub async fn generate_or_fetch_metadata( &self, content_hash: &str, content: &str, context: MetadataContext, generator: Option<&dyn MetadataGenerator>, ) -> Result<Option<DocumentMetadata>>
Generate or fetch metadata from cache
Sourcepub fn get_llm_cache_public(&self, key: &str) -> Result<Option<String>>
pub fn get_llm_cache_public(&self, key: &str) -> Result<Option<String>>
Get metadata from LLM cache (public API)
Sourcepub async fn reindex_collection_with_metadata(
&self,
name: &str,
generator: Option<&dyn MetadataGenerator>,
) -> Result<usize>
pub async fn reindex_collection_with_metadata( &self, name: &str, generator: Option<&dyn MetadataGenerator>, ) -> Result<usize>
Reindex all documents in a collection with optional metadata generation
Source§impl Database
impl Database
Sourcepub fn insert_content(&self, hash: &str, content: &str) -> Result<bool>
pub fn insert_content(&self, hash: &str, content: &str) -> Result<bool>
Insert content if not exists (content-addressable)
Sourcepub fn cleanup_orphaned_content(&self) -> Result<usize>
pub fn cleanup_orphaned_content(&self) -> Result<usize>
Delete orphaned content (not referenced by any active document)
Source§impl Database
impl Database
Sourcepub fn list_contexts(&self) -> Result<Vec<ContextInfo>>
pub fn list_contexts(&self) -> Result<Vec<ContextInfo>>
List all contexts
Sourcepub fn check_missing_contexts(&self) -> Result<Vec<String>>
pub fn check_missing_contexts(&self) -> Result<Vec<String>>
Check for collections missing context
Sourcepub fn remove_context(&self, path: &str) -> Result<bool>
pub fn remove_context(&self, path: &str) -> Result<bool>
Remove context for a path
Source§impl Database
impl Database
Sourcepub fn insert_doc(&self, doc: &DocumentInsert<'_>) -> Result<i64>
pub fn insert_doc(&self, doc: &DocumentInsert<'_>) -> Result<i64>
Insert new document using struct parameters
Sourcepub fn insert_document(
&self,
collection: &str,
path: &str,
title: &str,
hash: &str,
created_at: &str,
modified_at: &str,
source_type: &str,
source_uri: Option<&str>,
) -> Result<i64>
pub fn insert_document( &self, collection: &str, path: &str, title: &str, hash: &str, created_at: &str, modified_at: &str, source_type: &str, source_uri: Option<&str>, ) -> Result<i64>
Insert new document (legacy method)
Sourcepub fn update_document(
&self,
id: i64,
title: &str,
hash: &str,
modified_at: &str,
) -> Result<()>
pub fn update_document( &self, id: i64, title: &str, hash: &str, modified_at: &str, ) -> Result<()>
Update existing document (new content hash)
Sourcepub fn update_document_title(
&self,
id: i64,
title: &str,
modified_at: &str,
) -> Result<()>
pub fn update_document_title( &self, id: i64, title: &str, modified_at: &str, ) -> Result<()>
Update document title only
Sourcepub fn deactivate_document(&self, collection: &str, path: &str) -> Result<bool>
pub fn deactivate_document(&self, collection: &str, path: &str) -> Result<bool>
Soft-delete document (set active = 0)
Sourcepub fn find_active_document(
&self,
collection: &str,
path: &str,
) -> Result<Option<Document>>
pub fn find_active_document( &self, collection: &str, path: &str, ) -> Result<Option<Document>>
Find active document by collection and path
Sourcepub fn get_active_document_paths(&self, collection: &str) -> Result<Vec<String>>
pub fn get_active_document_paths(&self, collection: &str) -> Result<Vec<String>>
Get all active document paths in collection
Sourcepub fn find_by_docid(&self, docid: &str) -> Result<Option<DocumentResult>>
pub fn find_by_docid(&self, docid: &str) -> Result<Option<DocumentResult>>
Find document by docid (hash prefix)
Sourcepub fn delete_inactive_documents(&self) -> Result<usize>
pub fn delete_inactive_documents(&self) -> Result<usize>
Hard delete inactive documents
Sourcepub fn lookup_document(
&self,
query: &str,
collections: &HashMap<String, PathBuf>,
) -> Result<Option<DocumentResult>>
pub fn lookup_document( &self, query: &str, collections: &HashMap<String, PathBuf>, ) -> Result<Option<DocumentResult>>
Multi-lookup with fallback chain
Sourcepub fn fuzzy_find_documents(
&self,
query: &str,
limit: usize,
) -> Result<Vec<DocumentResult>>
pub fn fuzzy_find_documents( &self, query: &str, limit: usize, ) -> Result<Vec<DocumentResult>>
Fuzzy matching using simple contains + length
Sourcepub fn get_document(&self, query: &str) -> Result<String>
pub fn get_document(&self, query: &str) -> Result<String>
Get document content by query (docid, virtual path, etc)
Sourcepub fn list_documents_by_prefix(
&self,
prefix: &str,
) -> Result<Vec<DocumentListItem>>
pub fn list_documents_by_prefix( &self, prefix: &str, ) -> Result<Vec<DocumentListItem>>
List documents by prefix
Sourcepub fn get_documents_by_pattern(
&self,
pattern: &str,
) -> Result<Vec<DocumentContent>>
pub fn get_documents_by_pattern( &self, pattern: &str, ) -> Result<Vec<DocumentContent>>
Get multiple documents by pattern
Source§impl Database
impl Database
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Self>
pub fn open(path: impl AsRef<Path>) -> Result<Self>
Open database at path, creating if necessary
Sourcepub fn open_in_memory() -> Result<Self>
pub fn open_in_memory() -> Result<Self>
Open in-memory database (for testing)
Sourcepub fn initialize(&self) -> Result<()>
pub fn initialize(&self) -> Result<()>
Initialize database schema
Sourcepub fn schema_version(&self) -> Result<Option<i32>>
pub fn schema_version(&self) -> Result<Option<i32>>
Get current schema version
Source§impl Database
impl Database
Sourcepub fn add_metadata(&self, docid: &str, metadata: &UserMetadata) -> Result<()>
pub fn add_metadata(&self, docid: &str, metadata: &UserMetadata) -> Result<()>
Add or update user metadata for a document
§Arguments
docid- Document ID (short hash like “abc123” or full “#abc123”)metadata- User metadata to add/update
§Example
use agentroot_core::{Database, MetadataBuilder};
let db = Database::open("index.sqlite")?;
let metadata = MetadataBuilder::new()
.text("author", "John Doe")
.tags("labels", vec!["rust", "tutorial"])
.build();
db.add_metadata("#abc123", &metadata)?;Sourcepub fn get_metadata(&self, docid: &str) -> Result<Option<UserMetadata>>
pub fn get_metadata(&self, docid: &str) -> Result<Option<UserMetadata>>
Get user metadata for a document
Sourcepub fn remove_metadata_fields(
&self,
docid: &str,
fields: &[String],
) -> Result<()>
pub fn remove_metadata_fields( &self, docid: &str, fields: &[String], ) -> Result<()>
Remove specific metadata fields from a document
Sourcepub fn clear_metadata(&self, docid: &str) -> Result<()>
pub fn clear_metadata(&self, docid: &str) -> Result<()>
Clear all user metadata from a document
Sourcepub fn find_by_metadata(
&self,
filter: &MetadataFilter,
limit: usize,
) -> Result<Vec<String>>
pub fn find_by_metadata( &self, filter: &MetadataFilter, limit: usize, ) -> Result<Vec<String>>
Find documents matching metadata filter
Sourcepub fn list_with_metadata(
&self,
limit: usize,
) -> Result<Vec<(String, UserMetadata)>>
pub fn list_with_metadata( &self, limit: usize, ) -> Result<Vec<(String, UserMetadata)>>
List all documents with user metadata
Source§impl Database
impl Database
Sourcepub fn ensure_vec_table(&self, _dimensions: usize) -> Result<()>
pub fn ensure_vec_table(&self, _dimensions: usize) -> Result<()>
Ensure vector storage table exists
Sourcepub fn insert_embedding(
&self,
hash: &str,
seq: u32,
pos: usize,
model: &str,
embedding: &[f32],
) -> Result<()>
pub fn insert_embedding( &self, hash: &str, seq: u32, pos: usize, model: &str, embedding: &[f32], ) -> Result<()>
Insert embedding for a document chunk
Sourcepub fn has_vector_index(&self) -> bool
pub fn has_vector_index(&self) -> bool
Check if vector index exists and has data
Sourcepub fn get_all_embeddings(&self) -> Result<Vec<(String, Vec<f32>)>>
pub fn get_all_embeddings(&self) -> Result<Vec<(String, Vec<f32>)>>
Get all embeddings for similarity search
Sourcepub fn get_embeddings_for_collection(
&self,
collection: &str,
) -> Result<Vec<(String, Vec<f32>)>>
pub fn get_embeddings_for_collection( &self, collection: &str, ) -> Result<Vec<(String, Vec<f32>)>>
Get embeddings for specific hashes (for filtered search)
Sourcepub fn get_hashes_needing_embedding(&self) -> Result<Vec<(String, String)>>
pub fn get_hashes_needing_embedding(&self) -> Result<Vec<(String, String)>>
Get hashes that need embedding
Sourcepub fn count_hashes_needing_embedding(&self) -> Result<usize>
pub fn count_hashes_needing_embedding(&self) -> Result<usize>
Count hashes needing embedding
Sourcepub fn delete_embeddings(&self, hash: &str) -> Result<usize>
pub fn delete_embeddings(&self, hash: &str) -> Result<usize>
Delete embeddings for a hash
Sourcepub fn get_all_hashes_for_embedding(&self) -> Result<Vec<(String, String)>>
pub fn get_all_hashes_for_embedding(&self) -> Result<Vec<(String, String)>>
Get all hashes for embedding (for force re-embedding)
Sourcepub fn check_model_compatibility(
&self,
model: &str,
expected_dims: usize,
) -> Result<bool>
pub fn check_model_compatibility( &self, model: &str, expected_dims: usize, ) -> Result<bool>
Check if model dimensions are compatible with expected dimensions
Sourcepub fn get_cached_embedding(
&self,
chunk_hash: &str,
model: &str,
expected_dims: usize,
) -> Result<CacheLookupResult>
pub fn get_cached_embedding( &self, chunk_hash: &str, model: &str, expected_dims: usize, ) -> Result<CacheLookupResult>
Look up a cached embedding by chunk hash (performs dimension check)
Sourcepub fn get_cached_embedding_fast(
&self,
chunk_hash: &str,
model: &str,
) -> Result<CacheLookupResult>
pub fn get_cached_embedding_fast( &self, chunk_hash: &str, model: &str, ) -> Result<CacheLookupResult>
Look up a cached embedding by chunk hash (skips dimension check - caller must verify compatibility)
Sourcepub fn insert_chunk_embedding(
&self,
doc_hash: &str,
seq: u32,
pos: usize,
chunk_hash: &str,
model: &str,
embedding: &[f32],
) -> Result<()>
pub fn insert_chunk_embedding( &self, doc_hash: &str, seq: u32, pos: usize, chunk_hash: &str, model: &str, embedding: &[f32], ) -> Result<()>
Insert a chunk embedding with cache support
Sourcepub fn get_chunk_hashes_for_doc(
&self,
doc_hash: &str,
) -> Result<Vec<(u32, String)>>
pub fn get_chunk_hashes_for_doc( &self, doc_hash: &str, ) -> Result<Vec<(u32, String)>>
Get chunk hashes for a document
Sourcepub fn cleanup_orphaned_chunk_embeddings(&self) -> Result<usize>
pub fn cleanup_orphaned_chunk_embeddings(&self) -> Result<usize>
Clean up orphaned chunk embeddings (not referenced by any document)
Sourcepub fn register_model(&self, model: &str, dimensions: usize) -> Result<()>
pub fn register_model(&self, model: &str, dimensions: usize) -> Result<()>
Register model with its dimensions
Sourcepub fn get_model_dimensions(&self, model: &str) -> Result<Option<usize>>
pub fn get_model_dimensions(&self, model: &str) -> Result<Option<usize>>
Get stored model dimensions
Sourcepub fn count_cached_embeddings(&self, model: &str) -> Result<usize>
pub fn count_cached_embeddings(&self, model: &str) -> Result<usize>
Count cached chunk embeddings
Source§impl Database
impl Database
Sourcepub fn get_all_content(&self) -> Result<Vec<(String, String)>>
pub fn get_all_content(&self) -> Result<Vec<(String, String)>>
Get all content hashes and content
Sourcepub fn get_all_content_with_paths(
&self,
) -> Result<Vec<(String, String, Option<String>)>>
pub fn get_all_content_with_paths( &self, ) -> Result<Vec<(String, String, Option<String>)>>
Get all content with file paths
Source§impl Database
impl Database
Sourcepub fn search_fts(
&self,
query: &str,
options: &SearchOptions,
) -> Result<Vec<SearchResult>>
pub fn search_fts( &self, query: &str, options: &SearchOptions, ) -> Result<Vec<SearchResult>>
Perform BM25 full-text search
Source§impl Database
impl Database
Sourcepub fn search_vec_sync(
&self,
_query: &str,
options: &SearchOptions,
) -> Result<Vec<SearchResult>>
pub fn search_vec_sync( &self, _query: &str, options: &SearchOptions, ) -> Result<Vec<SearchResult>>
Synchronous vector search (placeholder for CLI - needs runtime)
Sourcepub fn search_hybrid_sync(
&self,
query: &str,
options: &SearchOptions,
) -> Result<Vec<SearchResult>>
pub fn search_hybrid_sync( &self, query: &str, options: &SearchOptions, ) -> Result<Vec<SearchResult>>
Synchronous hybrid search (placeholder for CLI - needs runtime)
Source§impl Database
impl Database
Sourcepub async fn search_vec(
&self,
query: &str,
embedder: &dyn Embedder,
options: &SearchOptions,
) -> Result<Vec<SearchResult>>
pub async fn search_vec( &self, query: &str, embedder: &dyn Embedder, options: &SearchOptions, ) -> Result<Vec<SearchResult>>
Perform vector similarity search