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
impl VectorStore
Sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Returns memory usage estimate in bytes.
Sourcepub fn with_capacity(
dimension: usize,
metric: &str,
capacity: usize,
) -> Result<VectorStore, JsValue>
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 dimensionmetric- 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.
Sourcepub fn reserve(&mut self, additional: usize)
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
Sourcepub fn insert_batch(&mut self, batch: JsValue) -> Result<(), JsValue>
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.
Sourcepub fn export_to_bytes(&self) -> Result<Vec<u8>, JsValue>
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)
Sourcepub async fn save(&self, db_name: &str) -> Result<(), JsValue>
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 theIndexedDBdatabase
§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");Sourcepub async fn load(db_name: &str) -> Result<VectorStore, JsValue>
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 theIndexedDBdatabase
§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 vectorsSourcepub fn import_from_bytes(bytes: &[u8]) -> Result<VectorStore, JsValue>
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
impl From<VectorStore> for JsValue
Source§fn from(value: VectorStore) -> Self
fn from(value: VectorStore) -> Self
Source§impl FromWasmAbi for VectorStore
impl FromWasmAbi for VectorStore
Source§impl IntoWasmAbi for VectorStore
impl IntoWasmAbi for VectorStore
Source§impl LongRefFromWasmAbi for VectorStore
impl LongRefFromWasmAbi for VectorStore
Source§impl OptionFromWasmAbi for VectorStore
impl OptionFromWasmAbi for VectorStore
Source§impl OptionIntoWasmAbi for VectorStore
impl OptionIntoWasmAbi for VectorStore
Source§impl RefFromWasmAbi for VectorStore
impl RefFromWasmAbi for VectorStore
Source§type Anchor = RcRef<VectorStore>
type Anchor = RcRef<VectorStore>
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§impl RefMutFromWasmAbi for VectorStore
impl RefMutFromWasmAbi for VectorStore
Source§impl TryFromJsValue for VectorStore
impl TryFromJsValue for VectorStore
Source§impl VectorFromWasmAbi for VectorStore
impl VectorFromWasmAbi for VectorStore
type Abi = <Box<[JsValue]> as FromWasmAbi>::Abi
unsafe fn vector_from_abi(js: Self::Abi) -> Box<[VectorStore]>
Source§impl VectorIntoWasmAbi for VectorStore
impl VectorIntoWasmAbi for VectorStore
type Abi = <Box<[JsValue]> as IntoWasmAbi>::Abi
fn vector_into_abi(vector: Box<[VectorStore]>) -> Self::Abi
Source§impl WasmDescribeVector for VectorStore
impl WasmDescribeVector for VectorStore
impl SupportsConstructor for VectorStore
impl SupportsInstanceProperty for VectorStore
impl SupportsStaticProperty for VectorStore
Auto Trait Implementations§
impl Freeze for VectorStore
impl RefUnwindSafe for VectorStore
impl Send for VectorStore
impl Sync for VectorStore
impl Unpin for VectorStore
impl UnwindSafe for VectorStore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
Source§type Abi = <T as IntoWasmAbi>::Abi
type Abi = <T as IntoWasmAbi>::Abi
IntoWasmAbi::AbiSource§fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
IntoWasmAbi::into_abi, except that it may throw and never
return in the case of Err.