aurora-db 0.6.2

A lightweight, real-time embedded database with built-in PubSub, reactive queries, background workers, and intelligent caching.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//! # Aurora Data Types
//!
//! This module defines the core data structures used throughout Aurora DB, 
//! including the fundamental `Value` enum, document schema definitions, 
//! and configuration options.
//!
//! ## Key Types
//! - **Value**: The universal data type for all document fields (String, Int, UUID, etc.).
//! - **Document**: A collection-native document containing data and a system ID.
//! - **FieldType / FieldDefinition**: Structures for defining and validating collection schemas.
//! - **AuroraConfig**: The primary configuration struct for database initialization.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::path::PathBuf;
use uuid::Uuid;

/// Validation constraint stored on a field definition.
/// Applied during insert/update to enforce data integrity.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FieldValidationConstraint {
    Format(String),
    Min(f64),
    Max(f64),
    MinLength(i64),
    MaxLength(i64),
    Pattern(String),
}

impl PartialEq for FieldValidationConstraint {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Format(a), Self::Format(b)) | (Self::Pattern(a), Self::Pattern(b)) => a == b,
            (Self::Min(a), Self::Min(b)) | (Self::Max(a), Self::Max(b)) => {
                a.to_bits() == b.to_bits()
            }
            (Self::MinLength(a), Self::MinLength(b)) | (Self::MaxLength(a), Self::MaxLength(b)) => {
                a == b
            }
            _ => false,
        }
    }
}

impl Eq for FieldValidationConstraint {}

impl Hash for FieldValidationConstraint {
    fn hash<H: Hasher>(&self, state: &mut H) {
        std::mem::discriminant(self).hash(state);
        match self {
            Self::Format(s) | Self::Pattern(s) => s.hash(state),
            Self::Min(f) | Self::Max(f) => f.to_bits().hash(state),
            Self::MinLength(i) | Self::MaxLength(i) => i.hash(state),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ScalarType {
    String,
    Int,
    Uuid,
    Bool,
    Float,
    DateTime,
    Object,
    Array,
    Any,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum FieldType {
    Scalar(ScalarType),
    Object,
    Array(ScalarType),
    Nested(Box<HashMap<String, FieldDefinition>>),
    Any,
}

impl Hash for FieldType {
    fn hash<H: Hasher>(&self, state: &mut H) {
        std::mem::discriminant(self).hash(state);
        match self {
            FieldType::Scalar(s) => s.hash(state),
            FieldType::Array(s) => s.hash(state),
            FieldType::Nested(m) => {
                let mut keys: Vec<_> = m.keys().collect();
                keys.sort();
                for k in keys {
                    k.hash(state);
                    m.get(k).unwrap().hash(state);
                }
            }
            _ => {}
        }
    }
}

impl fmt::Display for ScalarType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ScalarType::String => write!(f, "String"),
            ScalarType::Int => write!(f, "Int"),
            ScalarType::Uuid => write!(f, "Uuid"),
            ScalarType::Bool => write!(f, "Bool"),
            ScalarType::Float => write!(f, "Float"),
            ScalarType::DateTime => write!(f, "DateTime"),
            ScalarType::Object => write!(f, "Object"),
            ScalarType::Array => write!(f, "Array"),
            ScalarType::Any => write!(f, "Any"),
        }
    }
}

impl fmt::Display for FieldType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FieldType::Scalar(s) => write!(f, "{}", s),
            FieldType::Object => write!(f, "Object"),
            FieldType::Array(s) => write!(f, "Array<{}>", s),
            FieldType::Nested(_) => write!(f, "Nested"),
            FieldType::Any => write!(f, "Any"),
        }
    }
}

impl fmt::Display for FieldDefinition {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}{} (indexed: {}, unique: {})",
            self.field_type,
            if self.nullable { "?" } else { "!" },
            self.indexed,
            self.unique,
        )
    }
}

impl fmt::Display for Collection {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            serde_json::to_string_pretty(self).unwrap_or_default()
        )
    }
}

impl fmt::Display for DurabilityMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DurabilityMode::None => write!(f, "None"),
            DurabilityMode::WAL => write!(f, "WAL"),
            DurabilityMode::Strict => write!(f, "Strict"),
            DurabilityMode::Synchronous => write!(f, "Synchronous"),
        }
    }
}

impl fmt::Display for ColdStoreMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ColdStoreMode::HighThroughput => write!(f, "HighThroughput"),
            ColdStoreMode::LowSpace => write!(f, "LowSpace"),
        }
    }
}

