icydb_core/db/session/
write.rs1#[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 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 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 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 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 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 #[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 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 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 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 #[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 #[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 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 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 #[cfg(test)]
192 #[doc(hidden)]
193 pub fn clear_stores_for_tests(&self) {
194 self.db.with_store_registry(|reg| {
195 for (_, store) in reg.iter() {
198 store.with_data_mut(DataStore::clear);
199 store.with_index_mut(IndexStore::clear);
200 }
201 });
202 }
203}