pub struct IvfPqIndex { /* private fields */ }Expand description
IVF-PQ index: inverted file with product quantization.
Implementations§
Source§impl IvfPqIndex
impl IvfPqIndex
Sourcepub fn from_bytes(
bytes: &[u8],
memory: ScopedMemory,
) -> Result<Self, VectorError>
pub fn from_bytes( bytes: &[u8], memory: ScopedMemory, ) -> Result<Self, VectorError>
Decode an index written by Self::to_bytes, charging the PQ codec
to memory.
Fails with VectorError::CheckpointDeserializationError when the
bytes do not decode or the decoded cells disagree with the dimension,
the PQ code width, or the centroid count.
Source§impl IvfPqIndex
impl IvfPqIndex
Sourcepub fn new(dim: usize, params: IvfPqParams) -> Self
pub fn new(dim: usize, params: IvfPqParams) -> Self
Create an empty, untrained IVF-PQ index.
Sourcepub fn train(
&mut self,
vectors: &[&[f32]],
memory: ScopedMemory,
) -> Result<(), VectorError>
pub fn train( &mut self, vectors: &[&[f32]], memory: ScopedMemory, ) -> Result<(), VectorError>
Train the coarse centroids and the PQ codebooks on vectors, tracking
PQ codebook allocations against memory.
Fails with VectorError::InvalidInput when the set is empty, the
dimension is zero or not divisible by pq_m, the set is smaller than
pq_k, or the index already holds vectors (their codes would not
match new codebooks). A vector without the index dimension fails with
VectorError::DimensionMismatch. A failed training changes nothing.
Sourcepub fn insert_with_id(
&mut self,
id: u32,
vector: Vec<f32>,
) -> Result<(), VectorError>
pub fn insert_with_id( &mut self, id: u32, vector: Vec<f32>, ) -> Result<(), VectorError>
Add vector under the caller-assigned id.
Fails with VectorError::DimensionMismatch for a vector without the
index dimension, and with VectorError::InvalidInput when the index
is untrained or already holds id. A failed add changes nothing.
Sourcepub fn add(&mut self, vector: &[f32]) -> Result<u32, VectorError>
pub fn add(&mut self, vector: &[f32]) -> Result<u32, VectorError>
Add a vector under the next free id. Returns the id.
Fails as Self::insert_with_id does.
Sourcepub fn add_batch(&mut self, vectors: &[&[f32]]) -> Result<(), VectorError>
pub fn add_batch(&mut self, vectors: &[&[f32]]) -> Result<(), VectorError>
Add vectors under consecutive free ids. Stops at the first vector that fails to add.
Sourcepub fn is_trained(&self) -> bool
pub fn is_trained(&self) -> bool
Whether the index holds a trained codebook.
Sourcepub fn is_deleted(&self, id: u32) -> bool
pub fn is_deleted(&self, id: u32) -> bool
Whether id is held and soft-deleted.
Sourcepub fn delete(&mut self, id: u32) -> bool
pub fn delete(&mut self, id: u32) -> bool
Soft-delete id. false when the index does not hold it live.
Sourcepub fn undelete(&mut self, id: u32) -> bool
pub fn undelete(&mut self, id: u32) -> bool
Reverse a soft delete of id. false when id was not deleted.
Sourcepub fn get_vector(&self, id: u32) -> Option<&[f32]>
pub fn get_vector(&self, id: u32) -> Option<&[f32]>
The FP32 vector of a live id.
Sourcepub fn live_vectors(&self) -> Vec<(u32, Vec<f32>)>
pub fn live_vectors(&self) -> Vec<(u32, Vec<f32>)>
Every live entry as (id, vector), ordered by id.
Sourcepub fn roll_back_to(&mut self, next_id: u32)
pub fn roll_back_to(&mut self, next_id: u32)
Drop every entry with id next_id or later, so the next
Self::add takes next_id again. The training stays. A rollback
uses it to withdraw the newest adds.
Sourcepub fn compact(&mut self) -> usize
pub fn compact(&mut self) -> usize
Remove every soft-deleted entry. Returns the number removed. Ids of the remaining entries do not change.
Sourcepub fn live_count(&self) -> usize
pub fn live_count(&self) -> usize
Entries held and not soft-deleted.
Sourcepub fn tombstone_count(&self) -> usize
pub fn tombstone_count(&self) -> usize
Soft-deleted entries held.
pub fn is_empty(&self) -> bool
pub fn dim(&self) -> usize
pub fn n_cells(&self) -> usize
pub fn params(&self) -> &IvfPqParams
Sourcepub fn trained_on(&self) -> usize
pub fn trained_on(&self) -> usize
Vectors the codebooks were trained on. 0 when untrained.
Sourcepub fn trained_at_ms(&self) -> u64
pub fn trained_at_ms(&self) -> u64
Unix milliseconds the caller stamped the training with.
Sourcepub fn set_trained_at_ms(&mut self, ms: u64)
pub fn set_trained_at_ms(&mut self, ms: u64)
Stamp the training time, in Unix milliseconds.
Sourcepub fn memory_bytes(&self) -> usize
pub fn memory_bytes(&self) -> usize
Approximate heap bytes of the centroids, codes and vectors.
Source§impl IvfPqIndex
impl IvfPqIndex
Sourcepub fn search(
&self,
query: &[f32],
top_k: usize,
) -> Result<Vec<SearchResult>, VectorError>
pub fn search( &self, query: &[f32], top_k: usize, ) -> Result<Vec<SearchResult>, VectorError>
The top_k live entries nearest to query under the index metric.
A query without the index dimension fails with
VectorError::DimensionMismatch. A distance table over the memory
budget fails the search: skipping its cell would drop results.
Sourcepub fn search_with(
&self,
query: &[f32],
top_k: usize,
metric: DistanceMetric,
filter: Option<&RoaringBitmap>,
) -> Result<Vec<SearchResult>, VectorError>
pub fn search_with( &self, query: &[f32], top_k: usize, metric: DistanceMetric, filter: Option<&RoaringBitmap>, ) -> Result<Vec<SearchResult>, VectorError>
The top_k live entries nearest to query under metric, keeping
only ids in filter when one is given. Fails as Self::search.
Auto Trait Implementations§
impl Freeze for IvfPqIndex
impl RefUnwindSafe for IvfPqIndex
impl Send for IvfPqIndex
impl Sync for IvfPqIndex
impl Unpin for IvfPqIndex
impl UnsafeUnpin for IvfPqIndex
impl UnwindSafe for IvfPqIndex
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.