Skip to main content

RingDb

Struct RingDb 

Source
pub struct RingDb<T: Payload = ()> { /* private fields */ }
Expand description

Builder for a ring-query vector database.

Insert vectors with their associated payloads via add_vector(), then call build() to obtain a SealedRingDb.

T must implement Payload, which is derived with #[derive(Payload)]. Use T = () when no payload is needed.

§Example — no payload

use ringdb::{RingDb, RingDbConfig, RingQuery};

let mut db = RingDb::new(RingDbConfig::new(4)).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());

Implementations§

Source§

impl<T: Payload> RingDb<T>

Source

pub fn new(config: RingDbConfig) -> Result<Self>

Create a new empty RingDb.

The storage strategy (Serde or Pod) is determined entirely by T’s #[derive(Payload)] — no second constructor needed.

§Example — with Serde payload
use ringdb::{RingDb, RingDbConfig, RingQuery, Payload};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Payload)]
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();
Source

pub fn add_vector(&mut self, vector: &[f32], payload: T) -> Result<()>

Insert a single vector and its associated payload.

Vectors are assigned sequential IDs starting from 0. The slice length must equal dims.

Source

pub fn build(self) -> Result<SealedRingDb<T>>

Seal the database.

Transfers vectors to the compute backend and flushes the payload builder to its mmap. If RingDbConfig::persist_dir is set, all data is also written to disk (reload with RingDb::load).

Source

pub fn load( dir: &Path, backend_preference: BackendPreference, ) -> Result<SealedRingDb<T>>

Reconstruct a SealedRingDb from a directory previously written by build() with a persist dir configured.

The correct store variant is selected automatically based on T’s Payload impl — no separate load_pod method needed.

§Example
use ringdb::{RingDb, RingDbConfig, BackendPreference};
use std::path::Path;

// --- save ---
let mut db = RingDb::<()>::new(RingDbConfig::new(4).with_persist_dir("/tmp/mydb")).unwrap();
db.add_vector(&[1.0, 0.0, 0.0, 0.0], ()).unwrap();
let _sealed = db.build().unwrap();

// --- load ---
let loaded = RingDb::<()>::load(Path::new("/tmp/mydb"), BackendPreference::Cpu).unwrap();
Source

pub fn len(&self) -> usize

Number of vectors currently staged.

Source

pub fn is_empty(&self) -> bool

Returns true if no vectors have been inserted.

Source

pub fn dims(&self) -> usize

Number of dimensions per vector.

Source

pub fn backend_name(&self) -> &str

Name of the backend currently in use.

Auto Trait Implementations§

§

impl<T> Freeze for RingDb<T>
where <T as Payload>::Builder: Freeze,

§

impl<T = ()> !RefUnwindSafe for RingDb<T>

§

impl<T> Send for RingDb<T>
where <T as Payload>::Builder: Send,

§

impl<T> Sync for RingDb<T>
where <T as Payload>::Builder: Sync,

§

impl<T> Unpin for RingDb<T>
where <T as Payload>::Builder: Unpin,

§

impl<T> UnsafeUnpin for RingDb<T>
where <T as Payload>::Builder: UnsafeUnpin,

§

impl<T = ()> !UnwindSafe for RingDb<T>

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> 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, 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.