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
use aurora_db::{Aurora, AuroraConfig, FieldType, Value};
use std::sync::Arc;
use std::time::Instant;

#[tokio::test]
async fn test_memory_usage_growth() {
    println!("\n=== Memory Usage Growth Test ===");

    let temp_dir = tempfile::tempdir().unwrap();
    let db_path = temp_dir.path().join("memory_test.aurora");

    let mut config = AuroraConfig::default();
    config.db_path = db_path;
    config.enable_write_buffering = true;
    config.enable_wal = true;
    config.hot_cache_size_mb = 128; // Limit hot cache

    let db = Arc::new(Aurora::with_config(config).await.unwrap());

    db.new_collection(
        "memory_test",
        vec![
            (
                "data",
                aurora_db::types::FieldDefinition {
                    field_type: FieldType::SCALAR_STRING,
                    unique: false,
                    indexed: false,
                    nullable: true,
                    validations: vec![],
                    relation: None,
                },
            ),
            (
                "index",
                aurora_db::types::FieldDefinition {
                    field_type: FieldType::SCALAR_INT,
                    unique: false,
                    indexed: false,
                    nullable: true,
                    validations: vec![],
                    relation: None,
                },
            ),
        ],
    )
    .await
    .unwrap();

    // Get initial process info
    let pid = std::process::id();
    let initial_rss = get_process_rss_mb(pid);
    println!("Initial RSS: {} MB", initial_rss);

    // Insert documents in batches and measure memory
    let batch_size = 10000;
    let num_batches = 10; // 100k total documents

    for batch in 0..num_batches {
        println!("\nBatch {}/{}:", batch + 1, num_batches);

        let start = Instant::now();

        // Insert batch
        for i in 0..batch_size {
            let idx = batch * batch_size + i;
            db.insert_into(
                "memory_test",
                vec![
                    (
                        "data",
                        Value::String(format!(
                            "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Document {}",
                            idx
                        )),
                    ),
                    ("index", Value::Int(idx as i64)),
                ],
            )
            .await
            .unwrap();
        }

        let duration = start.elapsed();
        let current_rss = get_process_rss_mb(pid);
        let growth = current_rss - initial_rss;

        println!("  Inserted: {} documents", (batch + 1) * batch_size);
        println!("  Time: {:.2?}", duration);
        println!("  Current RSS: {} MB", current_rss);
        println!("  Growth: {} MB", growth);
        println!(
            "  Throughput: {:.2} docs/sec",
            batch_size as f64 / duration.as_secs_f64()
        );
    }

    // Final flush
    db.flush().unwrap();
    tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;

    let final_rss = get_process_rss_mb(pid);
    let total_growth = final_rss - initial_rss;

    println!("\n=== Summary ===");
    println!("Initial RSS: {} MB", initial_rss);
    println!("Final RSS: {} MB", final_rss);
    println!("Total growth: {} MB", total_growth);
    println!("Documents: 100,000");
    println!("Average per 10k docs: {:.2} MB", total_growth as f64 / 10.0);

    // Query to verify all data is accessible
    let query_start = Instant::now();
    let count = db.query("memory_test").collect().await.unwrap().len();
    let query_time = query_start.elapsed();

    println!(
        "Query verification: {} documents in {:.2?}",
        count, query_time
    );

    assert_eq!(count, 100000, "All documents should be queryable");

    // Memory growth should be reasonable (< 500MB for 100k docs)
    assert!(
        total_growth < 500,
        "Memory growth should be under 500MB (got {} MB)",
        total_growth
    );
}

#[tokio::test]
async fn test_hot_cache_eviction() {
    println!("\n=== Hot Cache Eviction Test ===");

    let temp_dir = tempfile::tempdir().unwrap();
    let db_path = temp_dir.path().join("cache_test.aurora");

    let mut config = AuroraConfig::default();
    config.db_path = db_path;
    config.hot_cache_size_mb = 10; // Very small cache to force evictions
    config.enable_write_buffering = false; // Direct writes for testing

    let db = Arc::new(Aurora::with_config(config).await.unwrap());

    db.new_collection(
        "cache_test",
        vec![(
            "data",
            aurora_db::types::FieldDefinition {
                field_type: FieldType::SCALAR_STRING,
                unique: false,
                indexed: false,
                nullable: true,
                validations: vec![],
                relation: None,
            },
        )],
    )
    .await
    .unwrap();

    println!("Inserting 10,000 documents with 10MB hot cache limit...");

    // Insert documents that should exceed cache
    for i in 0..10000 {
        db.insert_into(
            "cache_test",
            vec![(
                "data",
                Value::String(format!(
                    "Document {} with some padding text to increase size",
                    i
                )),
            )],
        )
        .await
        .unwrap();
    }

    println!("All documents inserted successfully");

    // Query some old documents (should be evicted from cache)
    let start = Instant::now();
    let doc1 = db.get_document("cache_test", &"0".to_string()).unwrap();
    let time1 = start.elapsed();

    // Query recent document (likely in cache)
    let start = Instant::now();
    let doc2 = db
        .query("cache_test")
        .filter(|f: &aurora_db::query::FilterBuilder| {
            f.eq(
                "data",
                "Document 9999 with some padding text to increase size",
            )
        })
        .collect()
        .await
        .unwrap();
    let time2 = start.elapsed();

    println!("Old document query time: {:?}", time1);
    println!("Recent document query time: {:?}", time2);

    assert!(
        doc1.is_some() || doc2.len() > 0,
        "Should be able to retrieve evicted documents from cold storage"
    );

    println!("SUCCESS: Hot cache eviction working correctly");
}

