icydb-core 0.211.2

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
use super::{BoundSqlDdlRequest, BoundSqlDdlStatement};
use crate::db::sql::ddl::index::ddl_key_item_report;
use crate::error::ConstraintDiagnostic;

///
/// SqlDdlPreparationReport
///
/// Compact report for a DDL command that has passed all pre-execution
/// frontend and schema-mutation checks.
///
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SqlDdlPreparationReport {
    mutation_kind: SqlDdlMutationKind,
    target_index: String,
    target_store: String,
    field_path: Vec<String>,
    execution_status: SqlDdlExecutionStatus,
    rows_scanned: usize,
    index_keys_written: usize,
    constraint_validation: Option<SqlConstraintValidationPage>,
}

impl SqlDdlPreparationReport {
    /// Return the prepared DDL mutation kind.
    #[must_use]
    pub const fn mutation_kind(&self) -> SqlDdlMutationKind {
        self.mutation_kind
    }

    /// Borrow the target accepted index name.
    #[must_use]
    pub const fn target_index(&self) -> &str {
        self.target_index.as_str()
    }

    /// Borrow the target accepted index store path.
    #[must_use]
    pub const fn target_store(&self) -> &str {
        self.target_store.as_str()
    }

    /// Borrow the target field path.
    #[must_use]
    pub const fn field_path(&self) -> &[String] {
        self.field_path.as_slice()
    }

    /// Return the execution status captured by this DDL report.
    #[must_use]
    pub const fn execution_status(&self) -> SqlDdlExecutionStatus {
        self.execution_status
    }

    /// Return rows scanned by DDL execution.
    #[must_use]
    pub const fn rows_scanned(&self) -> usize {
        self.rows_scanned
    }

    /// Return index keys written by DDL execution.
    #[must_use]
    pub const fn index_keys_written(&self) -> usize {
        self.index_keys_written
    }

    /// Borrow the bounded constraint-validation page, when this DDL advanced one.
    #[must_use]
    pub const fn constraint_validation(&self) -> Option<&SqlConstraintValidationPage> {
        self.constraint_validation.as_ref()
    }

    pub(in crate::db) const fn with_execution_status(
        mut self,
        execution_status: SqlDdlExecutionStatus,
    ) -> Self {
        self.execution_status = execution_status;
        self
    }

    pub(in crate::db) const fn with_execution_metrics(
        mut self,
        rows_scanned: usize,
        index_keys_written: usize,
    ) -> Self {
        self.rows_scanned = rows_scanned;
        self.index_keys_written = index_keys_written;
        self
    }

    pub(in crate::db) fn with_constraint_validation(
        mut self,
        constraint_validation: SqlConstraintValidationPage,
    ) -> Self {
        self.constraint_validation = Some(constraint_validation);
        self
    }
}

/// Current engine-owned state of one bounded constraint-validation job.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SqlConstraintValidationState {
    Forward,
    Verify,
    Restarted,
    Validated,
}

impl SqlConstraintValidationState {
    /// Return the stable outward state label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Forward => "forward",
            Self::Verify => "verify",
            Self::Restarted => "restarted",
            Self::Validated => "validated",
        }
    }
}

/// Revision proof status associated with one validation response.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SqlConstraintValidationRevisionStatus {
    Tracking,
    Captured,
    Invalidated,
    Complete,
}

impl SqlConstraintValidationRevisionStatus {
    /// Return the stable outward revision-status label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Tracking => "tracking",
            Self::Captured => "captured",
            Self::Invalidated => "invalidated",
            Self::Complete => "complete",
        }
    }
}

/// Typed response for one bounded `VALIDATE CONSTRAINT` step.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SqlConstraintValidationPage {
    constraint_id: u32,
    activation_epoch: Option<u64>,
    page_sequence: Option<u64>,
    state: SqlConstraintValidationState,
    revision_status: SqlConstraintValidationRevisionStatus,
    rows_scanned: u64,
    findings: Vec<ConstraintDiagnostic>,
    complete: bool,
}

