icydb_core/db/executor/
mod.rs

1mod coerce;
2mod context;
3mod delete;
4mod filter;
5mod load;
6mod save;
7
8pub use coerce::*;
9pub use context::*;
10pub use delete::*;
11pub use filter::*;
12pub use load::*;
13pub use save::*;
14
15use crate::{
16    Error,
17    db::{
18        DbError,
19        primitives::FilterExpr,
20        query::{QueryPlan, QueryPlanner},
21        store::DataKey,
22    },
23    obs::metrics::Span,
24    traits::EntityKind,
25};
26use thiserror::Error as ThisError;
27
28///
29/// ExecutorError
30///
31
32#[derive(Debug, ThisError)]
33pub enum ExecutorError {
34    #[error("data key exists: {0}")]
35    KeyExists(DataKey),
36
37    #[error("data key not found: {0}")]
38    KeyNotFound(DataKey),
39
40    #[error("index constraint violation: {0} ({1})")]
41    IndexViolation(String, String),
42}
43
44impl ExecutorError {
45    #[must_use]
46    pub fn index_violation(path: &str, index_fields: &[&str]) -> Self {
47        Self::IndexViolation(path.to_string(), index_fields.join(", "))
48    }
49}
50
51impl From<ExecutorError> for Error {
52    fn from(err: ExecutorError) -> Self {
53        DbError::from(err).into()
54    }
55}
56
57/// Plan a query for an entity given an optional filter.
58#[must_use]
59pub fn plan_for<E: EntityKind>(filter: Option<&FilterExpr>) -> QueryPlan {
60    QueryPlanner::new(filter).plan::<E>()
61}
62
63/// Convenience: set span rows from a usize length.
64pub const fn set_rows_from_len<E: EntityKind>(span: &mut Span<E>, len: usize) {
65    span.set_rows(len as u64);
66}