icydb-core 0.94.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
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
//! Module: executor::runtime_context
//! Responsibility: executor-scoped store/index read context and row decoding helpers.
//! Does not own: routing policy, plan lowering, or mutation commit semantics.
//! Boundary: read-only data/index access surface consumed by executor submodules.

#[cfg(test)]
mod tests;

use crate::{
    db::{
        Db,
        data::{DataKey, DataRow, DataStore, RawRow},
        direction::Direction,
        executor::{ExecutorError, OrderedKeyStream, saturating_row_len},
        predicate::MissingRowPolicy,
        registry::StoreHandle,
    },
    error::InternalError,
    traits::{CanisterKind, EntityKind, EntityValue, Path},
};
#[cfg(test)]
use crate::{types::EntityTag, value::StorageKey};
#[cfg(any(test, feature = "diagnostics"))]
use std::cell::RefCell;
use std::ops::Bound;

// -----------------------------------------------------------------------------
// Context Subdomains
// -----------------------------------------------------------------------------
// 1) Context handle and store access.
// 2) Row reads and consistency-aware materialization.
// 3) Key/spec helper utilities and decoding invariants.

///
/// Context
///

#[derive(Clone, Copy)]
pub(in crate::db) struct Context<'a, E: EntityKind + EntityValue> {
    pub(in crate::db::executor) db: &'a Db<E::Canister>,
}

impl<E> crate::db::executor::pipeline::contracts::LoadExecutor<E>
where
    E: EntityKind + EntityValue,
{
    /// Construct one load executor bound to a database handle and debug mode.
    #[must_use]
    pub(in crate::db) const fn new(db: Db<E::Canister>, debug: bool) -> Self {
        Self { db, debug }
    }
}

///
/// StoreLookup
///
/// StoreLookup is the object-safe store-registry lookup boundary used when
/// executor helpers need to resolve an arbitrary named store without carrying
/// a typed `Context<E>` through the call chain.
///

pub(in crate::db) trait StoreLookup {
    fn try_get_store(&self, path: &str) -> Result<StoreHandle, InternalError>;
}

impl<C> StoreLookup for Db<C>
where
    C: CanisterKind,
{
    fn try_get_store(&self, path: &str) -> Result<StoreHandle, InternalError> {
        self.with_store_registry(|registry| registry.try_get_store(path))
    }
}

///
/// StoreResolver
///
/// StoreResolver is the non-generic named-store lookup bundle used by
/// executor helpers that must resolve index-owned stores after the typed
/// boundary has already chosen the entity model/runtime shell.
///

#[derive(Clone, Copy)]
pub(in crate::db) struct StoreResolver<'a> {
    lookup: &'a dyn StoreLookup,
}

impl<'a> StoreResolver<'a> {
    /// Build one named-store resolver from one object-safe lookup boundary.
    #[must_use]
    pub(in crate::db) const fn new(lookup: &'a dyn StoreLookup) -> Self {
        Self { lookup }
    }

    /// Resolve one named store through the captured store-registry boundary.
    pub(in crate::db) fn try_get_store(self, path: &str) -> Result<StoreHandle, InternalError> {
        self.lookup.try_get_store(path)
    }
}

///
/// RowCheckMetrics
///
/// RowCheckMetrics aggregates one test-scoped view of the executor-owned
/// `row_check_required` boundary for secondary covering reads.
/// It lets perf probes separate secondary scan traversal, membership decode,
/// and authoritative row-presence probes without changing runtime policy.
///

#[cfg(any(test, feature = "diagnostics"))]
#[cfg_attr(all(test, not(feature = "diagnostics")), allow(unreachable_pub))]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct RowCheckMetrics {
    pub index_entries_scanned: u64,
    pub index_membership_single_key_entries: u64,
    pub index_membership_multi_key_entries: u64,
    pub index_membership_keys_decoded: u64,
    pub row_check_covering_candidates_seen: u64,
    pub row_check_rows_emitted: u64,
    pub row_presence_probe_count: u64,
    pub row_presence_probe_hits: u64,
    pub row_presence_probe_misses: u64,
    pub row_presence_probe_borrowed_data_store_count: u64,
    pub row_presence_probe_store_handle_count: u64,
    pub row_presence_key_to_raw_encodes: u64,
}

#[cfg(any(test, feature = "diagnostics"))]
std::thread_local! {
    static ROW_CHECK_METRICS: RefCell<Option<RowCheckMetrics>> = const {
        RefCell::new(None)
    };
}

