icydb_core/db/executor/
load.rs

1use crate::{
2    Error, Key,
3    db::{
4        Db,
5        executor::{
6            FilterEvaluator,
7            plan::{plan_for, set_rows_from_len},
8        },
9        primitives::{FilterDsl, FilterExpr, FilterExt, IntoFilterExpr, Order, SortExpr},
10        query::{LoadQuery, QueryPlan, QueryValidate},
11        response::{Response, ResponseError},
12        store::DataRow,
13    },
14    obs::metrics,
15    traits::{EntityKind, FieldValue},
16};
17use std::{cmp::Ordering, marker::PhantomData};
18
19///
20/// LoadExecutor
21///
22
23#[derive(Clone, Copy)]
24pub struct LoadExecutor<E: EntityKind> {
25    db: Db<E::Canister>,
26    debug: bool,
27    _marker: PhantomData<E>,
28}
29
30impl<E: EntityKind> LoadExecutor<E> {
31    // ======================================================================
32    // Construction & diagnostics
33    // ======================================================================
34
35    #[must_use]
36    pub const fn new(db: Db<E::Canister>, debug: bool) -> Self {
37        Self {
38            db,
39            debug,
40            _marker: PhantomData,
41        }
42    }
43
44    fn debug_log(&self, s: impl Into<String>) {
45        if self.debug {
46            println!("{}", s.into());
47        }
48    }
49
50    // ======================================================================
51    // Query builders (execute and return Response)
52    // ======================================================================
53
54    /// Execute a query for a single primary key.
55    pub fn one(&self, value: impl FieldValue) -> Result<Response<E>, Error> {
56        self.execute(LoadQuery::new().one::<E>(value))
57    }
58
59    /// Execute a query for the unit primary key.
60    pub fn only(&self) -> Result<Response<E>, Error> {
61        self.execute(LoadQuery::new().one::<E>(()))
62    }
63
64    /// Execute a query matching multiple primary keys.
65    pub fn many(
66        &self,
67        values: impl IntoIterator<Item = impl FieldValue>,
68    ) -> Result<Response<E>, Error> {
69        self.execute(LoadQuery::new().many::<E>(values))
70    }
71
72    /// Execute an unfiltered query for all rows.
73    pub fn all(&self) -> Result<Response<E>, Error> {
74        self.execute(LoadQuery::new())
75    }
76
77    /// Execute a query built from a filter.
78    pub fn filter<F, I>(&self, f: F) -> Result<Response<E>, Error>
79    where
80        F: FnOnce(FilterDsl) -> I,
81        I: IntoFilterExpr,
82    {
83        self.execute(LoadQuery::new().filter(f))
84    }
85
86    // ======================================================================
87    // Cardinality guards (delegated to Response)
88    // ======================================================================
89
90    /// Execute a query and require exactly one row.
91    pub fn require_one(&self, query: LoadQuery) -> Result<(), Error> {
92        self.execute(query)?.require_one()
93    }
94
95    /// Require exactly one row by primary key.
96    pub fn require_one_pk(&self, value: impl FieldValue) -> Result<(), Error> {
97        self.require_one(LoadQuery::new().one::<E>(value))
98    }
99
100    /// Require exactly one row from a filter.
101    pub fn require_one_filter<F, I>(&self, f: F) -> Result<(), Error>
102    where
103        F: FnOnce(FilterDsl) -> I,
104        I: IntoFilterExpr,
105    {
106        self.require_one(LoadQuery::new().filter(f))
107    }
108
109    // ======================================================================
110    // Existence checks (≥1 semantics, intentionally weaker)
111    // ======================================================================
112
113    /// Check whether at least one row matches the query.
114    pub fn exists(&self, query: LoadQuery) -> Result<bool, Error> {
115        let query = query.limit_1();
116        Ok(!self.execute_raw(&query)?.is_empty())
117    }
118
119    /// Check existence by primary key.
120    pub fn exists_one(&self, value: impl FieldValue) -> Result<bool, Error> {
121        self.exists(LoadQuery::new().one::<E>(value))
122    }
123
124    /// Check existence with a filter.
125    pub fn exists_filter<F, I>(&self, f: F) -> Result<bool, Error>
126    where
127        F: FnOnce(FilterDsl) -> I,
128        I: IntoFilterExpr,
129    {
130        self.exists(LoadQuery::new().filter(f))
131    }
132
133    /// Check whether the table contains any rows.
134    pub fn exists_any(&self) -> Result<bool, Error> {
135        self.exists(LoadQuery::new())
136    }
137
138    // ======================================================================
139    // Existence checks with not-found errors (fast path, no deserialization)
140    // ======================================================================
141
142    /// Require at least one row by primary key.
143    pub fn ensure_exists_one(&self, value: impl FieldValue) -> Result<(), Error> {
144        if self.exists_one(value)? {
145            Ok(())
146        } else {
147            Err(ResponseError::NotFound { entity: E::PATH }.into())
148        }
149    }
150
151    /// Require at least one row from a filter.
152    pub fn ensure_exists_filter<F, I>(&self, f: F) -> Result<(), Error>
153    where
154        F: FnOnce(FilterDsl) -> I,
155        I: IntoFilterExpr,
156    {
157        if self.exists_filter(f)? {
158            Ok(())
159        } else {
160            Err(ResponseError::NotFound { entity: E::PATH }.into())
161        }
162    }
163
164    // ======================================================================
165    // Execution & planning
166    // ======================================================================
167
168    /// Validate and return the query plan without executing.
169    pub fn explain(self, query: LoadQuery) -> Result<QueryPlan, Error> {
170        QueryValidate::<E>::validate(&query)?;
171
172        Ok(plan_for::<E>(query.filter.as_ref()))
173    }
174
175    fn execute_raw(&self, query: &LoadQuery) -> Result<Vec<DataRow>, Error> {
176        QueryValidate::<E>::validate(query)?;
177
178        let ctx = self.db.context::<E>();
179        let plan = plan_for::<E>(query.filter.as_ref());
180
181        if let Some(lim) = &query.limit {
182            Ok(ctx.rows_from_plan_with_pagination(plan, lim.offset, lim.limit)?)
183        } else {
184            Ok(ctx.rows_from_plan(plan)?)
185        }
186    }
187
188    /// Execute a full query and return a collection of entities.
189    pub fn execute(&self, query: LoadQuery) -> Result<Response<E>, Error> {
190        let mut span = metrics::Span::<E>::new(metrics::ExecKind::Load);
191        QueryValidate::<E>::validate(&query)?;
192
193        self.debug_log(format!("🧭 Executing query: {:?} on {}", query, E::PATH));
194
195        let ctx = self.db.context::<E>();
196        let plan = plan_for::<E>(query.filter.as_ref());
197
198        self.debug_log(format!("📄 Query plan: {plan:?}"));
199
200        // Fast path: pre-pagination
201        let pre_paginated = query.filter.is_none() && query.sort.is_none() && query.limit.is_some();
202        let mut rows: Vec<(Key, E)> = if pre_paginated {
203            let data_rows = self.execute_raw(&query)?;
204
205            self.debug_log(format!(
206                "📦 Scanned {} data rows before deserialization",
207                data_rows.len()
208            ));
209
210            let rows = ctx.deserialize_rows(data_rows)?;
211            self.debug_log(format!(
212                "🧩 Deserialized {} entities before filtering",
213                rows.len()
214            ));
215            rows
216        } else {
217            let data_rows = ctx.rows_from_plan(plan)?;
218            self.debug_log(format!(
219                "📦 Scanned {} data rows before deserialization",
220                data_rows.len()
221            ));
222
223            let rows = ctx.deserialize_rows(data_rows)?;
224            self.debug_log(format!(
225                "🧩 Deserialized {} entities before filtering",
226                rows.len()
227            ));
228
229            rows
230        };
231
232        // Filtering
233        if let Some(f) = &query.filter {
234            let simplified = f.clone().simplify();
235            Self::apply_filter(&mut rows, &simplified);
236
237            self.debug_log(format!(
238                "🔎 Applied filter -> {} entities remaining",
239                rows.len()
240            ));
241        }
242
243        // Sorting
244        if let Some(sort) = &query.sort
245            && rows.len() > 1
246        {
247            Self::apply_sort(&mut rows, sort);
248            self.debug_log("↕️ Applied sort expression");
249        }
250
251        // Pagination
252        if let Some(lim) = &query.limit
253            && !pre_paginated
254        {
255            apply_pagination(&mut rows, lim.offset, lim.limit);
256            self.debug_log(format!(
257                "📏 Applied pagination (offset={}, limit={:?}) -> {} entities",
258                lim.offset,
259                lim.limit,
260                rows.len()
261            ));
262        }
263
264        set_rows_from_len(&mut span, rows.len());
265        self.debug_log(format!("✅ Query complete -> {} final rows", rows.len()));
266
267        Ok(Response(rows))
268    }
269
270    /// Count rows matching a query.
271    pub fn count(&self, query: LoadQuery) -> Result<u32, Error> {
272        Ok(self.execute(query)?.count())
273    }
274
275    pub fn count_all(&self) -> Result<u32, Error> {
276        self.count(LoadQuery::new())
277    }
278
279    // apply_filter
280    fn apply_filter(rows: &mut Vec<(Key, E)>, filter: &FilterExpr) {
281        rows.retain(|(_, e)| FilterEvaluator::new(e).eval(filter));
282    }
283
284    // apply_sort
285    fn apply_sort(rows: &mut [(Key, E)], sort_expr: &SortExpr) {
286        rows.sort_by(|(_, ea), (_, eb)| {
287            for (field, direction) in sort_expr.iter() {
288                let va = ea.get_value(field);
289                let vb = eb.get_value(field);
290
291                // Define how to handle missing values (None)
292                let ordering = match (va, vb) {
293                    (None, None) => continue,             // both missing → move to next field
294                    (None, Some(_)) => Ordering::Less,    // None sorts before Some(_)
295                    (Some(_), None) => Ordering::Greater, // Some(_) sorts after None
296                    (Some(va), Some(vb)) => match va.partial_cmp(&vb) {
297                        Some(ord) => ord,
298                        None => continue, // incomparable values → move to next field
299                    },
300                };
301
302                // Apply direction (Asc/Desc)
303                let ordering = match direction {
304                    Order::Asc => ordering,
305                    Order::Desc => ordering.reverse(),
306                };
307
308                if ordering != Ordering::Equal {
309                    return ordering;
310                }
311            }
312
313            // all fields equal
314            Ordering::Equal
315        });
316    }
317}
318
319/// Apply offset/limit pagination to an in-memory vector, in-place.
320fn apply_pagination<T>(rows: &mut Vec<T>, offset: u32, limit: Option<u32>) {
321    let total = rows.len();
322    let start = usize::min(offset as usize, total);
323    let end = limit.map_or(total, |l| usize::min(start + l as usize, total));
324
325    if start >= end {
326        rows.clear();
327    } else {
328        rows.drain(..start);
329        rows.truncate(end - start);
330    }
331}
332
333///
334/// TESTS
335///
336
337#[cfg(test)]
338mod tests {
339    use super::{LoadExecutor, apply_pagination};
340    use crate::{
341        IndexSpec, Key, Value,
342        db::primitives::{Order, SortExpr},
343        traits::{
344            CanisterKind, EntityKind, FieldValues, Path, SanitizeAuto, SanitizeCustom, StoreKind,
345            ValidateAuto, ValidateCustom, View, Visitable,
346        },
347    };
348    use serde::{Deserialize, Serialize};
349
350    #[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
351    struct SortableEntity {
352        id: u64,
353        primary: i32,
354        secondary: i32,
355        optional_blob: Option<Vec<u8>>,
356    }
357
358    impl SortableEntity {
359        fn new(id: u64, primary: i32, secondary: i32, optional_blob: Option<Vec<u8>>) -> Self {
360            Self {
361                id,
362                primary,
363                secondary,
364                optional_blob,
365            }
366        }
367    }
368
369    struct SortableCanister;
370    struct SortableStore;
371
372    impl Path for SortableCanister {
373        const PATH: &'static str = "test::canister";
374    }
375
376    impl CanisterKind for SortableCanister {}
377
378    impl Path for SortableStore {
379        const PATH: &'static str = "test::store";
380    }
381
382    impl StoreKind for SortableStore {
383        type Canister = SortableCanister;
384    }
385
386    impl Path for SortableEntity {
387        const PATH: &'static str = "test::sortable";
388    }
389
390    impl View for SortableEntity {
391        type ViewType = Self;
392
393        fn to_view(&self) -> Self::ViewType {
394            self.clone()
395        }
396
397        fn from_view(view: Self::ViewType) -> Self {
398            view
399        }
400    }
401
402    impl SanitizeAuto for SortableEntity {}
403    impl SanitizeCustom for SortableEntity {}
404    impl ValidateAuto for SortableEntity {}
405    impl ValidateCustom for SortableEntity {}
406    impl Visitable for SortableEntity {}
407
408    impl FieldValues for SortableEntity {
409        fn get_value(&self, field: &str) -> Option<Value> {
410            match field {
411                "id" => Some(Value::Uint(self.id)),
412                "primary" => Some(Value::Int(i64::from(self.primary))),
413                "secondary" => Some(Value::Int(i64::from(self.secondary))),
414                "optional_blob" => self.optional_blob.clone().map(Value::Blob),
415                _ => None,
416            }
417        }
418    }
419
420    impl EntityKind for SortableEntity {
421        type PrimaryKey = u64;
422        type Store = SortableStore;
423        type Canister = SortableCanister;
424
425        const ENTITY_ID: u64 = 99;
426        const PRIMARY_KEY: &'static str = "id";
427        const FIELDS: &'static [&'static str] = &["id", "primary", "secondary", "optional_blob"];
428        const INDEXES: &'static [&'static IndexSpec] = &[];
429
430        fn key(&self) -> Key {
431            self.id.into()
432        }
433
434        fn primary_key(&self) -> Self::PrimaryKey {
435            self.id
436        }
437    }
438
439    #[test]
440    fn pagination_empty_vec() {
441        let mut v: Vec<i32> = vec![];
442        apply_pagination(&mut v, 0, Some(10));
443        assert!(v.is_empty());
444    }
445
446    #[test]
447    fn pagination_offset_beyond_len_clears() {
448        let mut v = vec![1, 2, 3];
449        apply_pagination(&mut v, 10, Some(5));
450        assert!(v.is_empty());
451    }
452
453    #[test]
454    fn pagination_no_limit_from_offset() {
455        let mut v = vec![1, 2, 3, 4, 5];
456        apply_pagination(&mut v, 2, None);
457        assert_eq!(v, vec![3, 4, 5]);
458    }
459
460    #[test]
461    fn pagination_exact_window() {
462        let mut v = vec![10, 20, 30, 40, 50];
463        // offset 1, limit 3 -> elements [20,30,40]
464        apply_pagination(&mut v, 1, Some(3));
465        assert_eq!(v, vec![20, 30, 40]);
466    }
467
468    #[test]
469    fn pagination_limit_exceeds_tail() {
470        let mut v = vec![10, 20, 30];
471        // offset 1, limit large -> [20,30]
472        apply_pagination(&mut v, 1, Some(999));
473        assert_eq!(v, vec![20, 30]);
474    }
475
476    #[test]
477    fn apply_sort_orders_descending() {
478        let mut rows = vec![
479            (Key::from(1_u64), SortableEntity::new(1, 10, 1, None)),
480            (Key::from(2_u64), SortableEntity::new(2, 30, 2, None)),
481            (Key::from(3_u64), SortableEntity::new(3, 20, 3, None)),
482        ];
483        let sort_expr = SortExpr::from(vec![("primary".to_string(), Order::Desc)]);
484
485        LoadExecutor::<SortableEntity>::apply_sort(rows.as_mut_slice(), &sort_expr);
486
487        let primary: Vec<i32> = rows.iter().map(|(_, e)| e.primary).collect();
488        assert_eq!(primary, vec![30, 20, 10]);
489    }
490
491    #[test]
492    fn apply_sort_uses_secondary_field_for_ties() {
493        let mut rows = vec![
494            (Key::from(1_u64), SortableEntity::new(1, 1, 5, None)),
495            (Key::from(2_u64), SortableEntity::new(2, 1, 8, None)),
496            (Key::from(3_u64), SortableEntity::new(3, 2, 3, None)),
497        ];
498        let sort_expr = SortExpr::from(vec![
499            ("primary".to_string(), Order::Asc),
500            ("secondary".to_string(), Order::Desc),
501        ]);
502
503        LoadExecutor::<SortableEntity>::apply_sort(rows.as_mut_slice(), &sort_expr);
504
505        let ids: Vec<u64> = rows.iter().map(|(_, e)| e.id).collect();
506        assert_eq!(ids, vec![2, 1, 3]);
507    }
508
509    #[test]
510    fn apply_sort_places_none_before_some_and_falls_back() {
511        let mut rows = vec![
512            (
513                Key::from(3_u64),
514                SortableEntity::new(3, 0, 0, Some(vec![3, 4])),
515            ),
516            (Key::from(1_u64), SortableEntity::new(1, 0, 0, None)),
517            (
518                Key::from(2_u64),
519                SortableEntity::new(2, 0, 0, Some(vec![2])),
520            ),
521        ];
522        let sort_expr = SortExpr::from(vec![
523            ("optional_blob".to_string(), Order::Asc),
524            ("id".to_string(), Order::Asc),
525        ]);
526
527        LoadExecutor::<SortableEntity>::apply_sort(rows.as_mut_slice(), &sort_expr);
528
529        let ids: Vec<u64> = rows.iter().map(|(_, e)| e.id).collect();
530        assert_eq!(ids, vec![1, 2, 3]);
531    }
532}