#[tokio::test]
async fn test_error_disk_full_simulation() {
    println!("\n=== Disk Full Simulation Test ===");

    // This test simulates what happens when disk is nearly full
    // by using a very small database and filling it up

    let temp_dir = tempfile::tempdir().unwrap();
    let db_path = temp_dir.path().join("disk_test.aurora");

    let db = Arc::new(Aurora::open(db_path.to_str().unwrap()).await.unwrap());

    db.new_collection(
        "disk_test",
        vec![(
            "data",
            aurora_db::types::FieldDefinition {
                field_type: FieldType::SCALAR_STRING,
                unique: false,
                indexed: false,
                nullable: true,
                validations: vec![],
                relation: None,
            },
        )],
    )
    .await
    .unwrap();

    println!("Attempting to insert large documents...");

    // Try to insert very large documents
    let large_data = "x".repeat(1024 * 1024); // 1MB per document
    let mut success_count = 0;
    let mut error_count = 0;

    for i in 0..100 {
        match db
            .insert_into(
                "disk_test",
                vec![("data", Value::String(format!("{}{}", large_data, i)))],
            )
            .await
        {
            Ok(_) => success_count += 1,
            Err(e) => {
                println!("Error on document {}: {:?}", i, e);
                error_count += 1;
                if error_count > 5 {
                    break; // Stop after a few errors
                }
            }
        }
    }

    println!("Success: {}, Errors: {}", success_count, error_count);
    println!("SUCCESS: Database handles large writes without panicking");
}

#[tokio::test]
async fn test_concurrent_collection_creation() {
    println!("\n=== Concurrent Collection Creation Test ===");

    let temp_dir = tempfile::tempdir().unwrap();
    let db_path = temp_dir.path().join("concurrent_collections.aurora");
    let db = Arc::new(Aurora::open(db_path.to_str().unwrap()).await.unwrap());

    let mut tasks = vec![];

    // Try to create 10 collections concurrently
    for i in 0..10 {
        let db_clone = Arc::clone(&db);
        tasks.push(tokio::spawn(async move {
            db_clone
                .new_collection(
                    &format!("collection_{}", i),
                    vec![(
                        "field",
                        aurora_db::types::FieldDefinition {
                            field_type: FieldType::SCALAR_STRING,
                            unique: false,
                            indexed: false,
                            nullable: true,
                            validations: vec![],
                            relation: None,
                        },
                    )],
                )
                .await
        }));
    }

    let mut success = 0;
    for task in tasks {
        if task.await.unwrap().is_ok() {
            success += 1;
        }
    }

    println!("Successfully created {} collections concurrently", success);
    assert_eq!(success, 10, "All collection creations should succeed");
}

#[tokio::test]
async fn test_rapid_flush_calls() {
    println!("\n=== Rapid Flush Calls Test ===");

    let temp_dir = tempfile::tempdir().unwrap();
    let db_path = temp_dir.path().join("flush_test.aurora");

    let mut config = AuroraConfig::default();
    config.db_path = db_path;
    config.enable_write_buffering = true;

    let db = Arc::new(Aurora::with_config(config).await.unwrap());

    db.new_collection(
        "flush_test",
        vec![(
            "data",
            aurora_db::types::FieldDefinition {
                field_type: FieldType::SCALAR_STRING,
                unique: false,
                indexed: false,
                nullable: true,
                validations: vec![],
                relation: None,
            },
        )],
    )
    .await
    .unwrap();

    // Insert some data
    for i in 0..100 {
        db.insert_into(
            "flush_test",
            vec![("data", Value::String(format!("data_{}", i)))],
        )
        .await
        .unwrap();
    }

    println!("Calling flush() rapidly 50 times...");

    let start = Instant::now();
    for _ in 0..50 {
        db.flush().unwrap();
    }
    let duration = start.elapsed();

    println!("All flushes completed in {:?}", duration);
    println!("SUCCESS: Rapid flush calls don't cause deadlocks or panics");

    // Verify data integrity
    let count = db.query("flush_test").collect().await.unwrap().len();
    assert_eq!(count, 100, "All data should be intact after rapid flushes");
}

// Helper function to get process RSS in MB
fn get_process_rss_mb(pid: u32) -> i64 {
    let _ = pid;
    #[cfg(target_os = "linux")]
    {
        if let Ok(status) = std::fs::read_to_string(format!("/proc/{}/status", pid)) {
            for line in status.lines() {
                if line.starts_with("VmRSS:") {
                    if let Some(kb_str) = line.split_whitespace().nth(1) {
                        if let Ok(kb) = kb_str.parse::<i64>() {
                            return kb / 1024; // Convert to MB
                        }
                    }
                }
            }
        }
    }

    #[cfg(target_os = "macos")]
    {
        use std::process::Command;
        let output = Command::new("ps")
            .args(&["-o", "rss=", "-p", &pid.to_string()])
            .output();

        if let Ok(output) = output {
            let s = String::from_utf8_lossy(&output.stdout);
            if let Ok(kb) = s.trim().parse::<i64>() {
                return kb / 1024;
            }
        }
    }

    // Fallback for non-Linux/macOS or if reading fails
    0
}