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