icydb-core 0.98.1

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! Module: model::index
//! Responsibility: runtime index metadata and expression-key contracts.
//! Does not own: index storage persistence or route-selection policy.
//! Boundary: authoritative index-level runtime model consumed by planner and executor code.

use crate::db::Predicate;
use std::fmt::{self, Display};

///
/// IndexExpression
///
/// Canonical deterministic expression key metadata for expression indexes.
/// This enum is semantic authority across schema/runtime/planner boundaries.
///
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IndexExpression {
    Lower(&'static str),
    Upper(&'static str),
    Trim(&'static str),
    LowerTrim(&'static str),
    Date(&'static str),
    Year(&'static str),
    Month(&'static str),
    Day(&'static str),
}

impl IndexExpression {
    /// Borrow the referenced field for this expression key item.
    #[must_use]
    pub const fn field(&self) -> &'static str {
        match self {
            Self::Lower(field)
            | Self::Upper(field)
            | Self::Trim(field)
            | Self::LowerTrim(field)
            | Self::Date(field)
            | Self::Year(field)
            | Self::Month(field)
            | Self::Day(field) => field,
        }
    }

    /// Return one stable discriminant for fingerprint hashing.
    #[must_use]
    pub const fn kind_tag(&self) -> u8 {
        match self {
            Self::Lower(_) => 0x01,
            Self::Upper(_) => 0x02,
            Self::Trim(_) => 0x03,
            Self::LowerTrim(_) => 0x04,
            Self::Date(_) => 0x05,
            Self::Year(_) => 0x06,
            Self::Month(_) => 0x07,
            Self::Day(_) => 0x08,
        }
    }

    /// Return whether planner/access Eq/In lookup lowering supports this expression
    /// under `TextCasefold` coercion in the current release.
    #[must_use]
    pub const fn supports_text_casefold_lookup(&self) -> bool {
        matches!(self, Self::Lower(_) | Self::Upper(_))
    }
}

impl Display for IndexExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Lower(field) => write!(f, "LOWER({field})"),
            Self::Upper(field) => write!(f, "UPPER({field})"),
            Self::Trim(field) => write!(f, "TRIM({field})"),
            Self::LowerTrim(field) => write!(f, "LOWER(TRIM({field}))"),
            Self::Date(field) => write!(f, "DATE({field})"),
            Self::Year(field) => write!(f, "YEAR({field})"),
            Self::Month(field) => write!(f, "MONTH({field})"),
            Self::Day(field) => write!(f, "DAY({field})"),
        }
    }
}

///
/// IndexKeyItem
///
/// Canonical index key-item metadata.
/// `Field` preserves field-key behavior.
/// `Expression` reserves deterministic expression-key identity metadata.
///
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IndexKeyItem {
    Field(&'static str),
    Expression(IndexExpression),
}

impl IndexKeyItem {
    /// Borrow this key-item's referenced field.
    #[must_use]
    pub const fn field(&self) -> &'static str {
        match self {
            Self::Field(field) => field,
            Self::Expression(expression) => expression.field(),
        }
    }

    /// Render one deterministic canonical text form for diagnostics/display.
    #[must_use]
    pub fn canonical_text(&self) -> String {
        match self {
            Self::Field(field) => (*field).to_string(),
            Self::Expression(expression) => expression.to_string(),
        }
    }
}

///
/// IndexKeyItemsRef
///
/// Borrowed view over index key-item metadata.
/// Field-only indexes use `Fields`; mixed/explicit key metadata uses `Items`.
///
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IndexKeyItemsRef {
    Fields(&'static [&'static str]),
    Items(&'static [IndexKeyItem]),
}

///
/// GeneratedIndexPredicateResolver
///
/// Generated filtered indexes resolve canonical predicate semantics through
/// one zero-argument function so runtime planning can borrow a shared static
/// AST without reparsing SQL text.
///
pub type GeneratedIndexPredicateResolver = fn() -> &'static Predicate;

///
/// IndexPredicateMetadata
///
/// Canonical generated filtered-index predicate metadata.
/// Raw SQL text is retained for diagnostics/display only.
/// Runtime semantics always flow through `semantics()`.
///
#[derive(Clone, Copy, Debug)]
pub struct IndexPredicateMetadata {
    sql: &'static str,
    semantics: GeneratedIndexPredicateResolver,
}

impl IndexPredicateMetadata {
    /// Build one generated filtered-index predicate metadata bundle.
    #[must_use]
    #[doc(hidden)]
    pub const fn generated(sql: &'static str, semantics: GeneratedIndexPredicateResolver) -> Self {
        Self { sql, semantics }
    }

    /// Borrow the original schema-declared predicate text for diagnostics.
    #[must_use]
    pub const fn sql(&self) -> &'static str {
        self.sql
    }

    /// Borrow the canonical generated predicate semantics.
    #[must_use]
    pub fn semantics(&self) -> &'static Predicate {
        (self.semantics)()
    }
}

impl PartialEq for IndexPredicateMetadata {
    fn eq(&self, other: &Self) -> bool {
        self.sql == other.sql && std::ptr::fn_addr_eq(self.semantics, other.semantics)
    }
}

impl Eq for IndexPredicateMetadata {}

///
/// IndexModel
///
/// Runtime-only descriptor for an index used by the executor and stores.
/// Keeps core decoupled from the schema `Index` shape.
/// Indexing is hash-based over `Value` equality for all variants.
/// Unique indexes enforce value equality; hash collisions surface as corruption.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct IndexModel {
    /// Stable per-entity ordinal used for runtime index identity.
    ordinal: u16,

    /// Stable index name used for diagnostics and planner identity.
    name: &'static str,
    store: &'static str,
    fields: &'static [&'static str],
    key_items: Option<&'static [IndexKeyItem]>,
    unique: bool,
    // Raw schema text remains for diagnostics/display only.
    // Runtime/planner semantics must use the generated canonical predicate AST.
    predicate: Option<IndexPredicateMetadata>,
}

