use std::collections::BTreeMap;
use relate_catalog::{ConstraintDef, IndexDef};
use relate_core::Value;
use serde::{Deserialize, Serialize};
use super::RedoOp;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum IndexKindSpec {
Range,
Point,
Text,
Fulltext,
Lookup,
Vector,
}
impl IndexKindSpec {
pub const ALL: [IndexKindSpec; 6] = [
IndexKindSpec::Range,
IndexKindSpec::Point,
IndexKindSpec::Text,
IndexKindSpec::Fulltext,
IndexKindSpec::Lookup,
IndexKindSpec::Vector,
];
pub fn display_name(&self) -> &'static str {
relate_parser::IndexKind::from(*self).display_name()
}
pub fn from_display_name(name: &str) -> Option<Self> {
Self::ALL
.into_iter()
.find(|kind| kind.display_name() == name)
}
pub fn provider_name(&self) -> &'static str {
match self {
IndexKindSpec::Range => "range-1.0",
IndexKindSpec::Point => "point-1.0",
IndexKindSpec::Text => "text-3.0",
IndexKindSpec::Fulltext => "fulltext-2.0",
IndexKindSpec::Lookup => "token-lookup-1.0",
IndexKindSpec::Vector => "vector-2026.08",
}
}
pub fn default_config(&self) -> Option<fn(&mut BTreeMap<String, Value>)> {
match self {
IndexKindSpec::Point => Some(apply_point_defaults),
IndexKindSpec::Fulltext => Some(apply_fulltext_defaults),
IndexKindSpec::Range
| IndexKindSpec::Text
| IndexKindSpec::Lookup
| IndexKindSpec::Vector => None,
}
}
pub fn is_lookup(&self) -> bool {
matches!(self, IndexKindSpec::Lookup)
}
pub fn resolve(detail: Option<&IndexDetailSpec>, legacy_property: &str) -> Self {
detail
.map(|detail| detail.kind)
.or_else(|| split_legacy_property_encoding(legacy_property).1)
.unwrap_or(IndexKindSpec::Range)
}
}
fn apply_point_defaults(config: &mut BTreeMap<String, Value>) {
const DEFAULTS: &[(&str, &[f64])] = &[
("spatial.cartesian.min", &[-1000000.0, -1000000.0]),
("spatial.cartesian.max", &[1000000.0, 1000000.0]),
(
"spatial.cartesian-3d.min",
&[-1000000.0, -1000000.0, -1000000.0],
),
(
"spatial.cartesian-3d.max",
&[1000000.0, 1000000.0, 1000000.0],
),
("spatial.wgs-84.min", &[-180.0, -90.0]),
("spatial.wgs-84.max", &[180.0, 90.0]),
("spatial.wgs-84-3d.min", &[-180.0, -90.0, -1000000.0]),
("spatial.wgs-84-3d.max", &[180.0, 90.0, 1000000.0]),
];
for (key, values) in DEFAULTS {
let coordinates = values
.iter()
.map(|value| Value::Float(*value))
.collect::<Vec<_>>();
config
.entry((*key).to_string())
.or_insert_with(|| Value::List(coordinates.into()));
}
}
fn apply_fulltext_defaults(config: &mut BTreeMap<String, Value>) {
config
.entry("fulltext.analyzer".to_string())
.or_insert_with(|| Value::String("standard-no-stop-words".into()));
config
.entry("fulltext.eventually_consistent".to_string())
.or_insert(Value::Bool(false));
}
impl From<IndexKindSpec> for relate_parser::IndexKind {
fn from(kind: IndexKindSpec) -> Self {
match kind {
IndexKindSpec::Range => relate_parser::IndexKind::Range,
IndexKindSpec::Point => relate_parser::IndexKind::Point,
IndexKindSpec::Text => relate_parser::IndexKind::Text,
IndexKindSpec::Fulltext => relate_parser::IndexKind::Fulltext,
IndexKindSpec::Lookup => relate_parser::IndexKind::Lookup,
IndexKindSpec::Vector => relate_parser::IndexKind::Vector,
}
}
}
impl From<relate_parser::IndexKind> for IndexKindSpec {
fn from(kind: relate_parser::IndexKind) -> Self {
match kind {
relate_parser::IndexKind::Range => IndexKindSpec::Range,
relate_parser::IndexKind::Point => IndexKindSpec::Point,
relate_parser::IndexKind::Text => IndexKindSpec::Text,
relate_parser::IndexKind::Fulltext => IndexKindSpec::Fulltext,
relate_parser::IndexKind::Lookup => IndexKindSpec::Lookup,
relate_parser::IndexKind::Vector => IndexKindSpec::Vector,
}
}
}
#[derive(Clone, Copy)]
pub struct ServingPairIndex<'a> {
pub kind: IndexKindSpec,
pub relationship: bool,
pub labels: &'a [String],
pub properties: &'a [String],
}
#[derive(Clone, Copy)]
pub struct ServingPairConstraint<'a> {
pub backed: bool,
pub relationship: bool,
pub label: &'a str,
pub properties: &'a [String],
}
pub fn serving_pair_conflicts(
index: ServingPairIndex<'_>,
constraint: ServingPairConstraint<'_>,
) -> bool {
index.kind == IndexKindSpec::Range
&& index.relationship == constraint.relationship
&& index.labels.len() == 1
&& constraint.label == index.labels[0]
&& constraint.properties == index.properties
&& constraint.backed
}
pub fn split_legacy_property_encoding(property: &str) -> (&str, Option<IndexKindSpec>) {
match property.split_once('#') {
None => (property, None),
Some((head, rest)) => (
head,
rest.split('#')
.next()
.and_then(IndexKindSpec::from_display_name),
),
}
}
pub fn legacy_property_encoding(property: &str, kind: IndexKindSpec, name: &str) -> String {
format!("{property}#{}#{name}", kind.display_name())
}
pub fn legacy_property_names(property: &str) -> Vec<String> {
split_legacy_property_encoding(property)
.0
.split(',')
.map(str::to_string)
.collect()
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IndexFacts {
pub kind: IndexKindSpec,
pub relationship: bool,
pub labels: Vec<String>,
pub properties: Vec<String>,
pub with_properties: Vec<String>,
}
impl IndexFacts {
pub fn resolve(
index: &IndexDef,
detail: Option<&IndexDetailSpec>,
backing: Option<&ConstraintDef>,
) -> Self {
let kind = IndexKindSpec::resolve(detail, &index.property);
let relationship = detail.is_some_and(|detail| detail.relationship)
|| backing.is_some_and(|constraint| constraint.relationship);
if let Some(detail) = detail {
return Self {
kind,
relationship,
labels: detail.labels.clone(),
properties: detail.properties.clone(),
with_properties: detail.with_properties.clone(),
};
}
if let Some(constraint) = backing {
return Self {
kind,
relationship,
labels: vec![constraint.label.clone()],
properties: constraint.properties.clone(),
with_properties: Vec::new(),
};
}
Self {
kind,
relationship,
labels: vec![index.label.clone()],
properties: legacy_property_names(&index.property),
with_properties: Vec::new(),
}
}
}
#[doc(hidden)]
pub fn resolve_index_facts(
indexes: &[relate_catalog::IndexDef],
details: &std::collections::BTreeMap<String, IndexDetailSpec>,
constraints: &[relate_catalog::ConstraintDef],
) -> Vec<IndexFacts> {
indexes
.iter()
.map(|index| {
let detail = details.get(&index.name);
let backing = backing_constraint_by_name(&index.name, constraints);
IndexFacts::resolve(index, detail, backing)
})
.collect()
}
#[doc(hidden)]
pub fn backing_constraint_by_name<'a>(
name: &str,
constraints: &'a [relate_catalog::ConstraintDef],
) -> Option<&'a relate_catalog::ConstraintDef> {
constraints
.iter()
.find(|def| def.name == name && def.is_index_backed())
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IndexDetailSpec {
pub kind: IndexKindSpec,
pub relationship: bool,
#[serde(default)]
pub labels: Vec<String>,
#[serde(default)]
pub properties: Vec<String>,
#[serde(default)]
pub with_properties: Vec<String>,
}
impl IndexDetailSpec {
pub fn covered_pairs(&self) -> Vec<(&str, &str, IndexKindSpec)> {
if self.relationship || self.labels.len() != 1 || self.properties.is_empty() {
return Vec::new();
}
let label = self.labels[0].as_str();
match self.kind {
IndexKindSpec::Text | IndexKindSpec::Point if self.properties.len() == 1 => {
vec![(label, self.properties[0].as_str(), self.kind)]
}
IndexKindSpec::Range => self
.properties
.iter()
.map(|property| (label, property.as_str(), IndexKindSpec::Range))
.collect(),
_ => Vec::new(),
}
}
pub fn range_seek_pairs(&self) -> Vec<(&str, &str)> {
self.covered_pairs()
.into_iter()
.filter_map(|(label, property, kind)| {
(kind == IndexKindSpec::Range).then_some((label, property))
})
.collect()
}
}
pub fn project_index_details(details: &mut BTreeMap<String, IndexDetailSpec>, ops: &[RedoOp]) {
for op in ops {
match op {
RedoOp::CatalogIndexDetail { name, detail, .. } => {
details.insert(name.clone(), detail.clone());
}
RedoOp::DropIndex { name } => {
details.remove(name);
}
_ => {}
}
}
}