Skip to main content

VectorIndex

Struct VectorIndex 

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

Public facade for Chassis vector index

This struct provides the main API for interacting with a Chassis index. It handles crash consistency, ghost node recovery, and provides a clean interface for vector insertion and search.

Implementations§

Source§

impl VectorIndex

Source

pub fn open<P: AsRef<Path>>( path: P, dims: u32, options: IndexOptions, ) -> Result<Self>

Open or create a vector index

§Arguments
  • path - Path to the index file
  • dims - Number of dimensions per vector
  • options - Index configuration options

A file in format v1 or v2 is migrated to v3 first (ADR-0008). After a crash, slots written after the last flush are simply reused.

§Errors

Returns an error if:

  • The file cannot be opened or created
  • The file is corrupted
  • Dimension mismatch with existing index
  • The file was created with a different max_connections
Source

pub fn add(&mut self, vector: &[f32]) -> Result<u64>

Add a vector and return the id assigned to it: one past the largest id used so far.

Ids are never reused by add, even after a delete. Use add_with_id to choose the id.

The vector, its id and its graph record go into the next slot; the slot counts only once linking finishes, so a failed or interrupted add leaves a slot the next add reuses.

§Errors

Returns an error if:

  • Vector dimensions don’t match index dimensions
  • Storage write fails
  • Graph write fails
Source

pub fn add_with_id(&mut self, id: u64, vector: &[f32]) -> Result<()>

Add a vector under the caller’s id. Search results report this id.

§Errors

Returns an error if a live vector already has id (delete it first to replace it), if id is u64::MAX (reserved), or for the same reasons as add.

Source

pub fn add_batch(&mut self, vectors: &[f32]) -> Result<Vec<u64>>

Add many vectors at once, given back to back, dims floats each, and return their ids, assigned as add assigns them. Linking runs on every core, so a large batch builds many times faster than adding one vector at a time; the graph then depends on thread timing (ADR-0010). All or nothing: after an error, none of the batch is added.

§Errors

Returns an error if vectors isn’t a whole number of vectors of the index’s dimensions, or if storing or linking them fails.

Source

pub fn add_batch_with_ids(&mut self, ids: &[u64], vectors: &[f32]) -> Result<()>

add_with_id for many vectors at once: ids[i] is the id of the i-th vector in vectors. Linking runs on every core, as for add_batch. All or nothing.

§Errors

Returns an error if the counts differ, an id is u64::MAX, repeats, or already exists, or storing or linking fails.

Source

pub fn delete(&mut self, id: u64) -> Result<bool>

Delete the vector with id. Returns false if no live vector has that id.

Search stops returning it immediately. Like an add, the delete is durable after the next flush(); a crash before then rolls it back.

§Errors

Returns an error if the index file cannot be read.

Source

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

Search for k nearest neighbors

§Arguments
  • query - Query vector (must match index dimensions)
  • k - Number of nearest neighbors to return
§Returns

Returns a vector of search results, sorted by distance (ascending), identified by the ids given to add/add_with_id. Deleted vectors are never returned.

§Errors

Returns an error if k is 0 or the query has other dimensions than the index

Source

pub fn search_filtered( &self, query: &[f32], k: usize, filter: impl Fn(u64) -> bool, ) -> Result<Vec<SearchResult>>

Like search, among only the vectors whose id filter accepts. When walking the graph would cost more than checking every vector, as when few match, it checks every vector and returns the exact nearest.

§Errors

Returns an error if k is 0 or the query has other dimensions than the index

Source

pub fn flush(&mut self) -> Result<()>

Flush all changes to disk

Dropping the index doesn’t flush: adds and deletes since the last flush are discarded, as after a crash.

This method ensures durability by:

  1. Flushing vector data to disk
  2. Flushing graph metadata to disk
§Performance Warning

This operation is expensive (1-50ms depending on storage device). Batch multiple add() calls and flush() once at the end.

§Errors

