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    name: &'static str,
16    store: &'static str,
17    fields: &'static [&'static str],
18    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    /// Return the stable index name.
38    #[must_use]
39    pub const fn name(&self) -> &'static str {
40        self.name
41    }
42
43    /// Return the backing index store path.
44    #[must_use]
45    pub const fn store(&self) -> &'static str {
46        self.store
47    }
48
49    /// Return the canonical index field list.
50    #[must_use]
51    pub const fn fields(&self) -> &'static [&'static str] {
52        self.fields
53    }
54
55    /// Return whether the index enforces value uniqueness.
56    #[must_use]
57    pub const fn is_unique(&self) -> bool {
58        self.unique
59    }
60
61    #[must_use]
62    /// Whether this index's field prefix matches the start of another index.
63    pub fn is_prefix_of(&self, other: &Self) -> bool {
64        self.fields().len() < other.fields().len() && other.fields().starts_with(self.fields())
65    }
66}
67
68impl Display for IndexModel {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        let fields = self.fields().join(", ");
71
72        if self.is_unique() {
73            write!(f, "{}: UNIQUE {}({})", self.name(), self.store(), fields)
74        } else {
75            write!(f, "{}: {}({})", self.name(), self.store(), fields)
76        }
77    }
78}