#[cfg(any(test, feature = "diagnostics"))]
fn update_row_check_metrics(update: impl FnOnce(&mut RowCheckMetrics)) {
    ROW_CHECK_METRICS.with(|metrics| {
        let mut metrics = metrics.borrow_mut();
        let Some(metrics) = metrics.as_mut() else {
            return;
        };

        update(metrics);
    });
}

///
/// RowPresenceProbeSource
///
/// Internal source tag for authoritative row-presence probes.
/// This keeps metrics able to distinguish already-borrowed data-store probes
/// from probes that still re-enter through one store handle boundary.
///

enum RowPresenceProbeSource {
    BorrowedDataStore,
    #[cfg(test)]
    StoreHandle,
}

///
/// FusedSecondaryCoveringAuthority
///
///
/// Executor-owned borrowed data-store authority for stale-fallback secondary
/// covering reads. It keeps row-visibility policy in the executor while
/// collapsing candidate admission, authoritative existence probing, and
/// consistency handling into one explicit boundary.
///

#[cfg(test)]
#[derive(Clone, Copy)]
pub(in crate::db::executor) struct FusedSecondaryCoveringAuthority<'a> {
    data: &'a DataStore,
    entity_tag: EntityTag,
    consistency: MissingRowPolicy,
}

#[cfg(test)]
impl<'a> FusedSecondaryCoveringAuthority<'a> {
    /// Construct one fused stale-row authority over one borrowed data-store
    /// boundary and one fixed entity identity.
    #[must_use]
    pub(in crate::db::executor) const fn new(
        data: &'a DataStore,
        entity_tag: EntityTag,
        consistency: MissingRowPolicy,
    ) -> Self {
        Self {
            data,
            entity_tag,
            consistency,
        }
    }

    /// Admit or reject one secondary covering candidate under the existing
    /// fail-closed stale-row contract.
    pub(in crate::db::executor) fn admits_storage_key(
        self,
        storage_key: StorageKey,
    ) -> Result<bool, InternalError> {
        // Phase 1: account for the candidate and encode one authoritative
        // row-store key directly from the entity tag plus storage key.
        record_row_check_covering_candidate_seen();
        record_row_presence_probe_source(RowPresenceProbeSource::BorrowedDataStore);
        record_row_presence_key_to_raw_encode();
        let raw_key = DataKey::raw_from_parts(self.entity_tag, storage_key)?;

        // Phase 2: probe the borrowed data-store authority and preserve the
        // current missing-row policy exactly.
        let row_exists = self.data.contains(&raw_key);
        record_row_presence_probe_result(row_exists);

        match self.consistency {
            MissingRowPolicy::Error => {
                if row_exists {
                    Ok(true)
                } else {
                    Err(
                        ExecutorError::missing_row(&DataKey::new(self.entity_tag, storage_key))
                            .into(),
                    )
                }
            }
            MissingRowPolicy::Ignore => Ok(row_exists),
        }
    }
}

#[cfg(any(test, feature = "diagnostics"))]
pub(in crate::db) fn record_row_check_index_entry_scanned() {
    update_row_check_metrics(|metrics| {
        metrics.index_entries_scanned = metrics.index_entries_scanned.saturating_add(1);
    });
}

#[cfg(not(any(test, feature = "diagnostics")))]
pub(in crate::db) const fn record_row_check_index_entry_scanned() {}

#[cfg(any(test, feature = "diagnostics"))]
pub(in crate::db) fn record_row_check_index_membership_single_key_entry() {
    update_row_check_metrics(|metrics| {
        metrics.index_membership_single_key_entries = metrics
            .index_membership_single_key_entries
            .saturating_add(1);
    });
}

#[cfg(not(any(test, feature = "diagnostics")))]
pub(in crate::db) const fn record_row_check_index_membership_single_key_entry() {}

#[cfg(any(test, feature = "diagnostics"))]
pub(in crate::db) fn record_row_check_index_membership_multi_key_entry() {
    update_row_check_metrics(|metrics| {
        metrics.index_membership_multi_key_entries =
            metrics.index_membership_multi_key_entries.saturating_add(1);
    });
}

#[cfg(not(any(test, feature = "diagnostics")))]
pub(in crate::db) const fn record_row_check_index_membership_multi_key_entry() {}

