Skip to main content

Storage

Struct Storage 

Source
pub struct Storage { /* private fields */ }
Expand description

SQLite-backed storage for Codemem memories, embeddings, and graph data.

Wraps rusqlite::Connection in a Mutex to satisfy Send + Sync bounds required by the StorageBackend trait.

Implementations§

Source§

impl Storage

Source

pub fn store_embedding( &self, memory_id: &str, embedding: &[f32], ) -> Result<(), CodememError>

Store an embedding for a memory.

Source

pub fn get_embedding( &self, memory_id: &str, ) -> Result<Option<Vec<f32>>, CodememError>

Get an embedding by memory ID.

Source

pub fn insert_graph_node(&self, node: &GraphNode) -> Result<(), CodememError>

Insert a graph node.

Source

pub fn get_graph_node( &self, id: &str, ) -> Result<Option<GraphNode>, CodememError>

Get a graph node by ID.

Source

pub fn delete_graph_node(&self, id: &str) -> Result<bool, CodememError>

Delete a graph node by ID.

Source

pub fn all_graph_nodes(&self) -> Result<Vec<GraphNode>, CodememError>

Get all graph nodes.

Source

pub fn insert_graph_edge(&self, edge: &Edge) -> Result<(), CodememError>

Insert a graph edge.

Source

pub fn get_edges_for_node( &self, node_id: &str, ) -> Result<Vec<Edge>, CodememError>

Get all edges from or to a node.

Source

pub fn all_graph_edges(&self) -> Result<Vec<Edge>, CodememError>

Get all graph edges.

Source

pub fn delete_graph_edges_for_node( &self, node_id: &str, ) -> Result<usize, CodememError>

Delete all graph edges connected to a node (as src or dst).

Source

pub fn graph_edges_for_namespace( &self, namespace: &str, ) -> Result<Vec<Edge>, CodememError>

Get all graph edges where both src and dst nodes belong to the given namespace.

Source

pub fn delete_graph_edge(&self, id: &str) -> Result<bool, CodememError>

Delete a graph edge by ID.

Source§

impl Storage

Source

pub fn insert_memory(&self, memory: &MemoryNode) -> Result<(), CodememError>

Insert a new memory. Returns Err(Duplicate) if content hash already exists.

Source

pub fn get_memory(&self, id: &str) -> Result<Option<MemoryNode>, CodememError>

Get a memory by ID. Updates access_count and last_accessed_at.

Source

pub fn update_memory( &self, id: &str, content: &str, importance: Option<f64>, ) -> Result<(), CodememError>

Update a memory’s content and re-hash.

Source

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

Delete a memory by ID.

Source

pub fn list_memory_ids(&self) -> Result<Vec<String>, CodememError>

List all memory IDs.

Source

pub fn list_memory_ids_for_namespace( &self, namespace: &str, ) -> Result<Vec<String>, CodememError>

List memory IDs scoped to a specific namespace.

Source

pub fn list_namespaces(&self) -> Result<Vec<String>, CodememError>

List all distinct namespaces.

Source

pub fn memory_count(&self) -> Result<usize, CodememError>

Get memory count.

Source§

impl Storage

Source

pub fn stats(&self) -> Result<StorageStats, CodememError>

Get database statistics.

Source

pub fn insert_consolidation_log( &self, cycle_type: &str, affected_count: usize, ) -> Result<(), CodememError>

Record a consolidation run.

Source

pub fn last_consolidation_runs( &self, ) -> Result<Vec<ConsolidationLogEntry>, CodememError>

Get the last consolidation run for each cycle type.

Source

pub fn get_repeated_searches( &self, min_count: usize, namespace: Option<&str>, ) -> Result<Vec<(String, usize, Vec<String>)>, CodememError>

Find repeated search patterns (Grep/Glob) by extracting the “pattern” field from memory metadata JSON. Returns (pattern, count, memory_ids) tuples where count >= min_count, ordered by count descending.

Source

pub fn get_file_hotspots( &self, min_count: usize, namespace: Option<&str>, ) -> Result<Vec<(String, usize, Vec<String>)>, CodememError>

Find file hotspots by extracting the “file_path” field from memory metadata.

Source

pub fn get_tool_usage_stats( &self, namespace: Option<&str>, ) -> Result<HashMap<String, usize>, CodememError>

Get tool usage statistics from memory metadata.

Source

