Skip to main content

ic_db_cli/
lib.rs

1//! Transport types shared by the local DB console and its fixture canister.
2#![forbid(unsafe_code)]
3
4#[cfg(feature = "host")]
5pub mod host;
6
7/// A local query result, in the fixture's positional Wire field order.
8#[derive(Debug, serde::Deserialize, serde::Serialize)]
9pub struct ConsolePage {
10    /// Display column names.
11    pub columns: Vec<String>,
12    /// Display cells in column order.
13    pub rows: Vec<Vec<String>>,
14    /// Encoded continuation for a paged read.
15    pub next: Option<Vec<u8>>,
16    /// Examined query candidates.
17    pub examined: u32,
18    /// Fetched source rows.
19    pub fetched: u32,
20    /// Encoded result bytes before transport framing.
21    pub output_bytes: u32,
22}
23
24/// Byte predicates on the fixture payload column.
25#[derive(Clone, Default, Debug, serde::Deserialize, serde::Serialize)]
26pub struct PayloadFilters {
27    /// Inclusive lower bound.
28    pub minimum: Option<Vec<u8>>,
29    /// Inclusive upper bound.
30    pub maximum: Option<Vec<u8>>,
31    /// Required prefix.
32    pub prefix: Option<Vec<u8>>,
33    /// Required suffix.
34    pub suffix: Option<Vec<u8>>,
35    /// Required contiguous bytes.
36    pub contains: Option<Vec<u8>>,
37}
38
39/// Local query command in fixture Wire field order.
40#[derive(Debug, serde::Deserialize, serde::Serialize)]
41pub struct QueryRequest {
42    /// Selected entity.
43    pub entity: String,
44    /// Encoded continuation.
45    pub cursor: Option<Vec<u8>>,
46    /// Maximum rows per page.
47    pub limit: u32,
48    /// Lower amount bound.
49    pub minimum: Option<u64>,
50    /// Selected category.
51    pub category: Option<u64>,
52    /// Selected entry ID.
53    pub entry_id: Option<u64>,
54    /// Ordered field specifications.
55    pub order: Vec<String>,
56    /// Reverse traversal.
57    pub descending: bool,
58    /// Candidate budget.
59    pub work: u32,
60    /// Output byte budget.
61    pub bytes: u32,
62    /// Selected output.
63    pub select: String,
64    /// Grouping and predicates.
65    pub options: QueryOptions,
66}
67
68/// Options carried together by the query endpoint.
69#[derive(Debug, serde::Deserialize, serde::Serialize)]
70pub struct QueryOptions {
71    /// Request distinct output.
72    pub distinct: bool,
73    /// Lower and upper group bounds.
74    pub group_bounds: (Option<String>, Option<String>),
75    /// Group ordering.
76    pub group_order: Option<String>,
77    /// Row predicates.
78    pub filters: RowFilters,
79}
80
81/// Local fixture status.
82#[derive(Debug, serde::Deserialize, serde::Serialize)]
83pub struct Status {
84    /// Schema fingerprint.
85    pub schema: [u8; 32],
86    /// Store incarnation.
87    pub incarnation: [u8; 16],
88    /// Entry revision.
89    pub revision: u64,
90    /// Wasm linear memory pages.
91    pub pages: u64,
92}
93
94/// Local schema inspection reply.
95#[derive(Debug, serde::Deserialize, serde::Serialize)]
96pub struct SchemaResponse {
97    /// Fixture status.
98    pub status: Status,
99    /// Matching entities.
100    pub entities: Vec<EntityView>,
101    /// Named value types.
102    pub types: Vec<TypeView>,
103}
104
105/// Inspected entity.
106#[derive(Debug, serde::Deserialize, serde::Serialize)]
107pub struct EntityView {
108    /// Entity name.
109    pub name: String,
110    /// Schema entity identifier.
111    pub id: u32,
112    /// Primary key field.
113    pub primary: String,
114    /// Stored fields.
115    pub fields: Vec<FieldView>,
116    /// Declared indexes.
117    pub indexes: Vec<IndexView>,
118}
119
120/// Inspected entity field.
121#[derive(Debug, serde::Deserialize, serde::Serialize)]
122pub struct FieldView {
123    /// Field name.
124    pub name: String,
125    /// Field type.
126    pub kind: String,
127    /// Optionality.
128    pub optional: bool,
129    /// Constraints and metadata.
130    pub info: String,
131    /// Reference target and index.
132    pub reference: Option<(String, String)>,
133}
134
135/// Index name, fields, uniqueness and sparse membership.
136pub type IndexView = (String, Vec<String>, bool, bool);
137
138/// Named type, kind and member descriptions.
139pub type TypeView = (String, String, Vec<(String, String, bool, String)>);
140
141/// Row predicates carried by the local query and retained-aggregate commands.
142#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
143pub struct RowFilters {
144    /// Lower bound, upper bound, prefix, suffix and substring on BinaryObject.payload.
145    pub payload: PayloadFilters,
146    /// Left field, comparison, right field.
147    pub compare: Option<[String; 3]>,
148    /// Field, comparison and length threshold.
149    pub length: Option<(String, String, u32)>,
150    /// Require a value in this optional field.
151    pub present: Option<String>,
152    /// Require absence in this optional field.
153    pub absent: Option<String>,
154    /// Field, comparison and exact UTF-8 literal.
155    pub text: Option<[String; 3]>,
156    /// Field and Boolean equality operand.
157    pub boolean: Option<(String, bool)>,
158    /// Field, inclusive lower field and inclusive upper field.
159    pub between_fields: Option<[String; 3]>,
160    /// Field, lower field and upper field for the strict outside-range comparison.
161    pub not_between_fields: Option<[String; 3]>,
162}
163
164/// Selection shared by the private retained fixture when starting and resuming a job.
165#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
166pub struct RetainedSelection {
167    /// Entity name.
168    pub entity: String,
169    /// Aggregate selection.
170    pub select: String,
171    /// Minimum, category and entry ID restrictions.
172    pub entity_filters: (Option<u64>, Option<u64>, Option<u64>),
173    /// Maximum output groups.
174    pub limit: u32,
175    /// Inclusive lower and upper group bounds.
176    pub group_bounds: (Option<String>, Option<String>),
177    /// Completed-group order.
178    pub group_order: Option<String>,
179    /// Row predicates.
180    pub filters: RowFilters,
181    /// Input fields and overall descending flag.
182    pub order: (Vec<String>, bool),
183}
184
185/// One bounded step of a retained fixture query.
186#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
187pub struct RetainedResume {
188    /// Original query.
189    pub selection: RetainedSelection,
190    /// Retained job handle.
191    pub handle: Vec<u8>,
192    /// Last saved step.
193    pub sequence: u64,
194    /// Input work allowance.
195    pub work: u32,
196    /// Output byte allowance.
197    pub bytes: u32,
198    /// Maximum results per page.
199    pub page_size: u32,
200    /// Previous result cursor.
201    pub after: Option<Vec<u8>>,
202}
203
204/// Display columns, rows and optional next cursor of a completed retained query.
205pub type RetainedOutput = (Vec<String>, Vec<Vec<String>>, Option<Vec<u8>>);
206
207/// Progress reported by the local fixture after one retained query step.
208#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
209pub struct RetainedProgress {
210    /// Last saved step.
211    pub sequence: u64,
212    /// Candidates examined so far.
213    pub examined: u64,
214    /// Input bytes examined so far.
215    pub input_bytes: u64,
216    /// Encoded result bytes in this step.
217    pub output_bytes: u64,
218    /// Current phase and any completed result.
219    pub phase: RetainedPhase,
220}
221
222/// Only a completed job has output.
223#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
224pub enum RetainedPhase {
225    /// Additional input remains.
226    Running,
227    /// Completed rows and continuation.
228    Complete(RetainedOutput),
229    /// Source rows changed.
230    Invalidated,
231    /// Job lifetime elapsed.
232    Expired,
233}
234
235impl RetainedPhase {
236    /// Name used by console progress messages.
237    #[must_use]
238    pub const fn label(&self) -> &'static str {
239        match self {
240            Self::Running => "Running",
241            Self::Complete(_) => "Complete",
242            Self::Invalidated => "Invalidated",
243            Self::Expired => "Expired",
244        }
245    }
246}
247
248/// Render bytes using two lowercase hexadecimal digits per byte and the supplied display prefix.
249#[must_use]
250pub fn lowercase_hex(bytes: &[u8], prefix: &str) -> String {
251    const DIGITS: &[u8; 16] = b"0123456789abcdef";
252    let mut output = String::with_capacity(prefix.len() + bytes.len() * 2);
253    output.push_str(prefix);
254    for &byte in bytes {
255        output.push(char::from(DIGITS[usize::from(byte >> 4)]));
256        output.push(char::from(DIGITS[usize::from(byte & 15)]));
257    }
258    output
259}