impl SqlConstraintValidationPage {
    /// Build the terminal response for an already or newly validated constraint.
    #[must_use]
    pub(in crate::db) const fn validated(constraint_id: u32, rows_scanned: u64) -> Self {
        Self {
            constraint_id,
            activation_epoch: None,
            page_sequence: None,
            state: SqlConstraintValidationState::Validated,
            revision_status: SqlConstraintValidationRevisionStatus::Complete,
            rows_scanned,
            findings: Vec::new(),
            complete: true,
        }
    }

    /// Build one non-terminal response from the durable job snapshot.
    #[must_use]
    pub(in crate::db) const fn pending(
        constraint_id: u32,
        activation_epoch: u64,
        state: SqlConstraintValidationState,
        revision_status: SqlConstraintValidationRevisionStatus,
        rows_scanned: u64,
    ) -> Self {
        Self {
            constraint_id,
            activation_epoch: Some(activation_epoch),
            page_sequence: None,
            state,
            revision_status,
            rows_scanned,
            findings: Vec::new(),
            complete: false,
        }
    }

    pub(in crate::db) fn with_findings(
        mut self,
        page_sequence: u64,
        findings: Vec<ConstraintDiagnostic>,
    ) -> Self {
        self.page_sequence = Some(page_sequence);
        self.findings = findings;
        self
    }

    /// Return the accepted constraint identity.
    #[must_use]
    pub const fn constraint_id(&self) -> u32 {
        self.constraint_id
    }

    /// Return the durable activation epoch, when validation remains active.
    #[must_use]
    pub const fn activation_epoch(&self) -> Option<u64> {
        self.activation_epoch
    }

    /// Return the acknowledgement identity of a retained finding page.
    #[must_use]
    pub const fn page_sequence(&self) -> Option<u64> {
        self.page_sequence
    }

    /// Return the current bounded validation state.
    #[must_use]
    pub const fn state(&self) -> SqlConstraintValidationState {
        self.state
    }

    /// Return the current revision-proof status.
    #[must_use]
    pub const fn revision_status(&self) -> SqlConstraintValidationRevisionStatus {
        self.revision_status
    }

    /// Return the cumulative classified-row count for this job.
    #[must_use]
    pub const fn rows_scanned(&self) -> u64 {
        self.rows_scanned
    }

    /// Borrow the retained bounded finding page.
    #[must_use]
    pub const fn findings(&self) -> &[ConstraintDiagnostic] {
        self.findings.as_slice()
    }

    /// Return whether validation and promotion are complete.
    #[must_use]
    pub const fn complete(&self) -> bool {
        self.complete
    }
}

///
/// SqlDdlMutationKind
///
/// Developer-facing SQL DDL mutation kind.
///
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SqlDdlMutationKind {
    AddCheckConstraint,
    AddDefaultedField,
    AddField,
    SetFieldDefault,
    DropFieldDefault,
    SetFieldNotNull,
    DropFieldNotNull,
    DropField,
    RenameField,
    AddFieldPathIndex,
    AddExpressionIndex,
    DropSecondaryIndex,
    DropCheckConstraint,
    ValidateConstraint,
}

impl SqlDdlMutationKind {
    /// Return the stable diagnostic label for this DDL mutation kind.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::AddCheckConstraint => "add_check_constraint",
            Self::AddDefaultedField => "add_defaulted_field",
            Self::AddField => "add_field",
            Self::SetFieldDefault => "set_field_default",
            Self::DropFieldDefault => "drop_field_default",
            Self::SetFieldNotNull => "set_field_not_null",
            Self::DropFieldNotNull => "drop_field_not_null",
            Self::DropField => "drop_field",
            Self::RenameField => "rename_field",
            Self::AddFieldPathIndex => "add_field_path_index",
            Self::AddExpressionIndex => "add_expression_index",
            Self::DropSecondaryIndex => "drop_secondary_index",
            Self::DropCheckConstraint => "drop_check_constraint",
            Self::ValidateConstraint => "validate_constraint",
        }
    }
}

///
/// SqlDdlExecutionStatus
///
/// SQL DDL execution state at the current boundary.
///
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SqlDdlExecutionStatus {
    PreparedOnly,
    Published,
    ActivationPublished,
    ValidationStarted,
    ValidationAdvanced,
    ValidationFindings,
    ValidationRestarted,
    Validated,
    NoOp,
}

