use hawdb_core::schema::{Catalog, IndexKind, PropertyType, TableKind};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct OptimizerSchemaKey {
labels: Vec<String>,
unique_constraints: Vec<(hawdb_core::schema::ConstraintSubject, String)>,
relationship_types: Vec<String>,
equality_indexes: Vec<(String, String)>,
range_indexes: Vec<(String, String)>,
full_text_indexes: Vec<(String, String)>,
composite_indexes: Vec<(String, Vec<String>)>,
property_descriptors: Vec<SchemaPropertyDescriptor>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct SchemaPropertyDescriptor {
table: String,
table_kind: TableKind,
property: String,
value_type: PropertyType,
nullable: bool,
}
impl OptimizerSchemaKey {
pub fn from_catalog(catalog: &Catalog) -> Self {
let labels = catalog.labels().map(|label| label.name.clone()).collect();
let relationship_types = catalog
.rel_types()
.map(|rel_type| rel_type.name.clone())
.collect();
let mut equality_indexes = Vec::new();
let mut range_indexes = Vec::new();
let mut full_text_indexes = Vec::new();
for index in catalog.property_indexes() {
let Some(label) = catalog.label_name(index.label_id) else {
continue;
};
let descriptor = (label.to_string(), index.property.clone());
match index.kind {
IndexKind::Equality => equality_indexes.push(descriptor),
IndexKind::Range => range_indexes.push(descriptor),
IndexKind::FullText => full_text_indexes.push(descriptor),
}
}
let composite_indexes = catalog
.composite_property_indexes()
.filter_map(|index| {
catalog
.label_name(index.label_id)
.map(|label| (label.to_string(), index.properties.clone()))
})
.collect();
let mut property_descriptors = catalog
.property_descriptors()
.filter_map(|property| {
let table = catalog.table_descriptor(property.table_id)?;
Some(SchemaPropertyDescriptor {
table: table.name.clone(),
table_kind: table.kind,
property: property.name.clone(),
value_type: property.value_type,
nullable: property.nullable,
})
})
.collect::<Vec<_>>();
property_descriptors.sort();
Self {
unique_constraints: catalog
.unique_constraints()
.map(|constraint| (constraint.subject, constraint.property.clone()))
.collect(),
labels,
relationship_types,
equality_indexes,
range_indexes,
full_text_indexes,
composite_indexes,
property_descriptors,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use hawdb_core::schema::SchemaObjectState;
#[test]
fn schema_key_ignores_descriptor_lifecycle_states() {
let mut catalog = Catalog::default();
let table = catalog.get_or_create_table(TableKind::Node, "T");
let property = catalog.get_or_create_property(table, "body", PropertyType::String, true);
let baseline = OptimizerSchemaKey::from_catalog(&catalog);
let mut churned = catalog.clone();
churned.set_property_state(property, SchemaObjectState::Backfill);
churned.set_table_state(table, SchemaObjectState::Validating);
assert_eq!(baseline, OptimizerSchemaKey::from_catalog(&churned));
let mut extended = catalog.clone();
extended.get_or_create_property(table, "title", PropertyType::Text, false);
assert_ne!(baseline, OptimizerSchemaKey::from_catalog(&extended));
}
}
hawdb_core::impl_retained_heap_for_struct!(OptimizerSchemaKey {
labels,
unique_constraints,
relationship_types,
equality_indexes,
range_indexes,
full_text_indexes,
composite_indexes,
property_descriptors
});
hawdb_core::impl_retained_heap_for_struct!(SchemaPropertyDescriptor {
table,
table_kind,
property,
value_type,
nullable
});