impl IndexModel {
    /// Construct one generated index descriptor.
    ///
    /// This constructor exists for derive/codegen output and trusted test
    /// fixtures. Runtime planning and execution treat `IndexModel` values as
    /// build-time-validated metadata.
    #[must_use]
    #[doc(hidden)]
    pub const fn generated(
        name: &'static str,
        store: &'static str,
        fields: &'static [&'static str],
        unique: bool,
    ) -> Self {
        Self::generated_with_ordinal_and_key_items_and_predicate(
            0, name, store, fields, None, unique, None,
        )
    }

    /// Construct one index descriptor with one explicit stable ordinal.
    #[must_use]
    #[doc(hidden)]
    pub const fn generated_with_ordinal(
        ordinal: u16,
        name: &'static str,
        store: &'static str,
        fields: &'static [&'static str],
        unique: bool,
    ) -> Self {
        Self::generated_with_ordinal_and_key_items_and_predicate(
            ordinal, name, store, fields, None, unique, None,
        )
    }

    /// Construct one index descriptor with an optional conditional predicate.
    #[must_use]
    #[doc(hidden)]
    pub const fn generated_with_predicate(
        name: &'static str,
        store: &'static str,
        fields: &'static [&'static str],
        unique: bool,
        predicate: Option<IndexPredicateMetadata>,
    ) -> Self {
        Self::generated_with_ordinal_and_key_items_and_predicate(
            0, name, store, fields, None, unique, predicate,
        )
    }

    /// Construct one index descriptor with an explicit stable ordinal and optional predicate.
    #[must_use]
    #[doc(hidden)]
    pub const fn generated_with_ordinal_and_predicate(
        ordinal: u16,
        name: &'static str,
        store: &'static str,
        fields: &'static [&'static str],
        unique: bool,
        predicate: Option<IndexPredicateMetadata>,
    ) -> Self {
        Self::generated_with_ordinal_and_key_items_and_predicate(
            ordinal, name, store, fields, None, unique, predicate,
        )
    }

    /// Construct one index descriptor with explicit canonical key-item metadata.
    #[must_use]
    #[doc(hidden)]
    pub const fn generated_with_key_items(
        name: &'static str,
        store: &'static str,
        fields: &'static [&'static str],
        key_items: &'static [IndexKeyItem],
        unique: bool,
    ) -> Self {
        Self::generated_with_ordinal_and_key_items_and_predicate(
            0,
            name,
            store,
            fields,
            Some(key_items),
            unique,
            None,
        )
    }

    /// Construct one index descriptor with an explicit stable ordinal and key-item metadata.
    #[must_use]
    #[doc(hidden)]
    pub const fn generated_with_ordinal_and_key_items(
        ordinal: u16,
        name: &'static str,
        store: &'static str,
        fields: &'static [&'static str],
        key_items: &'static [IndexKeyItem],
        unique: bool,
    ) -> Self {
        Self::generated_with_ordinal_and_key_items_and_predicate(
            ordinal,
            name,
            store,
            fields,
            Some(key_items),
            unique,
            None,
        )
    }

    /// Construct one index descriptor with explicit key-item + predicate metadata.
    #[must_use]
    #[doc(hidden)]
    pub const fn generated_with_key_items_and_predicate(
        name: &'static str,
        store: &'static str,
        fields: &'static [&'static str],
        key_items: Option<&'static [IndexKeyItem]>,
        unique: bool,
        predicate: Option<IndexPredicateMetadata>,
    ) -> Self {
        Self::generated_with_ordinal_and_key_items_and_predicate(
            0, name, store, fields, key_items, unique, predicate,
        )
    }

    /// Construct one index descriptor with full explicit runtime identity metadata.
    #[must_use]
    #[doc(hidden)]
    pub const fn generated_with_ordinal_and_key_items_and_predicate(
        ordinal: u16,
        name: &'static str,
        store: &'static str,
        fields: &'static [&'static str],
        key_items: Option<&'static [IndexKeyItem]>,
        unique: bool,
        predicate: Option<IndexPredicateMetadata>,
    ) -> Self {
        Self {
            ordinal,
            name,
            store,
            fields,
            key_items,
            unique,
            predicate,
        }
    }

    /// Return the stable index name.
    #[must_use]
    pub const fn name(&self) -> &'static str {
        self.name
    }

    /// Return the stable per-entity index ordinal.
    #[must_use]
    pub const fn ordinal(&self) -> u16 {
        self.ordinal
    }

    /// Return the backing index store path.
    #[must_use]
    pub const fn store(&self) -> &'static str {
        self.store
    }

    /// Return the canonical index field list.
    #[must_use]
    pub const fn fields(&self) -> &'static [&'static str] {
        self.fields
    }

    /// Borrow canonical key-item metadata for this index.
    #[must_use]
    pub const fn key_items(&self) -> IndexKeyItemsRef {
        if let Some(items) = self.key_items {
            IndexKeyItemsRef::Items(items)
        } else {
            IndexKeyItemsRef::Fields(self.fields)
        }
    }

    /// Return whether this index includes expression key items.
    #[must_use]
    pub const fn has_expression_key_items(&self) -> bool {
        let Some(items) = self.key_items else {
            return false;
        };

        let mut index = 0usize;
        while index < items.len() {
            if matches!(items[index], IndexKeyItem::Expression(_)) {
                return true;
            }
            index = index.saturating_add(1);
        }

        false
    }

    /// Return whether the index enforces value uniqueness.
    #[must_use]
    pub const fn is_unique(&self) -> bool {
        self.unique
    }

    /// Return optional schema-declared conditional index predicate text metadata.
    ///
    /// Runtime planning and execution treat this as display metadata only.
    #[must_use]
    pub const fn predicate(&self) -> Option<&'static str> {
        match self.predicate {
            Some(predicate) => Some(predicate.sql()),
            None => None,
        }
    }

    /// Return the canonical generated conditional index predicate semantics.
    #[must_use]
    pub fn predicate_semantics(&self) -> Option<&'static Predicate> {
        self.predicate.map(|predicate| predicate.semantics())
    }

    /// Whether this index's field prefix matches the start of another index.
    #[must_use]
    pub fn is_prefix_of(&self, other: &Self) -> bool {
        self.fields().len() < other.fields().len() && other.fields().starts_with(self.fields())
    }

    fn joined_key_items(&self) -> String {
        match self.key_items() {
            IndexKeyItemsRef::Fields(fields) => fields.join(", "),
            IndexKeyItemsRef::Items(items) => {
                let mut joined = String::new();

                for item in items {
                    if !joined.is_empty() {
                        joined.push_str(", ");
                    }
                    joined.push_str(item.canonical_text().as_str());
                }

                joined
            }
        }
    }
}

