VectorStore

Struct VectorStore 

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

A vector store for in-memory vector search.

§Performance

Uses contiguous memory layout for optimal cache locality and fast serialization. Vector data is stored in a single buffer rather than individual Vec allocations.

Implementations§

Source§

impl VectorStore

Source

pub fn new(dimension: usize, metric: &str) -> Result<VectorStore, JsValue>

Creates a new vector store with the specified dimension and distance metric.

§Arguments
  • dimension - Vector dimension (e.g., 768 for BERT, 1536 for GPT)
  • metric - Distance metric: “cosine”, “euclidean”, or “dot”
§Errors

Returns an error if the metric is not recognized.

Source

pub fn len(&self) -> usize

Returns the number of vectors in the store.

Source

pub fn is_empty(&self) -> bool

Returns true if the store is empty.

Source

pub fn dimension(&self) -> usize

Returns the vector dimension.

Source

pub fn insert(&mut self, id: u64, vector: &[f32]) -> Result<(), JsValue>

Inserts a vector with the given ID.

§Arguments
  • id - Unique identifier for the vector
  • vector - Float32Array of the vector data
§Errors

Returns an error if vector dimension doesn’t match store dimension.

Source

pub fn search(&self, query: &[f32], k: usize) -> Result<JsValue, JsValue>

Searches for the k nearest neighbors to the query vector.

§Arguments
  • query - Query vector as Float32Array
  • k - Number of results to return
§Returns

Array of [id, score] pairs sorted by relevance.

§Errors

Returns an error if query dimension doesn’t match store dimension.

Source

pub fn remove(&mut self, id: u64) -> bool

Removes a vector by ID.

Source

pub fn clear(&mut self)

Clears all vectors from the store.

Source

pub fn memory_usage(&self) -> usize

Returns memory usage estimate in bytes.

Source

pub fn with_capacity( dimension: usize, metric: &str, capacity: usize, ) -> Result<VectorStore, JsValue>

Creates a new vector store with pre-allocated capacity.

This is more efficient when you know the approximate number of vectors you’ll be inserting, as it avoids repeated memory allocations.

§Arguments
  • dimension - Vector dimension
  • metric - Distance metric: “cosine”, “euclidean”, or “dot”
  • capacity - Number of vectors to pre-allocate space for
§Errors

Returns an error if the metric is not recognized.

Source

pub fn reserve(&mut self, additional: usize)

Pre-allocates memory for the specified number of additional vectors.

Call this before bulk insertions to avoid repeated allocations.

§Arguments
  • additional - Number of additional vectors to reserve space for
Source

pub fn insert_batch(&mut self, batch: JsValue) -> Result<(), JsValue>

Inserts multiple vectors in a single batch operation.

This is significantly faster than calling insert() multiple times because it pre-allocates memory and reduces per-call overhead.

§Arguments
  • batch - JavaScript array of [id, Float32Array] pairs
§Errors

Returns an error if any vector dimension doesn’t match store dimension.

Source

pub fn export_to_bytes(&self) -> Result<Vec<u8>, JsValue>

Exports the vector store to a binary format.

The binary format contains:

  • Header: dimension (u32), metric (u8), count (u64)
  • For each vector: id (u64), data (f32 array)

Use this to persist data to IndexedDB or localStorage.

§Errors

This function currently does not return errors but uses Result for future extensibility.

§Performance

Perf: Pre-allocates exact buffer size to avoid reallocations. Throughput: ~1600 MB/s on 10k vectors (768D)

Source

pub async fn save(&self, db_name: &str) -> Result<(), JsValue>

Saves the vector store to IndexedDB.

This method persists all vectors to the browser’s IndexedDB, enabling offline-first applications.

§Arguments
  • db_name - Name of the IndexedDB database
§Errors

Returns an error if IndexedDB is not available or the save fails.

§Example
const store = new VectorStore(768, "cosine");
store.insert(1n, vector1);
await store.save("my-vectors");
Source

pub async fn load(db_name: &str) -> Result<VectorStore, JsValue>

Loads a vector store from IndexedDB.

This method restores all vectors from the browser’s IndexedDB.