#[cfg(any(test, feature = "diagnostics"))]
pub(in crate::db) fn record_row_check_index_membership_key_decoded() {
    update_row_check_metrics(|metrics| {
        metrics.index_membership_keys_decoded =
            metrics.index_membership_keys_decoded.saturating_add(1);
    });
}

#[cfg(not(any(test, feature = "diagnostics")))]
pub(in crate::db) const fn record_row_check_index_membership_key_decoded() {}

#[cfg(any(test, feature = "diagnostics"))]
pub(in crate::db) fn record_row_check_covering_candidate_seen() {
    update_row_check_metrics(|metrics| {
        metrics.row_check_covering_candidates_seen =
            metrics.row_check_covering_candidates_seen.saturating_add(1);
    });
}

#[cfg(not(any(test, feature = "diagnostics")))]
pub(in crate::db) const fn record_row_check_covering_candidate_seen() {}

#[cfg(any(test, feature = "diagnostics"))]
pub(in crate::db) fn record_row_check_row_emitted() {
    update_row_check_metrics(|metrics| {
        metrics.row_check_rows_emitted = metrics.row_check_rows_emitted.saturating_add(1);
    });
}

#[cfg(not(any(test, feature = "diagnostics")))]
pub(in crate::db) const fn record_row_check_row_emitted() {}

#[cfg(any(test, feature = "diagnostics"))]
fn record_row_presence_probe_source(source: RowPresenceProbeSource) {
    update_row_check_metrics(|metrics| match source {
        RowPresenceProbeSource::BorrowedDataStore => {
            metrics.row_presence_probe_borrowed_data_store_count = metrics
                .row_presence_probe_borrowed_data_store_count
                .saturating_add(1);
        }
        #[cfg(test)]
        RowPresenceProbeSource::StoreHandle => {
            metrics.row_presence_probe_store_handle_count = metrics
                .row_presence_probe_store_handle_count
                .saturating_add(1);
        }
    });
}

#[cfg(not(any(test, feature = "diagnostics")))]
const fn record_row_presence_probe_source(_source: RowPresenceProbeSource) {}

#[cfg(any(test, feature = "diagnostics"))]
fn record_row_presence_key_to_raw_encode() {
    update_row_check_metrics(|metrics| {
        metrics.row_presence_key_to_raw_encodes =
            metrics.row_presence_key_to_raw_encodes.saturating_add(1);
    });
}

#[cfg(not(any(test, feature = "diagnostics")))]
const fn record_row_presence_key_to_raw_encode() {}

#[cfg(any(test, feature = "diagnostics"))]
fn record_row_presence_probe_result(row_exists: bool) {
    update_row_check_metrics(|metrics| {
        metrics.row_presence_probe_count = metrics.row_presence_probe_count.saturating_add(1);
        if row_exists {
            metrics.row_presence_probe_hits = metrics.row_presence_probe_hits.saturating_add(1);
        } else {
            metrics.row_presence_probe_misses = metrics.row_presence_probe_misses.saturating_add(1);
        }
    });
}

#[cfg(not(any(test, feature = "diagnostics")))]
const fn record_row_presence_probe_result(_row_exists: bool) {}

///
/// with_row_check_metrics
///
/// Run one closure while collecting executor-owned `row_check_required`
/// metrics on the current thread, then return the closure result plus the
/// aggregated snapshot.
///

#[cfg(any(test, feature = "diagnostics"))]
#[cfg_attr(all(test, not(feature = "diagnostics")), allow(unreachable_pub))]
pub fn with_row_check_metrics<T>(f: impl FnOnce() -> T) -> (T, RowCheckMetrics) {
    ROW_CHECK_METRICS.with(|metrics| {
        debug_assert!(
            metrics.borrow().is_none(),
            "row_check metrics captures should not nest"
        );
        *metrics.borrow_mut() = Some(RowCheckMetrics::default());
    });

    let result = f();
    let metrics = ROW_CHECK_METRICS.with(|metrics| metrics.borrow_mut().take().unwrap_or_default());

    (result, metrics)
}

