pub struct MemoryStore { /* private fields */ }Expand description
Thread-safe handle to the memory database.
Clone is cheap (Arc internals). Send + Sync.
Implementations§
Source§impl MemoryStore
impl MemoryStore
Sourcepub fn open(config: MemoryConfig) -> Result<Self, MemoryError>
pub fn open(config: MemoryConfig) -> Result<Self, MemoryError>
Open or create a memory store at the configured base directory.
Creates the directory if it doesn’t exist, opens/creates SQLite, runs migrations, and initializes the HNSW index.
Sourcepub fn open_with_embedder(
config: MemoryConfig,
embedder: Box<dyn Embedder>,
) -> Result<Self, MemoryError>
pub fn open_with_embedder( config: MemoryConfig, embedder: Box<dyn Embedder>, ) -> Result<Self, MemoryError>
Open with a custom embedder (for testing or non-Ollama providers).
Sourcepub async fn rebuild_hnsw_index(&self) -> Result<(), MemoryError>
pub async fn rebuild_hnsw_index(&self) -> Result<(), MemoryError>
Rebuild the HNSW index from SQLite f32 embeddings.
Call this if sidecar files are missing, corrupted, or after reembed_all().
Sourcepub fn flush_hnsw(&self) -> Result<(), MemoryError>
pub fn flush_hnsw(&self) -> Result<(), MemoryError>
Persist the HNSW graph, vector data, and key mappings to disk.
Called automatically on drop, but can be called explicitly for durability.
Sourcepub async fn compact_hnsw(&self) -> Result<(), MemoryError>
pub async fn compact_hnsw(&self) -> Result<(), MemoryError>
Compact the HNSW index by rebuilding without tombstones.
Only rebuilds if the deleted ratio exceeds the compaction threshold.
Sourcepub async fn create_session(&self, channel: &str) -> Result<String, MemoryError>
pub async fn create_session(&self, channel: &str) -> Result<String, MemoryError>
Create a new conversation session. Returns the session ID (UUID v4).
Sourcepub async fn list_sessions(
&self,
limit: usize,
offset: usize,
) -> Result<Vec<Session>, MemoryError>
pub async fn list_sessions( &self, limit: usize, offset: usize, ) -> Result<Vec<Session>, MemoryError>
List recent sessions, newest first.
Sourcepub async fn delete_session(&self, session_id: &str) -> Result<(), MemoryError>
pub async fn delete_session(&self, session_id: &str) -> Result<(), MemoryError>
Delete a session and all its messages.
Sourcepub async fn add_message(
&self,
session_id: &str,
role: Role,
content: &str,
token_count: Option<u32>,
metadata: Option<Value>,
) -> Result<i64, MemoryError>
pub async fn add_message( &self, session_id: &str, role: Role, content: &str, token_count: Option<u32>, metadata: Option<Value>, ) -> Result<i64, MemoryError>
Append a message to a session. Returns the message’s auto-increment ID.
Sourcepub async fn get_recent_messages(
&self,
session_id: &str,
limit: usize,
) -> Result<Vec<Message>, MemoryError>
pub async fn get_recent_messages( &self, session_id: &str, limit: usize, ) -> Result<Vec<Message>, MemoryError>
Get the most recent N messages from a session, in chronological order.
Sourcepub async fn get_messages_within_budget(
&self,
session_id: &str,
max_tokens: u32,
) -> Result<Vec<Message>, MemoryError>
pub async fn get_messages_within_budget( &self, session_id: &str, max_tokens: u32, ) -> Result<Vec<Message>, MemoryError>
Get messages from a session up to max_tokens total.
Sourcepub async fn session_token_count(
&self,
session_id: &str,
) -> Result<u64, MemoryError>
pub async fn session_token_count( &self, session_id: &str, ) -> Result<u64, MemoryError>
Get total token count for a session.
Sourcepub async fn add_fact(
&self,
namespace: &str,
content: &str,
source: Option<&str>,
metadata: Option<Value>,
) -> Result<String, MemoryError>
pub async fn add_fact( &self, namespace: &str, content: &str, source: Option<&str>, metadata: Option<Value>, ) -> Result<String, MemoryError>
Store a fact with automatic embedding. Returns the fact ID (UUID v4).
Sourcepub async fn add_fact_with_embedding(
&self,
namespace: &str,
content: &str,
embedding: &[f32],
source: Option<&str>,
metadata: Option<Value>,
) -> Result<String, MemoryError>
pub async fn add_fact_with_embedding( &self, namespace: &str, content: &str, embedding: &[f32], source: Option<&str>, metadata: Option<Value>, ) -> Result<String, MemoryError>
Store a fact with a pre-computed embedding.
Sourcepub async fn update_fact(
&self,
fact_id: &str,
content: &str,
) -> Result<(), MemoryError>
pub async fn update_fact( &self, fact_id: &str, content: &str, ) -> Result<(), MemoryError>
Update a fact’s content. Re-embeds automatically.
Sourcepub async fn delete_fact(&self, fact_id: &str) -> Result<(), MemoryError>
pub async fn delete_fact(&self, fact_id: &str) -> Result<(), MemoryError>
Delete a fact by ID.
Sourcepub async fn delete_namespace(
&self,
namespace: &str,
) -> Result<usize, MemoryError>
pub async fn delete_namespace( &self, namespace: &str, ) -> Result<usize, MemoryError>
Delete all facts in a namespace. Returns the count of deleted facts.
Sourcepub async fn get_fact(&self, fact_id: &str) -> Result<Option<Fact>, MemoryError>
pub async fn get_fact(&self, fact_id: &str) -> Result<Option<Fact>, MemoryError>
Get a fact by ID.
Sourcepub async fn get_fact_embedding(
&self,
fact_id: &str,
) -> Result<Option<Vec<f32>>, MemoryError>
pub async fn get_fact_embedding( &self, fact_id: &str, ) -> Result<Option<Vec<f32>>, MemoryError>
Get a fact’s embedding vector.
Sourcepub async fn list_facts(
&self,
namespace: &str,
limit: usize,
offset: usize,
) -> Result<Vec<Fact>, MemoryError>
pub async fn list_facts( &self, namespace: &str, limit: usize, offset: usize, ) -> Result<Vec<Fact>, MemoryError>
List all facts in a namespace.
Sourcepub async fn ingest_document(
&self,
title: &str,
content: &str,
namespace: &str,
source_path: Option<&str>,
metadata: Option<Value>,
) -> Result<String, MemoryError>
pub async fn ingest_document( &self, title: &str, content: &str, namespace: &str, source_path: Option<&str>, metadata: Option<Value>, ) -> Result<String, MemoryError>
Ingest a document: chunk, embed all chunks, store everything.
Sourcepub async fn delete_document(
&self,
document_id: &str,
) -> Result<(), MemoryError>
pub async fn delete_document( &self, document_id: &str, ) -> Result<(), MemoryError>
Delete a document and all its chunks.
Sourcepub async fn list_documents(
&self,
namespace: &str,
limit: usize,
offset: usize,
) -> Result<Vec<Document>, MemoryError>
pub async fn list_documents( &self, namespace: &str, limit: usize, offset: usize, ) -> Result<Vec<Document>, MemoryError>
List documents in a namespace.
Sourcepub async fn search(
&self,
query: &str,
top_k: Option<usize>,
namespaces: Option<&[&str]>,
source_types: Option<&[SearchSourceType]>,
) -> Result<Vec<SearchResult>, MemoryError>
pub async fn search( &self, query: &str, top_k: Option<usize>, namespaces: Option<&[&str]>, source_types: Option<&[SearchSourceType]>, ) -> Result<Vec<SearchResult>, MemoryError>
Hybrid search across facts and document chunks.
Sourcepub async fn search_fts_only(
&self,
query: &str,
top_k: Option<usize>,
namespaces: Option<&[&str]>,
source_types: Option<&[SearchSourceType]>,
) -> Result<Vec<SearchResult>, MemoryError>
pub async fn search_fts_only( &self, query: &str, top_k: Option<usize>, namespaces: Option<&[&str]>, source_types: Option<&[SearchSourceType]>, ) -> Result<Vec<SearchResult>, MemoryError>
Full-text search only (no embeddings needed).
Sourcepub async fn search_vector_only(
&self,
query: &str,
top_k: Option<usize>,
namespaces: Option<&[&str]>,
source_types: Option<&[SearchSourceType]>,
) -> Result<Vec<SearchResult>, MemoryError>
pub async fn search_vector_only( &self, query: &str, top_k: Option<usize>, namespaces: Option<&[&str]>, source_types: Option<&[SearchSourceType]>, ) -> Result<Vec<SearchResult>, MemoryError>
Vector similarity search only (no FTS).
Sourcepub async fn add_message_embedded(
&self,
session_id: &str,
role: Role,
content: &str,
token_count: Option<u32>,
metadata: Option<Value>,
) -> Result<i64, MemoryError>
pub async fn add_message_embedded( &self, session_id: &str, role: Role, content: &str, token_count: Option<u32>, metadata: Option<Value>, ) -> Result<i64, MemoryError>
Append a message to a session with automatic embedding.
Sourcepub async fn search_conversations(
&self,
query: &str,
top_k: Option<usize>,
session_ids: Option<&[&str]>,
) -> Result<Vec<SearchResult>, MemoryError>
pub async fn search_conversations( &self, query: &str, top_k: Option<usize>, session_ids: Option<&[&str]>, ) -> Result<Vec<SearchResult>, MemoryError>
Hybrid search over conversation messages only.
Sourcepub fn chunk_text(&self, text: &str) -> Vec<TextChunk>
pub fn chunk_text(&self, text: &str) -> Vec<TextChunk>
Chunk text using the configured strategy and token counter.
Sourcepub async fn embed(&self, text: &str) -> Result<Vec<f32>, MemoryError>
pub async fn embed(&self, text: &str) -> Result<Vec<f32>, MemoryError>
Embed a single text via the configured provider.
Sourcepub async fn embed_batch(
&self,
texts: &[&str],
) -> Result<Vec<Vec<f32>>, MemoryError>
pub async fn embed_batch( &self, texts: &[&str], ) -> Result<Vec<Vec<f32>>, MemoryError>
Embed multiple texts in a batch.
Sourcepub async fn stats(&self) -> Result<MemoryStats, MemoryError>
pub async fn stats(&self) -> Result<MemoryStats, MemoryError>
Get database statistics.
Sourcepub async fn embeddings_are_dirty(&self) -> Result<bool, MemoryError>
pub async fn embeddings_are_dirty(&self) -> Result<bool, MemoryError>
Check if embeddings need re-generation after a model change.
Sourcepub async fn reembed_all(&self) -> Result<usize, MemoryError>
pub async fn reembed_all(&self) -> Result<usize, MemoryError>
Re-embed all facts, chunks, and messages. Call after changing embedding models.
Sourcepub async fn vacuum(&self) -> Result<(), MemoryError>
pub async fn vacuum(&self) -> Result<(), MemoryError>
Vacuum the database (reclaim space after deletions).
Trait Implementations§
Source§impl Clone for MemoryStore
impl Clone for MemoryStore
Source§fn clone(&self) -> MemoryStore
fn clone(&self) -> MemoryStore
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for MemoryStore
impl !RefUnwindSafe for MemoryStore
impl Send for MemoryStore
impl Sync for MemoryStore
impl Unpin for MemoryStore
impl UnsafeUnpin for MemoryStore
impl !UnwindSafe for MemoryStore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more