use std::{any::Any, sync::Arc};
use async_trait::async_trait;
use deepsize::DeepSizeOf;
use lance_core::Result;
use roaring::RoaringBitmap;
use serde::{Deserialize, Serialize};
pub mod optimize;
pub mod prefilter;
pub mod scalar;
pub mod traits;
pub mod vector;
pub use crate::traits::*;
pub const INDEX_FILE_NAME: &str = "index.idx";
pub const INDEX_AUXILIARY_FILE_NAME: &str = "auxiliary.idx";
pub const INDEX_METADATA_SCHEMA_KEY: &str = "lance:index";
pub mod pb {
#![allow(clippy::use_self)]
include!(concat!(env!("OUT_DIR"), "/lance.index.pb.rs"));
}
#[async_trait]
pub trait Index: Send + Sync + DeepSizeOf {
fn as_any(&self) -> &dyn Any;
fn as_index(self: Arc<Self>) -> Arc<dyn Index>;
fn as_vector_index(self: Arc<Self>) -> Result<Arc<dyn vector::VectorIndex>>;
fn statistics(&self) -> Result<serde_json::Value>;
fn index_type(&self) -> IndexType;
async fn calculate_included_frags(&self) -> Result<RoaringBitmap>;
}
#[derive(Debug, PartialEq, Eq, Copy, Hash, Clone, DeepSizeOf)]
pub enum IndexType {
Scalar = 0, BTree = 1, Bitmap = 2, LabelList = 3, Inverted = 4, Vector = 100,
}
impl std::fmt::Display for IndexType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Scalar | Self::BTree => write!(f, "BTree"),
Self::Bitmap => write!(f, "Bitmap"),
Self::LabelList => write!(f, "LabelList"),
Self::Inverted => write!(f, "Inverted"),
Self::Vector => write!(f, "Vector"),
}
}
}
impl IndexType {
pub fn is_scalar(&self) -> bool {
matches!(
self,
Self::Scalar | Self::BTree | Self::Bitmap | Self::LabelList | Self::Inverted
)
}
pub fn is_vector(&self) -> bool {
matches!(self, Self::Vector)
}
}
pub trait IndexParams: Send + Sync {
fn as_any(&self) -> &dyn Any;
fn index_type(&self) -> IndexType;
fn index_name(&self) -> &str;
}
#[derive(Serialize, Deserialize, Debug)]
pub struct IndexMetadata {
#[serde(rename = "type")]
pub index_type: String,
pub distance_type: String,
}