aurora-db 0.4.1

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
# Aurora DB


> A lightweight, real-time embedded database designed for modern applications

[![Crates.io](https://img.shields.io/crates/v/aurora-db.svg)](https://crates.io/crates/aurora-db)
[![Documentation](https://docs.rs/aurora-db/badge.svg)](https://docs.rs/aurora-db)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

## Why Aurora Exists


Most embedded databases force you to choose: either **simple key-value storage** with manual indexing and caching, or **heavy SQL databases** with complex setup. I wanted something different.

**Aurora was born from a simple need**: a database that **just works** for building real-time applications. No external services, no complex configuration, no choosing between twenty different storage engines. Just install it, open it, and start building.

### The Philosophy


**Real-time by default.** Data changes should propagate instantly. Your UI shouldn't poll—it should react. Background jobs should process reliably without external queues. This isn't optional; it's how modern applications work.

**Lightweight without compromise.** Embedded doesn't mean primitive. Aurora handles indexing, caching, pub/sub, reactive queries, and background workers—all built-in. You get production-grade features without the operational overhead.

**Performance through intelligence.** The hybrid hot/cold architecture wasn't an accident. Frequently accessed data lives in memory (200K ops/sec), everything else persists to disk (10K ops/sec). Schema-aware selective indexing means I only index what you actually query. Smart defaults, intelligent caching, zero configuration to get started.

### The Design


Every feature in Aurora solves a real problem:

- **PubSub** → Because polling databases is wasteful
- **Reactive Queries** → Because UIs should update automatically
- **Durable Workers** → Because background jobs shouldn't need Redis
- **Computed Fields** → Because derived data shouldn't duplicate storage
- **Write Buffering** → Because high-throughput writes shouldn't kill performance
- **Schema-Aware Indexing** → Because indexing every field is insane

This wasn't scope creep—it was intentional. Building real-time apps requires all these pieces, and they should work together seamlessly.

### What's Next


The current bottleneck is in-memory index management. I'm currently looking into how **DiceDB**, **Redis**, and **RocksDB** handle high-performance in-memory structures, exploring:

- Lock-free index structures inspired by DiceDB's concurrent patterns
- Memory-efficient data structures from Redis's designs
- RocksDB's LSM-tree approach for better write amplification
- Smarter cache eviction using access pattern analysis

---

## Table of Contents


### Getting Started


- [Schema Management]./src/docs/schema.md - Define collections and field types
- [CRUD Operations]./src/docs/crud.md - Create, read, update, and delete documents
- [Querying]./src/docs/querying.md - Filter, sort, and retrieve data

### Advanced Features


- [PubSub System]./src/docs/pubsub.md - Real-time change notifications
- [Reactive Queries]./src/docs/reactive.md - Auto-updating query results
- [Durable Workers]./src/docs/workers.md - Background job processing
- [Computed Fields]./src/docs/computed-fields.md - Derived values from existing data

### Optimization


- [Performance Guide]./src/docs/performance.md - Tuning, caching, and best practices

## Quick Start


### Installation


```toml
[dependencies]
aurora-db = "0.3.0"
```

### Basic Usage


```rust
use aurora_db::{Aurora, FieldType, Value};

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Open database
    let db = Aurora::open("myapp.db")?;

    // Create a collection
    db.new_collection("users", vec![
        ("name", FieldType::String, false),
        ("email", FieldType::String, true),  // unique
        ("age", FieldType::Int, false),
    ])?;

    // Insert a document
    let user_id = db.insert_into("users", vec![
        ("name", Value::String("Alice".to_string())),
        ("email", Value::String("alice@example.com".to_string())),
        ("age", Value::Int(30)),
    ]).await?;

    // Query documents
    let users = db.query("users")
        .filter(|f| f.gt("age", 25))
        .collect()
        .await?;

    println!("Found {} users over 25", users.len());

    Ok(())
}
```

## Feature Overview


### Collections & Schema


Define structured collections with type-safe fields and unique constraints.

```rust
db.new_collection("products", vec![
    ("sku", FieldType::String, true),      // unique
    ("name", FieldType::String, false),
    ("price", FieldType::Float, false),
    ("in_stock", FieldType::Bool, false),
])?;
```

[Learn more →](./src/docs/schema.md)

### Powerful Querying


Filter, sort, paginate, and search your data with an intuitive API.

```rust
let products = db.query("products")
    .filter(|f| f.eq("in_stock", true) && f.lt("price", 100.0))
    .order_by("price", true)
    .limit(10)
    .collect()
    .await?;
```

[Learn more →](./src/docs/querying.md)

### Real-time Updates


Subscribe to data changes with the PubSub system.

```rust
let mut subscription = db.subscribe("orders", None).await?;

while let Ok(event) = subscription.recv().await {
    println!("Order changed: {:?}", event);
}
```

[Learn more →](./src/docs/pubsub.md)

### ⚡ Reactive Queries


Automatically update query results when data changes.

```rust
let active_users = db.reactive_query("users")
    .filter(|f| f.eq("online", true))
    .build()
    .await?;

// Results automatically update when users go online/offline
let current_users = active_users.get_results().await?;
```

[Learn more →](./src/docs/reactive.md)

### Background Jobs


Process tasks asynchronously with automatic retries.

```rust
// Define a job handler
struct EmailHandler;

#[async_trait]

impl JobHandler for EmailHandler {
    async fn handle(&self, job: &Job) -> JobResult {
        send_email(&job.payload).await?;
        Ok(())
    }
}

// Start worker
let mut executor = WorkerExecutor::new(db.clone(), 4);
executor.register_handler("send_email", Box::new(EmailHandler));
executor.start().await?;

// Enqueue job
db.enqueue_job("send_email", payload, None, Priority::High).await?;
```

[Learn more →](./src/docs/workers.md)

### Computed Fields


Derive values automatically from document data.

```rust
let mut registry = ComputedFieldsRegistry::new();

// Full name from first + last
registry.register(
    "users",
    "full_name",
    Expression::Concat {
        fields: vec!["first_name".to_string(), "last_name".to_string()],
        separator: " ".to_string(),
    },
);

db.set_computed_fields(registry);
```

[Learn more →](./src/docs/computed-fields.md)

### High Performance


Optimized architecture with hot/cold storage and intelligent caching.

| Operation      | Throughput    |
| -------------- | ------------- |
| Hot Cache Read | ~200K ops/sec |
| Indexed Insert | ~17K ops/sec  |
| Indexed Query  | ~50K ops/sec  |

[Learn more →](./src/docs/performance.md)

## Architecture


Aurora uses a hybrid storage architecture:

```
┌─────────────────────────────────────────┐
│           Application Layer              │
├─────────────────────────────────────────┤
│  PubSub  │ Reactive │ Workers │ Computed │
├─────────────────────────────────────────┤
│            Query Engine                  │
├─────────────────────────────────────────┤
│   Hot Cache (In-Memory)  │   Indices    │
├──────────────────────────┴───────────────┤
│      Cold Storage (Sled - On Disk)      │
└─────────────────────────────────────────┘
```

### Storage Layers


- **Hot Cache**: In-memory cache for frequently accessed data (~200K ops/sec)
- **Cold Storage**: Persistent disk storage using Sled (~10K ops/sec)
- **Indices**: In-memory indices for fast lookups (O(1) complexity)

### Advanced Features


- **PubSub**: Real-time change notifications
- **Reactive Queries**: Auto-updating query results
- **Durable Workers**: Background job processing with retries
- **Computed Fields**: Virtual fields calculated at query time

## Configuration


Customize Aurora's behavior with `AuroraConfig`:

```rust
use aurora_db::{Aurora, AuroraConfig, EvictionPolicy};

let config = AuroraConfig {
    // Cache settings
    hot_cache_size_mb: 256,
    cold_cache_capacity_mb: 512,
    eviction_policy: EvictionPolicy::LRU,

    // Write buffering
    enable_write_buffering: true,
    write_buffer_size: 1000,
    write_buffer_flush_interval_ms: 100,

    // Index settings
    max_index_entries_per_field: 100000,

    // Cleanup intervals
    hot_cache_cleanup_interval_secs: 300,
    cold_flush_interval_ms: 1000,

    ..Default::default()
};

let db = Aurora::open_with_config("mydb.db", config)?;
```

[Learn more →](./src/docs/performance.md#configuration-tuning)

## Use Cases


### Real-time Applications


- Chat applications with live message updates
- Collaborative editing with reactive queries
- Live dashboards with auto-updating metrics
- Gaming leaderboards

### Background Processing


- Email delivery with retry logic
- Image processing pipelines
- Data exports and reports
- Webhook delivery

### Data-Intensive Apps


- E-commerce with inventory tracking
- Analytics with computed metrics
- Content management systems
- IoT data collection

## Best Practices


### 1. Index Strategy


```rust
//  DO: Index frequently queried fields
db.create_index("users", "email").await?;
db.create_index("orders", "customer_id").await?;

//  DON'T: Index every field
// Only index what you query
```

### 2. Query Optimization


```rust
//  DO: Use projections and limits
let users = db.query("users")
    .select(vec!["id", "name"])  // Only needed fields
    .limit(100)                   // Bounded result set
    .collect()
    .await?;

//  DON'T: Unbounded queries
let users = db.query("users").collect().await?;
```

### 3. Write Performance


```rust
//  DO: Use batch operations
db.batch_insert("logs", log_entries).await?;

//  DON'T: Individual inserts in loops
for entry in log_entries {
    db.insert_into("logs", entry).await?;
}
```

### 4. Real-time Updates


```rust
//  DO: Use reactive queries for UI state
let online_users = ReactiveState::new(
    db.clone(),
    "users",
    |f| f.eq("online", true)
).await?;

//  DON'T: Poll the database
loop {
    let users = db.query("users")
        .filter(|f| f.eq("online", true))
        .collect()
        .await?;
    tokio::time::sleep(Duration::from_secs(1)).await;
}
```

## Examples


Check out the `examples/` directory for complete examples:

- `reactive_demo.rs` - Reactive queries in action
- `contains_query_demo.rs` - Advanced querying
- `indexing_performance.rs` - Performance testing
- `index_demo.rs` - Index usage patterns

Run an example:

```bash
cargo run --example reactive_demo
```

## API Reference


For detailed API documentation:

```bash
cargo doc --open
```

## Community & Support


- **GitHub**: [github.com/bethel-nz/aurora]https://github.com/bethel-nz/aurora
- **Issues**: [github.com/bethel-nz/aurora/issues]https://github.com/bethel-nz/aurora/issues
- **Crates.io**: [crates.io/crates/aurora-db]https://crates.io/crates/aurora-db

## Version History


### v0.3.0 (Current)


- Schema-aware selective indexing (3-5x faster inserts)
- Schema caching for reduced overhead
- Optimized benchmark suite
- Fixed compiler warnings
- Comprehensive documentation

### v0.2.1


- PubSub system for real-time updates
- Reactive queries
- Durable workers with retry logic
- Computed fields
- Write buffer optimization
- Multiple cache eviction policies

### v0.1.x


- Initial release
- Basic CRUD operations
- Collections and schema
- Query system
- Transactions

## License


MIT License - see LICENSE file for details

---

**Ready to build?** Start with the [Schema Management](./src/docs/schema.md) guide!