ic_db/
canister_session.rs1use crate::{
4 Batch, CompiledCanister, CompiledStore, DatabaseSession, Entity, Error, InsertInput, Query,
5 QuerySession, Store, UniqueKey,
6};
7use std::{marker::PhantomData, rc::Rc};
8
9#[doc(hidden)]
11pub trait MountedStore: Store {
12 const SLOT: usize;
14}
15
16pub struct CanisterSession<C> {
18 stores: Rc<[DatabaseSession]>,
19 owner: PhantomData<fn() -> C>,
20}
21
22impl<C> Clone for CanisterSession<C> {
23 fn clone(&self) -> Self {
24 Self {
25 stores: self.stores.clone(),
26 owner: PhantomData,
27 }
28 }
29}
30
31impl<C: CompiledCanister> CanisterSession<C> {
32 pub fn try_attach<F>(
41 attach: impl FnMut(&CompiledStore) -> Result<DatabaseSession, F>,
42 ) -> Result<Self, F> {
43 let stores = C::STORES
44 .iter()
45 .map(attach)
46 .collect::<Result<Rc<[_]>, _>>()?;
47 Ok(Self {
48 stores,
49 owner: PhantomData,
50 })
51 }
52
53 fn session<S: MountedStore<Canister = C>>(&self) -> &DatabaseSession {
54 &self.stores[S::SLOT]
55 }
56
57 #[must_use]
59 pub fn batch<S: MountedStore<Canister = C>>(&self) -> StoreBatch<'_, S> {
60 StoreBatch {
61 session: self.session::<S>(),
62 batch: Batch::new(),
63 store: PhantomData,
64 }
65 }
66
67 #[must_use]
69 pub fn query<'a, E: Entity<Store: MountedStore<Canister = C>>>(&self) -> Query<'a, E> {
70 self.session::<E::Store>().query()
71 }
72
73 pub fn get<E: Entity<Store: MountedStore<Canister = C>>>(
77 &self,
78 key: impl Into<crate::Id<E>>,
79 ) -> Result<Option<E>, Error> {
80 self.session::<E::Store>().get::<E>(key)
81 }
82
83 pub fn get_many<E: Entity<Store: MountedStore<Canister = C>>>(
87 &self,
88 keys: &[impl Copy + Into<crate::Id<E>>],
89 ) -> Result<Vec<Option<E>>, Error> {
90 self.session::<E::Store>().get_many::<E>(keys)
91 }
92
93 pub fn get_unique<K: UniqueKey<Entity: Entity<Store: MountedStore<Canister = C>>>>(
97 &self,
98 key: &K,
99 ) -> Result<Option<K::Entity>, Error> {
100 self.session::<<K::Entity as Entity>::Store>()
101 .get_unique(key)
102 }
103
104 pub fn insert<E: Entity<Store: MountedStore<Canister = C>>>(
108 &self,
109 row: &E,
110 ) -> Result<(), Error> {
111 self.session::<E::Store>().insert(row)
112 }
113
114 pub fn insert_input<I: InsertInput<Entity: Entity<Store: MountedStore<Canister = C>>>>(
119 &self,
120 input: I,
121 ) -> Result<I::Entity, Error> {
122 self.session::<<I::Entity as Entity>::Store>()
123 .insert_input(input)
124 }
125
126 pub fn insert_if_absent<K: UniqueKey<Entity: Entity<Store: MountedStore<Canister = C>>>>(
130 &self,
131 input: impl InsertInput<Entity = K::Entity>,
132 ) -> Result<crate::InsertOutcome<K::Entity>, Error> {
133 self.session::<<K::Entity as Entity>::Store>()
134 .insert_if_absent::<K>(input)
135 }
136
137 pub fn replace<E: Entity<Store: MountedStore<Canister = C>>>(
141 &self,
142 row: &E,
143 ) -> Result<E, Error> {
144 self.session::<E::Store>().replace(row)
145 }
146
147 pub fn delete<E: Entity<Store: MountedStore<Canister = C>>>(
151 &self,
152 key: impl Into<crate::Id<E>>,
153 ) -> Result<(), Error> {
154 self.session::<E::Store>().delete::<E>(key)
155 }
156}
157
158impl<C: CompiledCanister, E: Entity<Store: MountedStore<Canister = C>>> QuerySession<E>
159 for CanisterSession<C>
160{
161 fn entity_query<'a>(&self) -> Query<'a, E> {
162 self.query()
163 }
164}
165
166pub struct StoreBatch<'a, S: Store> {
168 session: &'a DatabaseSession,
169 batch: Batch,
170 store: PhantomData<fn() -> S>,
171}
172
173impl<S: Store> StoreBatch<'_, S> {
174 pub fn insert<E: Entity<Store = S>>(&mut self, row: &E) -> Result<(), Error> {
178 self.batch.insert(row)
179 }
180
181 pub fn insert_input<I: InsertInput<Entity: Entity<Store = S>>>(
185 &mut self,
186 input: I,
187 ) -> Result<I::Entity, Error> {
188 self.batch.insert_input(input)
189 }
190
191 pub fn replace<E: Entity<Store = S>>(&mut self, row: &E) -> Result<(), Error> {
195 self.batch.replace(row)
196 }
197
198 pub fn delete<E: Entity<Store = S>>(
202 &mut self,
203 key: impl Into<crate::Id<E>>,
204 ) -> Result<(), Error> {
205 self.batch.delete::<E>(key)
206 }
207
208 pub fn commit(self) -> Result<(), Error> {
212 self.session.commit(self.batch)
213 }
214}