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
impl Vectors
Sourcepub fn put(&self, key: impl AsRef<[u8]>, v: &[f32]) -> Result<bool>
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.
Sourcepub fn get(&self, key: impl AsRef<[u8]>) -> Result<Option<Vec<f32>>>
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.
Sourcepub fn with<R>(
&self,
key: impl AsRef<[u8]>,
f: impl FnOnce(&[f32]) -> R,
) -> Result<Option<R>>
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.
Sourcepub fn remove(&self, key: impl AsRef<[u8]>) -> Result<bool>
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.
Sourcepub fn search(&self, q: &[f32], k: usize) -> Result<Vec<Match>>
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.
Sourcepub fn near(&self, key: impl AsRef<[u8]>, k: usize) -> Result<Vec<Match>>
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”.