pub struct RingDb<T = ()> { /* private fields */ }Expand description
Builder for a ring-query vector database.
Insert vectors (and their associated payloads) with
add_vector(), then call build() to
transfer ownership to the compute backend and obtain a SealedRingDb
that can be queried.
T is the payload type stored alongside each vector. Use T = () when
no payload is needed.
§Example — no payload
use ringdb::{RingDb, RingDbConfig, RingQuery};
let config = RingDbConfig::new(4);
let mut db = RingDb::new(config).unwrap();
db.add_vector(&[1.0, 0.0, 0.0, 0.0], ()).unwrap();
db.add_vector(&[0.0, 1.0, 0.0, 0.0], ()).unwrap();
let db = db.build().unwrap();
let result = db.query(&RingQuery { query: &[1.0f32, 0.0, 0.0, 0.0], d: 1.0, lambda: 0.1 }).unwrap();
println!("hits: {:?}", result.ids);§Example — with payload
use ringdb::{RingDb, RingDbConfig, RingQuery};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct Meta { label: String }
let mut db: RingDb<Meta> = RingDb::new(RingDbConfig::new(2)).unwrap();
db.add_vector(&[1.0, 0.0], Meta { label: "dog".into() }).unwrap();
db.add_vector(&[0.0, 1.0], Meta { label: "cat".into() }).unwrap();
let db = db.build().unwrap();
let result = db.query(&RingQuery { query: &[1.0f32, 0.0], d: 1.0, lambda: 0.1 }).unwrap();
let payloads = db.fetch_payloads(&result.ids).unwrap();Implementations§
Source§impl<T: Serialize + DeserializeOwned> RingDb<T>
impl<T: Serialize + DeserializeOwned> RingDb<T>
Sourcepub fn new(config: RingDbConfig) -> Result<Self, RingDbError>
pub fn new(config: RingDbConfig) -> Result<Self, RingDbError>
Create a new empty RingDb with the given configuration.
Sourcepub fn add_vector(
&mut self,
vector: &[f32],
payload: T,
) -> Result<(), RingDbError>
pub fn add_vector( &mut self, vector: &[f32], payload: T, ) -> Result<(), RingDbError>
Insert a single vector and its associated payload.
Vectors are assigned sequential IDs starting from 0.
The slice length must equal dims.
Sourcepub fn build(self) -> Result<SealedRingDb<T>, RingDbError>
pub fn build(self) -> Result<SealedRingDb<T>, RingDbError>
Transfer ownership of the accumulated data to the compute backend and seal the database.
Vector data is moved into the backend (zero-cost for the CPU backend).
Payloads are serialized and moved into a cold anonymous mmap — the
staging Vec<T> is dropped immediately after.
Sourcepub fn backend_name(&self) -> &str
pub fn backend_name(&self) -> &str
Name of the backend currently in use.
Auto Trait Implementations§
impl<T> Freeze for RingDb<T>
impl<T = ()> !RefUnwindSafe for RingDb<T>
impl<T> Send for RingDb<T>where
T: Send,
impl<T> Sync for RingDb<T>where
T: Sync,
impl<T> Unpin for RingDb<T>where
T: Unpin,
impl<T> UnsafeUnpin for RingDb<T>
impl<T = ()> !UnwindSafe for RingDb<T>
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> 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