icydb-core 0.212.3

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
use crate::{
    db::{
        Predicate,
        schema::{
            AcceptedConstraintKind, AcceptedFieldKind, FieldId, PersistedIndexExpressionOp,
            PersistedIndexKeyItemSnapshot, PersistedIndexKeySnapshot, SchemaFieldSlot,
            SchemaInsertDefault, SchemaVersion, compiled_schema_proposal_for_model,
        },
    },
    model::{
        entity::{CheckConstraintModel, EntityModel, PrimaryKeyModel, RelationEdgeModel},
        field::{
            CompositeCodec, CompositeFieldModel, CompositeShapeModel, EnumVariantModel,
            FieldDatabaseDefault, FieldKind, FieldModel, FieldStorageDecode, LeafCodec,
            ScalarCodec,
        },
        index::{IndexExpression, IndexKeyItem, IndexModel},
    },
    testing::entity_model_from_static,
    value::Value,
};
use std::sync::LazyLock;

static PROFILE_COMPOSITE_FIELDS: [CompositeFieldModel; 2] = [
    CompositeFieldModel::generated("score", FieldKind::Nat64, false),
    CompositeFieldModel::generated("nickname", FieldKind::Text { max_len: None }, false),
];
static PROFILE_COMPOSITE_SHAPE: CompositeShapeModel =
    CompositeShapeModel::Record(&PROFILE_COMPOSITE_FIELDS);
static PROFILE_COMPOSITE_KIND: FieldKind = FieldKind::Composite {
    path: "schema::proposal::tests::Profile",
    codec: CompositeCodec::StructuralV1,
    shape: &PROFILE_COMPOSITE_SHAPE,
};
static FIELDS: [FieldModel; 4] = [
    FieldModel::generated("id", FieldKind::Ulid),
    FieldModel::generated_with_storage_decode_and_nullability(
        "name",
        FieldKind::Text { max_len: None },
        FieldStorageDecode::ByKind,
        true,
    ),
    FieldModel::generated("rank", FieldKind::Nat64),
    FieldModel::generated_with_storage_decode_and_nullability(
        "profile",
        PROFILE_COMPOSITE_KIND,
        FieldStorageDecode::CatalogValue,
        false,
    ),
];
static NAME_INDEX: IndexModel =
    IndexModel::generated_with_ordinal(1, "idx_entity__name", "entity::name", &["name"], false);
static PROFILE_NICKNAME_INDEX: IndexModel = IndexModel::generated_with_ordinal(
    2,
    "idx_entity__profile_nickname",
    "entity::profile_nickname",
    &["profile.nickname"],
    false,
);
static EXPRESSION_KEY_ITEMS: [IndexKeyItem; 1] =
    [IndexKeyItem::Expression(IndexExpression::Lower("name"))];
