icydb-core 0.94.0

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
568
569
570
571
572
573
574
575
576
577
578
//! Module: query::plan::continuation
//! Responsibility: planner-owned continuation contracts and grouped/scalar resume windows.
//! Does not own: cursor token decoding internals or executor-side re-derivation policy.
//! Boundary: emits immutable continuation semantics consumed by runtime layers.

use crate::{
    db::{
        access::AccessPlan,
        cursor::{ContinuationSignature, CursorPlanError, GroupedPlannedCursor, PlannedCursor},
        query::plan::{
            AccessPlannedQuery, ExecutionOrderContract, ExecutionShapeSignature,
            GroupedCursorPolicyViolation, grouped_cursor_policy_violation,
        },
    },
    value::Value,
};

///
/// PlannedContinuationContract
///
/// Immutable planner-owned continuation semantic contract.
/// Runtime layers consume this contract and must not re-derive continuation
/// shape, window, or grouped/scalar compatibility semantics independently.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) struct PlannedContinuationContract {
    pub(in crate::db) shape_signature: ExecutionShapeSignature,
    pub(in crate::db) boundary_arity: usize,
    pub(in crate::db) window_size: usize,
    pub(in crate::db) order_contract: ExecutionOrderContract,
    page_limit: Option<usize>,
    access: AccessPlan<Value>,
    grouped_cursor_policy_violation: Option<GroupedCursorPolicyViolation>,
}

///
/// GroupedContinuationWindow
///
/// Planner-contract grouped continuation paging window.
/// Carries grouped page limit/offset/window progression semantics derived once
/// from immutable continuation contract state plus validated grouped cursor state.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) struct GroupedContinuationWindow {
    limit: Option<usize>,
    initial_offset_for_page: usize,
    selection_bound: Option<usize>,
    resume_initial_offset: u32,
    resume_boundary: Option<Value>,
}

impl GroupedContinuationWindow {
    // Construct one immutable grouped continuation paging window.
    const fn new(
        limit: Option<usize>,
        initial_offset_for_page: usize,
        selection_bound: Option<usize>,
        resume_initial_offset: u32,
        resume_boundary: Option<Value>,
    ) -> Self {
        Self {
            limit,
            initial_offset_for_page,
            selection_bound,
            resume_initial_offset,
            resume_boundary,
        }
    }

    /// Decompose grouped continuation window fields into grouped-fold tuple order.
    #[must_use]
    pub(in crate::db) fn into_parts(
        self,
    ) -> (Option<usize>, usize, Option<usize>, u32, Option<Value>) {
        (
            self.limit,
            self.initial_offset_for_page,
            self.selection_bound,
            self.resume_initial_offset,
            self.resume_boundary,
        )
    }
}

///
/// GroupedWindowProjection
///
/// Internal grouped paging-window projection assembled from one planned
/// continuation contract plus validated grouped cursor state.
/// This keeps grouped resume arithmetic in one local phase instead of spreading
/// it across the outward `GroupedContinuationWindow` construction path.
///

struct GroupedWindowProjection {
    limit: Option<usize>,
    initial_offset_for_page: usize,
    selection_bound: Option<usize>,
    resume_initial_offset: u32,
    resume_boundary: Option<Value>,
}

impl GroupedWindowProjection {
    // Project grouped window arithmetic from planner-owned continuation state.
    fn from_contract_and_cursor(
        contract: &PlannedContinuationContract,
        cursor: &GroupedPlannedCursor,
    ) -> Self {
        let resume_initial_offset = if cursor.is_empty() {
            contract.effective_offset(false)
        } else {
            cursor.initial_offset()
        };
        let initial_offset_for_page = if cursor.is_empty() {
            contract.window_size()
        } else {
            0
        };
        let selection_bound = contract.page_limit().and_then(|limit| {
            limit
                .checked_add(initial_offset_for_page)
                .and_then(|count| count.checked_add(1))
        });
        let resume_boundary = cursor
            .last_group_key()
            .map(|last_group_key| Value::List(last_group_key.to_vec()));

        Self {
            limit: contract.page_limit(),
            initial_offset_for_page,
            selection_bound,
            resume_initial_offset,
            resume_boundary,
        }
    }

    // Finalize the outward grouped paging-window DTO.
    fn into_window(self) -> GroupedContinuationWindow {
        GroupedContinuationWindow::new(
            self.limit,
            self.initial_offset_for_page,
            self.selection_bound,
            self.resume_initial_offset,
            self.resume_boundary,
        )
    }
}