impl fmt::Display for AuroraConfig {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            serde_json::to_string_pretty(self).unwrap_or_default()
        )
    }
}

impl Default for FieldType {
    fn default() -> Self {
        FieldType::Any
    }
}

impl FieldType {
    pub const SCALAR_STRING: FieldType = FieldType::Scalar(ScalarType::String);
    pub const SCALAR_INT: FieldType = FieldType::Scalar(ScalarType::Int);
    pub const SCALAR_BOOL: FieldType = FieldType::Scalar(ScalarType::Bool);
    pub const SCALAR_FLOAT: FieldType = FieldType::Scalar(ScalarType::Float);
    pub const SCALAR_UUID: FieldType = FieldType::Scalar(ScalarType::Uuid);
    pub const SCALAR_OBJECT: FieldType = FieldType::Scalar(ScalarType::Object);
    pub const SCALAR_ARRAY: FieldType = FieldType::Scalar(ScalarType::Array);
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct Relation {
    pub to: String,
    pub key: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, Default)]
pub struct FieldDefinition {
    pub field_type: FieldType,
    pub unique: bool,
    pub indexed: bool,
    pub nullable: bool,
    /// Validation constraints applied on insert/update.
    #[serde(default)]
    pub validations: Vec<FieldValidationConstraint>,
    /// Relationship metadata for foreign keys.
    pub relation: Option<Relation>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Collection {
    pub name: String,
    pub fields: HashMap<String, FieldDefinition>,
}

/// Represents a single document in an Aurora collection.
///
/// A document consists of a unique system ID (`_sid`) and a set of key-value pairs (`data`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Document {
    /// The unique internal system ID for this document.
    #[serde(rename = "_sid", alias = "id")]
    pub _sid: String,
    /// The document's fields and values.
    pub data: HashMap<String, Value>,
}

impl Document {
    /// Creates a new document with a unique ID and empty data.
    pub fn new() -> Self {
        Self {
            _sid: Uuid::now_v7().to_string(),
            data: HashMap::new(),
        }
    }

    /// Returns the system ID of the document.
    pub fn id(&self) -> &str {
        &self._sid
    }

    /// Gets a value from the document's data by field name.
    pub fn get(&self, field: &str) -> Option<&Value> {
        self.data.get(field)
    }

    /// Maps the document data into a user-defined struct that implements `Deserialize`.
    ///
    /// This automatically aliases the internal `_sid` to `id` if not present in the data,
    /// and also provides `_sid` explicitly.
    pub fn bind<T: serde::de::DeserializeOwned>(mut self) -> crate::error::Result<T> {
        self.data
            .insert("_sid".to_string(), Value::String(self._sid.clone()));

        if !self.data.contains_key("id") {
            self.data
                .insert("id".to_string(), Value::String(self._sid.clone()));
        }

        let json = serde_json::to_value(&self.data)?;
        Ok(serde_json::from_value(json)?)
    }
}

impl fmt::Display for Document {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{\n  \"_sid\": \"{}\",\n  \"data\": {{\n", self._sid)?;
        let mut first = true;
        for (k, v) in &self.data {
            if !first {
                write!(f, ",\n")?;
            }
            write!(f, "    \"{}\": {}", k, v)?;
            first = false;
        }
        write!(f, "\n  }}\n}}")
    }
}

/// Represents any value that can be stored in an Aurora document.
///
/// This enum covers all supported scalar and collection types in AQL.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Value {
    /// A null value.
    Null,
    /// A boolean value.
    Bool(bool),
    /// A 64-bit integer.
    Int(i64),
    /// A 64-bit floating point number.
    Float(f64),
    /// A UTF-8 string.
    String(String),
    /// A Universally Unique Identifier (UUID).
    Uuid(Uuid),
    /// A UTC date and time.
    DateTime(DateTime<Utc>),
    /// An ordered list of values.
    Array(Vec<Value>),
    /// A map of string keys to values.
    Object(HashMap<String, Value>),
}

impl PartialEq for Value {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Value::Null, Value::Null) => true,
            (Value::Bool(a), Value::Bool(b)) => a == b,
            (Value::Int(a), Value::Int(b)) => a == b,
            (Value::Int(a), Value::Float(b)) => *a as f64 == *b,
            (Value::Float(a), Value::Int(b)) => *a == *b as f64,
            (Value::Float(a), Value::Float(b)) => (a - b).abs() < f64::EPSILON,
            (Value::String(a), Value::String(b)) => a == b,
            (Value::Uuid(a), Value::Uuid(b)) => a == b,
            (Value::Uuid(u), Value::String(s)) | (Value::String(s), Value::Uuid(u)) => {
                u.to_string() == *s
            }
            (Value::DateTime(a), Value::DateTime(b)) => a == b,
            (Value::Array(a), Value::Array(b)) => a == b,
            (Value::Object(a), Value::Object(b)) => a == b,
            _ => false,
        }
    }
}

