PersistenceBackend

Trait PersistenceBackend 

Source
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 &self to enable sharing via Arc<dyn PersistenceBackend>
  • Use interior mutability (e.g., Mutex) for mutable state
  • All methods must be thread-safe (Send + Sync bound)
  • Prefer returning Error::NotFound over None for missing IDs
  • Use structured error variants from crate::Error

Required Methods§

Source

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.

Source

fn get(&self, id: &MemoryId) -> Result<Option<Memory>>

Retrieves a memory by ID.

§Errors

Returns an error if the retrieval operation fails.

Source

fn delete(&self, id: &MemoryId) -> Result<bool>

Deletes a memory by ID.

Uses interior mutability for thread-safe concurrent access.

§Errors

Returns an error if the deletion operation fails.

Source

fn list_ids(&self) -> Result<Vec<MemoryId>>

Lists all memory IDs.

§Errors

Returns an error if the list operation fails.

Provided Methods§

Source

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.

Source

fn exists(&self, id: &MemoryId) -> Result<bool>

Checks if a memory exists.

§Errors

Returns an error if the existence check fails.

Source

fn count(&self) -> Result<usize>

Returns the total count of memories.

§Errors

Returns an error if the count operation fails.

Implementors§