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
7#[cfg(test)]
8use crate::db::{DataStore, IndexStore};
9use crate::{
10    db::{DbSession, PersistedRow, WriteBatchResponse, data::UpdatePatch, executor::MutationMode},
11    error::InternalError,
12    traits::{CanisterKind, EntityValue},
13};
14
15impl<C: CanisterKind> DbSession<C> {
16    /// Insert one entity row.
17    pub fn insert<E>(&self, entity: E) -> Result<E, InternalError>
18    where
19        E: PersistedRow<Canister = C> + EntityValue,
20    {
21        self.execute_save_entity(|save| save.insert(entity))
22    }
23
24    /// Insert a single-entity-type batch atomically in one commit window.
25    ///
26    /// If any item fails pre-commit validation, no row in the batch is persisted.
27    ///
28    /// This API is not a multi-entity transaction surface.
29    pub fn insert_many_atomic<E>(
30        &self,
31        entities: impl IntoIterator<Item = E>,
32    ) -> Result<WriteBatchResponse<E>, InternalError>
33    where
34        E: PersistedRow<Canister = C> + EntityValue,
35    {
36        self.execute_save_batch(|save| save.insert_many_atomic(entities))
37    }
38
39    /// Insert a batch with explicitly non-atomic semantics.
40    ///
41    /// WARNING: fail-fast and non-atomic. Earlier inserts may commit before an error.
42    pub fn insert_many_non_atomic<E>(
43        &self,
44        entities: impl IntoIterator<Item = E>,
45    ) -> Result<WriteBatchResponse<E>, InternalError>
46    where
47        E: PersistedRow<Canister = C> + EntityValue,
48    {
49        self.execute_save_batch(|save| save.insert_many_non_atomic(entities))
50    }
51
52    /// Replace one existing entity row.
53    pub fn replace<E>(&self, entity: E) -> Result<E, InternalError>
54    where
55        E: PersistedRow<Canister = C> + EntityValue,
56    {
57        self.execute_save_entity(|save| save.replace(entity))
58    }
59
60    /// Apply one structural mutation under one explicit write-mode contract.
61    ///
62    /// This is the public core session boundary for structural writes:
63    /// callers provide the key, field patch, and intended mutation mode, and
64    /// the session routes that through the shared structural mutation pipeline.
65    pub fn mutate_structural<E>(
66        &self,
67        key: E::Key,
68        patch: UpdatePatch,
69        mode: MutationMode,
70    ) -> Result<E, InternalError>
71    where
72        E: PersistedRow<Canister = C> + EntityValue,
73    {
74        self.execute_save_entity(|save| save.apply_structural_mutation(mode, key, patch))
75    }
76
77    /// Apply one structural full-row replacement, inserting if missing.
78    ///
79    /// Replace semantics rebuild the after-image from an empty row layout, so
80    /// omitted fields do not inherit old-row values implicitly.
81    #[cfg(test)]
82    pub(in crate::db) fn replace_structural<E>(
83        &self,
84        key: E::Key,
85        patch: UpdatePatch,
86    ) -> Result<E, InternalError>
87    where
88        E: PersistedRow<Canister = C> + EntityValue,
89    {
90        self.mutate_structural(key, patch, MutationMode::Replace)
91    }
92
93    /// Replace a single-entity-type batch atomically in one commit window.
94    ///
95    /// If any item fails pre-commit validation, no row in the batch is persisted.
96    ///
97    /// This API is not a multi-entity transaction surface.
98    pub fn replace_many_atomic<E>(
99        &self,
100        entities: impl IntoIterator<Item = E>,
101    ) -> Result<WriteBatchResponse<E>, InternalError>
102    where
103        E: PersistedRow<Canister = C> + EntityValue,
104    {
105        self.execute_save_batch(|save| save.replace_many_atomic(entities))
106    }
107
108    /// Replace a batch with explicitly non-atomic semantics.
109    ///
110    /// WARNING: fail-fast and non-atomic. Earlier replaces may commit before an error.
111    pub fn replace_many_non_atomic<E>(
112        &self,
113        entities: impl IntoIterator<Item = E>,
114    ) -> Result<WriteBatchResponse<E>, InternalError>
115    where
116        E: PersistedRow<Canister = C> + EntityValue,
117    {
118        self.execute_save_batch(|save| save.replace_many_non_atomic(entities))
119    }
120
121    /// Update one existing entity row.
122    pub fn update<E>(&self, entity: E) -> Result<E, InternalError>
123    where
124        E: PersistedRow<Canister = C> + EntityValue,
125    {
126        self.execute_save_entity(|save| save.update(entity))
127    }
128
129    /// Apply one structural insert from a patch-defined full after-image.
130    ///
131    /// Insert semantics require the patch to describe the full row payload
132    /// because no old-row baseline exists to fill missing fields.
133    #[cfg(test)]
134    pub(in crate::db) fn insert_structural<E>(
135        &self,
136        key: E::Key,
137        patch: UpdatePatch,
138    ) -> Result<E, InternalError>
139    where
140        E: PersistedRow<Canister = C> + EntityValue,
141    {
142        self.mutate_structural(key, patch, MutationMode::Insert)
143    }
144
145    /// Apply one structural field patch to an existing entity row.
146    ///
147    /// This session-owned boundary keeps structural mutation out of the raw
148    /// executor surface while still routing through the same typed save
149    /// preflight before commit staging.
150    #[cfg(test)]
151    pub(in crate::db) fn update_structural<E>(
152        &self,
153        key: E::Key,
154        patch: UpdatePatch,
155    ) -> Result<E, InternalError>
156    where
157        E: PersistedRow<Canister = C> + EntityValue,
158    {
159        self.mutate_structural(key, patch, MutationMode::Update)
160    }
161
162    /// Update a single-entity-type batch atomically in one commit window.
163    ///
164    /// If any item fails pre-commit validation, no row in the batch is persisted.
165    ///
166    /// This API is not a multi-entity transaction surface.
167    pub fn update_many_atomic<E>(
168        &self,
169        entities: impl IntoIterator<Item = E>,
170    ) -> Result<WriteBatchResponse<E>, InternalError>
171    where
172        E: PersistedRow<Canister = C> + EntityValue,
173    {
174        self.execute_save_batch(|save| save.update_many_atomic(entities))
175    }
176
177    /// Update a batch with explicitly non-atomic semantics.
178    ///
179    /// WARNING: fail-fast and non-atomic. Earlier updates may commit before an error.
180    pub fn update_many_non_atomic<E>(
181        &self,
182        entities: impl IntoIterator<Item = E>,
183    ) -> Result<WriteBatchResponse<E>, InternalError>
184    where
185        E: PersistedRow<Canister = C> + EntityValue,
186    {
187        self.execute_save_batch(|save| save.update_many_non_atomic(entities))
188    }
189
190    /// TEST ONLY: clear all registered data and index stores for this database.
191    #[cfg(test)]
192    #[doc(hidden)]
193    pub fn clear_stores_for_tests(&self) {
194        self.db.with_store_registry(|reg| {
195            // Test cleanup only: clearing all stores is set-like and does not
196            // depend on registry iteration order.
197            for (_, store) in reg.iter() {
198                store.with_data_mut(DataStore::clear);
199                store.with_index_mut(IndexStore::clear);
200            }
201        });
202    }
203}