Skip to main content

cartel_sqlite/settings/
limits.rs

1use std::{num, time};
2
3#[derive(Clone, Copy, PartialEq, Eq)]
4pub struct Limits {
5    pub parameters: Parameters,
6    pub scan: Scan,
7    /// Maximum live SQLite savepoint frames in one transaction.
8    pub savepoints: usize,
9    pub program: Program,
10    pub work: Work,
11}
12
13#[derive(Clone, Copy, PartialEq, Eq)]
14pub struct Work {
15    /// One aggregate bound for configuration, initialization, and statement preparation.
16    /// Charges a final quantum per preparation and execution to bound partial intervals.
17    pub open: Budget,
18    /// One bound for a query or transaction-control call, including busy retries.
19    pub call: Budget,
20    /// One bound for deferred rollback and savepoint cleanup.
21    pub cleanup: Progress,
22}
23
24impl Work {
25    pub const fn uniform(budget: Budget) -> Self {
26        Self {
27            open: budget,
28            call: budget,
29            cleanup: budget.progress,
30        }
31    }
32}
33
34#[derive(Clone, Copy, PartialEq, Eq)]
35pub struct Budget {
36    /// Aggregate SQLite virtual-machine work for the whole operation.
37    pub progress: Progress,
38    /// Cooperative lock-contention policy for the whole operation.
39    pub busy: Busy,
40}
41
42#[derive(Clone, Copy, PartialEq, Eq)]
43pub struct Parameters {
44    pub items: usize,
45    pub bytes: usize,
46}
47
48#[derive(Clone, Copy, PartialEq, Eq)]
49pub struct Scan {
50    pub rows: usize,
51    pub row_bytes: usize,
52}
53
54#[derive(Clone, Copy, PartialEq, Eq)]
55#[repr(transparent)]
56pub struct Progress(num::NonZero<u64>);
57
58impl Progress {
59    pub const fn new(quanta: num::NonZero<u64>) -> Self {
60        Self(quanta)
61    }
62
63    pub const fn get(self) -> u64 {
64        self.0.get()
65    }
66}
67
68#[derive(Clone, Copy, PartialEq, Eq)]
69#[repr(transparent)]
70pub struct Program(num::NonZero<u32>);
71
72impl Program {
73    pub const fn new(ops: num::NonZero<u32>) -> Self {
74        Self(ops)
75    }
76
77    pub const fn get(self) -> u32 {
78        self.0.get()
79    }
80
81    pub(crate) fn sqlite_ops(self) -> num::NonZero<i32> {
82        self.0
83            .min(num::NonZero::<i32>::MAX.cast_unsigned())
84            .cast_signed()
85    }
86}
87
88#[derive(Clone, Copy, PartialEq, Eq)]
89pub struct Busy {
90    /// Maximum elapsed time from the first busy result.
91    pub window: time::Duration,
92    /// Retry delay: zero yields once; positive reserves and reuses one driver timer slot.
93    pub interval: time::Duration,
94    /// Maximum number of attempts after the first busy result.
95    pub retries: num::NonZero<u32>,
96}