Returns an error if the flush fails

Source

pub fn rollback(&mut self) -> Result<()>

Discards every add, delete and user_version since the last flush, as closing the index and opening it again would, without letting go of the writer’s lock. Call it where a write’s transaction in your own store rolls back, before the next flush commits what that write left.

§Errors

Returns an error if the file can’t be read or synced. Later flushes then fail: close the index and open it again, which recovers it to its last flush.

Source

pub fn backup<P: AsRef<Path>>(&mut self, path: P) -> Result<()>

Flushes, then writes a copy of the index to path (ADR-0024): a backup holding every add and delete so far, which opens like any index. The copy is written beside path and renamed over it once whole, so a crash never leaves half a backup there; an existing file at path is replaced. Restore a backup by copying it into place while nothing has the index open.

§Errors

Returns an error if the flush fails, path is the index’s own file, or the copy can’t be written.

Source

pub fn compact(&mut self) -> Result<()>

Rewrites the index without its deleted vectors and with a newly built graph, then replaces the file with the copy (ADR-0011). Ids don’t change. Like flush, it makes every add and delete so far durable. It takes as long as building the index, on every core, and needs free disk for a second copy of the live vectors. Readers in other processes keep searching and move to the new file by themselves.

§Errors

Returns an error, leaving the index as it was, if the copy can’t be written or, on Windows, another process has the index open.

Source

pub fn use_huge_pages(&mut self)

Asks for huge pages from here on, as opening with IndexOptions::huge_pages at Some(true) does.

Source

pub fn keep_small_pages(&mut self)

Asks for no huge pages from here on, not even by warm, as opening with IndexOptions::huge_pages at Some(false) does. What was asked for already stays asked.

Source

pub fn warm(&mut self)

Reads what the index holds into memory on another thread, as opening with IndexOptions::warm does, without waiting for it: searches go on meanwhile. While that is under way, asking again does nothing. The option is on from here on, so a file opened again, as a reader does after a compaction, is read in too.

Source

pub fn get(&self, id: u64) -> Result<Option<Vec<f32>>>

The vector with id, or None if no live vector has it. It comes back as the index keeps it: scaled to length 1 in a cosine index, and rounded in half precision.

§Errors

Returns an error if the index file cannot be read.

Source

pub fn contains(&self, id: u64) -> Result<bool>

Whether a live vector has id.

§Errors

Returns an error if the index file cannot be read.

Source

pub fn ids(&self) -> Result<Vec<u64>>

The ids of the live vectors, in the order they were added.

§Errors

Returns an error if the index file cannot be read.

Source

pub fn check(&self) -> Result<Check>

Checks what opening can’t: that every live vector is within a search’s reach, and that every vector is made of finite numbers. It reads the whole graph and every vector.

§Errors

Returns an error if the index file cannot be read.

Source

pub fn len(&self) -> u64

Get the number of live (not deleted) vectors in the index

Source

pub fn deleted_count(&self) -> u64

Deleted vectors the file still holds, until compact removes them.

Source

pub fn user_version(&self) -> u64

Your application’s number for the index, as SQLite’s user_version is for a database: 0 until you set one. The next flush commits it with the data (ADR-0023), so after a crash it says which flush the index holds. Keep the same number in your own store, in the transaction that flush sits in, and compare the two on open and as each write begins. It is the number last set, flushed or not; rollback sets it back to the flushed one.

Source

pub fn set_user_version(&mut self, version: u64)

Sets the number the next flush commits with the data; see user_version. Until then, a crash leaves the one flushed before.

Source

pub fn is_empty(&self) -> bool

Check if the index is empty

Source

pub fn dimensions(&self) -> u32

Get the dimensionality of vectors in this index

Source

pub fn metric(&self) -> DistanceMetric

The distance metric the index was created with

Source

pub fn precision(&self) -> Precision

What the file keeps of each component of a vector

Trait Implementations§

Source§

impl Debug for VectorIndex

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V