§Arguments
  • db_name - Name of the IndexedDB database
§Errors

Returns an error if the database doesn’t exist or is corrupted.

§Example
const store = await VectorStore.load("my-vectors");
console.log(store.len); // Number of restored vectors
Source

pub async fn delete_database(db_name: &str) -> Result<(), JsValue>

Deletes the IndexedDB database.

Use this to clear all persisted data.

§Arguments
  • db_name - Name of the IndexedDB database to delete
§Errors

Returns an error if the deletion fails.

Source

pub fn import_from_bytes(bytes: &[u8]) -> Result<VectorStore, JsValue>

Imports a vector store from binary format.

Use this to restore data from IndexedDB or localStorage.

§Errors

Returns an error if:

  • The data is too short or corrupted
  • The magic number is invalid
  • The version is unsupported
  • The metric byte is invalid

Trait Implementations§

Source§

impl From<VectorStore> for JsValue

Source§

fn from(value: VectorStore) -> Self

Converts to this type from the input type.
Source§

impl FromWasmAbi for VectorStore

Source§

type Abi = u32

The Wasm ABI type that this converts from when coming back out from the ABI boundary.
Source§

unsafe fn from_abi(js: u32) -> Self

Recover a Self from Self::Abi. Read more
Source§

impl IntoWasmAbi for VectorStore

Source§

type Abi = u32

The Wasm ABI type that this converts into when crossing the ABI boundary.
Source§

fn into_abi(self) -> u32

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary.
Source§

impl LongRefFromWasmAbi for VectorStore

Source§

type Abi = u32

Same as RefFromWasmAbi::Abi
Source§

type Anchor = RcRef<VectorStore>

Same as RefFromWasmAbi::Anchor
Source§

unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
Source§

impl OptionFromWasmAbi for VectorStore

Source§

fn is_none(abi: &Self::Abi) -> bool

Tests whether the argument is a “none” instance. If so it will be deserialized as None, and otherwise it will be passed to FromWasmAbi.
Source§

impl OptionIntoWasmAbi for VectorStore

Source§

fn none() -> Self::Abi

Returns an ABI instance indicating “none”, which JS will interpret as the None branch of this option. Read more
Source§

impl RefFromWasmAbi for VectorStore

Source§

type Abi = u32

The Wasm ABI type references to Self are recovered from.
Source§

type Anchor = RcRef<VectorStore>

The type that holds the reference to Self for the duration of the invocation of the function that has an &Self parameter. This is required to ensure that the lifetimes don’t persist beyond one function call, and so that they remain anonymous.
Source§

unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor

Recover a Self::Anchor from Self::Abi. Read more
Source§

impl RefMutFromWasmAbi for VectorStore

Source§

type Abi = u32

Same as RefFromWasmAbi::Abi
Source§

type Anchor = RcRefMut<VectorStore>

Same as RefFromWasmAbi::Anchor
Source§

unsafe fn ref_mut_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
Source§

impl TryFromJsValue for VectorStore

Source§

fn try_from_js_value(value: JsValue) -> Result<Self, JsValue>

Performs the conversion.
Source§

fn try_from_js_value_ref(value: &JsValue) -> Option<Self>

Performs the conversion.
Source§

impl VectorFromWasmAbi for VectorStore

Source§

type Abi = <Box<[JsValue]> as FromWasmAbi>::Abi

Source§

unsafe fn vector_from_abi(js: Self::Abi) -> Box<[VectorStore]>

Source§

impl VectorIntoWasmAbi for VectorStore

Source§

impl WasmDescribe for VectorStore

Source§

impl WasmDescribeVector for VectorStore

Source§

impl SupportsConstructor for VectorStore

Source§

impl SupportsInstanceProperty for VectorStore

Source§

impl SupportsStaticProperty for VectorStore

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> ReturnWasmAbi for T
where T: IntoWasmAbi,

Source§

type Abi = <T as IntoWasmAbi>::Abi

Same as IntoWasmAbi::Abi
Source§

fn return_abi(self) -> <T as ReturnWasmAbi>::Abi

Same as IntoWasmAbi::into_abi, except that it may throw and never return in the case of Err.
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.