Skip to main content

StorageEngine

Struct StorageEngine 

Source
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

Source

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>

Open or create a database at the specified path

Source

pub fn store_memory(&self, memory: &Memory) -> Result<()>

Store a memory record

Source

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

Retrieve a memory by ID

Source

pub fn get_memory_by_u64(&self, key: u64) -> Result<Option<Memory>>

Retrieve a memory by its u64 key (used by vector index)

Source

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

Delete a memory by ID

Source

pub fn list_memory_ids(&self) -> Result<Vec<MemoryId>>

Get all memory IDs (for testing/debugging)

Source

pub fn count_memories(&self) -> Result<usize>

Get memory count

Source

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.

Source

pub fn path(&self) -> &Path

Get the database path

Source

pub fn store_vector_index(&self, buffer: &[u8]) -> Result<()>

Store vector index data

Source

pub fn load_vector_index(&self) -> Result<Option<Vec<u8>>>

Load vector index data

Source

pub fn store_bm25_index(&self, buffer: &[u8]) -> Result<()>

Store BM25 index data

Source

pub fn load_bm25_index(&self) -> Result<Option<Vec<u8>>>

Load BM25 index data

Source

pub fn store_causal_graph(&self, data: &[u8]) -> Result<()>

Store causal graph data

Source

pub fn load_causal_graph(&self) -> Result<Option<Vec<u8>>>

Load causal graph data

Source

pub fn store_entity(&self, entity: &Entity) -> Result<()>

Store an entity

Source

pub fn get_entity(&self, id: &EntityId) -> Result<Option<Entity>>

Get an entity by ID

Source

pub fn find_entity_by_name(&self, name: &str) -> Result<Option<Entity>>

Find an entity by name (case-insensitive)

Source

pub fn delete_entity(&self, id: &EntityId) -> Result<bool>

Delete an entity

Source

pub fn list_entities(&self) -> Result<Vec<Entity>>

List all entities

Source

pub fn count_entities(&self) -> Result<usize>

Count entities

Source

pub fn store_entity_graph(&self, data: &[u8]) -> Result<()>

Store entity graph data

Source

pub fn load_entity_graph(&self) -> Result<Option<Vec<u8>>>

Load entity graph data

Source

pub fn store_relationship_graph(&self, data: &[u8]) -> Result<()>

Store entity-to-entity relationship graph data

Source

pub fn load_relationship_graph(&self) -> Result<Option<Vec<u8>>>

Load entity-to-entity relationship graph data

Source

pub fn has_relationship_graph(&self) -> Result<bool>

Check if the relationship graph key exists in storage

Source

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

Source

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)

Source

pub fn delete_content_hash(&self, hash: &str) -> Result<()>

Delete content hash mapping

Source

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

Source

pub fn find_by_logical_key(&self, key: &str) -> Result<Option<MemoryId>>

Find memory ID by logical key

Returns None if key not found

Source

pub fn delete_logical_key(&self, key: &str) -> Result<()>

Delete logical key mapping

Source

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

Source

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.

Source

pub fn count_namespace(&self, namespace: &str) -> Result<usize>

Count memories in a specific namespace

§Arguments
  • namespace - The namespace to count (empty string “” for default)
§Returns

Number of memories in the namespace

Source

pub fn list_namespace_ids(&self, namespace: &str) -> Result<Vec<MemoryId>>

List all memory IDs in a specific namespace

§Arguments
  • namespace - The namespace to list (empty string “” for default)
§Returns

Vector of MemoryIds in the namespace

Source

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 name
  • value - The field value
  • namespace - The namespace the memory belongs to
  • memory_id - The memory ID to associate with this field value
Source

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 name
  • value - The field value
  • namespace - The namespace
  • memory_id - The memory ID to remove
Source

pub fn find_by_metadata( &self, field: &str, value: &str, namespace: &str, ) -> Result<Vec<MemoryId>>

Find all memory IDs matching a metadata field value

§Arguments
  • field - The metadata field name
  • value - The field value to match
  • namespace - The namespace to search in
§Returns

Vector of memory IDs that have the specified metadata field value

Source

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 deleted
  • indexed_fields - List of fields that are indexed
Source

pub fn store_entity_profile(&self, profile: &EntityProfile) -> Result<()>

Store an entity profile

Profiles are keyed by the normalized (lowercase) entity name for case-insensitive lookups.

§Arguments
  • profile - The entity profile to store
§Example
let profile = EntityProfile::new(EntityId::new(), "Alice".into(), "person".into());
storage.store_entity_profile(&profile)?;
Source

pub fn get_entity_profile(&self, name: &str) -> Result<Option<EntityProfile>>

Get an entity profile by name (case-insensitive)

§Arguments
  • name - The entity name to look up
§Returns

The entity profile if found, or None

§Example
let profile = storage.get_entity_profile("Alice")?;
if let Some(p) = profile {
    println!("Found profile for {}", p.name);
}
Source

pub fn list_entity_profiles(&self) -> Result<Vec<EntityProfile>>

List all entity profiles

§Returns

Vector of all entity profiles in the database

§Performance

O(n) where n = number of profiles. Use with caution on large databases.

Source

pub fn delete_entity_profile(&self, name: &str) -> Result<bool>

Delete an entity profile by name (case-insensitive)

§Arguments
  • name - The entity name to delete
§Returns

true if the profile was deleted, false if it didn’t exist

Source

pub fn count_entity_profiles(&self) -> Result<usize>

Count entity profiles

§Returns

Number of entity profiles in the database

Source

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.

Source

pub fn store_fact_embedding(&self, key: &[u8], embedding: &[f32]) -> Result<()>

Store a fact embedding

Key is a 16-byte hash of (entity_name, fact_type, value). Value is the embedding as raw little-endian f32 bytes.

Source

pub fn get_fact_embedding(&self, key: &[u8]) -> Result<Option<Vec<f32>>>

Get a fact embedding by key

Returns the embedding as Vec, or None if not found.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more