Skip to main content

icydb_core/model/
index.rs

1use std::fmt::{self, Display};
2
3///
4/// IndexModel
5///
6/// Runtime-only descriptor for an index used by the executor and stores.
7/// Keeps core decoupled from the schema `Index` shape.
8/// Indexing is hash-based over `Value` equality for all variants.
9/// Unique indexes enforce value equality; hash collisions surface as corruption.
10///
11
12#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13pub struct IndexModel {
14    /// Stable index name used for diagnostics and planner identity.
15    pub name: &'static str,
16    pub store: &'static str,
17    pub fields: &'static [&'static str],
18    pub unique: bool,
19}
20
21impl IndexModel {
22    #[must_use]
23    pub const fn new(
24        name: &'static str,
25        store: &'static str,
26        fields: &'static [&'static str],
27        unique: bool,
28    ) -> Self {
29        Self {
30            name,
31            store,
32            fields,
33            unique,
34        }
35    }
36
37    #[must_use]
38    /// Whether this index's field prefix matches the start of another index.
39    pub fn is_prefix_of(&self, other: &Self) -> bool {
40        self.fields.len() < other.fields.len() && other.fields.starts_with(self.fields)
41    }
42}
43
44impl Display for IndexModel {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        let fields = self.fields.join(", ");
47
48        if self.unique {
49            write!(f, "{}: UNIQUE {}({})", self.name, self.store, fields)
50        } else {
51            write!(f, "{}: {}({})", self.name, self.store, fields)
52        }
53    }
54}