impl<'a, E> Context<'a, E>
where
    E: EntityKind + EntityValue,
{
    // ------------------------------------------------------------------
    // Context setup
    // ------------------------------------------------------------------

    /// Construct one executor context bound to a database handle.
    #[must_use]
    pub(in crate::db) const fn new(db: &'a Db<E::Canister>) -> Self {
        Self { db }
    }

    // ------------------------------------------------------------------
    // Store access
    // ------------------------------------------------------------------

    /// Execute one closure against the entity's data store handle.
    pub(in crate::db) fn with_store<R>(
        &self,
        f: impl FnOnce(&DataStore) -> R,
    ) -> Result<R, InternalError> {
        self.db.with_store_registry(|reg| {
            reg.try_get_store(E::Store::PATH)
                .map(|store| store.with_data(f))
        })
    }

    /// Recover the structural store handle once for generic-free executor runtime helpers.
    pub(in crate::db::executor) fn structural_store(&self) -> Result<StoreHandle, InternalError> {
        self.db
            .with_store_registry(|reg| reg.try_get_store(E::Store::PATH))
    }

    // ------------------------------------------------------------------
    // Row reads
    // ------------------------------------------------------------------

    /// Read one raw row by key, returning not-found as an error.
    pub(in crate::db) fn read(&self, key: &DataKey) -> Result<RawRow, InternalError> {
        self.with_store(|s| {
            let raw = key.to_raw()?;
            s.get(&raw)
                .ok_or_else(|| InternalError::store_not_found(key.to_string()))
        })?
    }
}

// Read one raw row under one consistency contract from structural store authority.
pub(in crate::db::executor) fn read_row_with_consistency_from_store(
    store: StoreHandle,
    key: &DataKey,
    consistency: MissingRowPolicy,
) -> Result<Option<RawRow>, InternalError> {
    let read_row = |key: &DataKey| -> Result<Option<RawRow>, InternalError> {
        let raw = key.to_raw()?;

        Ok(store.with_data(|data| data.get(&raw)))
    };

    match consistency {
        MissingRowPolicy::Error => match read_row(key)? {
            Some(row) => Ok(Some(row)),
            None => Err(ExecutorError::missing_row(key).into()),
        },
        MissingRowPolicy::Ignore => read_row(key),
    }
}

// Read only row presence under one consistency contract from structural store
// authority. Tests keep this generic selector so they can verify the metrics
// split between store-handle and borrowed-data-store probing.
#[cfg(test)]
pub(in crate::db::executor) fn read_row_presence_with_consistency_from_store(
    store: StoreHandle,
    key: &DataKey,
    consistency: MissingRowPolicy,
) -> Result<bool, InternalError> {
    store.with_data(|data| {
        read_row_presence_with_consistency(
            data,
            key,
            consistency,
            RowPresenceProbeSource::StoreHandle,
        )
    })
}

// Read only row presence under one consistency contract from one already
// borrowed data-store boundary. Covering-read decode paths use this helper to
// batch stale-row filtering under one store borrow instead of re-entering the
// registry per decoded secondary key.
pub(in crate::db::executor) fn read_row_presence_with_consistency_from_data_store(
    data: &DataStore,
    key: &DataKey,
    consistency: MissingRowPolicy,
) -> Result<bool, InternalError> {
    read_row_presence_with_consistency(
        data,
        key,
        consistency,
        RowPresenceProbeSource::BorrowedDataStore,
    )
}

fn read_row_presence_with_consistency(
    data: &DataStore,
    key: &DataKey,
    consistency: MissingRowPolicy,
    source: RowPresenceProbeSource,
) -> Result<bool, InternalError> {
    let row_exists = probe_row_presence(data, key, source)?;

    match consistency {
        MissingRowPolicy::Error => {
            if row_exists {
                Ok(true)
            } else {
                Err(ExecutorError::missing_row(key).into())
            }
        }
        MissingRowPolicy::Ignore => Ok(row_exists),
    }
}

fn probe_row_presence(
    data: &DataStore,
    key: &DataKey,
    source: RowPresenceProbeSource,
) -> Result<bool, InternalError> {
    record_row_presence_probe_source(source);
    record_row_presence_key_to_raw_encode();
    let raw = key.to_raw()?;
    let row_exists = data.contains(&raw);
    record_row_presence_probe_result(row_exists);

    Ok(row_exists)
}

// Read one persisted row under one consistency contract and preserve the source data key.
pub(in crate::db::executor) fn read_data_row_with_consistency_from_store(
    store: StoreHandle,
    key: &DataKey,
    consistency: MissingRowPolicy,
) -> Result<Option<DataRow>, InternalError> {
    let Some(row) = read_row_with_consistency_from_store(store, key, consistency)? else {
        return Ok(None);
    };

    Ok(Some((key.clone(), row)))
}

