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
impl VectorIndex
Sourcepub fn open<P: AsRef<Path>>(
path: P,
dims: u32,
options: IndexOptions,
) -> Result<Self>
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 filedims- Number of dimensions per vectoroptions- 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
Sourcepub fn add(&mut self, vector: &[f32]) -> Result<u64>
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
Sourcepub fn add_with_id(&mut self, id: u64, vector: &[f32]) -> Result<()>
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.
Sourcepub fn add_batch(&mut self, vectors: &[f32]) -> Result<Vec<u64>>
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.
Sourcepub fn add_batch_with_ids(&mut self, ids: &[u64], vectors: &[f32]) -> Result<()>
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.
Sourcepub fn delete(&mut self, id: u64) -> Result<bool>
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.
Sourcepub fn search(&self, query: &[f32], k: usize) -> Result<Vec<SearchResult>>
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
Sourcepub fn search_filtered(
&self,
query: &[f32],
k: usize,
filter: impl Fn(u64) -> bool,
) -> Result<Vec<SearchResult>>
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
Sourcepub fn flush(&mut self) -> Result<()>
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:
- Flushing vector data to disk
- 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
Sourcepub fn rollback(&mut self) -> Result<()>
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.
Sourcepub fn backup<P: AsRef<Path>>(&mut self, path: P) -> Result<()>
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.
Sourcepub fn compact(&mut self) -> Result<()>
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.
Sourcepub fn use_huge_pages(&mut self)
pub fn use_huge_pages(&mut self)
Asks for huge pages from here on, as opening with IndexOptions::huge_pages at
Some(true) does.
Sourcepub fn keep_small_pages(&mut self)
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.
Sourcepub fn warm(&mut self)
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.
Sourcepub fn get(&self, id: u64) -> Result<Option<Vec<f32>>>
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.
Sourcepub fn ids(&self) -> Result<Vec<u64>>
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.
Sourcepub fn check(&self) -> Result<Check>
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.
Sourcepub fn deleted_count(&self) -> u64
pub fn deleted_count(&self) -> u64
Deleted vectors the file still holds, until compact removes them.
Sourcepub fn user_version(&self) -> u64
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.
Sourcepub fn set_user_version(&mut self, version: u64)
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.
Sourcepub fn dimensions(&self) -> u32
pub fn dimensions(&self) -> u32
Get the dimensionality of vectors in this index
Sourcepub fn metric(&self) -> DistanceMetric
pub fn metric(&self) -> DistanceMetric
The distance metric the index was created with