pub fn get_decision_chains( &self, min_count: usize, namespace: Option<&str>, ) -> Result<Vec<(String, usize, Vec<String>)>, CodememError>

Find decision chains: files with multiple Edit/Write memories over time.

Source

pub fn ensure_session_column(&self) -> Result<(), CodememError>

Ensure session_id column exists on memories table.

Source

pub fn start_session( &self, id: &str, namespace: Option<&str>, ) -> Result<(), CodememError>

Start a new session.

Source

pub fn end_session( &self, id: &str, summary: Option<&str>, ) -> Result<(), CodememError>

End a session by setting ended_at and optionally a summary.

Source

pub fn list_sessions( &self, namespace: Option<&str>, ) -> Result<Vec<Session>, CodememError>

List sessions, optionally filtered by namespace.

Source§

impl Storage

Source

pub fn open(path: &Path) -> Result<Self, CodememError>

Open (or create) a Codemem database at the given path.

Source

pub fn open_in_memory() -> Result<Self, CodememError>

Open an in-memory database (for testing).

Source

pub fn content_hash(content: &str) -> String

Compute SHA-256 hash of content for deduplication.

Trait Implementations§

Source§

impl StorageBackend for Storage

Source§

fn insert_memory(&self, memory: &MemoryNode) -> Result<(), CodememError>

Insert a new memory. Returns Err(Duplicate) if content hash already exists.
Source§

fn get_memory(&self, id: &str) -> Result<Option<MemoryNode>, CodememError>

Get a memory by ID. Updates access_count and last_accessed_at.
Source§

fn get_memories_batch( &self, ids: &[&str], ) -> Result<Vec<MemoryNode>, CodememError>

Get multiple memories by IDs in a single batch operation.
Source§

fn update_memory( &self, id: &str, content: &str, importance: Option<f64>, ) -> Result<(), CodememError>

Update a memory’s content and optionally its importance. Re-computes content hash.
Source§

fn delete_memory(&self, id: &str) -> Result<bool, CodememError>

Delete a memory by ID. Returns true if a row was deleted.
Source§

fn list_memory_ids(&self) -> Result<Vec<String>, CodememError>

List all memory IDs, ordered by created_at descending.
Source§

fn list_memory_ids_for_namespace( &self, namespace: &str, ) -> Result<Vec<String>, CodememError>

List memory IDs scoped to a specific namespace.
Source§

fn list_namespaces(&self) -> Result<Vec<String>, CodememError>

List all distinct namespaces.
Source§

fn memory_count(&self) -> Result<usize, CodememError>

Get total memory count.
Source§

fn store_embedding( &self, memory_id: &str, embedding: &[f32], ) -> Result<(), CodememError>

Store an embedding vector for a memory.
Source§

fn get_embedding( &self, memory_id: &str, ) -> Result<Option<Vec<f32>>, CodememError>

Get an embedding by memory ID.
Source§

fn delete_embedding(&self, memory_id: &str) -> Result<bool, CodememError>

Delete an embedding by memory ID. Returns true if a row was deleted.
Source§

fn list_all_embeddings(&self) -> Result<Vec<(String, Vec<f32>)>, CodememError>

List all stored embeddings as (memory_id, embedding_vector) pairs.
Source§

fn insert_graph_node(&self, node: &GraphNode) -> Result<(), CodememError>

Insert or replace a graph node.
Source§

fn get_graph_node(&self, id: &str) -> Result<Option<GraphNode>, CodememError>

Get a graph node by ID.
Source§

fn delete_graph_node(&self, id: &str) -> Result<bool, CodememError>

Delete a graph node by ID. Returns true if a row was deleted.
Source§

fn all_graph_nodes(&self) -> Result<Vec<GraphNode>, CodememError>

Get all graph nodes.
Source§

fn insert_graph_edge(&self, edge: &Edge) -> Result<(), CodememError>

Insert or replace a graph edge.
Source§

fn get_edges_for_node(&self, node_id: &str) -> Result<Vec<Edge>, CodememError>

Get all edges from or to a node.
Source§

fn all_graph_edges(&self) -> Result<Vec<Edge>, CodememError>

Get all graph edges.
Source§

fn delete_graph_edges_for_node( &self, node_id: &str, ) -> Result<usize, CodememError>

Delete all graph edges connected to a node. Returns count deleted.
Source§

fn start_session( &self, id: &str, namespace: Option<&str>, ) -> Result<(), CodememError>

Start a new session.
Source§

