1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! Route typed operations to the single-store session admitted by the host.
use crate::{
Batch, CompiledCanister, CompiledStore, DatabaseSession, Entity, Error, InsertInput, Query,
QuerySession, Store, UniqueKey,
};
use std::{marker::PhantomData, rc::Rc};
/// Implemented by schema assembly for stores in a compiled canister roster.
#[doc(hidden)]
pub trait MountedStore: Store {
/// Executable-local position in the owner's roster.
const SLOT: usize;
}
/// All store sessions for one declared canister. Clones share the same leases.
pub struct CanisterSession<C> {
stores: Rc<[DatabaseSession]>,
owner: PhantomData<fn() -> C>,
}
impl<C> Clone for CanisterSession<C> {
fn clone(&self) -> Self {
Self {
stores: self.stores.clone(),
owner: PhantomData,
}
}
}
impl<C: CompiledCanister> CanisterSession<C> {
/// Attach every store once, in compiled roster order. The host must acquire
/// the lease named by each descriptor's key and create or reopen it with
/// that descriptor's schema. A failed attachment drops collected handles.
/// A host creating stores must trap its lifecycle message on failure to
/// roll back any preceding creates.
///
/// # Errors
/// Returns the first host error without exposing a partially attached session.
pub fn try_attach<F>(
attach: impl FnMut(&CompiledStore) -> Result<DatabaseSession, F>,
) -> Result<Self, F> {
let stores = C::STORES
.iter()
.map(attach)
.collect::<Result<Rc<[_]>, _>>()?;
Ok(Self {
stores,
owner: PhantomData,
})
}
fn session<S: MountedStore<Canister = C>>(&self) -> &DatabaseSession {
&self.stores[S::SLOT]
}
/// Prepare dependent changes in one store, inferred from the entities added.
#[must_use]
pub fn batch<S: MountedStore<Canister = C>>(&self) -> StoreBatch<'_, S> {
StoreBatch {
session: self.session::<S>(),
batch: Batch::new(),
store: PhantomData,
}
}
/// Build a live query in the entity's declared store.
#[must_use]
pub fn query<'a, E: Entity<Store: MountedStore<Canister = C>>>(&self) -> Query<'a, E> {
self.session::<E::Store>().query()
}
/// Read an entity by its primary key.
/// # Errors
/// Propagates the owning store's lookup errors.
pub fn get<E: Entity<Store: MountedStore<Canister = C>>>(
&self,
key: impl Into<crate::Id<E>>,
) -> Result<Option<E>, Error> {
self.session::<E::Store>().get::<E>(key)
}
/// Read up to 32 primary keys in input order, preserving duplicates and missing slots.
/// # Errors
/// Propagates the owning store session’s lookup and limit errors.
pub fn get_many<E: Entity<Store: MountedStore<Canister = C>>>(
&self,
keys: &[E::Key],
) -> Result<Vec<Option<E>>, Error> {
self.session::<E::Store>().get_many::<E>(keys)
}
/// Read an entity using its generated unique key.
/// # Errors
/// Propagates the owning store's unique lookup errors.
pub fn get_unique<K: UniqueKey<Entity: Entity<Store: MountedStore<Canister = C>>>>(
&self,
key: &K,
) -> Result<Option<K::Entity>, Error> {
self.session::<<K::Entity as Entity>::Store>()
.get_unique(key)
}
/// Insert one row in its declared store.
/// # Errors
/// Propagates batch preparation and commit errors.
pub fn insert<E: Entity<Store: MountedStore<Canister = C>>>(
&self,
row: &E,
) -> Result<(), Error> {
self.session::<E::Store>().insert(row)
}
/// Insert one generated input in its declared store and return the committed row.
/// Managed fields receive the current operation time.
/// # Errors
/// Propagates [`DatabaseSession::insert_input`] preparation and commit errors.
pub fn insert_input<I: InsertInput<Entity: Entity<Store: MountedStore<Canister = C>>>>(
&self,
input: I,
) -> Result<I::Entity, Error> {
self.session::<<I::Entity as Entity>::Store>()
.insert_input(input)
}
/// Replace one row in its declared store and return the stored value.
/// # Errors
/// Propagates batch preparation and commit errors.
pub fn replace<E: Entity<Store: MountedStore<Canister = C>>>(
&self,
row: &E,
) -> Result<E, Error> {
self.session::<E::Store>().replace(row)
}
/// Delete one row from its declared store by primary key.
/// # Errors
/// Propagates batch preparation and commit errors.
pub fn delete<E: Entity<Store: MountedStore<Canister = C>>>(
&self,
key: impl Into<crate::Id<E>>,
) -> Result<(), Error> {
self.session::<E::Store>().delete::<E>(key)
}
}
impl<C: CompiledCanister, E: Entity<Store: MountedStore<Canister = C>>> QuerySession<E>
for CanisterSession<C>
{
fn entity_query<'a>(&self) -> Query<'a, E> {
self.query()
}
}
/// A synchronous batch bound to one mounted store. Dropping it discards its prepared changes.
pub struct StoreBatch<'a, S: Store> {
session: &'a DatabaseSession,
batch: Batch,
store: PhantomData<fn() -> S>,
}
impl<S: Store> StoreBatch<'_, S> {
/// Queue an insertion in this store.
/// # Errors
/// Propagates [`Batch::insert`] admission errors.
pub fn insert<E: Entity<Store = S>>(&mut self, row: &E) -> Result<(), Error> {
self.batch.insert(row)
}
/// Queue an insertion using the entity's generated input and return the prepared row.
/// # Errors
/// Propagates [`Batch::insert_input`] admission errors.
pub fn insert_input<I: InsertInput<Entity: Entity<Store = S>>>(
&mut self,
input: I,
) -> Result<I::Entity, Error> {
self.batch.insert_input(input)
}
/// Queue a replacement in this store.
/// # Errors
/// Propagates [`Batch::replace`] admission errors.
pub fn replace<E: Entity<Store = S>>(&mut self, row: &E) -> Result<(), Error> {
self.batch.replace(row)
}
/// Queue a deletion in this store.
/// # Errors
/// Propagates [`Batch::delete`] admission errors.
pub fn delete<E: Entity<Store = S>>(
&mut self,
key: impl Into<crate::Id<E>>,
) -> Result<(), Error> {
self.batch.delete::<E>(key)
}
/// Apply the prepared changes through the owning store session.
/// # Errors
/// Propagates [`DatabaseSession::commit`] borrow and commit errors.
pub fn commit(self) -> Result<(), Error> {
self.session.commit(self.batch)
}
}