static EXPRESSION_INDEX: IndexModel = IndexModel::generated_with_ordinal_and_key_items(
    3,
    "idx_entity__lower_name",
    "entity::lower_name",
    &["name"],
    &EXPRESSION_KEY_ITEMS,
    false,
);
static INDEXES: [&IndexModel; 3] = [&NAME_INDEX, &PROFILE_NICKNAME_INDEX, &EXPRESSION_INDEX];
static RELATION_LOCAL_FIELDS: [&FieldModel; 1] = [&FIELDS[2]];
static RELATIONS: [RelationEdgeModel; 1] = [RelationEdgeModel::generated(
    "score_owner",
    "schema::proposal::tests::ScoreOwner",
    &RELATION_LOCAL_FIELDS,
)];
static MODEL: EntityModel = entity_model_from_static(
    "schema::proposal::tests::Entity",
    "Entity",
    &FIELDS[0],
    0,
    &FIELDS,
    &INDEXES,
);
static RELATION_MODEL: EntityModel = EntityModel::generated_with_primary_key_model_and_relations(
    "schema::proposal::tests::RelationEntity",
    "RelationEntity",
    1,
    PrimaryKeyModel::scalar(&FIELDS[0]),
    0,
    &FIELDS,
    &INDEXES,
    &RELATIONS,
);
static UNIQUE_NAME_INDEX: IndexModel = IndexModel::generated_with_ordinal(
    1,
    "idx_entity__unique_name",
    "entity::unique_name",
    &["name"],
    true,
);
static STRUCTURAL_INDEXES: [&IndexModel; 1] = [&UNIQUE_NAME_INDEX];
static STRUCTURAL_MODEL: EntityModel = EntityModel::generated_with_primary_key_model_and_relations(
    "schema::proposal::tests::StructuralEntity",
    "StructuralEntity",
    1,
    PrimaryKeyModel::scalar(&FIELDS[0]),
    0,
    &FIELDS,
    &STRUCTURAL_INDEXES,
    &RELATIONS,
);
fn generated_rank_check() -> &'static Predicate {
    static CHECK: LazyLock<Predicate> =
        LazyLock::new(|| Predicate::gte("rank".to_string(), Value::Int64(0)));

    &CHECK
}
static GENERATED_CHECKS: [CheckConstraintModel; 1] = [CheckConstraintModel::generated(
    "rank_nonnegative",
    "rank >= 0",
    generated_rank_check,
)];
static CHECK_MODEL: EntityModel =
    EntityModel::generated_with_primary_key_model_relations_and_checks(
        "schema::proposal::tests::CheckEntity",
        "CheckEntity",
        1,
        PrimaryKeyModel::scalar(&FIELDS[0]),
        0,
        &FIELDS,
        &INDEXES,
        &[],
        &GENERATED_CHECKS,
    );
static VERSIONED_MODEL: EntityModel = EntityModel::generated(
    "schema::proposal::tests::VersionedEntity",
    "VersionedEntity",
    4,
    &FIELDS[0],
    0,
    &FIELDS,
    &INDEXES,
);
static COMPOSITE_PRIMARY_KEY_FIELDS: [&FieldModel; 2] = [&FIELDS[0], &FIELDS[2]];
static COMPOSITE_MODEL: EntityModel = EntityModel::generated_with_primary_key_model(
    "schema::proposal::tests::CompositeEntity",
    "CompositeEntity",
    1,
    PrimaryKeyModel::ordered(&COMPOSITE_PRIMARY_KEY_FIELDS),
    0,
    &FIELDS,
    &INDEXES,
);
static STATUS_VARIANTS: [EnumVariantModel; 3] = [
    EnumVariantModel::new("Active", None, FieldStorageDecode::ByKind),
    EnumVariantModel::new(
        "Loaded",
        Some(&FieldKind::Nat64),
        FieldStorageDecode::ByKind,
    ),
    EnumVariantModel::new("Paused", None, FieldStorageDecode::ByKind),
];
const STATUS_KIND: FieldKind = FieldKind::Enum {
    path: "schema::proposal::tests::Status",
    variants: &STATUS_VARIANTS,
};
static ENUM_DEFAULT_FIELDS: [FieldModel; 2] = [
    FieldModel::generated("id", FieldKind::Ulid),
    FieldModel::generated_with_storage_decode_nullability_write_policies_database_default_and_nested_fields(
        "status",
        STATUS_KIND,
        FieldStorageDecode::ByKind,
        false,
        None,
        None,
        FieldDatabaseDefault::AuthoredEnumUnit {
            enum_path: "schema::proposal::tests::Status",
            variant: "Active",
        },
        &[],
    ),
];
static ENUM_DEFAULT_MODEL: EntityModel = entity_model_from_static(
    "schema::proposal::tests::EnumDefaultEntity",
    "EnumDefaultEntity",
    &ENUM_DEFAULT_FIELDS[0],
    0,
    &ENUM_DEFAULT_FIELDS,
    &[],
);

#[test]
fn compiled_schema_proposal_assigns_initial_field_ids_from_slots() {
    let proposal = compiled_schema_proposal_for_model(&MODEL);

    assert_eq!(proposal.entity_path(), "schema::proposal::tests::Entity");
    assert_eq!(proposal.entity_name(), "Entity");
    assert_eq!(proposal.declared_schema_version(), SchemaVersion::initial());
    assert_eq!(proposal.first_primary_key_field_id(), FieldId::new(1));
    assert_eq!(proposal.primary_key_field_ids(), &[FieldId::new(1)]);
    assert_eq!(proposal.fields().len(), 4);
    assert_eq!(
        proposal.indexes().len(),
        3,
        "field-path and expression indexes should both have accepted-index proposals",
    );

    let ids = proposal
        .fields()
        .iter()
        .map(super::CompiledFieldProposal::id)
        .collect::<Vec<_>>();
    assert_eq!(
        ids,
        vec![
            FieldId::new(1),
            FieldId::new(2),
            FieldId::new(3),
            FieldId::new(4),
        ],
    );
}