///
/// GroupedCursorAction
///
/// Internal grouped continuation action discriminator.
/// Keeps grouped-plan requirement messages and cursor-policy gating shared
/// across grouped cursor preparation, grouped cursor revalidation, and grouped
/// paging-window projection so those entrypoints do not drift independently.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum GroupedCursorAction {
    Prepare,
    Revalidate,
    PagingWindow,
}

impl GroupedCursorAction {
    // Return the canonical grouped-plan requirement text for one grouped cursor action.
    const fn grouped_plan_required_message(self) -> &'static str {
        match self {
            Self::Prepare => "grouped cursor preparation requires grouped logical plans",
            Self::Revalidate => "grouped cursor revalidation requires grouped logical plans",
            Self::PagingWindow => "grouped paging window requires grouped logical plans",
        }
    }
}

/// Derive the effective offset under cursor-window semantics.
///
/// Offset applies only for initial requests. Once a continuation cursor is
/// present, offset must be treated as `0` to avoid double-skipping rows.
#[must_use]
pub(in crate::db) const fn effective_offset_for_cursor_window(
    window_size: u32,
    cursor_present: bool,
) -> u32 {
    if cursor_present { 0 } else { window_size }
}

///
/// ScalarAccessWindowPlan
///
/// Planner-owned scalar access-window DTO.
/// Carries effective offset and optional page limit so downstream route/runtime
/// layers consume one canonical window projection contract.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::db) struct ScalarAccessWindowPlan {
    effective_offset: u32,
    limit: Option<u32>,
}

impl ScalarAccessWindowPlan {
    /// Construct one planner-projected scalar access-window plan.
    #[must_use]
    pub(in crate::db) const fn new(effective_offset: u32, limit: Option<u32>) -> Self {
        Self {
            effective_offset,
            limit,
        }
    }

    /// Return optional page limit.
    #[must_use]
    pub(in crate::db) const fn limit(self) -> Option<u32> {
        self.limit
    }

    /// Return optional keep-count horizon (`effective_offset + limit`).
    #[must_use]
    pub(in crate::db) fn keep_count(self) -> Option<usize> {
        let limit = self.limit?;
        let offset = usize::try_from(self.effective_offset).unwrap_or(usize::MAX);
        let limit = usize::try_from(limit).unwrap_or(usize::MAX);

        Some(offset.saturating_add(limit))
    }

    /// Return effective offset projected into access-window lower-bound form.
    #[must_use]
    pub(in crate::db) fn lower_bound(self) -> usize {
        usize::try_from(self.effective_offset).unwrap_or(usize::MAX)
    }

    /// Return bounded fetch count including one continuation lookahead row.
    ///
    /// This projection keeps lookahead arithmetic planner-owned so route/runtime
    /// layers consume one explicit fetch contract without policy branching.
    #[must_use]
    pub(in crate::db) fn fetch_count(self) -> Option<usize> {
        let keep_count = self.keep_count();
        if self.limit.is_none() {
            return keep_count;
        }
        if self.limit == Some(0) {
            return Some(0);
        }

        keep_count.map(|fetch| fetch.saturating_add(1))
    }
}

impl PlannedContinuationContract {
    #[must_use]
    pub(in crate::db) const fn new(
        shape_signature: ExecutionShapeSignature,
        boundary_arity: usize,
        window_size: usize,
        order_contract: ExecutionOrderContract,
        page_limit: Option<usize>,
        access: AccessPlan<Value>,
        grouped_cursor_policy_violation: Option<GroupedCursorPolicyViolation>,
    ) -> Self {
        Self {
            shape_signature,
            boundary_arity,
            window_size,
            order_contract,
            page_limit,
            access,
            grouped_cursor_policy_violation,
        }
    }

    #[must_use]
    pub(in crate::db) const fn is_grouped(&self) -> bool {
        self.order_contract.is_grouped()
    }

    /// Borrow the immutable execution-order contract projected by planning.
    #[must_use]
    pub(in crate::db) const fn order_contract(&self) -> &ExecutionOrderContract {
        &self.order_contract
    }

    /// Borrow planner-projected scalar page limit under this continuation contract.
    #[must_use]
    pub(in crate::db) const fn page_limit(&self) -> Option<usize> {
        self.page_limit
    }

    /// Borrow planner-projected window size (`offset`) under this contract.
    #[must_use]
    pub(in crate::db) const fn window_size(&self) -> usize {
        self.window_size
    }

    /// Borrow planner-projected access plan used for continuation consistency checks.
    #[must_use]
    pub(in crate::db) const fn access_plan(&self) -> &AccessPlan<Value> {
        &self.access
    }

