Skip to main content

sim_incremental_core/
budget.rs

1//! Budget records for incremental query verification and snapshots.
2
3/// The resource class that exhausted a query run.
4#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
5pub enum BudgetKind {
6    /// Query execution work units were exhausted.
7    Work,
8    /// Dependency observation records were exhausted.
9    Observations,
10    /// Nested query depth was exhausted.
11    Depth,
12    /// Output units were exhausted.
13    Output,
14}
15
16/// Limits applied while verifying queries.
17#[derive(Clone, Copy, Debug, Eq, PartialEq)]
18pub struct QueryBudgets {
19    /// Maximum query executions and explicit work charges.
20    pub max_work: usize,
21    /// Maximum dependency observations recorded during the run.
22    pub max_observations: usize,
23    /// Maximum nested query stack depth.
24    pub max_depth: usize,
25    /// Maximum output units charged by query results or user code.
26    pub max_output: usize,
27    /// Maximum causal predecessors retained for each explained node.
28    pub max_explanation_causes: usize,
29}
30
31impl QueryBudgets {
32    /// Returns an unbounded query budget.
33    #[must_use]
34    pub const fn unlimited() -> Self {
35        Self {
36            max_work: usize::MAX,
37            max_observations: usize::MAX,
38            max_depth: usize::MAX,
39            max_output: usize::MAX,
40            max_explanation_causes: usize::MAX,
41        }
42    }
43
44    /// Returns a query budget with explicit limits.
45    #[must_use]
46    pub const fn new(
47        max_work: usize,
48        max_observations: usize,
49        max_depth: usize,
50        max_output: usize,
51    ) -> Self {
52        Self {
53            max_work,
54            max_observations,
55            max_depth,
56            max_output,
57            max_explanation_causes: usize::MAX,
58        }
59    }
60
61    /// Returns these budgets with an explicit per-node explanation bound.
62    #[must_use]
63    pub const fn with_explanation_causes(mut self, max_explanation_causes: usize) -> Self {
64        self.max_explanation_causes = max_explanation_causes;
65        self
66    }
67}
68
69impl Default for QueryBudgets {
70    fn default() -> Self {
71        Self::unlimited()
72    }
73}
74
75/// Limits applied when exporting a graph snapshot.
76#[derive(Clone, Copy, Debug, Eq, PartialEq)]
77pub struct SnapshotBudgets {
78    /// Maximum memo nodes exported into the snapshot.
79    pub max_nodes: usize,
80    /// Maximum dependency edges exported into the snapshot.
81    pub max_edges: usize,
82}
83
84impl SnapshotBudgets {
85    /// Returns an unbounded snapshot budget.
86    #[must_use]
87    pub const fn unlimited() -> Self {
88        Self {
89            max_nodes: usize::MAX,
90            max_edges: usize::MAX,
91        }
92    }
93
94    /// Returns a snapshot budget with explicit node and edge limits.
95    #[must_use]
96    pub const fn new(max_nodes: usize, max_edges: usize) -> Self {
97        Self {
98            max_nodes,
99            max_edges,
100        }
101    }
102}
103
104impl Default for SnapshotBudgets {
105    fn default() -> Self {
106        Self::unlimited()
107    }
108}