lance_index/vector/v3/
subindex.rsuse std::collections::HashMap;
use std::fmt::Debug;
use std::sync::Arc;
use arrow_array::{ArrayRef, RecordBatch};
use deepsize::DeepSizeOf;
use lance_core::{Error, Result};
use snafu::{location, Location};
use crate::vector::storage::VectorStore;
use crate::vector::{flat, hnsw};
use crate::{prefilter::PreFilter, vector::Query};
pub trait IvfSubIndex: Send + Sync + Debug + DeepSizeOf {
type QueryParams: Send + Sync + for<'a> From<&'a Query>;
type BuildParams: Clone + Send + Sync;
fn load(data: RecordBatch) -> Result<Self>
where
Self: Sized;
fn name() -> &'static str;
fn metadata_key() -> &'static str;
fn schema() -> arrow_schema::SchemaRef;
fn search(
&self,
query: ArrayRef,
k: usize,
params: Self::QueryParams,
storage: &impl VectorStore,
prefilter: Arc<dyn PreFilter>,
) -> Result<RecordBatch>;
fn index_vectors(storage: &impl VectorStore, params: Self::BuildParams) -> Result<Self>
where
Self: Sized;
fn remap(&self, mapping: &HashMap<u64, Option<u64>>) -> Result<Self>
where
Self: Sized;
fn to_batch(&self) -> Result<RecordBatch>;
}
pub enum SubIndexType {
Flat,
Hnsw,
}
impl std::fmt::Display for SubIndexType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Flat => write!(f, "{}", flat::index::FlatIndex::name()),
Self::Hnsw => write!(f, "{}", hnsw::builder::HNSW::name()),
}
}
}
impl TryFrom<&str> for SubIndexType {
type Error = Error;
fn try_from(value: &str) -> Result<Self> {
match value {
"FLAT" => Ok(Self::Flat),
"HNSW" => Ok(Self::Hnsw),
_ => Err(Error::Index {
message: format!("unknown sub index type {}", value),
location: location!(),
}),
}
}
}