    /// Borrow grouped cursor-policy violation, if continuation is disallowed for grouped shape.
    #[must_use]
    pub(in crate::db) const fn grouped_cursor_policy_violation(
        &self,
    ) -> Option<GroupedCursorPolicyViolation> {
        self.grouped_cursor_policy_violation
    }

    #[must_use]
    pub(in crate::db) const fn continuation_signature(&self) -> ContinuationSignature {
        self.shape_signature.continuation_signature()
    }

    #[must_use]
    pub(in crate::db) const fn boundary_arity(&self) -> usize {
        self.boundary_arity
    }

    /// Return expected initial offset encoded in continuation tokens.
    #[must_use]
    pub(in crate::db) fn expected_initial_offset(&self) -> u32 {
        u32::try_from(self.window_size()).unwrap_or(u32::MAX)
    }

    /// Return effective offset for this request under cursor-window semantics.
    ///
    /// Offset is consumed only for initial requests; continuation requests resume
    /// from cursor boundary and therefore use offset `0`.
    #[must_use]
    pub(in crate::db) fn effective_offset(&self, cursor_present: bool) -> u32 {
        effective_offset_for_cursor_window(self.expected_initial_offset(), cursor_present)
    }

    /// Validate scalar cursor bytes against this immutable continuation contract.
    pub(in crate::db) fn prepare_scalar_cursor(
        &self,
        entity_path: &'static str,
        entity_tag: crate::types::EntityTag,
        entity_model: &crate::model::entity::EntityModel,
        bytes: Option<&[u8]>,
    ) -> Result<PlannedCursor, CursorPlanError> {
        if self.is_grouped() {
            return Err(CursorPlanError::continuation_cursor_invariant(
                "grouped plans require grouped cursor preparation",
            ));
        }

        crate::db::cursor::prepare_cursor(
            self.access_plan().resolve_strategy().as_path().cloned(),
            entity_path,
            entity_tag,
            entity_model,
            self.order_contract.order_spec(),
            self.order_contract.direction(),
            self.continuation_signature(),
            self.expected_initial_offset(),
            bytes,
        )
    }

    /// Validate grouped cursor bytes against this immutable continuation contract.
    #[cfg(test)]
    pub(in crate::db) fn prepare_grouped_cursor(
        &self,
        entity_path: &'static str,
        bytes: Option<&[u8]>,
    ) -> Result<GroupedPlannedCursor, CursorPlanError> {
        self.validate_grouped_cursor_contract(GroupedCursorAction::Prepare, bytes.is_some())?;

        crate::db::cursor::prepare_grouped_cursor(
            entity_path,
            self.order_contract.order_spec(),
            self.order_contract.direction(),
            self.continuation_signature(),
            self.expected_initial_offset(),
            bytes,
        )
    }

    /// Validate one already-decoded grouped cursor token against this immutable continuation contract.
    pub(in crate::db) fn prepare_grouped_cursor_token(
        &self,
        entity_path: &'static str,
        cursor: Option<crate::db::cursor::GroupedContinuationToken>,
    ) -> Result<GroupedPlannedCursor, CursorPlanError> {
        self.validate_grouped_cursor_contract(GroupedCursorAction::Prepare, cursor.is_some())?;

        crate::db::cursor::prepare_grouped_cursor_token(
            entity_path,
            self.order_contract.order_spec(),
            self.order_contract.direction(),
            self.continuation_signature(),
            self.expected_initial_offset(),
            cursor,
        )
    }

    /// Revalidate scalar cursor state against this immutable continuation contract.
    pub(in crate::db) fn revalidate_scalar_cursor(
        &self,
        entity_tag: crate::types::EntityTag,
        entity_model: &crate::model::entity::EntityModel,
        cursor: PlannedCursor,
    ) -> Result<PlannedCursor, CursorPlanError> {
        if self.is_grouped() {
            return Err(CursorPlanError::continuation_cursor_invariant(
                "grouped plans require grouped cursor revalidation",
            ));
        }

        crate::db::cursor::revalidate_cursor(
            self.access_plan().resolve_strategy().as_path().cloned(),
            entity_tag,
            entity_model,
            self.order_contract.order_spec(),
            self.order_contract.direction(),
            self.expected_initial_offset(),
            cursor,
        )
    }

    /// Revalidate grouped cursor state against this immutable continuation contract.
    pub(in crate::db) fn revalidate_grouped_cursor(
        &self,
        cursor: GroupedPlannedCursor,
    ) -> Result<GroupedPlannedCursor, CursorPlanError> {
        self.validate_grouped_cursor_contract(GroupedCursorAction::Revalidate, !cursor.is_empty())?;

        crate::db::cursor::revalidate_grouped_cursor(self.expected_initial_offset(), cursor)
    }

