pub(super) mod commit_window;
pub(super) mod save;
mod save_validation;
use crate::{
db::{
Db,
commit::ensure_recovered,
data::{
DataKey, PersistedRow, SerializedUpdatePatch, UpdatePatch,
serialize_entity_slots_as_complete_serialized_patch, serialize_update_patch_fields,
},
executor::{
Context, EntityAuthority, route::build_execution_route_plan_for_mutation,
validate_executor_plan_for_authority,
},
query::plan::AccessPlannedQuery,
},
error::InternalError,
traits::{EntityKind, EntityValue},
};
pub(super) use commit_window::{
PreparedRowOpDelta, commit_delete_row_ops_with_window,
commit_delete_row_ops_with_window_for_path, commit_prepared_single_save_row_op_with_window,
commit_save_row_ops_with_window, emit_index_delta_metrics,
synchronized_store_handles_for_prepared_row_ops,
};
pub(in crate::db::executor) struct MutationInput {
data_key: DataKey,
serialized_slots: SerializedUpdatePatch,
}
impl MutationInput {
#[must_use]
pub(in crate::db::executor) const fn new(
data_key: DataKey,
serialized_slots: SerializedUpdatePatch,
) -> Self {
Self {
data_key,
serialized_slots,
}
}
pub(in crate::db::executor) fn from_entity<E>(entity: &E) -> Result<Self, InternalError>
where
E: PersistedRow + EntityValue,
{
let key = entity.id().key();
let data_key = DataKey::try_new::<E>(key)?;
let serialized_slots = serialize_entity_slots_as_complete_serialized_patch(entity)?;
Ok(Self::new(data_key, serialized_slots))
}
pub(in crate::db::executor) fn from_update_patch<E>(
key: E::Key,
patch: &UpdatePatch,
) -> Result<Self, InternalError>
where
E: PersistedRow + EntityValue,
{
let data_key = DataKey::try_new::<E>(key)?;
let serialized_slots = serialize_update_patch_fields(E::MODEL, patch)?;
Ok(Self::new(data_key, serialized_slots))
}
#[must_use]
pub(in crate::db::executor) const fn data_key(&self) -> &DataKey {
&self.data_key
}
#[must_use]
pub(in crate::db::executor) const fn serialized_slots(&self) -> &SerializedUpdatePatch {
&self.serialized_slots
}
}
pub(in crate::db::executor) fn mutation_write_context<E>(
db: &'_ Db<E::Canister>,
) -> Result<Context<'_, E>, InternalError>
where
E: EntityKind + EntityValue,
{
ensure_recovered(db)?;
Ok(db.context::<E>())
}
pub(in crate::db::executor) fn preflight_mutation_plan_for_authority(
authority: EntityAuthority,
plan: &AccessPlannedQuery,
) -> Result<(), InternalError> {
validate_executor_plan_for_authority(authority, plan)?;
build_execution_route_plan_for_mutation(authority, plan)?;
Ok(())
}