1mod query;
7#[cfg(feature = "sql")]
8mod sql;
9#[cfg(all(test, feature = "sql"))]
13mod tests;
14mod write;
15
16use crate::{
17 db::{
18 Db, EntityFieldDescription, EntitySchemaDescription, FluentDeleteQuery, FluentLoadQuery,
19 IndexState, IntegrityReport, MigrationPlan, MigrationRunOutcome, MissingRowPolicy,
20 PersistedRow, Query, QueryError, StorageReport, StoreRegistry, WriteBatchResponse,
21 commit::EntityRuntimeHooks,
22 data::DataKey,
23 executor::{DeleteExecutor, LoadExecutor, SaveExecutor},
24 query::plan::VisibleIndexes,
25 schema::{
26 describe_entity_model, show_indexes_for_model,
27 show_indexes_for_model_with_runtime_state,
28 },
29 },
30 error::InternalError,
31 metrics::sink::{MetricsSink, with_metrics_sink},
32 model::entity::EntityModel,
33 traits::{CanisterKind, EntityKind, EntityValue, Path},
34 value::Value,
35};
36use std::thread::LocalKey;
37
38#[cfg(feature = "diagnostics")]
39pub use query::QueryExecutionAttribution;
40#[cfg(all(feature = "sql", feature = "diagnostics"))]
41pub use sql::SqlQueryExecutionAttribution;
42#[cfg(feature = "sql")]
43pub use sql::SqlStatementResult;
44#[cfg(all(feature = "sql", feature = "diagnostics"))]
45pub use sql::{SqlProjectionMaterializationMetrics, with_sql_projection_materialization_metrics};
46
47pub struct DbSession<C: CanisterKind> {
54 db: Db<C>,
55 debug: bool,
56 metrics: Option<&'static dyn MetricsSink>,
57}
58
59impl<C: CanisterKind> DbSession<C> {
60 #[must_use]
62 pub(crate) const fn new(db: Db<C>) -> Self {
63 Self {
64 db,
65 debug: false,
66 metrics: None,
67 }
68 }
69
70 #[must_use]
72 pub const fn new_with_hooks(
73 store: &'static LocalKey<StoreRegistry>,
74 entity_runtime_hooks: &'static [EntityRuntimeHooks<C>],
75 ) -> Self {
76 Self::new(Db::new_with_hooks(store, entity_runtime_hooks))
77 }
78
79 #[must_use]
81 pub const fn debug(mut self) -> Self {
82 self.debug = true;
83 self
84 }
85
86 #[must_use]
88 pub const fn metrics_sink(mut self, sink: &'static dyn MetricsSink) -> Self {
89 self.metrics = Some(sink);
90 self
91 }
92
93 const fn fluent_load_query<E>(&self, consistency: MissingRowPolicy) -> FluentLoadQuery<'_, E>
96 where
97 E: EntityKind<Canister = C>,
98 {
99 FluentLoadQuery::new(self, Query::new(consistency))
100 }
101
102 fn fluent_delete_query<E>(&self, consistency: MissingRowPolicy) -> FluentDeleteQuery<'_, E>
106 where
107 E: PersistedRow<Canister = C>,
108 {
109 FluentDeleteQuery::new(self, Query::new(consistency).delete())
110 }
111
112 fn with_metrics<T>(&self, f: impl FnOnce() -> T) -> T {
113 if let Some(sink) = self.metrics {
114 with_metrics_sink(sink, f)
115 } else {
116 f()
117 }
118 }
119
120 fn execute_save_with<E, T, R>(
122 &self,
123 op: impl FnOnce(SaveExecutor<E>) -> Result<T, InternalError>,
124 map: impl FnOnce(T) -> R,
125 ) -> Result<R, InternalError>
126 where
127 E: PersistedRow<Canister = C> + EntityValue,
128 {
129 let value = self.with_metrics(|| op(self.save_executor::<E>()))?;
130
131 Ok(map(value))
132 }
133
134 fn execute_save_entity<E>(
136 &self,
137 op: impl FnOnce(SaveExecutor<E>) -> Result<E, InternalError>,
138 ) -> Result<E, InternalError>
139 where
140 E: PersistedRow<Canister = C> + EntityValue,
141 {
142 self.execute_save_with(op, std::convert::identity)
143 }
144
145 fn execute_save_batch<E>(
146 &self,
147 op: impl FnOnce(SaveExecutor<E>) -> Result<Vec<E>, InternalError>,
148 ) -> Result<WriteBatchResponse<E>, InternalError>
149 where
150 E: PersistedRow<Canister = C> + EntityValue,
151 {
152 self.execute_save_with(op, WriteBatchResponse::new)
153 }
154
155 #[must_use]
161 pub const fn load<E>(&self) -> FluentLoadQuery<'_, E>
162 where
163 E: EntityKind<Canister = C>,
164 {
165 self.fluent_load_query(MissingRowPolicy::Ignore)
166 }
167
168 #[must_use]
170 pub const fn load_with_consistency<E>(
171 &self,
172 consistency: MissingRowPolicy,
173 ) -> FluentLoadQuery<'_, E>
174 where
175 E: EntityKind<Canister = C>,
176 {
177 self.fluent_load_query(consistency)
178 }
179
180 #[must_use]
182 pub fn delete<E>(&self) -> FluentDeleteQuery<'_, E>
183 where
184 E: PersistedRow<Canister = C>,
185 {
186 self.fluent_delete_query(MissingRowPolicy::Ignore)
187 }
188
189 #[must_use]
191 pub fn delete_with_consistency<E>(
192 &self,
193 consistency: MissingRowPolicy,
194 ) -> FluentDeleteQuery<'_, E>
195 where
196 E: PersistedRow<Canister = C>,
197 {
198 self.fluent_delete_query(consistency)
199 }
200
201 #[must_use]
205 pub const fn select_one(&self) -> Value {
206 Value::Int(1)
207 }
208
209 #[must_use]
216 pub fn show_indexes<E>(&self) -> Vec<String>
217 where
218 E: EntityKind<Canister = C>,
219 {
220 self.show_indexes_for_store_model(E::Store::PATH, E::MODEL)
221 }
222
223 #[must_use]
229 pub fn show_indexes_for_model(&self, model: &'static EntityModel) -> Vec<String> {
230 show_indexes_for_model(model)
231 }
232
233 pub(in crate::db) fn show_indexes_for_store_model(
237 &self,
238 store_path: &str,
239 model: &'static EntityModel,
240 ) -> Vec<String> {
241 let runtime_state = self
242 .db
243 .with_store_registry(|registry| registry.try_get_store(store_path).ok())
244 .map(|store| store.index_state());
245
246 show_indexes_for_model_with_runtime_state(model, runtime_state)
247 }
248
249 #[must_use]
251 pub fn show_columns<E>(&self) -> Vec<EntityFieldDescription>
252 where
253 E: EntityKind<Canister = C>,
254 {
255 self.show_columns_for_model(E::MODEL)
256 }
257
258 #[must_use]
260 pub fn show_columns_for_model(
261 &self,
262 model: &'static EntityModel,
263 ) -> Vec<EntityFieldDescription> {
264 describe_entity_model(model).fields().to_vec()
265 }
266
267 #[must_use]
269 pub fn show_entities(&self) -> Vec<String> {
270 self.db.runtime_entity_names()
271 }
272
273 #[must_use]
278 pub fn show_tables(&self) -> Vec<String> {
279 self.show_entities()
280 }
281
282 fn visible_indexes_for_store_model(
285 &self,
286 store_path: &str,
287 model: &'static EntityModel,
288 ) -> Result<VisibleIndexes<'static>, QueryError> {
289 let store = self
292 .db
293 .recovered_store(store_path)
294 .map_err(QueryError::execute)?;
295 let state = store.index_state();
296 if state != IndexState::Ready {
297 return Ok(VisibleIndexes::none());
298 }
299 debug_assert_eq!(state, IndexState::Ready);
300
301 Ok(VisibleIndexes::planner_visible(model.indexes()))
304 }
305
306 #[must_use]
311 pub fn describe_entity<E>(&self) -> EntitySchemaDescription
312 where
313 E: EntityKind<Canister = C>,
314 {
315 self.describe_entity_model(E::MODEL)
316 }
317
318 #[must_use]
320 pub fn describe_entity_model(&self, model: &'static EntityModel) -> EntitySchemaDescription {
321 describe_entity_model(model)
322 }
323
324 pub fn storage_report(
326 &self,
327 name_to_path: &[(&'static str, &'static str)],
328 ) -> Result<StorageReport, InternalError> {
329 self.db.storage_report(name_to_path)
330 }
331
332 pub fn storage_report_default(&self) -> Result<StorageReport, InternalError> {
334 self.db.storage_report_default()
335 }
336
337 pub fn integrity_report(&self) -> Result<IntegrityReport, InternalError> {
339 self.db.integrity_report()
340 }
341
342 pub fn execute_migration_plan(
347 &self,
348 plan: &MigrationPlan,
349 max_steps: usize,
350 ) -> Result<MigrationRunOutcome, InternalError> {
351 self.with_metrics(|| self.db.execute_migration_plan(plan, max_steps))
352 }
353
354 #[must_use]
359 pub(in crate::db) const fn load_executor<E>(&self) -> LoadExecutor<E>
360 where
361 E: EntityKind<Canister = C> + EntityValue,
362 {
363 LoadExecutor::new(self.db, self.debug)
364 }
365
366 #[must_use]
367 pub(in crate::db) const fn delete_executor<E>(&self) -> DeleteExecutor<E>
368 where
369 E: PersistedRow<Canister = C> + EntityValue,
370 {
371 DeleteExecutor::new(self.db)
372 }
373
374 #[must_use]
375 pub(in crate::db) const fn save_executor<E>(&self) -> SaveExecutor<E>
376 where
377 E: PersistedRow<Canister = C> + EntityValue,
378 {
379 SaveExecutor::new(self.db, self.debug)
380 }
381}
382
383#[doc(hidden)]
388pub fn debug_remove_entity_row_data_only<C, E>(
389 session: &DbSession<C>,
390 key: &E::Key,
391) -> Result<bool, InternalError>
392where
393 C: CanisterKind,
394 E: PersistedRow<Canister = C> + EntityValue,
395{
396 let store = session.db.recovered_store(E::Store::PATH)?;
399
400 let data_key = DataKey::try_from_field_value(E::ENTITY_TAG, key)?;
403 let raw_key = data_key.to_raw()?;
404 let storage_key = data_key.storage_key();
405
406 let removed = store.with_data_mut(|data| data.remove(&raw_key).is_some());
410 if !removed {
411 return Ok(false);
412 }
413
414 store.with_index_mut(|index| index.mark_memberships_missing_for_storage_key(storage_key))?;
415
416 Ok(true)
417}
418
419#[doc(hidden)]
425pub fn debug_mark_store_index_state<C>(
426 session: &DbSession<C>,
427 store_path: &str,
428 state: IndexState,
429) -> Result<(), InternalError>
430where
431 C: CanisterKind,
432{
433 let store = session.db.recovered_store(store_path)?;
436
437 match state {
440 IndexState::Building => store.mark_index_building(),
441 IndexState::Ready => store.mark_index_ready(),
442 IndexState::Dropping => store.mark_index_dropping(),
443 }
444
445 Ok(())
446}