Skip to main content

Vectors

Struct Vectors 

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

A collection of vectors, reached by Db::vectors.

Keys are byte strings the way the keyspace’s are, so anything that is bytes will do.

Implementations§

Source§

impl Vectors

Source

pub fn dim(&self) -> usize

How many coordinates a vector in this collection has.

Source

pub fn metric(&self) -> Metric

What nearness means here.

Source

pub fn put(&self, key: impl AsRef<[u8]>, v: &[f32]) -> Result<bool>

Put a vector in under key, and say whether the key is new.

Replacing is the same call, which is what makes re-embedding a document one line. The old code comes out of its partition and the new one goes into whichever partition it belongs to now, so nothing accumulates and there is no rebuild waiting at the end of it.

§Errors

Code::Invalid when the vector is not Vectors::dim long, when it holds a coordinate that is not a number, or when a cosine collection is handed a vector of length zero, which has no direction to store. Code::Full for a key past the length limit.

Source

pub fn get(&self, key: impl AsRef<[u8]>) -> Result<Option<Vec<f32>>>

The vector under key, if there is one.

A cosine collection hands back the unit vector it stored rather than the one it was given. See the module note on the metric for why.

§Errors

Code::Invalid if called from inside a callback that is already holding this database.

Source

pub fn with<R>( &self, key: impl AsRef<[u8]>, f: impl FnOnce(&[f32]) -> R, ) -> Result<Option<R>>

The same, handed to a closure where it lies, which copies nothing.

The owned form is what most code wants and this is Y29’s other half: the vectors are contiguous floats already, so a caller that only wants to measure one against something never has to allocate to do it.

§Errors

As Vectors::get.

Source

pub fn contains(&self, key: impl AsRef<[u8]>) -> Result<bool>

Whether the collection holds a vector under key.

§Errors

As Vectors::get.

Source

pub fn remove(&self, key: impl AsRef<[u8]>) -> Result<bool>

Take a vector out, saying whether it was there.

A delete here is a delete and not a tombstone: the member leaves its posting and the last member of that posting moves into the hole. That is the difference between this and a graph index, where deletes pile up until somebody rebuilds.

§Errors

As Vectors::get.

Source

pub fn len(&self) -> Result<usize>

How many vectors are in the collection.

§Errors

As Vectors::get.

Source

pub fn is_empty(&self) -> Result<bool>

Whether there are none.

§Errors

As Vectors::get.

Source

pub fn search(&self, q: &[f32], k: usize) -> Result<Vec<Match>>

The k nearest keys to q, nearest first.

Fewer than k come back when the collection holds fewer than that, and nothing comes back from an empty one.

let db = yo::open(yo::MEMORY)?;
let v = db.vectors("words", 2)?;

v.put("north", &[0.0, 1.0])?;
v.put("east", &[1.0, 0.0])?;
v.put("west", &[-1.0, 0.0])?;

let hits = v.search(&[0.2, 0.9], 2)?;
assert_eq!(hits[0].key, b"north".to_vec());
assert_eq!(hits.len(), 2);
§Errors

Code::Invalid when q is not Vectors::dim long or holds a coordinate that is not a number.

Source

pub fn near(&self, key: impl AsRef<[u8]>, k: usize) -> Result<Vec<Match>>

The k nearest keys to the vector already stored under key, which is the more-like-this search.

key itself is never one of the answers, because it is always the nearest and nobody asked what a thing is most similar to itself.

§Errors

Code::NotFound when nothing is stored under key, because an empty answer would otherwise mean both “no such key” and “nothing near it”.

Trait Implementations§

Source§

impl Clone for Vectors

Source§

fn clone(&self) -> Vectors

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
Source§

impl Debug for Vectors

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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<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, 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> 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.