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
impl VectorDb
Sourcepub async fn open(config: Config) -> Result<Self>
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?;Sourcepub async fn create_collection(
&self,
name: &str,
dimensions: usize,
metric: DistanceMetric,
) -> Result<()>
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.
Sourcepub async fn delete_collection(&self, name: &str) -> Result<()>
pub async fn delete_collection(&self, name: &str) -> Result<()>
Sourcepub fn collection_exists(&self, name: &str) -> bool
pub fn collection_exists(&self, name: &str) -> bool
Check if a collection exists.
Sourcepub fn list_collections(&self) -> Vec<String>
pub fn list_collections(&self) -> Vec<String>
List all collection names.
Sourcepub fn get_collection(&self, name: &str) -> Result<Arc<Collection>>
pub fn get_collection(&self, name: &str) -> Result<Arc<Collection>>
Sourcepub async fn insert(
&self,
collection: &str,
id: &str,
vector: &[f32],
metadata: Option<VectorMetadata>,
) -> Result<()>
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.
Sourcepub async fn insert_batch<'a, I>(
&self,
collection: &str,
vectors: I,
) -> Result<usize>
pub async fn insert_batch<'a, I>( &self, collection: &str, vectors: I, ) -> Result<usize>
Sourcepub async fn update(
&self,
collection: &str,
id: &str,
vector: &[f32],
metadata: Option<VectorMetadata>,
) -> Result<()>
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.
Sourcepub async fn search(
&self,
collection: &str,
query: &[f32],
limit: usize,
) -> Result<Vec<SearchResult>>
pub async fn search( &self, collection: &str, query: &[f32], limit: usize, ) -> Result<Vec<SearchResult>>
Sourcepub async fn search_with_threshold(
&self,
collection: &str,
query: &[f32],
limit: usize,
min_score: f32,
) -> Result<Vec<SearchResult>>
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).
Sourcepub async fn get(
&self,
collection: &str,
id: &str,
) -> Result<Option<(Vec<f32>, Option<VectorMetadata>)>>
pub async fn get( &self, collection: &str, id: &str, ) -> Result<Option<(Vec<f32>, Option<VectorMetadata>)>>
Sourcepub fn count(&self, collection: &str) -> Result<usize>
pub fn count(&self, collection: &str) -> Result<usize>
Get the number of vectors in a collection.
Sourcepub fn collection_stats(&self, collection: &str) -> Result<CollectionStats>
pub fn collection_stats(&self, collection: &str) -> Result<CollectionStats>
Get collection statistics.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for VectorDb
impl RefUnwindSafe for VectorDb
impl Send for VectorDb
impl Sync for VectorDb
impl Unpin for VectorDb
impl UnsafeUnpin for VectorDb
impl UnwindSafe for VectorDb
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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