Skip to main content

SqliteStorage

Struct SqliteStorage 

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

SQLite-based storage implementation.

Provides persistent storage for RLM state with full ACID guarantees.

§Examples

use rlm_rs::storage::{SqliteStorage, Storage};

let mut storage = SqliteStorage::open("rlm-state.db").unwrap();
storage.init().unwrap();

Implementations§

Source§

impl SqliteStorage

Source

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

Opens or creates a SQLite database at the given path.

§Arguments
  • path - Path to the database file. Parent directory must exist.
§Errors

Returns an error if the database cannot be opened or initialized.

Source

pub fn in_memory() -> Result<Self>

Creates an in-memory SQLite database.

Useful for testing.

§Errors

Returns an error if the database cannot be created.

Source

pub fn path(&self) -> Option<&Path>

Returns the database path (None for in-memory).

Source§

impl SqliteStorage

Source

pub fn store_embedding( &mut self, chunk_id: i64, embedding: &[f32], model_name: Option<&str>, ) -> Result<()>

Stores an embedding for a chunk.

§Arguments
  • chunk_id - The chunk ID to associate the embedding with.
  • embedding - The embedding vector (f32 array).
  • model_name - Optional name of the model that generated the embedding.
§Errors

Returns an error if the embedding cannot be stored.

Source

pub fn get_embedding(&self, chunk_id: i64) -> Result<Option<Vec<f32>>>

Retrieves the embedding for a chunk.

§Errors

Returns an error if the query fails.

Source

pub fn get_embedding_models(&self, buffer_id: i64) -> Result<Vec<String>>

Gets the distinct model names used for embeddings in a buffer.

Returns the set of model names used to generate embeddings for chunks belonging to the specified buffer.

§Errors

Returns an error if the query fails.

Source

pub fn get_embedding_model_counts( &self, buffer_id: i64, ) -> Result<Vec<(Option<String>, i64)>>

Gets the count of embeddings by model name for a buffer.

Returns a list of (model_name, count) pairs.

§Errors

Returns an error if the query fails.

Source

pub fn store_embeddings_batch( &mut self, embeddings: &[(i64, Vec<f32>)], model_name: Option<&str>, ) -> Result<()>

Stores embeddings for multiple chunks in a batch.

§Errors

Returns an error if any embedding cannot be stored.

Source

pub fn delete_embedding(&mut self, chunk_id: i64) -> Result<()>

Deletes the embedding for a chunk.

§Errors

Returns an error if deletion fails.

Source

pub fn search_fts(&self, query: &str, limit: usize) -> Result<Vec<(i64, f64)>>

Performs FTS5 BM25 full-text search.

Returns chunk IDs and their BM25 scores (lower is better match).

§Arguments
  • query - The search query (supports FTS5 query syntax).
  • limit - Maximum number of results to return.
§Errors

Returns an error if the search fails.

Source

pub fn get_all_embeddings(&self) -> Result<Vec<(i64, Vec<f32>)>>

Returns all chunk embeddings for vector similarity search.

§Errors

Returns an error if the query fails.

Source

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

Counts chunks with embeddings.

§Errors

Returns an error if the count fails.

Source

pub fn has_embedding(&self, chunk_id: i64) -> Result<bool>

Checks if a chunk has an embedding.

§Errors

Returns an error if the query fails.

Source

pub fn get_chunks_needing_embedding( &self, buffer_id: i64, current_model: Option<&str>, ) -> Result<Vec<i64>>

Gets chunk IDs that need embedding (either no embedding or wrong model).

This is used for incremental embedding updates. Returns chunks that:

  • Have no embedding at all, OR
  • Have an embedding with a different model name (if current_model is provided)
§Arguments
  • buffer_id - The buffer to check.
  • current_model - Optional model name to check against. If provided, chunks with different models are included.
§Errors

Returns an error if the query fails.

Source

pub fn get_chunks_without_embedding(&self, buffer_id: i64) -> Result<Vec<i64>>

Gets chunks without any embedding for a buffer.

Simpler version of get_chunks_needing_embedding when model doesn’t matter.

§Errors

Returns an error if the query fails.

Source

pub fn delete_embeddings_by_model( &mut self, buffer_id: i64, model_name: Option<&str>, ) -> Result<usize>

Deletes embeddings with a specific model name.

Useful for cleaning up embeddings from old models before re-embedding.

§Arguments
  • buffer_id - The buffer to clean.
  • model_name - The model name to match (or None to match NULL).
§Returns

The number of embeddings deleted.

§Errors

Returns an error if deletion fails.

Source

pub fn get_embedding_stats(&self, buffer_id: i64) -> Result<EmbeddingStats>

Gets embedding statistics for a buffer.

Returns counts of embedded vs total chunks, and model breakdown.

§Errors

Returns an error if the query fails.

Trait Implementations§

Source§

impl Storage for SqliteStorage

Source§

fn init(&mut self) -> Result<()>

Initializes storage (creates schema, runs migrations). Read more
Source§

fn is_initialized(&self) -> Result<bool>

Checks if storage is initialized. Read more
Source§

fn reset(&mut self) -> Result<()>

Resets all stored state. Read more
Source§

fn save_context(&mut self, context: &Context) -> Result<()>

Saves the current context state. Read more
Source§

fn load_context(&self) -> Result<Option<Context>>

Loads the context state. Read more
Source§

fn delete_context(&mut self) -> Result<()>

Deletes the current context. Read more
Source§

fn add_buffer(&mut self, buffer: &Buffer) -> Result<i64>

Adds a buffer to storage. Read more
Source§

fn get_buffer(&self, id: i64) -> Result<Option<Buffer>>

Retrieves a buffer by ID. Read more
Source§

fn get_buffer_by_name(&self, name: &str) -> Result<Option<Buffer>>

Retrieves a buffer by name. Read more
Source§

fn list_buffers(&self) -> Result<Vec<Buffer>>

Lists all buffers. Read more
Source§

fn update_buffer(&mut self, buffer: &Buffer) -> Result<()>

Updates an existing buffer. Read more
Source§

fn delete_buffer(&mut self, id: i64) -> Result<()>

Deletes a buffer by ID. Read more
Source§

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

Returns the count of buffers. Read more
Source§

fn add_chunks(&mut self, buffer_id: i64, chunks: &[Chunk]) -> Result<()>

Adds chunks for a buffer. Read more
Source§

fn get_chunks(&self, buffer_id: i64) -> Result<Vec<Chunk>>

Retrieves all chunks for a buffer. Read more
Source§

fn get_chunk(&self, id: i64) -> Result<Option<Chunk>>

Retrieves a specific chunk by ID. Read more
Source§

fn delete_chunks(&mut self, buffer_id: i64) -> Result<()>

Deletes all chunks for a buffer. Read more
Source§

fn chunk_count(&self, buffer_id: i64) -> Result<usize>

Returns the count of chunks for a buffer. Read more
Source§

fn export_buffers(&self) -> Result<String>

Exports all buffers as a concatenated string. Read more
Source§

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

Gets storage statistics. Read more

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V