Skip to main content

VectorStore

Struct VectorStore 

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

In-memory flat vector store backed by a Vec<VectorEntry>.

The configured Distance controls both how vectors are stored (similarity metrics pre-normalise; distance metrics store verbatim) and how queries are scored.

Implementations§

Source§

impl VectorStore

Source

pub fn to_snapshot(&self) -> IndexSnapshot

Produce an IndexSnapshot capturing the current store contents.

Source

pub fn from_snapshot(snapshot: IndexSnapshot) -> Result<Self, RagError>

Build a VectorStore from a previously-produced snapshot.

Returns RagError::Persistence if the schema version is unknown, and RagError::DimensionMismatch if any stored entry has a vector whose length disagrees with the snapshot’s dim.

Source

pub fn save_json(&self, path: impl AsRef<Path>) -> Result<(), RagError>

Serialise this store to path as pretty-printed JSON.

Source

pub fn load_json(path: impl AsRef<Path>) -> Result<Self, RagError>

Deserialise a store previously written by VectorStore::save_json.

Returns RagError::Persistence on malformed JSON or unknown schema version, and RagError::DimensionMismatch if any stored entry’s vector length disagrees with the snapshot’s dim.

Source§

impl VectorStore

Source

pub fn new(dim: usize) -> Self

Create an empty cosine-similarity store for vectors of dim dim.

Source

pub fn new_with_distance(dim: usize, distance: Distance) -> Self

Create an empty store with a specific Distance metric.

Source

pub fn insert( &mut self, vector: Vec<f32>, chunk: Chunk, ) -> Result<usize, RagError>

Insert a vector+chunk pair into the store.

Behaviour depends on the store’s Distance:

  • Similarity metrics (Cosine, DotProduct, Angular) L2-normalise the stored vector up-front so that scoring is cheap.
  • True distance metrics (Euclidean, Hamming) preserve the vector verbatim.

Returns the assigned entry id. Errors:

Source

pub fn search(&self, query: &[f32], top_k: usize) -> Vec<SearchResult>

Return the top-top_k entries by score.

The query vector is normalised internally when the metric is a similarity; it is not mutated. Results are returned in descending score order (see SearchResult::score for polarity).

Source

pub fn search_with_threshold( &self, query: &[f32], top_k: usize, min_score: f32, ) -> Vec<SearchResult>

Like Self::search but discards results whose score is below min_score.

Source

pub fn search_filtered( &self, query: &[f32], top_k: usize, filter: &MetadataFilter, ) -> Result<Vec<SearchResult>, RagError>

Search filtered by a MetadataFilter.

Filter evaluation is post-scoring; the metric is evaluated against every entry, then results that fail the filter are discarded.

Source

pub fn len(&self) -> usize

Number of entries currently in the store.

Source

pub fn is_empty(&self) -> bool

Returns true if the store contains no entries.

Source

pub fn clear(&mut self)

Remove all entries from the store (preserves the configured dimension and distance metric).

Source

pub fn memory_usage_bytes(&self) -> usize

Approximate heap memory used by the stored vectors and chunk texts. This is a lower-bound estimate: it counts vector bytes and chunk-text bytes but ignores allocator overhead and struct padding.

Source

pub fn dim(&self) -> usize

The embedding dimensionality this store was constructed with.

Source

pub fn distance(&self) -> Distance

The active distance metric.

Trait Implementations§

Source§

impl Debug for VectorStore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for VectorStore

Source§

fn default() -> VectorStore

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for VectorStore

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for VectorStore

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> 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, 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
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,