pub struct StorageEngine { /* private fields */ }Expand description
Storage engine wrapper around redb
Provides ACID transactions and persistent storage for all MnemeFusion data.
Implementations§
Source§impl StorageEngine
impl StorageEngine
Sourcepub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
Open or create a database at the specified path
Sourcepub fn store_memory(&self, memory: &Memory) -> Result<()>
pub fn store_memory(&self, memory: &Memory) -> Result<()>
Store a memory record
Sourcepub fn get_memory_by_u64(&self, key: u64) -> Result<Option<Memory>>
pub fn get_memory_by_u64(&self, key: u64) -> Result<Option<Memory>>
Retrieve a memory by its u64 key (used by vector index)
Sourcepub fn delete_memory(&self, id: &MemoryId) -> Result<bool>
pub fn delete_memory(&self, id: &MemoryId) -> Result<bool>
Delete a memory by ID
Sourcepub fn list_memory_ids(&self) -> Result<Vec<MemoryId>>
pub fn list_memory_ids(&self) -> Result<Vec<MemoryId>>
Get all memory IDs (for testing/debugging)
Sourcepub fn count_memories(&self) -> Result<usize>
pub fn count_memories(&self) -> Result<usize>
Get memory count
Sourcepub fn find_memory_by_dialog_id(
&self,
dialog_id: &str,
) -> Result<Option<Memory>>
pub fn find_memory_by_dialog_id( &self, dialog_id: &str, ) -> Result<Option<Memory>>
Find a memory by its dialog_id metadata value (O(n) scan).
dialog_id is not stored in a separate index, so this iterates all memories. With ~3643 memories in current DBs, this takes ~1-5ms — acceptable for the bounded number of calls (≤18 per query for adjacent-turn bridging).
Returns the first memory whose metadata[“dialog_id”] == dialog_id.
Sourcepub fn store_vector_index(&self, buffer: &[u8]) -> Result<()>
pub fn store_vector_index(&self, buffer: &[u8]) -> Result<()>
Store vector index data
Sourcepub fn store_bm25_index(&self, buffer: &[u8]) -> Result<()>
pub fn store_bm25_index(&self, buffer: &[u8]) -> Result<()>
Store BM25 index data
Sourcepub fn store_causal_graph(&self, data: &[u8]) -> Result<()>
pub fn store_causal_graph(&self, data: &[u8]) -> Result<()>
Store causal graph data
Sourcepub fn store_entity(&self, entity: &Entity) -> Result<()>
pub fn store_entity(&self, entity: &Entity) -> Result<()>
Store an entity
Sourcepub fn find_entity_by_name(&self, name: &str) -> Result<Option<Entity>>
pub fn find_entity_by_name(&self, name: &str) -> Result<Option<Entity>>
Find an entity by name (case-insensitive)
Sourcepub fn delete_entity(&self, id: &EntityId) -> Result<bool>
pub fn delete_entity(&self, id: &EntityId) -> Result<bool>
Delete an entity
Sourcepub fn list_entities(&self) -> Result<Vec<Entity>>
pub fn list_entities(&self) -> Result<Vec<Entity>>
List all entities
Sourcepub fn count_entities(&self) -> Result<usize>
pub fn count_entities(&self) -> Result<usize>
Count entities
Sourcepub fn store_entity_graph(&self, data: &[u8]) -> Result<()>
pub fn store_entity_graph(&self, data: &[u8]) -> Result<()>
Store entity graph data
Sourcepub fn store_relationship_graph(&self, data: &[u8]) -> Result<()>
pub fn store_relationship_graph(&self, data: &[u8]) -> Result<()>
Store entity-to-entity relationship graph data
Sourcepub fn load_relationship_graph(&self) -> Result<Option<Vec<u8>>>
pub fn load_relationship_graph(&self) -> Result<Option<Vec<u8>>>
Load entity-to-entity relationship graph data
Sourcepub fn has_relationship_graph(&self) -> Result<bool>
pub fn has_relationship_graph(&self) -> Result<bool>
Check if the relationship graph key exists in storage
Sourcepub fn store_content_hash(&self, hash: &str, memory_id: &MemoryId) -> Result<()>
pub fn store_content_hash(&self, hash: &str, memory_id: &MemoryId) -> Result<()>
Store content hash → memory ID mapping
Used for deduplication to quickly find if content already exists
Sourcepub fn find_by_content_hash(&self, hash: &str) -> Result<Option<MemoryId>>
pub fn find_by_content_hash(&self, hash: &str) -> Result<Option<MemoryId>>
Find memory ID by content hash
Returns None if hash not found (content is unique)
Sourcepub fn delete_content_hash(&self, hash: &str) -> Result<()>
pub fn delete_content_hash(&self, hash: &str) -> Result<()>
Delete content hash mapping
Sourcepub fn store_logical_key(&self, key: &str, memory_id: &MemoryId) -> Result<()>
pub fn store_logical_key(&self, key: &str, memory_id: &MemoryId) -> Result<()>
Store logical key → memory ID mapping
Used for upsert operations with developer-defined keys
Sourcepub fn find_by_logical_key(&self, key: &str) -> Result<Option<MemoryId>>
pub fn find_by_logical_key(&self, key: &str) -> Result<Option<MemoryId>>
Find memory ID by logical key
Returns None if key not found
Sourcepub fn delete_logical_key(&self, key: &str) -> Result<()>
pub fn delete_logical_key(&self, key: &str) -> Result<()>
Delete logical key mapping
Sourcepub fn update_logical_key(
&self,
key: &str,
new_memory_id: &MemoryId,
) -> Result<()>
pub fn update_logical_key( &self, key: &str, new_memory_id: &MemoryId, ) -> Result<()>
Update logical key mapping to point to a new memory ID
This is used when an upsert operation replaces an existing memory
Sourcepub fn list_namespaces(&self) -> Result<Vec<String>>
pub fn list_namespaces(&self) -> Result<Vec<String>>
List all namespaces in the database
Scans all memories and extracts unique namespace values. Returns sorted list of namespace strings (excluding default namespace “”).
§Performance
O(n) where n = total memories. Can be cached if needed.
Sourcepub fn count_namespace(&self, namespace: &str) -> Result<usize>
pub fn count_namespace(&self, namespace: &str) -> Result<usize>
Sourcepub fn add_to_metadata_index(
&self,
field: &str,
value: &str,
namespace: &str,
memory_id: &MemoryId,
) -> Result<()>
pub fn add_to_metadata_index( &self, field: &str, value: &str, namespace: &str, memory_id: &MemoryId, ) -> Result<()>
Store a metadata field value in the index
Associates a memory with a specific metadata field value in a namespace.
§Arguments
field- The metadata field namevalue- The field valuenamespace- The namespace the memory belongs tomemory_id- The memory ID to associate with this field value
Sourcepub fn remove_from_metadata_index(
&self,
field: &str,
value: &str,
namespace: &str,
memory_id: &MemoryId,
) -> Result<()>
pub fn remove_from_metadata_index( &self, field: &str, value: &str, namespace: &str, memory_id: &MemoryId, ) -> Result<()>
Remove a memory from a metadata index entry
§Arguments
field- The metadata field namevalue- The field valuenamespace- The namespacememory_id- The memory ID to remove
Sourcepub fn find_by_metadata(
&self,
field: &str,
value: &str,
namespace: &str,
) -> Result<Vec<MemoryId>>
pub fn find_by_metadata( &self, field: &str, value: &str, namespace: &str, ) -> Result<Vec<MemoryId>>
Sourcepub fn remove_metadata_indexes_for_memory(
&self,
memory: &Memory,
indexed_fields: &[String],
) -> Result<()>
pub fn remove_metadata_indexes_for_memory( &self, memory: &Memory, indexed_fields: &[String], ) -> Result<()>
Remove all metadata index entries for a memory
Used when deleting a memory to clean up all its metadata indexes.
§Arguments
memory- The memory being deletedindexed_fields- List of fields that are indexed
Sourcepub fn store_entity_profile(&self, profile: &EntityProfile) -> Result<()>
pub fn store_entity_profile(&self, profile: &EntityProfile) -> Result<()>
Sourcepub fn get_entity_profile(&self, name: &str) -> Result<Option<EntityProfile>>
pub fn get_entity_profile(&self, name: &str) -> Result<Option<EntityProfile>>
Sourcepub fn list_entity_profiles(&self) -> Result<Vec<EntityProfile>>
pub fn list_entity_profiles(&self) -> Result<Vec<EntityProfile>>
Sourcepub fn delete_entity_profile(&self, name: &str) -> Result<bool>
pub fn delete_entity_profile(&self, name: &str) -> Result<bool>
Sourcepub fn count_entity_profiles(&self) -> Result<usize>
pub fn count_entity_profiles(&self) -> Result<usize>
Sourcepub fn list_entity_profile_names(&self) -> Result<Vec<String>>
pub fn list_entity_profile_names(&self) -> Result<Vec<String>>
List all entity profile names (lightweight, no full deserialization)
Returns the table keys directly, which are the lowercased entity names.
Much faster than list_entity_profiles() when you only need names.