#[test]
fn compiled_enum_default_persists_catalog_ids() {
    let proposal = compiled_schema_proposal_for_model(&ENUM_DEFAULT_MODEL);
    let (catalog, composite_catalog) =
        crate::db::schema::build_initial_accepted_catalogs_for_tests(&[&ENUM_DEFAULT_MODEL])
            .expect("generated accepted catalogs should build");
    let snapshot = proposal
        .initial_persisted_schema_snapshot_with_catalogs(&catalog, &composite_catalog)
        .expect("authored enum default should admit through its store catalog");
    let field = &snapshot.fields()[1];
    let payload = field
        .insert_default()
        .slot_payload()
        .expect("accepted enum default should persist one slot payload");

    assert_eq!(payload.first(), Some(&0x84));
    let contract = crate::db::schema::AcceptedFieldDecodeContract::new(
        field.name(),
        field.kind(),
        field.nullable(),
        field.storage_decode(),
        field.leaf_codec(),
    );
    crate::db::data::validate_default_payload_for_accepted_field_contract(
        &catalog,
        &composite_catalog,
        contract,
        payload,
    )
    .expect("generated enum default bytes should satisfy bundle validation");
}

#[test]
fn compiled_enum_default_rejects_unknown_and_payload_variants() {
    let catalog = crate::db::schema::enum_catalog::build_initial_accepted_enum_catalog(&[
        &ENUM_DEFAULT_MODEL,
    ])
    .expect("generated enum catalog should build");
    let kind = crate::db::schema::enum_catalog::resolve_model_field_kind(&catalog, STATUS_KIND)
        .expect("test enum kind should resolve through its catalog");

    for variant in ["Missing", "Loaded"] {
        let field = super::CompiledFieldProposal {
            id: FieldId::new(2),
            name: "status",
            slot: SchemaFieldSlot::new(1),
            kind: STATUS_KIND,
            nested_leaves: Vec::new(),
            nullable: false,
            database_default: FieldDatabaseDefault::AuthoredEnumUnit {
                enum_path: "schema::proposal::tests::Status",
                variant,
            },
            write_policy: crate::db::schema::SchemaFieldWritePolicy::none(),
            storage_decode: FieldStorageDecode::ByKind,
            leaf_codec: LeafCodec::Structural,
        };

        assert!(
            field.persisted_database_default(&catalog, &kind).is_err(),
            "{variant} must reject as an authored unit-enum default",
        );
    }
}

#[test]
fn compiled_schema_proposal_carries_declared_schema_version() {
    let proposal = compiled_schema_proposal_for_model(&VERSIONED_MODEL);
    let snapshot = proposal.initial_persisted_schema_snapshot();

    assert_eq!(proposal.declared_schema_version(), SchemaVersion::new(4));
    assert_eq!(
        proposal.initial_row_layout().current_version(),
        crate::db::schema::RowLayoutVersion::INITIAL
    );
    assert_eq!(snapshot.version(), SchemaVersion::new(4));
    assert_eq!(
        snapshot.row_layout().current_version(),
        crate::db::schema::RowLayoutVersion::INITIAL
    );
}

#[test]
fn compiled_schema_proposal_preserves_ordered_primary_key_field_ids() {
    let proposal = compiled_schema_proposal_for_model(&COMPOSITE_MODEL);

    assert_eq!(proposal.first_primary_key_field_id(), FieldId::new(1));
    assert_eq!(
        proposal.primary_key_field_ids(),
        &[FieldId::new(1), FieldId::new(3)],
    );
}

