Skip to main content

sqlite_types/
lib.rs

1//! Shared SQLite execution types for local and remote depot client backends.
2
3mod page_bytes;
4
5pub use page_bytes::PageBytes;
6
7pub const HEAD_FENCE_MISMATCH_GROUP: &str = "depot";
8pub const HEAD_FENCE_MISMATCH_CODE: &str = "head_fence_mismatch";
9
10pub fn is_head_fence_mismatch(group: &str, code: &str) -> bool {
11	group == HEAD_FENCE_MISMATCH_GROUP && code == HEAD_FENCE_MISMATCH_CODE
12}
13
14/// The error a remote SQLite request reports when its transaction changed more pages than one
15/// commit may carry.
16pub const TRANSACTION_TOO_LARGE_GROUP: &str = "sqlite";
17pub const TRANSACTION_TOO_LARGE_CODE: &str = "transaction_too_large";
18
19pub fn is_transaction_too_large(group: &str, code: &str) -> bool {
20	group == TRANSACTION_TOO_LARGE_GROUP && code == TRANSACTION_TOO_LARGE_CODE
21}
22
23/// Pages per storage shard. Shared rather than each side keeping its own copy: the client cuts
24/// staged commit segments on these boundaries and the engine rejects segments that are not cut on
25/// them, so two definitions could disagree and make every large commit fail.
26pub const SHARD_SIZE: u32 = 64;
27
28/// Shards one staged commit segment may span.
29pub const COMMIT_SEGMENT_MAX_SHARDS: u32 = 5;
30
31/// Actor-side ceiling: 128 MiB of 4 KiB pages. The server additionally enforces
32/// its configured filesystem transaction byte cap before admitting each segment.
33/// Large commits stream as bounded segments and publish at one commit sequence.
34pub const MAX_COMMIT_DIRTY_PAGES: usize = 32_768;
35
36/// Where a staged commit segment carrying `pgno` must start.
37pub fn segment_start_for_page(pgno: u32) -> u32 {
38	pgno / SHARD_SIZE * SHARD_SIZE
39}
40
41/// Cuts pages into shard-aligned segments, each spanning at most `COMMIT_SEGMENT_MAX_SHARDS`.
42///
43/// Shard alignment remains part of the existing wire protocol; the server now
44/// writes these pages into one ordinary SQLite file through FS transactions.
45///
46/// `pages` must be sorted ascending by page number; the caller sorts once rather than this sorting
47/// per call.
48pub fn cut_page_segments<T>(pages: &[T], pgno_of: impl Fn(&T) -> u32) -> Vec<(u32, &[T])> {
49	let mut segments = Vec::new();
50	let mut start = 0;
51	while start < pages.len() {
52		let first_shard = pgno_of(&pages[start]) / SHARD_SIZE;
53		// Saturating, so pages at the very top of the page space cut one final segment rather than
54		// wrapping into an empty one and looping.
55		let shard_limit = first_shard.saturating_add(COMMIT_SEGMENT_MAX_SHARDS);
56		let end = pages[start..]
57			.iter()
58			.position(|page| pgno_of(page) / SHARD_SIZE >= shard_limit)
59			.map_or(pages.len(), |offset| start + offset);
60		segments.push((first_shard * SHARD_SIZE, &pages[start..end]));
61		start = end;
62	}
63
64	segments
65}
66
67#[derive(Clone, Debug, PartialEq)]
68pub enum BindParam {
69	Null,
70	Integer(i64),
71	Float(f64),
72	Text(String),
73	Blob(Vec<u8>),
74}
75
76#[derive(Clone, Debug, PartialEq)]
77pub struct ExecResult {
78	pub changes: i64,
79}
80
81#[derive(Clone, Debug, PartialEq)]
82pub struct QueryResult {
83	pub columns: Vec<String>,
84	pub rows: Vec<Vec<ColumnValue>>,
85}
86
87#[derive(Clone, Debug, PartialEq)]
88pub struct ExecuteResult {
89	pub columns: Vec<String>,
90	pub rows: Vec<Vec<ColumnValue>>,
91	pub changes: i64,
92	pub last_insert_row_id: Option<i64>,
93}
94
95impl ExecuteResult {
96	pub fn into_query_result(self) -> QueryResult {
97		QueryResult {
98			columns: self.columns,
99			rows: self.rows,
100		}
101	}
102
103	pub fn into_exec_result(self) -> ExecResult {
104		ExecResult {
105			changes: self.changes,
106		}
107	}
108}
109
110#[derive(Clone, Debug, PartialEq)]
111pub enum ColumnValue {
112	Null,
113	Integer(i64),
114	Float(f64),
115	Text(String),
116	Blob(Vec<u8>),
117}
118
119