use crate::superfile::{
builder::FtsConfig,
fts::tokenize::ASCII_LOWER_TOKENIZER,
vector::{builder::VectorConfig, distance::Metric},
};
const DEFAULT_ROT_SEED: u64 = 0x5EED_5EED_5EED_5EED;
#[derive(Debug, Clone)]
struct VectorIndex {
column: String,
dim: usize,
n_cent: usize,
metric: Metric,
}
#[derive(Debug, Clone)]
struct FtsIndex {
column: String,
analyzer: String,
}
#[derive(Debug, Clone, Default)]
pub struct IndexSpec {
fts: Vec<FtsIndex>,
vectors: Vec<VectorIndex>,
}
impl IndexSpec {
pub fn new() -> Self {
Self::default()
}
pub fn fts(self, column: impl Into<String>) -> Self {
self.fts_with_analyzer(column, ASCII_LOWER_TOKENIZER)
}
pub fn fts_with_analyzer(
mut self,
column: impl Into<String>,
analyzer: impl Into<String>,
) -> Self {
self.fts.push(FtsIndex {
column: column.into(),
analyzer: analyzer.into(),
});
self
}
pub fn vector(
mut self,
column: impl Into<String>,
dim: usize,
n_cent: usize,
metric: Metric,
) -> Self {
self.vectors.push(VectorIndex {
column: column.into(),
dim,
n_cent,
metric,
});
self
}
pub(crate) fn fts_columns(&self) -> Vec<String> {
self.fts.iter().map(|f| f.column.clone()).collect()
}
pub(crate) fn fts_analyzers(&self) -> Vec<String> {
self.fts.iter().map(|f| f.analyzer.clone()).collect()
}
#[cfg(feature = "remote")]
pub(crate) fn vector_indexes(&self) -> impl Iterator<Item = (&str, usize, usize, Metric)> {
self.vectors
.iter()
.map(|v| (v.column.as_str(), v.dim, v.n_cent, v.metric))
}
pub(crate) fn to_configs(&self) -> (Vec<FtsConfig>, Vec<VectorConfig>) {
let fts = self
.fts
.iter()
.map(|f| FtsConfig {
column: f.column.clone(),
positions: false,
})
.collect();
let vectors = self
.vectors
.iter()
.map(|v| {
VectorConfig::new(
v.column.clone(),
v.dim,
v.n_cent,
DEFAULT_ROT_SEED,
v.metric,
)
})
.collect();
(fts, vectors)
}
}