icydb_core/db/session/
write.rs1#[cfg(test)]
2use crate::db::{DataStore, IndexStore};
3use crate::{
4 db::{
5 DbSession, PersistedRow, WriteBatchResponse, data::UpdatePatch,
6 executor::StructuralMutationMode,
7 },
8 error::InternalError,
9 traits::{CanisterKind, EntityValue},
10};
11
12impl<C: CanisterKind> DbSession<C> {
13 pub fn insert<E>(&self, entity: E) -> Result<E, InternalError>
15 where
16 E: PersistedRow<Canister = C> + EntityValue,
17 {
18 self.execute_save_entity(|save| save.insert(entity))
19 }
20
21 pub fn insert_many_atomic<E>(
27 &self,
28 entities: impl IntoIterator<Item = E>,
29 ) -> Result<WriteBatchResponse<E>, InternalError>
30 where
31 E: PersistedRow<Canister = C> + EntityValue,
32 {
33 self.execute_save_batch(|save| save.insert_many_atomic(entities))
34 }
35
36 pub fn insert_many_non_atomic<E>(
40 &self,
41 entities: impl IntoIterator<Item = E>,
42 ) -> Result<WriteBatchResponse<E>, InternalError>
43 where
44 E: PersistedRow<Canister = C> + EntityValue,
45 {
46 self.execute_save_batch(|save| save.insert_many_non_atomic(entities))
47 }
48
49 pub fn replace<E>(&self, entity: E) -> Result<E, InternalError>
51 where
52 E: PersistedRow<Canister = C> + EntityValue,
53 {
54 self.execute_save_entity(|save| save.replace(entity))
55 }
56
57 pub fn mutate_structural<E>(
63 &self,
64 key: E::Key,
65 patch: UpdatePatch,
66 mode: StructuralMutationMode,
67 ) -> Result<E, InternalError>
68 where
69 E: PersistedRow<Canister = C> + EntityValue,
70 {
71 self.execute_save_entity(|save| save.apply_structural_mutation(mode, key, patch))
72 }
73
74 #[allow(dead_code)]
79 pub(in crate::db) fn replace_structural<E>(
80 &self,
81 key: E::Key,
82 patch: UpdatePatch,
83 ) -> Result<E, InternalError>
84 where
85 E: PersistedRow<Canister = C> + EntityValue,
86 {
87 self.mutate_structural(key, patch, StructuralMutationMode::Replace)
88 }
89
90 pub fn replace_many_atomic<E>(
96 &self,
97 entities: impl IntoIterator<Item = E>,
98 ) -> Result<WriteBatchResponse<E>, InternalError>
99 where
100 E: PersistedRow<Canister = C> + EntityValue,
101 {
102 self.execute_save_batch(|save| save.replace_many_atomic(entities))
103 }
104
105 pub fn replace_many_non_atomic<E>(
109 &self,
110 entities: impl IntoIterator<Item = E>,
111 ) -> Result<WriteBatchResponse<E>, InternalError>
112 where
113 E: PersistedRow<Canister = C> + EntityValue,
114 {
115 self.execute_save_batch(|save| save.replace_many_non_atomic(entities))
116 }
117
118 pub fn update<E>(&self, entity: E) -> Result<E, InternalError>
120 where
121 E: PersistedRow<Canister = C> + EntityValue,
122 {
123 self.execute_save_entity(|save| save.update(entity))
124 }
125
126 #[allow(dead_code)]
131 pub(in crate::db) fn insert_structural<E>(
132 &self,
133 key: E::Key,
134 patch: UpdatePatch,
135 ) -> Result<E, InternalError>
136 where
137 E: PersistedRow<Canister = C> + EntityValue,
138 {
139 self.mutate_structural(key, patch, StructuralMutationMode::Insert)
140 }
141
142 #[allow(dead_code)]
148 pub(in crate::db) fn update_structural<E>(
149 &self,
150 key: E::Key,
151 patch: UpdatePatch,
152 ) -> Result<E, InternalError>
153 where
154 E: PersistedRow<Canister = C> + EntityValue,
155 {
156 self.mutate_structural(key, patch, StructuralMutationMode::Update)
157 }
158
159 pub fn update_many_atomic<E>(
165 &self,
166 entities: impl IntoIterator<Item = E>,
167 ) -> Result<WriteBatchResponse<E>, InternalError>
168 where
169 E: PersistedRow<Canister = C> + EntityValue,
170 {
171 self.execute_save_batch(|save| save.update_many_atomic(entities))
172 }
173
174 pub fn update_many_non_atomic<E>(
178 &self,
179 entities: impl IntoIterator<Item = E>,
180 ) -> Result<WriteBatchResponse<E>, InternalError>
181 where
182 E: PersistedRow<Canister = C> + EntityValue,
183 {
184 self.execute_save_batch(|save| save.update_many_non_atomic(entities))
185 }
186
187 #[cfg(test)]
189 #[doc(hidden)]
190 pub fn clear_stores_for_tests(&self) {
191 self.db.with_store_registry(|reg| {
192 for (_, store) in reg.iter() {
195 store.with_data_mut(DataStore::clear);
196 store.with_index_mut(IndexStore::clear);
197 }
198 });
199 }
200}