    /// Derive grouped paging contracts from validated grouped cursor state.
    pub(in crate::db) fn project_grouped_paging_window(
        &self,
        cursor: &GroupedPlannedCursor,
    ) -> Result<GroupedContinuationWindow, CursorPlanError> {
        self.validate_grouped_cursor_contract(
            GroupedCursorAction::PagingWindow,
            !cursor.is_empty(),
        )?;

        Ok(GroupedWindowProjection::from_contract_and_cursor(self, cursor).into_window())
    }

    // Enforce grouped continuation ownership once for all grouped cursor entrypoints.
    fn validate_grouped_cursor_contract(
        &self,
        action: GroupedCursorAction,
        cursor_applied: bool,
    ) -> Result<(), CursorPlanError> {
        if !self.is_grouped() {
            return Err(CursorPlanError::continuation_cursor_invariant(
                action.grouped_plan_required_message(),
            ));
        }

        self.validate_grouped_cursor_policy_if_applied(cursor_applied)
    }

    // Apply grouped cursor-policy violations only when continuation is actually reused.
    fn validate_grouped_cursor_policy_if_applied(
        &self,
        cursor_applied: bool,
    ) -> Result<(), CursorPlanError> {
        if !cursor_applied {
            return Ok(());
        }

        self.validate_grouped_cursor_policy()
    }

    fn validate_grouped_cursor_policy(&self) -> Result<(), CursorPlanError> {
        if let Some(violation) = self.grouped_cursor_policy_violation() {
            return Err(violation.into_cursor_plan_error());
        }

        Ok(())
    }
}

impl AccessPlannedQuery {
    /// Project planner-owned scalar access-window contract.
    #[must_use]
    pub(in crate::db) fn scalar_access_window_plan(
        &self,
        cursor_present: bool,
    ) -> ScalarAccessWindowPlan {
        let page_window = PlannedPageWindow::from_query(self);
        let effective_offset =
            effective_offset_for_cursor_window(page_window.offset_u32(), cursor_present);

        ScalarAccessWindowPlan::new(effective_offset, page_window.limit_u32())
    }

    /// Build one immutable continuation contract from planner-owned semantics.
    #[must_use]
    pub(in crate::db) fn planned_continuation_contract(
        &self,
        entity_path: &'static str,
    ) -> Option<PlannedContinuationContract> {
        if !self.scalar_plan().mode.is_load() {
            return None;
        }

        let page_window = PlannedPageWindow::from_query(self);
        let shape_signature = self.execution_shape_signature(entity_path);
        let boundary_arity = self.grouped_plan().map_or_else(
            || {
                self.scalar_plan()
                    .order
                    .as_ref()
                    .map_or(0, |order| order.fields.len())
            },
            |grouped| grouped.group.group_fields.len(),
        );
        let is_grouped = self.grouped_plan().is_some();
        let order_contract =
            ExecutionOrderContract::from_plan(is_grouped, self.scalar_plan().order.as_ref());
        let access = self.access.clone();
        let grouped_cursor_policy_violation = self
            .grouped_plan()
            .and_then(|grouped| grouped_cursor_policy_violation(grouped, true));

        Some(PlannedContinuationContract::new(
            shape_signature,
            boundary_arity,
            page_window.offset_usize(),
            order_contract,
            page_window.limit_usize(),
            access,
            grouped_cursor_policy_violation,
        ))
    }
}

// Freeze one planner-owned page-window view so scalar access-window planning
// and continuation-contract assembly do not each re-derive page offset/limit
// conversions from the same logical page spec.
struct PlannedPageWindow {
    offset: u32,
    limit: Option<u32>,
}

impl PlannedPageWindow {
    // Project the logical page window from one access-planned query.
    fn from_query(plan: &AccessPlannedQuery) -> Self {
        let page = plan.scalar_plan().page.as_ref();

        Self {
            offset: page.map_or(0, |page| page.offset),
            limit: page.and_then(|page| page.limit),
        }
    }

    // Borrow the canonical u32 page offset used by cursor-window semantics.
    const fn offset_u32(&self) -> u32 {
        self.offset
    }

    // Borrow the canonical u32 page limit used by scalar access-window planning.
    const fn limit_u32(&self) -> Option<u32> {
        self.limit
    }

    // Project the page offset into continuation-contract usize form.
    fn offset_usize(&self) -> usize {
        usize::try_from(self.offset).unwrap_or(usize::MAX)
    }

    // Project the page limit into continuation-contract usize form.
    fn limit_usize(&self) -> Option<usize> {
        self.limit
            .map(|limit| usize::try_from(limit).unwrap_or(usize::MAX))
    }
}