pub trait PersistenceBackend: Send + Sync {
// Required methods
fn store(&self, memory: &Memory) -> Result<()>;
fn get(&self, id: &MemoryId) -> Result<Option<Memory>>;
fn delete(&self, id: &MemoryId) -> Result<bool>;
fn list_ids(&self) -> Result<Vec<MemoryId>>;
// Provided methods
fn get_batch(&self, ids: &[MemoryId]) -> Result<Vec<Memory>> { ... }
fn exists(&self, id: &MemoryId) -> Result<bool> { ... }
fn count(&self) -> Result<usize> { ... }
}Expand description
Trait for persistence layer backends.
Persistence backends are the authoritative source of truth for memories. They handle long-term storage and retrieval.
§Implementor Notes
- Methods use
&selfto enable sharing viaArc<dyn PersistenceBackend> - Use interior mutability (e.g.,
Mutex) for mutable state - All methods must be thread-safe (
Send + Syncbound) - Prefer returning
Error::NotFoundoverNonefor missing IDs - Use structured error variants from
crate::Error
Required Methods§
Sourcefn store(&self, memory: &Memory) -> Result<()>
fn store(&self, memory: &Memory) -> Result<()>
Stores a memory.
Uses interior mutability for thread-safe concurrent access.
§Errors
Returns an error if the storage operation fails.
Provided Methods§
Sourcefn get_batch(&self, ids: &[MemoryId]) -> Result<Vec<Memory>>
fn get_batch(&self, ids: &[MemoryId]) -> Result<Vec<Memory>>
Retrieves multiple memories by their IDs in a single batch operation.
This method avoids N+1 queries by fetching all requested memories in a single database round-trip (where supported by the backend).
§Default Implementation
Falls back to calling get() for each ID. Backends should override
this with an optimized batch query (e.g., SELECT ... WHERE id IN (...)).
§Errors
Returns an error if any retrieval operation fails.