/// Fold persisted row payload bytes over one full-scan page window through structural store authority.
pub(in crate::db::executor) fn sum_row_payload_bytes_full_scan_window_with_store(
    store: StoreHandle,
    direction: Direction,
    offset: usize,
    limit: Option<usize>,
) -> u64 {
    store.with_data(|store| match direction {
        Direction::Asc => sum_payload_bytes_from_row_lengths(
            store.iter().map(|entry| entry.value().len()),
            offset,
            limit,
        ),
        Direction::Desc => sum_payload_bytes_from_row_lengths(
            store.iter().rev().map(|entry| entry.value().len()),
            offset,
            limit,
        ),
    })
}

/// Fold persisted row payload bytes over one key-range page window through structural store authority.
pub(in crate::db::executor) fn sum_row_payload_bytes_key_range_window_with_store(
    store: StoreHandle,
    start: &DataKey,
    end: &DataKey,
    direction: Direction,
    offset: usize,
    limit: Option<usize>,
) -> Result<u64, InternalError> {
    let start_raw = start.to_raw()?;
    let end_raw = end.to_raw()?;
    let total = store.with_data(|store| match direction {
        Direction::Asc => sum_payload_bytes_from_row_lengths(
            store
                .range((Bound::Included(start_raw), Bound::Included(end_raw)))
                .map(|entry| entry.value().len()),
            offset,
            limit,
        ),
        Direction::Desc => sum_payload_bytes_from_row_lengths(
            store
                .range((Bound::Included(start_raw), Bound::Included(end_raw)))
                .rev()
                .map(|entry| entry.value().len()),
            offset,
            limit,
        ),
    });

    Ok(total)
}

/// Fold persisted row payload bytes over one ordered key stream page window through structural store authority.
pub(in crate::db::executor) fn sum_row_payload_bytes_from_ordered_key_stream_with_store<S>(
    store: StoreHandle,
    key_stream: &mut S,
    consistency: MissingRowPolicy,
    offset: usize,
    limit: Option<usize>,
) -> Result<u64, InternalError>
where
    S: OrderedKeyStream + ?Sized,
{
    sum_row_payload_bytes_from_ordered_key_stream_shared(
        key_stream,
        &mut |key| read_row_with_consistency_from_store(store, key, consistency),
        offset,
        limit,
    )
}

const fn payload_window_limit_exhausted(limit_remaining: Option<usize>) -> bool {
    matches!(limit_remaining, Some(0))
}

const fn payload_window_accept_row(
    offset_remaining: &mut usize,
    limit_remaining: &mut Option<usize>,
) -> bool {
    if *offset_remaining > 0 {
        *offset_remaining = offset_remaining.saturating_sub(1);
        return false;
    }

    if let Some(remaining) = limit_remaining.as_mut() {
        if *remaining == 0 {
            return false;
        }
        *remaining = remaining.saturating_sub(1);
    }

    true
}

fn sum_payload_bytes_from_row_lengths(
    row_lengths: impl Iterator<Item = usize>,
    offset: usize,
    limit: Option<usize>,
) -> u64 {
    let mut total = 0u64;
    let mut offset_remaining = offset;
    let mut limit_remaining = limit;

    for row_len in row_lengths {
        if payload_window_limit_exhausted(limit_remaining) {
            break;
        }
        if !payload_window_accept_row(&mut offset_remaining, &mut limit_remaining) {
            continue;
        }

        total = total.saturating_add(saturating_row_len(row_len));
    }

    total
}

// Shared ordered key-stream scan loop used by payload-byte aggregation.
// Entity wrappers provide consistency-aware row reads via callback injection.
fn sum_row_payload_bytes_from_ordered_key_stream_shared<S, F>(
    key_stream: &mut S,
    read_row: &mut F,
    offset: usize,
    limit: Option<usize>,
) -> Result<u64, InternalError>
where
    S: OrderedKeyStream + ?Sized,
    F: FnMut(&DataKey) -> Result<Option<RawRow>, InternalError>,
{
    let mut total = 0u64;
    let mut offset_remaining = offset;
    let mut limit_remaining = limit;

    while let Some(key) = key_stream.next_key()? {
        if payload_window_limit_exhausted(limit_remaining) {
            break;
        }

        // Index-backed and composite stream rows remain row-authoritative:
        // missing-row ignore skips stale keys, strict mode fails closed.
        let Some(row) = read_row(&key)? else {
            continue;
        };
        if !payload_window_accept_row(&mut offset_remaining, &mut limit_remaining) {
            continue;
        }

        total = total.saturating_add(saturating_row_len(row.len()));
    }

    Ok(total)
}