#[test]
fn compiled_schema_proposal_preserves_generated_relation_edges() {
    let proposal = compiled_schema_proposal_for_model(&RELATION_MODEL);

    assert_eq!(proposal.relations().len(), 1);
    assert_eq!(proposal.relations()[0].id().get(), 1);
    assert_eq!(proposal.relations()[0].name(), "score_owner");
    assert_eq!(
        proposal.relations()[0].target_path(),
        "schema::proposal::tests::ScoreOwner"
    );
    assert_eq!(
        proposal.relations()[0].local_field_ids(),
        &[FieldId::new(3)]
    );

    let snapshot = proposal.initial_persisted_schema_snapshot();
    assert_eq!(snapshot.relations().len(), 1);
    assert_eq!(snapshot.relations()[0].id().get(), 1);
    assert_eq!(snapshot.relations()[0].name(), "score_owner");
    assert_eq!(
        snapshot.relations()[0].target_path(),
        "schema::proposal::tests::ScoreOwner"
    );
    assert_eq!(
        snapshot.relations()[0].local_field_ids(),
        &[FieldId::new(3)]
    );
}

#[test]
fn initial_constraint_registry_is_deterministic_and_complete() {
    let snapshot =
        compiled_schema_proposal_for_model(&STRUCTURAL_MODEL).initial_persisted_schema_snapshot();
    let constraints = snapshot.constraints();

    assert_eq!(snapshot.constraint_id_allocator().high_water(), 6);
    assert_eq!(
        constraints
            .iter()
            .map(|constraint| (constraint.id().get(), constraint.name()))
            .collect::<Vec<_>>(),
        vec![
            (1, "__icydb_primary_key"),
            (2, "__icydb_not_null_1"),
            (3, "__icydb_not_null_3"),
            (4, "__icydb_not_null_4"),
            (5, "idx_entity__unique_name"),
            (6, "score_owner"),
        ],
    );
}

#[test]
fn generated_check_proposal_binds_to_accepted_field_identity_and_literal_codec() {
    let snapshot =
        compiled_schema_proposal_for_model(&CHECK_MODEL).initial_persisted_schema_snapshot();
    let check = snapshot
        .constraints()
        .iter()
        .find(|constraint| constraint.name() == "rank_nonnegative")
        .expect("generated check should enter the initial accepted catalog");

    assert_eq!(snapshot.constraint_id_allocator().high_water(), 5);
    let AcceptedConstraintKind::Check { expression } = check.kind() else {
        panic!("generated named check must bind as accepted check semantics");
    };
    assert_eq!(expression.dependencies(), vec![FieldId::new(3)]);
}

#[test]
fn compiled_schema_proposal_preserves_generated_storage_contracts() {
    let proposal = compiled_schema_proposal_for_model(&MODEL);
    let name = &proposal.fields()[1];

    assert_eq!(name.name(), "name");
    assert_eq!(name.slot(), SchemaFieldSlot::from_generated_index(1));
    std::assert_matches!(name.kind(), FieldKind::Text { max_len: None });
    assert!(name.nullable());
    assert_eq!(name.database_default(), FieldDatabaseDefault::None);
    assert_eq!(name.storage_decode(), FieldStorageDecode::ByKind);
    assert_eq!(name.leaf_codec(), LeafCodec::Scalar(ScalarCodec::Text));
}

#[test]
fn compiled_schema_proposal_builds_initial_row_layout() {
    let proposal = compiled_schema_proposal_for_model(&MODEL);
    let layout = proposal.initial_row_layout();

    assert_eq!(
        layout.current_version(),
        crate::db::schema::RowLayoutVersion::INITIAL
    );
    assert_eq!(
        layout.field_to_slot(),
        &[
            (FieldId::new(1), SchemaFieldSlot::from_generated_index(0)),
            (FieldId::new(2), SchemaFieldSlot::from_generated_index(1)),
            (FieldId::new(3), SchemaFieldSlot::from_generated_index(2)),
            (FieldId::new(4), SchemaFieldSlot::from_generated_index(3)),
        ]
    );
}

