Skip to main content

FlatIndex

Struct FlatIndex 

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

Brute-force exact nearest-neighbour index.

See the crate-level docs for the design notes and the iqdb_index::IndexCore / iqdb_index::Index contracts this type satisfies.

§Examples

use std::sync::Arc;

use iqdb_flat::{FlatConfig, FlatIndex};
use iqdb_index::{Index, IndexCore};
use iqdb_types::{DistanceMetric, SearchParams, VectorId};

let mut idx = FlatIndex::new(2, DistanceMetric::Euclidean, FlatConfig)?;
idx.insert(VectorId::from(1u64), Arc::<[f32]>::from(&[0.0, 0.0][..]), None)?;
idx.insert(VectorId::from(2u64), Arc::<[f32]>::from(&[3.0, 4.0][..]), None)?;

let hits = idx.search(&[0.0, 0.0], &SearchParams::new(1, DistanceMetric::Euclidean))?;
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].id, VectorId::U64(1));

Implementations§

Source§

impl FlatIndex

Source

pub fn new_unconfigured(dim: usize, metric: DistanceMetric) -> Result<Self>

Builds an empty index for dim-component vectors compared under metric.

Returns IqdbError::InvalidConfig when dim == 0. This is the same construction surface as Index::new; calling it directly is the convenient path when the concrete type is known and there is nothing to configure.

§Examples
use iqdb_flat::FlatIndex;
use iqdb_types::DistanceMetric;

let idx = FlatIndex::new_unconfigured(3, DistanceMetric::Cosine)?;
assert_eq!(idx.dim(), 3);
assert!(idx.is_empty());
Source

pub fn dim(&self) -> usize

The dimensionality the index was built for.

§Examples
use iqdb_flat::{FlatConfig, FlatIndex};
use iqdb_index::Index;
use iqdb_types::DistanceMetric;

let idx = FlatIndex::new(8, DistanceMetric::Euclidean, FlatConfig)?;
assert_eq!(idx.dim(), 8);
Source

pub fn metric(&self) -> DistanceMetric

The distance metric the index was built for.

§Examples
use iqdb_flat::{FlatConfig, FlatIndex};
use iqdb_index::Index;
use iqdb_types::DistanceMetric;

let idx = FlatIndex::new(8, DistanceMetric::Cosine, FlatConfig)?;
assert_eq!(idx.metric(), DistanceMetric::Cosine);
Source

pub fn len(&self) -> usize

The number of searchable vectors in the index.

§Examples
use std::sync::Arc;

use iqdb_flat::{FlatConfig, FlatIndex};
use iqdb_index::{Index, IndexCore};
use iqdb_types::{DistanceMetric, VectorId};

let mut idx = FlatIndex::new(1, DistanceMetric::Euclidean, FlatConfig)?;
assert_eq!(idx.len(), 0);
idx.insert(VectorId::from(1u64), Arc::<[f32]>::from(&[0.0][..]), None)?;
assert_eq!(idx.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true when the index holds no vectors.

§Examples
use iqdb_flat::{FlatConfig, FlatIndex};
use iqdb_index::Index;
use iqdb_types::DistanceMetric;

let idx = FlatIndex::new(1, DistanceMetric::Euclidean, FlatConfig)?;
assert!(idx.is_empty());

Trait Implementations§

Source§

impl Debug for FlatIndex

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Index for FlatIndex

Source§

type Config = FlatConfig

Configuration consumed at construction time. Read more
Source§

fn new( dim: usize, metric: DistanceMetric, _config: Self::Config, ) -> Result<Self>

Build a fresh index of dim vectors under metric with config. Read more
Source§

impl IndexCore for FlatIndex

Source§

fn insert_batch( &mut self, items: Vec<(VectorId, Arc<[f32]>, Option<Metadata>)>, ) -> Result<()>

Reserves capacity for all backing stores up front, then inserts each item via insert.

This is the same fail-fast contract as the trait default (the first error returns immediately; inserts before it remain), but a single reserve(items.len()) on each of the four Vecs and the HashMap replaces the O(log n) incremental reallocations the default loop would trigger — a measurable win for bulk loads.

Source§

fn search(&self, query: &[f32], params: &SearchParams) -> Result<Vec<Hit>>

Searches for the top-k nearest neighbours under params.metric.

Returns IqdbError::DimensionMismatch if query.len() != self.dim, IqdbError::InvalidMetric if the params metric does not match the index’s, and IqdbError::InvalidFilter if params.filter is supplied and rejected by FilterEvaluator::new (depth or In cardinality past iqdb_filter’s MAX_FILTER_DEPTH / MAX_IN_VALUES). A pathological filter surfaces as a clean error rather than overflowing the search thread.

Source§

fn insert( &mut self, id: VectorId, vector: Arc<[f32]>, metadata: Option<Metadata>, ) -> Result<()>

Insert one vector into the index. Read more
Source§

fn delete(&mut self, id: &VectorId) -> Result<()>

Remove the vector identified by id from the search space. Read more
Source§

fn len(&self) -> usize

The number of vectors currently searchable in the index. Read more
Source§

fn is_empty(&self) -> bool

Returns true when the index holds no searchable vectors. Read more
Source§

fn dim(&self) -> usize

The dimensionality of vectors this index was configured for.
Source§

fn metric(&self) -> DistanceMetric

The distance metric this index was configured for.
Source§

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

Flush any pending state to durable storage. Read more
Source§

fn stats(&self) -> IndexStats

A runtime snapshot of the index’s state. Read more
Source§

fn search_batch( &self, queries: &[&[f32]], params: &SearchParams, ) -> Result<Vec<Vec<Hit>>, IqdbError>

Run a batch of top-k searches with shared params. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error