impl SqlDdlExecutionStatus {
    /// Return the stable diagnostic label for this execution status.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::PreparedOnly => "prepared_only",
            Self::Published => "published",
            Self::ActivationPublished => "activation_published",
            Self::ValidationStarted => "validation_started",
            Self::ValidationAdvanced => "validation_advanced",
            Self::ValidationFindings => "validation_findings",
            Self::ValidationRestarted => "validation_restarted",
            Self::Validated => "validated",
            Self::NoOp => "no_op",
        }
    }
}

pub(in crate::db) fn ddl_preparation_report(bound: &BoundSqlDdlRequest) -> SqlDdlPreparationReport {
    match bound.statement() {
        BoundSqlDdlStatement::AddCheckConstraint(add) => prepared_report(
            SqlDdlMutationKind::AddCheckConstraint,
            add.constraint_name(),
            add.entity_name(),
            add.expression()
                .dependencies()
                .into_iter()
                .map(|field_id| field_id.get().to_string())
                .collect(),
        ),
        BoundSqlDdlStatement::AddColumn(add) => prepared_report(
            if add.field().insert_default().is_none() {
                SqlDdlMutationKind::AddField
            } else {
                SqlDdlMutationKind::AddDefaultedField
            },
            add.field().name(),
            add.entity_name(),
            vec![add.field().name().to_string()],
        ),
        BoundSqlDdlStatement::AlterColumnDefault(alter) => prepared_report(
            alter.mutation_kind(),
            alter.field_name(),
            alter.entity_name(),
            vec![alter.field_name().to_string()],
        ),
        BoundSqlDdlStatement::AlterColumnNullability(alter) => prepared_report(
            alter.mutation_kind(),
            alter.field_name(),
            alter.entity_name(),
            vec![alter.field_name().to_string()],
        ),
        BoundSqlDdlStatement::DropColumn(drop) => prepared_report(
            SqlDdlMutationKind::DropField,
            drop.field_name(),
            drop.entity_name(),
            vec![drop.field_name().to_string()],
        ),
        BoundSqlDdlStatement::RenameColumn(rename) => prepared_report(
            SqlDdlMutationKind::RenameField,
            rename.new_name(),
            rename.entity_name(),
            vec![rename.old_name().to_string(), rename.new_name().to_string()],
        ),
        BoundSqlDdlStatement::CreateIndex(create) => {
            let target = create.candidate_index();

            prepared_report(
                if target.key().is_field_path_only() {
                    SqlDdlMutationKind::AddFieldPathIndex
                } else {
                    SqlDdlMutationKind::AddExpressionIndex
                },
                target.name(),
                target.store(),
                ddl_key_item_report(create.key_items()),
            )
        }
        BoundSqlDdlStatement::DropIndex(drop) => prepared_report(
            SqlDdlMutationKind::DropSecondaryIndex,
            drop.index_name(),
            drop.dropped_index().store(),
            drop.field_path().to_vec(),
        ),
        BoundSqlDdlStatement::DropConstraint(drop) => prepared_report(
            SqlDdlMutationKind::DropCheckConstraint,
            drop.constraint_name(),
            drop.entity_name(),
            Vec::new(),
        ),
        BoundSqlDdlStatement::ValidateConstraint(validate) => prepared_report(
            SqlDdlMutationKind::ValidateConstraint,
            validate.constraint_name(),
            validate.entity_name(),
            Vec::new(),
        ),
        BoundSqlDdlStatement::NoOp(no_op) => prepared_report(
            no_op.mutation_kind(),
            no_op.index_name(),
            no_op.target_store(),
            no_op.field_path().to_vec(),
        ),
    }
}

fn prepared_report(
    mutation_kind: SqlDdlMutationKind,
    target_index: &str,
    target_store: &str,
    field_path: Vec<String>,
) -> SqlDdlPreparationReport {
    SqlDdlPreparationReport {
        mutation_kind,
        target_index: target_index.to_string(),
        target_store: target_store.to_string(),
        field_path,
        execution_status: SqlDdlExecutionStatus::PreparedOnly,
        rows_scanned: 0,
        index_keys_written: 0,
        constraint_validation: None,
    }
}