impl Display for IndexModel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let fields = self.joined_key_items();
        if self.is_unique() {
            if let Some(predicate) = self.predicate() {
                write!(
                    f,
                    "{}: UNIQUE {}({}) WHERE {}",
                    self.name(),
                    self.store(),
                    fields,
                    predicate
                )
            } else {
                write!(f, "{}: UNIQUE {}({})", self.name(), self.store(), fields)
            }
        } else if let Some(predicate) = self.predicate() {
            write!(
                f,
                "{}: {}({}) WHERE {}",
                self.name(),
                self.store(),
                fields,
                predicate
            )
        } else {
            write!(f, "{}: {}({})", self.name(), self.store(), fields)
        }
    }
}

///
/// TESTS
///

#[cfg(test)]
mod tests {
    use crate::{
        db::Predicate,
        model::index::{
            IndexExpression, IndexKeyItem, IndexKeyItemsRef, IndexModel, IndexPredicateMetadata,
        },
    };
    use std::sync::LazyLock;

    static ACTIVE_TRUE_PREDICATE: LazyLock<Predicate> =
        LazyLock::new(|| Predicate::eq("active".to_string(), true.into()));

    fn active_true_predicate() -> &'static Predicate {
        &ACTIVE_TRUE_PREDICATE
    }

    #[test]
    fn index_model_with_predicate_exposes_predicate_metadata() {
        let model = IndexModel::generated_with_predicate(
            "users|email|active",
            "users::index",
            &["email"],
            false,
            Some(IndexPredicateMetadata::generated(
                "active = true",
                active_true_predicate,
            )),
        );

        assert_eq!(model.predicate(), Some("active = true"));
        assert_eq!(model.predicate_semantics(), Some(active_true_predicate()),);
        assert_eq!(
            model.to_string(),
            "users|email|active: users::index(email) WHERE active = true"
        );
    }

    #[test]
    fn index_model_without_predicate_preserves_display_shape() {
        let model = IndexModel::generated("users|email", "users::index", &["email"], true);

        assert_eq!(model.predicate(), None);
        assert_eq!(model.to_string(), "users|email: UNIQUE users::index(email)");
    }

    #[test]
    fn index_model_with_explicit_key_items_exposes_expression_items() {
        static KEY_ITEMS: [IndexKeyItem; 2] = [
            IndexKeyItem::Field("tenant_id"),
            IndexKeyItem::Expression(IndexExpression::Lower("email")),
        ];
        let model = IndexModel::generated_with_key_items(
            "users|tenant|email_expr",
            "users::index",
            &["tenant_id"],
            &KEY_ITEMS,
            false,
        );

        assert!(model.has_expression_key_items());
        assert_eq!(
            model.to_string(),
            "users|tenant|email_expr: users::index(tenant_id, LOWER(email))"
        );
        assert!(matches!(
            model.key_items(),
            IndexKeyItemsRef::Items(items)
                if items == KEY_ITEMS.as_slice()
        ));
    }

    #[test]
    fn index_expression_lookup_support_matrix_is_explicit() {
        assert!(IndexExpression::Lower("email").supports_text_casefold_lookup());
        assert!(IndexExpression::Upper("email").supports_text_casefold_lookup());
        assert!(!IndexExpression::Trim("email").supports_text_casefold_lookup());
        assert!(!IndexExpression::LowerTrim("email").supports_text_casefold_lookup());
        assert!(!IndexExpression::Date("created_at").supports_text_casefold_lookup());
        assert!(!IndexExpression::Year("created_at").supports_text_casefold_lookup());
        assert!(!IndexExpression::Month("created_at").supports_text_casefold_lookup());
        assert!(!IndexExpression::Day("created_at").supports_text_casefold_lookup());
    }
}