#[test]
fn compiled_schema_proposal_builds_initial_persisted_snapshot() {
    let proposal = compiled_schema_proposal_for_model(&MODEL);
    let snapshot = proposal.initial_persisted_schema_snapshot();

    assert_eq!(snapshot.version(), SchemaVersion::initial());
    assert_eq!(snapshot.entity_path(), "schema::proposal::tests::Entity");
    assert_eq!(snapshot.entity_name(), "Entity");
    assert_eq!(snapshot.first_primary_key_field_id(), FieldId::new(1));
    assert_eq!(snapshot.fields().len(), 4);
    assert_eq!(snapshot.indexes().len(), 3);

    let name = &snapshot.fields()[1];
    assert_eq!(name.id(), FieldId::new(2));
    assert_eq!(name.name(), "name");
    assert_eq!(name.slot(), SchemaFieldSlot::from_generated_index(1));
    std::assert_matches!(name.kind(), AcceptedFieldKind::Text { max_len: None });
    assert!(name.nullable());
    assert_eq!(name.insert_default(), &SchemaInsertDefault::None);
    assert_eq!(name.storage_decode(), FieldStorageDecode::ByKind);
    assert_eq!(name.leaf_codec(), LeafCodec::Scalar(ScalarCodec::Text));

    let profile = &snapshot.fields()[3];
    assert_eq!(profile.name(), "profile");
    assert_eq!(profile.nested_leaves().len(), 2);
    assert_eq!(profile.nested_leaves()[0].path(), &["nickname".to_string()],);
    std::assert_matches!(
        profile.nested_leaves()[0].kind(),
        AcceptedFieldKind::Text { max_len: None }
    );
    assert_eq!(profile.nested_leaves()[1].path(), &["score".to_string()]);
    std::assert_matches!(profile.nested_leaves()[1].kind(), AcceptedFieldKind::Nat64);

    let name_index = &snapshot.indexes()[0];
    assert_eq!(name_index.schema_id().get(), 1);
    assert_eq!(name_index.ordinal(), 1);
    assert_eq!(name_index.name(), "idx_entity__name");
    assert!(!name_index.unique());
    assert_eq!(name_index.key().field_paths().len(), 1);
    assert_eq!(
        name_index.key().field_paths()[0].field_id(),
        FieldId::new(2)
    );
    assert_eq!(
        name_index.key().field_paths()[0].slot(),
        SchemaFieldSlot::from_generated_index(1)
    );
    assert_eq!(
        name_index.key().field_paths()[0].path(),
        &["name".to_string()]
    );

    let nested_index = &snapshot.indexes()[1];
    assert_eq!(nested_index.schema_id().get(), 2);
    assert_eq!(nested_index.name(), "idx_entity__profile_nickname");
    assert_eq!(
        nested_index.key().field_paths()[0].field_id(),
        FieldId::new(4)
    );
    assert_eq!(
        nested_index.key().field_paths()[0].path(),
        &["profile".to_string(), "nickname".to_string()]
    );
    std::assert_matches!(
        nested_index.key().field_paths()[0].kind(),
        AcceptedFieldKind::Text { max_len: None }
    );

    let expression_index = &snapshot.indexes()[2];
    assert_eq!(expression_index.schema_id().get(), 3);
    assert_eq!(expression_index.ordinal(), 3);
    assert_eq!(expression_index.name(), "idx_entity__lower_name");
    let PersistedIndexKeySnapshot::Items(items) = expression_index.key() else {
        panic!("expression index should preserve explicit key items");
    };
    assert_eq!(items.len(), 1);
    let PersistedIndexKeyItemSnapshot::Expression(expression) = &items[0] else {
        panic!("expression index key should persist an accepted expression item");
    };
    assert_eq!(expression.op(), PersistedIndexExpressionOp::Lower);
    assert_eq!(expression.source().field_id(), FieldId::new(2));
    assert_eq!(
        expression.source().slot(),
        SchemaFieldSlot::from_generated_index(1)
    );
    assert_eq!(expression.source().path(), &["name".to_string()]);
    std::assert_matches!(
        expression.input_kind(),
        AcceptedFieldKind::Text { max_len: None }
    );
    std::assert_matches!(
        expression.output_kind(),
        AcceptedFieldKind::Text { max_len: None }
    );
    assert_eq!(expression.canonical_text(), "expr:v1:LOWER(name)");
}