1use chrono::{DateTime, Utc};
17use serde::{Deserialize, Serialize};
18use std::collections::HashMap;
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
26pub struct BlockId(pub u64);
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
30pub struct PageId(pub u64);
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
34pub struct TransactionId(pub u64);
35
36#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
38pub struct NodeId(pub String);
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
42pub struct ShardId(pub u32);
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
46pub struct Lsn(pub u64);
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
54pub enum BlockType {
55 TableData,
56 TimeSeriesData,
57 DocumentData,
58 IndexData,
59 LogEntry,
60 Metadata,
61}
62
63#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
65pub enum CompressionType {
66 #[default]
67 None,
68 Lz4,
69 Zstd,
70 Snappy,
71}
72
73#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
75pub enum EncryptionType {
76 #[default]
77 None,
78 Aes256Gcm,
79}
80
81#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
87pub enum Value {
88 Null,
89 Boolean(bool),
90 Integer(i64),
91 Float(f64),
92 String(String),
93 #[serde(with = "serde_bytes")]
94 Bytes(Vec<u8>),
95 Timestamp(DateTime<Utc>),
96 Array(Vec<Value>),
97 Object(HashMap<String, Value>),
98}
99
100impl Value {
101 pub fn type_name(&self) -> &'static str {
103 match self {
104 Value::Null => "null",
105 Value::Boolean(_) => "boolean",
106 Value::Integer(_) => "integer",
107 Value::Float(_) => "float",
108 Value::String(_) => "string",
109 Value::Bytes(_) => "bytes",
110 Value::Timestamp(_) => "timestamp",
111 Value::Array(_) => "array",
112 Value::Object(_) => "object",
113 }
114 }
115
116 pub fn is_null(&self) -> bool {
118 matches!(self, Value::Null)
119 }
120
121 pub fn as_bool(&self) -> Option<bool> {
123 match self {
124 Value::Boolean(b) => Some(*b),
125 _ => None,
126 }
127 }
128
129 pub fn as_i64(&self) -> Option<i64> {
131 match self {
132 Value::Integer(i) => Some(*i),
133 _ => None,
134 }
135 }
136
137 pub fn as_f64(&self) -> Option<f64> {
139 match self {
140 Value::Float(f) => Some(*f),
141 Value::Integer(i) => Some(*i as f64),
142 _ => None,
143 }
144 }
145
146 pub fn as_str(&self) -> Option<&str> {
148 match self {
149 Value::String(s) => Some(s),
150 _ => None,
151 }
152 }
153
154 pub fn as_bytes(&self) -> Option<&[u8]> {
156 match self {
157 Value::Bytes(b) => Some(b),
158 _ => None,
159 }
160 }
161
162 pub fn as_timestamp(&self) -> Option<&DateTime<Utc>> {
164 match self {
165 Value::Timestamp(t) => Some(t),
166 _ => None,
167 }
168 }
169
170 pub fn as_array(&self) -> Option<&Vec<Value>> {
172 match self {
173 Value::Array(a) => Some(a),
174 _ => None,
175 }
176 }
177
178 pub fn as_object(&self) -> Option<&HashMap<String, Value>> {
180 match self {
181 Value::Object(o) => Some(o),
182 _ => None,
183 }
184 }
185}
186
187#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
193pub enum DataType {
194 Any,
195 Boolean,
196 TinyInt,
197 SmallInt,
198 Integer,
199 BigInt,
200 Float,
201 Double,
202 Decimal { precision: u8, scale: u8 },
203 Char(u16),
204 Varchar(u16),
205 Text,
206 Binary(u32),
207 Varbinary(u32),
208 Blob,
209 Date,
210 Time,
211 Timestamp,
212 TimestampTz,
213 Interval,
214 Json,
215 Jsonb,
216 Uuid,
217 Array(Box<DataType>),
218}
219
220#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
226pub struct Row {
227 pub values: Vec<Value>,
228}
229
230impl Row {
231 pub fn new(values: Vec<Value>) -> Self {
232 Self { values }
233 }
234
235 pub fn get(&self, index: usize) -> Option<&Value> {
236 self.values.get(index)
237 }
238
239 pub fn len(&self) -> usize {
240 self.values.len()
241 }
242
243 pub fn is_empty(&self) -> bool {
244 self.values.is_empty()
245 }
246}
247
248#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
250pub struct ColumnDef {
251 pub name: String,
252 pub data_type: DataType,
253 pub nullable: bool,
254 pub default: Option<String>,
255}
256
257#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
263pub struct TimeRange {
264 pub start: DateTime<Utc>,
265 pub end: DateTime<Utc>,
266}
267
268impl TimeRange {
269 pub fn new(start: DateTime<Utc>, end: DateTime<Utc>) -> Self {
270 Self { start, end }
271 }
272
273 pub fn contains(&self, timestamp: &DateTime<Utc>) -> bool {
274 timestamp >= &self.start && timestamp < &self.end
275 }
276
277 pub fn duration(&self) -> chrono::Duration {
278 self.end - self.start
279 }
280}
281
282#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
288pub struct KeyRange {
289 pub start: Option<Vec<u8>>,
290 pub end: Option<Vec<u8>>,
291}
292
293impl KeyRange {
294 pub fn new(start: Option<Vec<u8>>, end: Option<Vec<u8>>) -> Self {
295 Self { start, end }
296 }
297
298 pub fn unbounded() -> Self {
299 Self {
300 start: None,
301 end: None,
302 }
303 }
304
305 pub fn contains(&self, key: &[u8]) -> bool {
306 let after_start = self.start.as_ref().map_or(true, |s| key >= s.as_slice());
307 let before_end = self.end.as_ref().map_or(true, |e| key < e.as_slice());
308 after_start && before_end
309 }
310}