ic_db/
canister_session.rs1use crate::{
4 Batch, CompiledCanister, CompiledStore, DatabaseSession, Entity, Error, InsertInput, Query,
5 QuerySession, Store, Timestamp, 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 self.bind_batch(Batch::new())
61 }
62
63 #[must_use]
65 pub fn batch_at<S: MountedStore<Canister = C>>(&self, time: Timestamp) -> StoreBatch<'_, S> {
66 self.bind_batch(Batch::at(time))
67 }
68
69 fn bind_batch<S: MountedStore<Canister = C>>(&self, batch: Batch) -> StoreBatch<'_, S> {
70 StoreBatch {
71 session: self.session::<S>(),
72 batch,
73 store: PhantomData,
74 }
75 }
76
77 #[must_use]
79 pub fn query<'a, E: Entity<Store: MountedStore<Canister = C>>>(&self) -> Query<'a, E> {
80 self.session::<E::Store>().query()
81 }
82
83 pub fn get<E: Entity<Store: MountedStore<Canister = C>>>(
87 &self,
88 key: E::Key,
89 ) -> Result<Option<E>, Error> {
90 self.session::<E::Store>().get::<E>(key)
91 }
92
93 pub fn get_many<E: Entity<Store: MountedStore<Canister = C>>>(
97 &self,
98 keys: &[E::Key],
99 ) -> Result<Vec<Option<E>>, Error> {
100 self.session::<E::Store>().get_many::<E>(keys)
101 }
102
103 pub fn get_unique<K: UniqueKey<Entity: Entity<Store: MountedStore<Canister = C>>>>(
107 &self,
108 key: &K,
109 ) -> Result<Option<K::Entity>, Error> {
110 self.session::<<K::Entity as Entity>::Store>()
111 .get_unique(key)
112 }
113
114 pub fn insert<E: Entity<Store: MountedStore<Canister = C>>>(
118 &self,
119 row: &E,
120 ) -> Result<(), Error> {
121 self.session::<E::Store>().insert(row)
122 }
123
124 pub fn insert_input<I: InsertInput<Entity: Entity<Store: MountedStore<Canister = C>>>>(
129 &self,
130 input: I,
131 ) -> Result<I::Entity, Error> {
132 self.session::<<I::Entity as Entity>::Store>()
133 .insert_input(input)
134 }
135
136 pub fn replace<E: Entity<Store: MountedStore<Canister = C>>>(
140 &self,
141 row: &E,
142 ) -> Result<(), Error> {
143 self.session::<E::Store>().replace(row)
144 }
145
146 pub fn delete<E: Entity<Store: MountedStore<Canister = C>>>(
150 &self,
151 key: E::Key,
152 ) -> Result<(), Error> {
153 self.session::<E::Store>().delete::<E>(key)
154 }
155}
156
157impl<C: CompiledCanister, E: Entity<Store: MountedStore<Canister = C>>> QuerySession<E>
158 for CanisterSession<C>
159{
160 fn entity_query<'a>(&self) -> Query<'a, E> {
161 self.query()
162 }
163}
164
165pub struct StoreBatch<'a, S: Store> {
167 session: &'a DatabaseSession,
168 batch: Batch,
169 store: PhantomData<fn() -> S>,
170}
171
172impl<S: Store> StoreBatch<'_, S> {
173 pub fn insert<E: Entity<Store = S>>(&mut self, row: &E) -> Result<(), Error> {
177 self.batch.insert(row)
178 }
179
180 pub fn insert_input<I: InsertInput<Entity: Entity<Store = S>>>(
184 &mut self,
185 input: I,
186 ) -> Result<I::Entity, Error> {
187 self.batch.insert_input(input)
188 }
189
190 pub fn replace<E: Entity<Store = S>>(&mut self, row: &E) -> Result<(), Error> {
194 self.batch.replace(row)
195 }
196
197 pub fn delete<E: Entity<Store = S>>(&mut self, key: E::Key) -> Result<(), Error> {
201 self.batch.delete::<E>(key)
202 }
203
204 pub fn commit(self) -> Result<(), Error> {
208 self.session.commit(self.batch)
209 }
210}