icydb-core 0.183.26

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
Documentation
//! Module: db::commit::apply
//! Responsibility: apply precomputed row/index mutations to stores.
//! Does not own: mutation preparation, commit-marker durability, or recovery orchestration.
//! Boundary: commit::{prepared_op,prepare,rebuild,replay} -> commit::apply (one-way).

use crate::db::commit::{PreparedIndexMutation, PreparedRowCommitOp};

impl PreparedIndexMutation {
    /// Apply one precomputed index mutation infallibly.
    pub(crate) fn apply(self) {
        self.index_store.with_borrow_mut(|store| {
            if let Some(value) = self.value {
                store.insert(self.key, value);
            } else {
                store.remove(&self.key);
            }
        });
    }
}

impl PreparedRowCommitOp {
    /// Apply the prepared row operation infallibly.
    pub(crate) fn apply(self) {
        // Phase 1: apply all index mutations first so rollback snapshots can
        // mirror this order exactly in reverse.
        for index_op in self.index_ops {
            index_op.apply();
        }

        // Phase 2: apply the authoritative row-store mutation.
        let data_generation = self.data_store.with_borrow_mut(|store| {
            if let Some(value) = self.data_value {
                store.insert(self.data_key, value);
            } else {
                store.remove(&self.data_key);
            }
            store.generation()
        });

        self.data_index_store.with_borrow_mut(|store| {
            store.mark_prefix_cardinality_data_generation(data_generation);
        });
    }
}