fn end_session( &self, id: &str, summary: Option<&str>, ) -> Result<(), CodememError>

End a session with optional summary.
Source§

fn list_sessions( &self, namespace: Option<&str>, limit: usize, ) -> Result<Vec<Session>, CodememError>

List sessions, optionally filtered by namespace, up to limit.
Source§

fn insert_consolidation_log( &self, cycle_type: &str, affected_count: usize, ) -> Result<(), CodememError>

Record a consolidation run.
Source§

fn last_consolidation_runs( &self, ) -> Result<Vec<ConsolidationLogEntry>, CodememError>

Get the last consolidation run for each cycle type.
Source§

fn get_repeated_searches( &self, min_count: usize, namespace: Option<&str>, ) -> Result<Vec<(String, usize, Vec<String>)>, CodememError>

Find repeated search patterns. Returns (pattern, count, memory_ids).
Source§

fn get_file_hotspots( &self, min_count: usize, namespace: Option<&str>, ) -> Result<Vec<(String, usize, Vec<String>)>, CodememError>

Find file hotspots. Returns (file_path, count, memory_ids).
Source§

fn get_tool_usage_stats( &self, namespace: Option<&str>, ) -> Result<Vec<(String, usize)>, CodememError>

Get tool usage statistics. Returns (tool_name, count) pairs.
Source§

fn get_decision_chains( &self, min_count: usize, namespace: Option<&str>, ) -> Result<Vec<(String, usize, Vec<String>)>, CodememError>

Find decision chains. Returns (file_path, count, memory_ids).
Source§

fn decay_stale_memories( &self, threshold_ts: i64, decay_factor: f64, ) -> Result<usize, CodememError>

Decay importance of stale memories older than threshold_ts by decay_factor. Returns count of affected memories.
Source§

fn list_memories_for_creative( &self, ) -> Result<Vec<(String, String, Vec<String>)>, CodememError>

List memories for creative consolidation: (id, memory_type, tags).
Source§

fn find_cluster_duplicates( &self, ) -> Result<Vec<(String, String, f64)>, CodememError>

Find near-duplicate memories by content hash prefix similarity. Returns (id1, id2, similarity) pairs.
Source§

fn find_forgettable( &self, importance_threshold: f64, ) -> Result<Vec<String>, CodememError>

Find memories eligible for forgetting (low importance). Returns list of memory IDs.
Source§

fn insert_memories_batch( &self, memories: &[MemoryNode], ) -> Result<(), CodememError>

Insert multiple memories in a single batch. Default impl calls insert_memory in a loop.
Source§

fn store_embeddings_batch( &self, items: &[(&str, &[f32])], ) -> Result<(), CodememError>

Store multiple embeddings in a single batch. Default impl calls store_embedding in a loop.
Source§

fn load_file_hashes(&self) -> Result<HashMap<String, String>, CodememError>

Load all file hashes for incremental indexing. Returns path -> hash map.
Source§

fn save_file_hashes( &self, hashes: &HashMap<String, String>, ) -> Result<(), CodememError>

Save file hashes for incremental indexing.
Source§

fn insert_graph_nodes_batch( &self, nodes: &[GraphNode], ) -> Result<(), CodememError>

Insert multiple graph nodes in a single batch. Default impl calls insert_graph_node in a loop.
Source§

fn insert_graph_edges_batch(&self, edges: &[Edge]) -> Result<(), CodememError>

Insert multiple graph edges in a single batch. Default impl calls insert_graph_edge in a loop.
Source§

fn find_unembedded_memories( &self, ) -> Result<Vec<(String, String)>, CodememError>

Find memories that have no embeddings yet. Returns (id, content) pairs.
Source§

fn search_graph_nodes( &self, query: &str, namespace: Option<&str>, limit: usize, ) -> Result<Vec<GraphNode>, CodememError>

Search graph nodes by label (case-insensitive LIKE). Returns matching nodes sorted by centrality descending, limited to limit results.
Source§

fn list_memories_filtered( &self, namespace: Option<&str>, memory_type: Option<&str>, ) -> Result<Vec<MemoryNode>, CodememError>

List memories with optional namespace and memory_type filters.
Source§

fn graph_edges_for_namespace( &self, namespace: &str, ) -> Result<Vec<Edge>, CodememError>

Get edges filtered by namespace (edges where both src and dst nodes have the given namespace).
Source§

fn stats(&self) -> Result<StorageStats, CodememError>

Get database statistics.

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, 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.