Skip to main content

icydb_core/db/session/
write.rs

1//! Module: db::session::write
2//! Responsibility: session-owned typed write APIs for insert, replace, update,
3//! and structural mutation entrypoints over the shared save pipeline.
4//! Does not own: commit staging, mutation execution, or persistence encoding.
5//! Boundary: keeps public session write semantics above the executor save surface.
6
7use super::accepted_save_contract_for_descriptor;
8use crate::{
9    db::{
10        DbSession, PersistedRow, WriteBatchResponse,
11        data::{FieldSlot, StructuralPatch},
12        executor::MutationMode,
13        schema::{AcceptedFieldAbsencePolicy, AcceptedRowLayoutRuntimeContract},
14    },
15    error::InternalError,
16    traits::{CanisterKind, EntityCreateInput, EntityValue},
17    value::Value,
18};
19
20// Append one session-resolved structural field update. The caller passes the
21// accepted runtime contract that already crossed schema reconciliation, so
22// field-name lookup follows persisted row-layout metadata rather than generated
23// declaration order.
24fn append_accepted_structural_patch_field(
25    entity_path: &'static str,
26    descriptor: &AcceptedRowLayoutRuntimeContract<'_>,
27    patch: StructuralPatch,
28    field_name: &str,
29    value: Value,
30) -> Result<StructuralPatch, InternalError> {
31    let slot = descriptor
32        .field_slot_index_by_name(field_name)
33        .ok_or_else(|| InternalError::mutation_structural_field_unknown(entity_path, field_name))?;
34
35    Ok(patch.set(FieldSlot::from_validated_index(slot), value))
36}
37
38// Enforce public structural patch policy before the executor materializes an
39// entity through generated derive code. This keeps database write ownership and
40// absence/default policy owned by accepted schema metadata instead of
41// accidentally relying on executor-local generated field metadata, Rust
42// `Default`, or derive-local missing slot behavior.
43fn validate_structural_patch_schema_policy<E>(
44    descriptor: &AcceptedRowLayoutRuntimeContract<'_>,
45    patch: &StructuralPatch,
46    mode: MutationMode,
47) -> Result<(), InternalError>
48where
49    E: PersistedRow + EntityValue,
50{
51    reject_explicit_generated_fields_from_accepted_patch::<E>(descriptor, patch)?;
52
53    if matches!(mode, MutationMode::Update) {
54        return Ok(());
55    }
56
57    let mut provided_slots = vec![false; descriptor.required_slot_count()];
58    for entry in patch.entries() {
59        let slot = entry.slot().index();
60        if slot < provided_slots.len() {
61            provided_slots[slot] = true;
62        }
63    }
64
65    // Every omitted field must be allowed by accepted schema absence policy.
66    // Future database defaults should extend `AcceptedFieldAbsencePolicy`; this
67    // check must not inspect `Default` impls or generated construction values.
68    for field in descriptor.fields() {
69        let slot = usize::from(field.slot().get());
70        if provided_slots.get(slot).copied().unwrap_or(false) {
71            continue;
72        }
73
74        if matches!(field.absence_policy(), AcceptedFieldAbsencePolicy::Required) {
75            return Err(
76                InternalError::mutation_structural_patch_required_field_missing(
77                    E::PATH,
78                    field.name(),
79                ),
80            );
81        }
82    }
83
84    Ok(())
85}
86
87// Preserve generated-field ownership diagnostics ahead of sparse-patch
88// required-field diagnostics. Public structural writes must not author fields
89// whose values are owned by accepted schema write policy, except for the
90// redundant primary-key slot because the structural API already carries the
91// authoritative key separately.
92fn reject_explicit_generated_fields_from_accepted_patch<E>(
93    descriptor: &AcceptedRowLayoutRuntimeContract<'_>,
94    patch: &StructuralPatch,
95) -> Result<(), InternalError>
96where
97    E: PersistedRow + EntityValue,
98{
99    for entry in patch.entries() {
100        let slot = entry.slot().index();
101        let Some(accepted_field) = descriptor.field_for_slot_index(slot) else {
102            continue;
103        };
104        let write_policy = accepted_field.write_policy();
105
106        if write_policy.insert_generation().is_some()
107            && !descriptor.is_primary_key_field_name(accepted_field.name())
108        {
109            return Err(InternalError::mutation_generated_field_explicit(
110                E::PATH,
111                accepted_field.name(),
112            ));
113        }
114    }
115
116    Ok(())
117}
118
119impl<C: CanisterKind> DbSession<C> {
120    /// Insert one entity row.
121    pub fn insert<E>(&self, entity: E) -> Result<E, InternalError>
122    where
123        E: PersistedRow<Canister = C> + EntityValue,
124    {
125        self.execute_save_entity(|save| save.insert(entity))
126    }
127
128    /// Insert one authored typed input.
129    pub fn create<I>(&self, input: I) -> Result<I::Entity, InternalError>
130    where
131        I: EntityCreateInput,
132        I::Entity: PersistedRow<Canister = C> + EntityValue,
133    {
134        self.execute_save_entity(|save| save.create(input))
135    }
136
137    /// Insert a single-entity-type batch atomically in one commit window.
138    ///
139    /// If any item fails pre-commit validation, no row in the batch is persisted.
140    ///
141    /// This API is not a multi-entity transaction surface.
142    pub fn insert_many_atomic<E>(
143        &self,
144        entities: impl IntoIterator<Item = E>,
145    ) -> Result<WriteBatchResponse<E>, InternalError>
146    where
147        E: PersistedRow<Canister = C> + EntityValue,
148    {
149        self.execute_save_batch(|save| save.insert_many_atomic(entities))
150    }
151
152    /// Insert a batch with explicitly non-atomic semantics.
153    ///
154    /// WARNING: fail-fast and non-atomic. Earlier inserts may commit before an error.
155    pub fn insert_many_non_atomic<E>(
156        &self,
157        entities: impl IntoIterator<Item = E>,
158    ) -> Result<WriteBatchResponse<E>, InternalError>
159    where
160        E: PersistedRow<Canister = C> + EntityValue,
161    {
162        self.execute_save_batch(|save| save.insert_many_non_atomic(entities))
163    }
164
165    /// Replace one existing entity row.
166    pub fn replace<E>(&self, entity: E) -> Result<E, InternalError>
167    where
168        E: PersistedRow<Canister = C> + EntityValue,
169    {
170        self.execute_save_entity(|save| save.replace(entity))
171    }
172
173    /// Apply one structural mutation under one explicit write-mode contract.
174    ///
175    /// This is the public core session boundary for structural writes:
176    /// callers provide the key, field patch, and intended mutation mode, and
177    /// the session routes that through the shared structural mutation pipeline.
178    pub fn mutate_structural<E>(
179        &self,
180        key: E::Key,
181        patch: StructuralPatch,
182        mode: MutationMode,
183    ) -> Result<E, InternalError>
184    where
185        E: PersistedRow<Canister = C> + EntityValue,
186    {
187        let accepted_schema = self.ensure_accepted_schema_snapshot::<E>()?;
188        let (descriptor, _) = AcceptedRowLayoutRuntimeContract::from_generated_compatible_schema(
189            &accepted_schema,
190            E::MODEL,
191        )?;
192        validate_structural_patch_schema_policy::<E>(&descriptor, &patch, mode)?;
193        let (
194            row_decode_contract,
195            mutation_row_decode_contract,
196            accepted_schema_info,
197            accepted_schema_fingerprint,
198        ) = accepted_save_contract_for_descriptor::<E>(&accepted_schema, &descriptor)?;
199
200        self.execute_save_with_checked_accepted_row_contract(
201            row_decode_contract,
202            accepted_schema_info,
203            accepted_schema_fingerprint,
204            |save| save.apply_structural_mutation(mode, key, patch, mutation_row_decode_contract),
205            std::convert::identity,
206        )
207    }
208
209    /// Build one structural patch through the accepted schema row layout.
210    ///
211    /// This is the session-owned patch construction boundary for callers that
212    /// can provide all dynamic field updates at once. It resolves field names
213    /// through the accepted row-layout descriptor before the patch reaches the
214    /// generated-compatible write codec bridge.
215    pub fn structural_patch<E, I, S>(&self, fields: I) -> Result<StructuralPatch, InternalError>
216    where
217        E: PersistedRow<Canister = C> + EntityValue,
218        I: IntoIterator<Item = (S, Value)>,
219        S: AsRef<str>,
220    {
221        let accepted_schema = self.ensure_accepted_schema_snapshot::<E>()?;
222        let (descriptor, _) = AcceptedRowLayoutRuntimeContract::from_generated_compatible_schema(
223            &accepted_schema,
224            E::MODEL,
225        )?;
226        let mut patch = StructuralPatch::new();
227
228        // Phase 1: resolve every caller-provided field name against the
229        // accepted descriptor so public structural patch construction no
230        // longer has to choose slots from generated model field order.
231        for (field_name, value) in fields {
232            let field_name = field_name.as_ref();
233            patch = append_accepted_structural_patch_field(
234                E::PATH,
235                &descriptor,
236                patch,
237                field_name,
238                value,
239            )?;
240        }
241
242        Ok(patch)
243    }
244
245    /// Apply one structural replacement, inserting if missing.
246    ///
247    /// Replace semantics still do not inherit omitted fields from the old row.
248    /// Missing fields must materialize through explicit defaults or managed
249    /// field preflight, or the write fails closed.
250    #[cfg(test)]
251    pub(in crate::db) fn replace_structural<E>(
252        &self,
253        key: E::Key,
254        patch: StructuralPatch,
255    ) -> Result<E, InternalError>
256    where
257        E: PersistedRow<Canister = C> + EntityValue,
258    {
259        self.mutate_structural(key, patch, MutationMode::Replace)
260    }
261
262    /// Replace a single-entity-type batch atomically in one commit window.
263    ///
264    /// If any item fails pre-commit validation, no row in the batch is persisted.
265    ///
266    /// This API is not a multi-entity transaction surface.
267    pub fn replace_many_atomic<E>(
268        &self,
269        entities: impl IntoIterator<Item = E>,
270    ) -> Result<WriteBatchResponse<E>, InternalError>
271    where
272        E: PersistedRow<Canister = C> + EntityValue,
273    {
274        self.execute_save_batch(|save| save.replace_many_atomic(entities))
275    }
276
277    /// Replace a batch with explicitly non-atomic semantics.
278    ///
279    /// WARNING: fail-fast and non-atomic. Earlier replaces may commit before an error.
280    pub fn replace_many_non_atomic<E>(
281        &self,
282        entities: impl IntoIterator<Item = E>,
283    ) -> Result<WriteBatchResponse<E>, InternalError>
284    where
285        E: PersistedRow<Canister = C> + EntityValue,
286    {
287        self.execute_save_batch(|save| save.replace_many_non_atomic(entities))
288    }
289
290    /// Update one existing entity row.
291    pub fn update<E>(&self, entity: E) -> Result<E, InternalError>
292    where
293        E: PersistedRow<Canister = C> + EntityValue,
294    {
295        self.execute_save_entity(|save| save.update(entity))
296    }
297
298    /// Apply one structural insert from a patch-defined after-image.
299    ///
300    /// Insert semantics no longer require a pre-built full row image.
301    /// Missing fields still fail closed unless derive-owned materialization can
302    /// supply them through explicit defaults or managed-field preflight.
303    #[cfg(test)]
304    pub(in crate::db) fn insert_structural<E>(
305        &self,
306        key: E::Key,
307        patch: StructuralPatch,
308    ) -> Result<E, InternalError>
309    where
310        E: PersistedRow<Canister = C> + EntityValue,
311    {
312        self.mutate_structural(key, patch, MutationMode::Insert)
313    }
314
315    /// Apply one structural field patch to an existing entity row.
316    ///
317    /// This session-owned boundary keeps structural mutation out of the raw
318    /// executor surface while still routing through the same typed save
319    /// preflight before commit staging.
320    #[cfg(test)]
321    pub(in crate::db) fn update_structural<E>(
322        &self,
323        key: E::Key,
324        patch: StructuralPatch,
325    ) -> Result<E, InternalError>
326    where
327        E: PersistedRow<Canister = C> + EntityValue,
328    {
329        self.mutate_structural(key, patch, MutationMode::Update)
330    }
331
332    /// Update a single-entity-type batch atomically in one commit window.
333    ///
334    /// If any item fails pre-commit validation, no row in the batch is persisted.
335    ///
336    /// This API is not a multi-entity transaction surface.
337    pub fn update_many_atomic<E>(
338        &self,
339        entities: impl IntoIterator<Item = E>,
340    ) -> Result<WriteBatchResponse<E>, InternalError>
341    where
342        E: PersistedRow<Canister = C> + EntityValue,
343    {
344        self.execute_save_batch(|save| save.update_many_atomic(entities))
345    }
346
347    /// Update a batch with explicitly non-atomic semantics.
348    ///
349    /// WARNING: fail-fast and non-atomic. Earlier updates may commit before an error.
350    pub fn update_many_non_atomic<E>(
351        &self,
352        entities: impl IntoIterator<Item = E>,
353    ) -> Result<WriteBatchResponse<E>, InternalError>
354    where
355        E: PersistedRow<Canister = C> + EntityValue,
356    {
357        self.execute_save_batch(|save| save.update_many_non_atomic(entities))
358    }
359}