1use crate::{
8 db::{
9 DbSession, PersistedRow, WriteBatchResponse,
10 data::{FieldSlot, StructuralPatch},
11 executor::MutationMode,
12 schema::{AcceptedFieldAbsencePolicy, AcceptedRowLayoutRuntimeDescriptor},
13 },
14 error::InternalError,
15 traits::{CanisterKind, EntityCreateInput, EntityValue},
16 value::Value,
17};
18
19fn append_accepted_structural_patch_field(
24 entity_path: &'static str,
25 descriptor: &AcceptedRowLayoutRuntimeDescriptor<'_>,
26 patch: StructuralPatch,
27 field_name: &str,
28 value: Value,
29) -> Result<StructuralPatch, InternalError> {
30 let slot = descriptor
31 .field_slot_index_by_name(field_name)
32 .ok_or_else(|| InternalError::mutation_structural_field_unknown(entity_path, field_name))?;
33
34 Ok(patch.set(FieldSlot::from_validated_index(slot), value))
35}
36
37fn validate_structural_patch_schema_policy<E>(
43 descriptor: &AcceptedRowLayoutRuntimeDescriptor<'_>,
44 patch: &StructuralPatch,
45 mode: MutationMode,
46) -> Result<(), InternalError>
47where
48 E: PersistedRow + EntityValue,
49{
50 reject_explicit_generated_fields_from_accepted_patch::<E>(descriptor, patch)?;
51
52 if matches!(mode, MutationMode::Update) {
53 return Ok(());
54 }
55
56 let mut provided_slots = vec![false; descriptor.required_slot_count()];
57 for entry in patch.entries() {
58 let slot = entry.slot().index();
59 if slot < provided_slots.len() {
60 provided_slots[slot] = true;
61 }
62 }
63
64 for field in descriptor.fields() {
68 let slot = usize::from(field.slot().get());
69 if provided_slots.get(slot).copied().unwrap_or(false) {
70 continue;
71 }
72
73 if matches!(field.absence_policy(), AcceptedFieldAbsencePolicy::Required) {
74 return Err(
75 InternalError::mutation_structural_patch_required_field_missing(
76 E::PATH,
77 field.name(),
78 ),
79 );
80 }
81 }
82
83 Ok(())
84}
85
86fn reject_explicit_generated_fields_from_accepted_patch<E>(
92 descriptor: &AcceptedRowLayoutRuntimeDescriptor<'_>,
93 patch: &StructuralPatch,
94) -> Result<(), InternalError>
95where
96 E: PersistedRow + EntityValue,
97{
98 for entry in patch.entries() {
99 let slot = entry.slot().index();
100 let Some(accepted_field) = descriptor.field_for_slot_index(slot) else {
101 continue;
102 };
103 let write_policy = accepted_field.write_policy();
104
105 if write_policy.insert_generation().is_some()
106 && accepted_field.name() != descriptor.primary_key_name()
107 {
108 return Err(InternalError::mutation_generated_field_explicit(
109 E::PATH,
110 accepted_field.name(),
111 ));
112 }
113 }
114
115 Ok(())
116}
117
118impl<C: CanisterKind> DbSession<C> {
119 pub fn insert<E>(&self, entity: E) -> Result<E, InternalError>
121 where
122 E: PersistedRow<Canister = C> + EntityValue,
123 {
124 self.execute_save_entity(|save| save.insert(entity))
125 }
126
127 pub fn create<I>(&self, input: I) -> Result<I::Entity, InternalError>
129 where
130 I: EntityCreateInput,
131 I::Entity: PersistedRow<Canister = C> + EntityValue,
132 {
133 self.execute_save_entity(|save| save.create(input))
134 }
135
136 pub fn insert_many_atomic<E>(
142 &self,
143 entities: impl IntoIterator<Item = E>,
144 ) -> Result<WriteBatchResponse<E>, InternalError>
145 where
146 E: PersistedRow<Canister = C> + EntityValue,
147 {
148 self.execute_save_batch(|save| save.insert_many_atomic(entities))
149 }
150
151 pub fn insert_many_non_atomic<E>(
155 &self,
156 entities: impl IntoIterator<Item = E>,
157 ) -> Result<WriteBatchResponse<E>, InternalError>
158 where
159 E: PersistedRow<Canister = C> + EntityValue,
160 {
161 self.execute_save_batch(|save| save.insert_many_non_atomic(entities))
162 }
163
164 pub fn replace<E>(&self, entity: E) -> Result<E, InternalError>
166 where
167 E: PersistedRow<Canister = C> + EntityValue,
168 {
169 self.execute_save_entity(|save| save.replace(entity))
170 }
171
172 pub fn mutate_structural<E>(
178 &self,
179 key: E::Key,
180 patch: StructuralPatch,
181 mode: MutationMode,
182 ) -> Result<E, InternalError>
183 where
184 E: PersistedRow<Canister = C> + EntityValue,
185 {
186 let accepted_schema = self.ensure_accepted_schema_snapshot::<E>()?;
187 let (descriptor, _) = AcceptedRowLayoutRuntimeDescriptor::from_generated_compatible_schema(
188 &accepted_schema,
189 E::MODEL,
190 )?;
191 validate_structural_patch_schema_policy::<E>(&descriptor, &patch, mode)?;
192
193 let row_decode_contract = descriptor.row_decode_contract();
194 let mutation_row_decode_contract = row_decode_contract.clone();
195
196 self.execute_save_with_checked_accepted_row_contract(
197 row_decode_contract,
198 |save| {
199 save.apply_structural_mutation(mode, key, patch, Some(mutation_row_decode_contract))
200 },
201 std::convert::identity,
202 )
203 }
204
205 pub fn structural_patch<E, I, S>(&self, fields: I) -> Result<StructuralPatch, InternalError>
212 where
213 E: PersistedRow<Canister = C> + EntityValue,
214 I: IntoIterator<Item = (S, Value)>,
215 S: AsRef<str>,
216 {
217 let accepted_schema = self.ensure_accepted_schema_snapshot::<E>()?;
218 let (descriptor, _) = AcceptedRowLayoutRuntimeDescriptor::from_generated_compatible_schema(
219 &accepted_schema,
220 E::MODEL,
221 )?;
222 let mut patch = StructuralPatch::new();
223
224 for (field_name, value) in fields {
228 let field_name = field_name.as_ref();
229 patch = append_accepted_structural_patch_field(
230 E::PATH,
231 &descriptor,
232 patch,
233 field_name,
234 value,
235 )?;
236 }
237
238 Ok(patch)
239 }
240
241 #[cfg(test)]
247 pub(in crate::db) fn replace_structural<E>(
248 &self,
249 key: E::Key,
250 patch: StructuralPatch,
251 ) -> Result<E, InternalError>
252 where
253 E: PersistedRow<Canister = C> + EntityValue,
254 {
255 self.mutate_structural(key, patch, MutationMode::Replace)
256 }
257
258 pub fn replace_many_atomic<E>(
264 &self,
265 entities: impl IntoIterator<Item = E>,
266 ) -> Result<WriteBatchResponse<E>, InternalError>
267 where
268 E: PersistedRow<Canister = C> + EntityValue,
269 {
270 self.execute_save_batch(|save| save.replace_many_atomic(entities))
271 }
272
273 pub fn replace_many_non_atomic<E>(
277 &self,
278 entities: impl IntoIterator<Item = E>,
279 ) -> Result<WriteBatchResponse<E>, InternalError>
280 where
281 E: PersistedRow<Canister = C> + EntityValue,
282 {
283 self.execute_save_batch(|save| save.replace_many_non_atomic(entities))
284 }
285
286 pub fn update<E>(&self, entity: E) -> Result<E, InternalError>
288 where
289 E: PersistedRow<Canister = C> + EntityValue,
290 {
291 self.execute_save_entity(|save| save.update(entity))
292 }
293
294 #[cfg(test)]
300 pub(in crate::db) fn insert_structural<E>(
301 &self,
302 key: E::Key,
303 patch: StructuralPatch,
304 ) -> Result<E, InternalError>
305 where
306 E: PersistedRow<Canister = C> + EntityValue,
307 {
308 self.mutate_structural(key, patch, MutationMode::Insert)
309 }
310
311 #[cfg(test)]
317 pub(in crate::db) fn update_structural<E>(
318 &self,
319 key: E::Key,
320 patch: StructuralPatch,
321 ) -> Result<E, InternalError>
322 where
323 E: PersistedRow<Canister = C> + EntityValue,
324 {
325 self.mutate_structural(key, patch, MutationMode::Update)
326 }
327
328 pub fn update_many_atomic<E>(
334 &self,
335 entities: impl IntoIterator<Item = E>,
336 ) -> Result<WriteBatchResponse<E>, InternalError>
337 where
338 E: PersistedRow<Canister = C> + EntityValue,
339 {
340 self.execute_save_batch(|save| save.update_many_atomic(entities))
341 }
342
343 pub fn update_many_non_atomic<E>(
347 &self,
348 entities: impl IntoIterator<Item = E>,
349 ) -> Result<WriteBatchResponse<E>, InternalError>
350 where
351 E: PersistedRow<Canister = C> + EntityValue,
352 {
353 self.execute_save_batch(|save| save.update_many_non_atomic(entities))
354 }
355}