Skip to main content

InMemoryStore

Struct InMemoryStore 

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

In-memory vector store using brute-force search.

This is the simplest implementation, suitable for:

  • Testing and development
  • Small datasets (< 10,000 vectors)
  • Prototyping before moving to a production database

§Example

use phago_vectors::{InMemoryStore, VectorStore, VectorRecord};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let store = InMemoryStore::new(3);

    // Insert records
    store.upsert(vec![
        VectorRecord::new("a", vec![1.0, 0.0, 0.0]),
        VectorRecord::new("b", vec![0.0, 1.0, 0.0]),
        VectorRecord::new("c", vec![0.7, 0.7, 0.0]),
    ]).await?;

    // Search
    let results = store.search(&[1.0, 0.0, 0.0], 2).await?;
    assert_eq!(results[0].id, "a");

    Ok(())
}

Implementations§

Source§

impl InMemoryStore

Source

pub fn new(dimension: usize) -> Self

Create a new in-memory store with the specified dimension.

Uses cosine similarity by default.

Source

pub fn with_config(dimension: usize, metric: DistanceMetric) -> Self

Create a new in-memory store with a specific distance metric.

Trait Implementations§

Source§

impl VectorStore for InMemoryStore

Source§

fn name(&self) -> &str

Get the name of this backend.
Source§

fn dimension(&self) -> usize

Get the vector dimension.
Source§

fn metric(&self) -> DistanceMetric

Get the distance metric.
Source§

fn upsert<'life0, 'async_trait>( &'life0 self, records: Vec<VectorRecord>, ) -> Pin<Box<dyn Future<Output = VectorResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Insert or update records in the store. Read more
Source§

fn search<'life0, 'life1, 'async_trait>( &'life0 self, vector: &'life1 [f32], k: usize, ) -> Pin<Box<dyn Future<Output = VectorResult<Vec<SearchResult>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Search for similar vectors. Read more
Source§

fn search_with_filter<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, vector: &'life1 [f32], k: usize, filter: &'life2 HashMap<String, Value>, ) -> Pin<Box<dyn Future<Output = VectorResult<Vec<SearchResult>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Search with metadata filter. Read more
Source§

fn get<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 str, ) -> Pin<Box<dyn Future<Output = VectorResult<Option<VectorRecord>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Get a record by ID.
Source§

fn get_batch<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, ids: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = VectorResult<Vec<VectorRecord>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Get multiple records by ID.
Source§

fn delete<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 str, ) -> Pin<Box<dyn Future<Output = VectorResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete a record by ID.
Source§

fn delete_batch<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, ids: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = VectorResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Delete multiple records by ID.
Source§

fn count<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = VectorResult<usize>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Get the total number of records in the store.
Source§

fn clear<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = VectorResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Clear all records from the store.

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