1use std::{any::Any, sync::Arc};
13
14use async_trait::async_trait;
15use deepsize::DeepSizeOf;
16use lance_core::{Error, Result};
17use roaring::RoaringBitmap;
18use serde::{Deserialize, Serialize};
19use snafu::location;
20use std::convert::TryFrom;
21
22pub mod frag_reuse;
23pub mod metrics;
24pub mod optimize;
25pub mod prefilter;
26pub mod scalar;
27pub mod traits;
28pub mod vector;
29
30pub use crate::traits::*;
31
32pub const INDEX_FILE_NAME: &str = "index.idx";
33pub const INDEX_AUXILIARY_FILE_NAME: &str = "auxiliary.idx";
38pub const INDEX_METADATA_SCHEMA_KEY: &str = "lance:index";
39
40pub mod pb {
41 #![allow(clippy::use_self)]
42 include!(concat!(env!("OUT_DIR"), "/lance.index.pb.rs"));
43}
44
45#[async_trait]
48pub trait Index: Send + Sync + DeepSizeOf {
49 fn as_any(&self) -> &dyn Any;
51
52 fn as_index(self: Arc<Self>) -> Arc<dyn Index>;
54
55 fn as_vector_index(self: Arc<Self>) -> Result<Arc<dyn vector::VectorIndex>>;
57
58 fn statistics(&self) -> Result<serde_json::Value>;
60
61 async fn prewarm(&self) -> Result<()>;
65
66 fn index_type(&self) -> IndexType;
68
69 async fn calculate_included_frags(&self) -> Result<RoaringBitmap>;
74}
75
76#[derive(Debug, PartialEq, Eq, Copy, Hash, Clone, DeepSizeOf)]
78pub enum IndexType {
79 Scalar = 0, BTree = 1, Bitmap = 2, LabelList = 3, Inverted = 4, NGram = 5, FragmentReuse = 6,
93
94 Vector = 100, IvfFlat = 101,
98 IvfSq = 102,
99 IvfPq = 103,
100 IvfHnswSq = 104,
101 IvfHnswPq = 105,
102}
103
104impl std::fmt::Display for IndexType {
105 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
106 match self {
107 Self::Scalar | Self::BTree => write!(f, "BTree"),
108 Self::Bitmap => write!(f, "Bitmap"),
109 Self::LabelList => write!(f, "LabelList"),
110 Self::Inverted => write!(f, "Inverted"),
111 Self::NGram => write!(f, "NGram"),
112 Self::FragmentReuse => write!(f, "FragmentReuse"),
113 Self::Vector | Self::IvfPq => write!(f, "IVF_PQ"),
114 Self::IvfFlat => write!(f, "IVF_FLAT"),
115 Self::IvfSq => write!(f, "IVF_SQ"),
116 Self::IvfHnswSq => write!(f, "IVF_HNSW_SQ"),
117 Self::IvfHnswPq => write!(f, "IVF_HNSW_PQ"),
118 }
119 }
120}
121
122impl TryFrom<i32> for IndexType {
123 type Error = Error;
124
125 fn try_from(value: i32) -> Result<Self> {
126 match value {
127 v if v == Self::Scalar as i32 => Ok(Self::Scalar),
128 v if v == Self::BTree as i32 => Ok(Self::BTree),
129 v if v == Self::Bitmap as i32 => Ok(Self::Bitmap),
130 v if v == Self::LabelList as i32 => Ok(Self::LabelList),
131 v if v == Self::NGram as i32 => Ok(Self::NGram),
132 v if v == Self::Inverted as i32 => Ok(Self::Inverted),
133 v if v == Self::Vector as i32 => Ok(Self::Vector),
134 v if v == Self::IvfFlat as i32 => Ok(Self::IvfFlat),
135 v if v == Self::IvfSq as i32 => Ok(Self::IvfSq),
136 v if v == Self::IvfPq as i32 => Ok(Self::IvfPq),
137 v if v == Self::IvfHnswSq as i32 => Ok(Self::IvfHnswSq),
138 v if v == Self::IvfHnswPq as i32 => Ok(Self::IvfHnswPq),
139 _ => Err(Error::InvalidInput {
140 source: format!("the input value {} is not a valid IndexType", value).into(),
141 location: location!(),
142 }),
143 }
144 }
145}
146
147impl IndexType {
148 pub fn is_scalar(&self) -> bool {
149 matches!(
150 self,
151 Self::Scalar
152 | Self::BTree
153 | Self::Bitmap
154 | Self::LabelList
155 | Self::Inverted
156 | Self::NGram
157 )
158 }
159
160 pub fn is_vector(&self) -> bool {
161 matches!(
162 self,
163 Self::Vector
164 | Self::IvfPq
165 | Self::IvfHnswSq
166 | Self::IvfHnswPq
167 | Self::IvfFlat
168 | Self::IvfSq
169 )
170 }
171
172 pub fn version(&self) -> i32 {
178 match self {
179 Self::Scalar => 0,
180 Self::BTree => 0,
181 Self::Bitmap => 0,
182 Self::LabelList => 0,
183 Self::Inverted => 0,
184 Self::NGram => 0,
185 Self::FragmentReuse => 0,
186
187 Self::Vector
190 | Self::IvfFlat
191 | Self::IvfSq
192 | Self::IvfPq
193 | Self::IvfHnswSq
194 | Self::IvfHnswPq => 1,
195 }
196 }
197}
198
199pub trait IndexParams: Send + Sync {
200 fn as_any(&self) -> &dyn Any;
201
202 fn index_type(&self) -> IndexType;
203
204 fn index_name(&self) -> &str;
205}
206
207#[derive(Serialize, Deserialize, Debug)]
208pub struct IndexMetadata {
209 #[serde(rename = "type")]
210 pub index_type: String,
211 pub distance_type: String,
212}