impl Eq for Value {}

impl PartialOrd for Value {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Value {
    fn cmp(&self, other: &Self) -> Ordering {
        match (self, other) {
            (Value::Null, Value::Null) => Ordering::Equal,
            (Value::Null, _) => Ordering::Less,
            (_, Value::Null) => Ordering::Greater,

            (Value::Bool(a), Value::Bool(b)) => a.cmp(b),
            (Value::Bool(_), _) => Ordering::Less,
            (_, Value::Bool(_)) => Ordering::Greater,

            (Value::Int(a), Value::Int(b)) => a.cmp(b),
            (Value::Int(a), Value::Float(b)) => (*a as f64).total_cmp(b),
            (Value::Float(a), Value::Int(b)) => a.total_cmp(&(*b as f64)),
            (Value::Float(a), Value::Float(b)) => a.total_cmp(b),
            (Value::Int(_) | Value::Float(_), _) => Ordering::Less,
            (_, Value::Int(_) | Value::Float(_)) => Ordering::Greater,

            (Value::String(a), Value::String(b)) => a.cmp(b),
            (Value::Uuid(u), Value::String(s)) => u.to_string().cmp(s),
            (Value::String(s), Value::Uuid(u)) => s.cmp(&u.to_string()),
            (Value::String(_), _) => Ordering::Less,
            (_, Value::String(_)) => Ordering::Greater,

            (Value::Uuid(a), Value::Uuid(b)) => a.cmp(b),
            (Value::Uuid(_), _) => Ordering::Less,
            (_, Value::Uuid(_)) => Ordering::Greater,

            (Value::DateTime(a), Value::DateTime(b)) => a.cmp(b),
            (Value::DateTime(_), _) => Ordering::Less,
            (_, Value::DateTime(_)) => Ordering::Greater,

            (Value::Array(a), Value::Array(b)) => a.cmp(b),
            (Value::Array(_), _) => Ordering::Less,
            (_, Value::Array(_)) => Ordering::Greater,

            (Value::Object(_), Value::Object(_)) => Ordering::Equal, // Object ordering not strictly defined
        }
    }
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Value::Null => write!(f, "null"),
            Value::Bool(b) => write!(f, "{}", b),
            Value::Int(i) => write!(f, "{}", i),
            Value::Float(fl) => write!(f, "{}", fl),
            Value::String(s) => write!(f, "\"{}\"", s),
            Value::Uuid(u) => write!(f, "\"{}\"", u),
            Value::DateTime(dt) => write!(f, "\"{}\"", dt),
            Value::Array(arr) => {
                write!(f, "[")?;
                for (i, val) in arr.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{}", val)?;
                }
                write!(f, "]")
            }
            Value::Object(obj) => {
                write!(f, "{{")?;
                let mut first = true;
                for (k, v) in obj {
                    if !first {
                        write!(f, ", ")?;
                    }
                    write!(f, "\"{}\": {}", k, v)?;
                    first = false;
                }
                write!(f, "}}")
            }
        }
    }
}

impl Value {
    /// Returns the value as a string slice, if it is a `Value::String`.
    pub fn as_str(&self) -> Option<&str> {
        if let Value::String(s) = self {
            Some(s)
        } else {
            None
        }
    }

    /// Returns the value as an `i64`, if it is a `Value::Int`.
    pub fn as_i64(&self) -> Option<i64> {
        if let Value::Int(i) = self {
            Some(*i)
        } else {
            None
        }
    }

    /// Returns the value as an `f64`, if it is a `Value::Float`.
    pub fn as_f64(&self) -> Option<f64> {
        if let Value::Float(f) = self {
            Some(*f)
        } else {
            None
        }
    }

    /// Returns the value as a `bool`, if it is a `Value::Bool`.
    pub fn as_bool(&self) -> Option<bool> {
        if let Value::Bool(b) = self {
            Some(*b)
        } else {
            None
        }
    }

    /// Returns the value as a reference to a `Vec<Value>`, if it is a `Value::Array`.
    pub fn as_array(&self) -> Option<&Vec<Value>> {
        if let Value::Array(a) = self {
            Some(a)
        } else {
            None
        }
    }

    /// Returns the value as a reference to a `HashMap`, if it is a `Value::Object`.
    pub fn as_object(&self) -> Option<&HashMap<String, Value>> {
        if let Value::Object(o) = self {
            Some(o)
        } else {
            None
        }
    }

