Skip to main content

VectorDb

Struct VectorDb 

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

The main vector database instance.

VectorDb manages multiple collections, each containing vectors of a specific dimensionality. It provides thread-safe access to all operations.

§Thread Safety

All operations on VectorDb are thread-safe. Uses scc::HashMap for lock-free concurrent access that is safe across .await points. Multiple readers can access the database concurrently, and write operations use fine-grained locking per entry.

Implementations§

Source§

impl VectorDb

Source

pub async fn open(config: Config) -> Result<Self>

Open or create a vector database with the given configuration.

§Arguments
  • config - Database configuration (memory or persistent).
§Example
ⓘ
// In-memory database
let db = VectorDb::open(Config::memory()).await?;

// Persistent database
let db = VectorDb::open(Config::persistent("./data/vectors")).await?;
Source

pub async fn create_collection( &self, name: &str, dimensions: usize, metric: DistanceMetric, ) -> Result<()>

Create a new collection with the specified parameters.

§Arguments
  • name - Unique name for the collection.
  • dimensions - Dimensionality of vectors (e.g., 384 for BGE-small).
  • metric - Distance metric to use for similarity calculations.
§Errors

Returns an error if a collection with the same name already exists.

Source

pub async fn delete_collection(&self, name: &str) -> Result<()>

Delete a collection and all its data.

§Arguments
  • name - Name of the collection to delete.
§Errors

Returns an error if the collection doesn’t exist.

Source

pub fn collection_exists(&self, name: &str) -> bool

Check if a collection exists.

Source

pub fn list_collections(&self) -> Vec<String>

List all collection names.

Source

pub fn get_collection(&self, name: &str) -> Result<Arc<Collection>>

Get a reference to a collection.

§Arguments
  • name - Name of the collection.
§Errors

Returns an error if the collection doesn’t exist.

Source

pub async fn insert( &self, collection: &str, id: &str, vector: &[f32], metadata: Option<VectorMetadata>, ) -> Result<()>

Insert a vector into a collection.

§Arguments
  • collection - Name of the collection.
  • id - Unique string identifier for the vector.
  • vector - The embedding vector to insert.
  • metadata - Optional metadata to associate with the vector.
§Errors

Returns an error if the collection doesn’t exist or the vector dimensions don’t match the collection.

Source

pub async fn insert_batch<'a, I>( &self, collection: &str, vectors: I, ) -> Result<usize>
where I: IntoIterator<Item = (&'a str, &'a [f32], Option<VectorMetadata>)>,

Insert multiple vectors into a collection.

This is more efficient than calling insert repeatedly as it batches the index updates.

§Arguments
  • collection - Name of the collection.
  • vectors - Iterator of (id, vector, metadata) tuples.
§Returns

The number of vectors successfully inserted.

Source

pub async fn update( &self, collection: &str, id: &str, vector: &[f32], metadata: Option<VectorMetadata>, ) -> Result<()>

Update a vector in a collection.

This is equivalent to delete + insert but may be more efficient for some index implementations.

Source

pub async fn delete(&self, collection: &str, id: &str) -> Result<bool>

Delete a vector from a collection.

§Arguments
  • collection - Name of the collection.
  • id - ID of the vector to delete.
§Returns

true if the vector was found and deleted, false if it didn’t exist.

Source

pub async fn delete_batch( &self, collection: &str, ids: &[&str], ) -> Result<usize>

Delete multiple vectors from a collection.

§Returns

The number of vectors actually deleted.

Source

pub async fn search( &self, collection: &str, query: &[f32], limit: usize, ) -> Result<Vec<SearchResult>>

Search for similar vectors.

§Arguments
  • collection - Name of the collection to search.
  • query - Query vector to find similar vectors to.
  • limit - Maximum number of results to return.
§Returns

A vector of search results, sorted by similarity (best first).

Source

pub async fn search_with_threshold( &self, collection: &str, query: &[f32], limit: usize, min_score: f32, ) -> Result<Vec<SearchResult>>

Search with a minimum score threshold.

§Arguments
  • collection - Name of the collection.
  • query - Query vector.
  • limit - Maximum results.
  • min_score - Minimum similarity score (0.0 to 1.0 for cosine).
Source

pub async fn get( &self, collection: &str, id: &str, ) -> Result<Option<(Vec<f32>, Option<VectorMetadata>)>>

Get a vector by ID.

§Returns

The vector and its metadata if found.

Source

pub fn contains(&self, collection: &str, id: &str) -> Result<bool>

Check if a vector exists.

Source

pub fn count(&self, collection: &str) -> Result<usize>

Get the number of vectors in a collection.

Source

pub fn collection_stats(&self, collection: &str) -> Result<CollectionStats>

Get collection statistics.

Source

pub async fn persist(&self) -> Result<()>

Persist the current state to disk.

This is only relevant for persistent databases. For in-memory databases, this is a no-op.

Source

pub async fn compact(&self, collection: &str) -> Result<()>

Force a compaction of the HNSW indices.

This can reclaim space after many deletions.

Trait Implementations§

Source§

impl Clone for VectorDb

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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

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