    /// Coerces the value to a target field type if possible.
    ///
    /// This is used for type normalization during ingestion.
    pub fn coerce_to(&self, target: &FieldType) -> Value {
        match target {
            FieldType::Scalar(ScalarType::String) => match self {
                Value::String(s) => Value::String(s.clone()),
                Value::Null => Value::Null,
                _ => Value::String(self.to_string()),
            },
            FieldType::Scalar(ScalarType::Int) => match self {
                Value::Int(i) => Value::Int(*i),
                Value::Float(f) => Value::Int(*f as i64),
                Value::String(s) => s.parse().map(Value::Int).unwrap_or(Value::Null),
                _ => Value::Null,
            },
            FieldType::Scalar(ScalarType::Float) => match self {
                Value::Float(f) => Value::Float(*f),
                Value::Int(i) => Value::Float(*i as f64),
                Value::String(s) => s.parse().map(Value::Float).unwrap_or(Value::Null),
                _ => Value::Null,
            },
            FieldType::Scalar(ScalarType::Uuid) => match self {
                Value::Uuid(u) => Value::Uuid(*u),
                Value::String(s) => Uuid::parse_str(s).map(Value::Uuid).unwrap_or(Value::Null),
                _ => Value::Null,
            },
            FieldType::Scalar(ScalarType::Bool) => match self {
                Value::Bool(b) => Value::Bool(*b),
                Value::String(s) => Value::Bool(s == "true"),
                _ => Value::Null,
            },
            FieldType::Scalar(ScalarType::DateTime) => match self {
                Value::DateTime(dt) => Value::DateTime(*dt),
                Value::String(s) => s
                    .parse::<DateTime<Utc>>()
                    .map(Value::DateTime)
                    .unwrap_or(Value::Null),
                _ => Value::Null,
            },
            _ => self.clone(),
        }
    }
}

impl From<String> for Value {
    fn from(v: String) -> Self {
        Value::String(v)
    }
}
impl From<&str> for Value {
    fn from(v: &str) -> Self {
        Value::String(v.to_string())
    }
}
impl From<bool> for Value {
    fn from(v: bool) -> Self {
        Value::Bool(v)
    }
}
impl From<f64> for Value {
    fn from(v: f64) -> Self {
        Value::Float(v)
    }
}
impl From<i64> for Value {
    fn from(v: i64) -> Self {
        Value::Int(v)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum DurabilityMode {
    None,
    WAL,
    Strict,
    /// Synchronous mode: every write blocks until it reaches durable storage.
    /// No buffering, no WAL — safest but slowest.
    Synchronous,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ColdStoreMode {
    HighThroughput,
    LowSpace,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuroraConfig {
    pub db_path: PathBuf,
    pub create_dirs: bool,
    pub hot_cache_size_mb: usize,
    pub hot_cache_cleanup_interval_secs: u64,
    pub eviction_policy: crate::storage::EvictionPolicy,
    pub cold_cache_capacity_mb: usize,
    pub cold_flush_interval_ms: Option<u64>,
    pub cold_mode: ColdStoreMode,
    pub auto_compact: bool,
    pub compact_interval_mins: u64,
    pub max_index_entries_per_field: usize,
    pub enable_write_buffering: bool,
    pub write_buffer_size: usize,
    pub write_buffer_flush_interval_ms: u64,
    pub durability_mode: DurabilityMode,
    pub enable_wal: bool,
    pub checkpoint_interval_ms: u64,
    pub audit_log_path: Option<String>,
    pub workers_enabled: bool,
    pub worker_threads: usize,
}

impl Default for AuroraConfig {
    fn default() -> Self {
        Self {
            db_path: PathBuf::from("aurora_db"),
            create_dirs: true,
            hot_cache_size_mb: 256,
            hot_cache_cleanup_interval_secs: 60,
            eviction_policy: crate::storage::EvictionPolicy::LRU,
            cold_cache_capacity_mb: 1024,
            cold_flush_interval_ms: Some(5000),
            cold_mode: ColdStoreMode::HighThroughput,
            auto_compact: true,
            compact_interval_mins: 60,
            max_index_entries_per_field: 100_000,
            enable_write_buffering: true,
            write_buffer_size: 10_000,
            write_buffer_flush_interval_ms: 1000,
            durability_mode: DurabilityMode::WAL,
            enable_wal: true,
            checkpoint_interval_ms: 10000,
            audit_log_path: None,
            workers_enabled: false,
            worker_threads: 4,
        }
    }
}