aedb 0.2.10

Embedded Rust storage engine with transactional commits, WAL durability, and snapshot-consistent reads
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
use super::{
    AedbConfig, AedbError, AedbErrorCode, AedbInstance, CallerContext, ColumnDef, ColumnType,
    ConsistencyMode, DdlOperation, DurabilityMode, Expr, IdempotencyKey, Mutation, Permission,
    Query, QueryError, QueryOptions, RECOVERY_CACHE_TTL, RecoveryCache, RecoveryMode, Row,
    TransactionEnvelope, Value, WriteClass, WriteIntent, create_table, mk_recovery_view,
};
use std::fs;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tempfile::tempdir;

#[test]
fn recovery_cache_refresh_keeps_recent_entry() {
    let mut cache = RecoveryCache::default();
    for seq in 1..=16 {
        cache.put(seq, mk_recovery_view(seq));
    }
    assert!(
        cache.get(1).is_some(),
        "first entry should exist before refresh"
    );

    cache.put(17, mk_recovery_view(17));

    assert!(
        cache.get(1).is_some(),
        "refreshing an entry must protect it from immediate eviction"
    );
    assert!(
        cache.get(2).is_none(),
        "oldest non-refreshed entry should be evicted first"
    );
}

#[test]
fn recovery_cache_prunes_expired_entries() {
    let mut cache = RecoveryCache::default();
    cache.put(7, mk_recovery_view(7));
    {
        let entry = cache.entries.get_mut(&7).expect("entry");
        entry.created = Instant::now() - RECOVERY_CACHE_TTL - Duration::from_secs(1);
    }
    cache.prune_expired();
    assert!(cache.get(7).is_none(), "expired entry must be removed");
}

#[tokio::test]
async fn existence_and_introspection_apis_report_catalog_state() {
    let dir = tempdir().expect("temp");
    let db = AedbInstance::open_anonymous(AedbConfig::default(), dir.path()).expect("open");

    db.create_project("p").await.expect("project");
    db.create_scope("p", "s1").await.expect("scope");
    db.commit(Mutation::Ddl(DdlOperation::CreateTable {
        owner_id: None,
        if_not_exists: false,
        project_id: "p".into(),
        scope_id: "s1".into(),
        table_name: "users".into(),
        columns: vec![
            ColumnDef {
                name: "id".into(),
                col_type: ColumnType::Integer,
                nullable: false,
            },
            ColumnDef {
                name: "name".into(),
                col_type: ColumnType::Text,
                nullable: false,
            },
        ],
        primary_key: vec!["id".into()],
    }))
    .await
    .expect("table");
    db.commit(Mutation::Ddl(DdlOperation::CreateIndex {
        project_id: "p".into(),
        scope_id: "s1".into(),
        table_name: "users".into(),
        index_name: "idx_users_name".into(),
        if_not_exists: false,
        columns: vec!["name".into()],
        index_type: crate::catalog::schema::IndexType::BTree,
        partial_filter: None,
    }))
    .await
    .expect("index");

    assert!(db.project_exists("p").await.expect("project_exists"));
    assert!(db.scope_exists("p", "s1").await.expect("scope_exists"));
    assert!(
        db.table_exists("p", "s1", "users")
            .await
            .expect("table_exists")
    );
    assert!(
        db.index_exists("p", "s1", "users", "idx_users_name")
            .await
            .expect("index_exists")
    );

    let projects = db.list_projects().await.expect("list projects");
    assert!(projects.iter().any(|p| p.project_id == "p"));

    let scopes = db.list_scopes_info("p").await.expect("list scopes");
    let scope = scopes
        .iter()
        .find(|s| s.scope_id == "s1")
        .expect("scope info");
    assert_eq!(scope.table_count, 1);

    let tables = db.list_tables_info("p", "s1").await.expect("list tables");
    let table = tables
        .iter()
        .find(|t| t.table_name == "users")
        .expect("table info");
    assert_eq!(table.column_count, 2);
    assert_eq!(table.index_count, 1);
}

#[tokio::test]
async fn dependency_aware_ddl_batch_reorders_create_dependencies() {
    let dir = tempdir().expect("temp");
    let db = AedbInstance::open_anonymous(AedbConfig::default(), dir.path()).expect("open");

    let batch = db
        .commit_ddl_batch_dependency_aware(vec![
            DdlOperation::CreateIndex {
                project_id: "arcana".into(),
                scope_id: "ops".into(),
                table_name: "users".into(),
                index_name: "idx_users_name".into(),
                if_not_exists: true,
                columns: vec!["name".into()],
                index_type: crate::catalog::schema::IndexType::BTree,
                partial_filter: None,
            },
            DdlOperation::CreateTable {
                owner_id: None,
                if_not_exists: true,
                project_id: "arcana".into(),
                scope_id: "ops".into(),
                table_name: "users".into(),
                columns: vec![
                    ColumnDef {
                        name: "id".into(),
                        col_type: ColumnType::Integer,
                        nullable: false,
                    },
                    ColumnDef {
                        name: "name".into(),
                        col_type: ColumnType::Text,
                        nullable: false,
                    },
                ],
                primary_key: vec!["id".into()],
            },
            DdlOperation::CreateScope {
                owner_id: None,
                project_id: "arcana".into(),
                scope_id: "ops".into(),
                if_not_exists: true,
            },
            DdlOperation::CreateProject {
                owner_id: None,
                project_id: "arcana".into(),
                if_not_exists: true,
            },
        ])
        .await
        .expect("dependency-aware batch");

    assert_eq!(batch.results.len(), 4);
    assert!(
        db.index_exists("arcana", "ops", "users", "idx_users_name")
            .await
            .expect("index exists")
    );
}

#[tokio::test]
async fn ddl_errors_expose_stable_error_codes() {
    let dir = tempdir().expect("temp");
    let db = AedbInstance::open_anonymous(AedbConfig::default(), dir.path()).expect("open");
    db.create_project("arcana").await.expect("project");

    let err = db
        .commit(Mutation::Ddl(DdlOperation::CreateIndex {
            project_id: "arcana".into(),
            scope_id: "app".into(),
            table_name: "missing_users".into(),
            index_name: "idx_missing".into(),
            if_not_exists: false,
            columns: vec!["name".into()],
            index_type: crate::catalog::schema::IndexType::BTree,
            partial_filter: None,
        }))
        .await
        .expect_err("create index on missing table should fail");

    assert_eq!(err.code(), AedbErrorCode::TableNotFound);
}

#[tokio::test]
async fn snapshot_limit_enforced_on_read_path() {
    let dir = tempdir().expect("temp");
    let db = AedbInstance::open_anonymous(
        AedbConfig {
            max_concurrent_snapshots: 1,
            ..AedbConfig::default()
        },
        dir.path(),
    )
    .expect("open");
    db.create_project("p").await.expect("project");
    db.commit(Mutation::Ddl(DdlOperation::GrantPermission {
        actor_id: None,
        delegable: false,
        caller_id: "alice".into(),
        permission: Permission::KvRead {
            project_id: "p".into(),
            scope_id: Some("app".into()),
            prefix: None,
        },
    }))
    .await
    .expect("grant");
    let caller = CallerContext::new("alice");

    let handle = {
        let mut mgr = db.snapshot_manager.lock();
        mgr.acquire_bounded(
            crate::snapshot::reader::SnapshotReadView {
                keyspace: Arc::new(crate::storage::keyspace::Keyspace::default().snapshot()),
                catalog: Arc::new(crate::catalog::Catalog::default()),
                seq: 0,
            },
            1,
        )
        .expect("occupy")
    };

    let err = db
        .kv_get("p", "app", b"k", ConsistencyMode::AtLatest, &caller)
        .await
        .expect_err("snapshot cap");
    assert!(matches!(
        err,
        crate::query::error::QueryError::SnapshotLimitReached
    ));

    let mut mgr = db.snapshot_manager.lock();
    mgr.release(handle);
    let _ = mgr.gc();
}

#[tokio::test]
async fn idempotent_retry_does_not_double_apply_non_idempotent_mutation() {
    let dir = tempdir().expect("temp");
    let db = Arc::new(AedbInstance::open_anonymous(AedbConfig::default(), dir.path()).expect("open"));
    db.create_project("p").await.expect("project");

    let key = IdempotencyKey([42u8; 16]);
    let mut tasks = Vec::new();
    for _ in 0..8 {
        let db = Arc::clone(&db);
        let key = key.clone();
        tasks.push(tokio::spawn(async move {
            db.commit_envelope(TransactionEnvelope {
                caller: None,
                idempotency_key: Some(key),
                write_class: WriteClass::Standard,
                assertions: Vec::new(),
                read_set: Default::default(),
                write_intent: WriteIntent {
                    mutations: vec![Mutation::KvIncU256 {
                        project_id: "p".into(),
                        scope_id: "app".into(),
                        key: b"idem-counter".to_vec(),
                        amount_be: {
                            let mut out = [0u8; 32];
                            out[31] = 1;
                            out
                        },
                    }],
                },
                base_seq: 0,
            })
            .await
            .expect("idempotent commit")
        }));
    }

    let mut seqs = std::collections::BTreeSet::new();
    let mut outcomes = Vec::new();
    for t in tasks {
        let res = t.await.expect("join");
        seqs.insert(res.commit_seq);
        outcomes.push(res.idempotency);
    }
    assert_eq!(seqs.len(), 1, "all retries must resolve to one commit_seq");
    assert!(
        outcomes
            .iter()
            .any(|o| matches!(o, crate::commit::executor::IdempotencyOutcome::Duplicate)),
        "at least one retry should report duplicate outcome"
    );

    let entry = db
        .kv_get_no_auth("p", "app", b"idem-counter", ConsistencyMode::AtLatest)
        .await
        .expect("kv_get")
        .expect("counter exists");
    assert_eq!(
        primitive_types::U256::from_big_endian(&entry.value),
        primitive_types::U256::one(),
        "idempotent retries must apply mutation exactly once"
    );
}

#[tokio::test]
#[ignore = "manual perf probe: durability knob sweep (batch/coalescing)"]
async fn benchmark_durability_knob_sweep() {
    fn percentile(sorted: &[u128], p: f64) -> u128 {
        if sorted.is_empty() {
            return 0;
        }
        let percentile_index = ((sorted.len() as f64 - 1.0) * p).round() as usize;
        sorted[percentile_index.min(sorted.len() - 1)]
    }

    #[derive(Clone)]
    struct Profile {
        name: &'static str,
        batch_interval_ms: u64,
        batch_max_bytes: usize,
        coalesce_enabled: bool,
        coalesce_window_us: u64,
    }

    async fn run_profile(profile: &Profile) -> (f64, u128, u128, u64, u64) {
        let dir = tempdir().expect("temp");
        let mut config = AedbConfig {
            durability_mode: DurabilityMode::Batch,
            batch_interval_ms: profile.batch_interval_ms,
            batch_max_bytes: profile.batch_max_bytes,
            recovery_mode: RecoveryMode::Permissive,
            hash_chain_required: false,
            durable_ack_coalescing_enabled: profile.coalesce_enabled,
            durable_ack_coalesce_window_us: profile.coalesce_window_us,
            ..AedbConfig::default()
        };
        config.manifest_hmac_key = None;
        let db = Arc::new(AedbInstance::open_anonymous(config, dir.path()).expect("open"));
        db.create_project("p").await.expect("project");

        for i in 0..8_000usize {
            db.commit(Mutation::KvSet {
                project_id: "p".into(),
                scope_id: "app".into(),
                key: format!("sweep-seed:{i:05}").into_bytes(),
                value: vec![b's'; 128],
            })
            .await
            .expect("seed");
        }

        let workers = 8usize;
        let commits_per_worker = 500usize;
        let started = Instant::now();
        let mut tasks = Vec::with_capacity(workers);
        for worker in 0..workers {
            let db = Arc::clone(&db);
            tasks.push(tokio::spawn(async move {
                let mut lats = Vec::with_capacity(commits_per_worker);
                for i in 0..commits_per_worker {
                    let t0 = Instant::now();
                    db.commit(Mutation::KvSet {
                        project_id: "p".into(),
                        scope_id: "app".into(),
                        key: format!("sweep:{worker:02}:{i:06}").into_bytes(),
                        value: vec![b'x'; 256],
                    })
                    .await
                    .expect("commit");
                    lats.push(t0.elapsed().as_micros());
                }
                lats
            }));
        }

        let mut all_lat = Vec::with_capacity(workers * commits_per_worker);
        for task in tasks {
            let mut lats = task.await.expect("worker join");
            all_lat.append(&mut lats);
        }
        all_lat.sort_unstable();

        let elapsed = started.elapsed().as_secs_f64().max(0.000_001);
        let tps = (workers * commits_per_worker) as f64 / elapsed;
        let p50 = percentile(&all_lat, 0.50);
        let p99 = percentile(&all_lat, 0.99);
        let op = db.operational_metrics().await;
        (tps, p50, p99, op.wal_sync_ops, op.avg_wal_sync_micros)
    }

    let profiles = vec![
        Profile {
            name: "baseline_10ms_1mb_no_coalesce",
            batch_interval_ms: 10,
            batch_max_bytes: 1024 * 1024,
            coalesce_enabled: false,
            coalesce_window_us: 0,
        },
        Profile {
            name: "trial_20ms_4mb_coalesce_1000us",
            batch_interval_ms: 20,
            batch_max_bytes: 4 * 1024 * 1024,
            coalesce_enabled: true,
            coalesce_window_us: 1000,
        },
        Profile {
            name: "trial_20ms_8mb_coalesce_1500us",
            batch_interval_ms: 20,
            batch_max_bytes: 8 * 1024 * 1024,
            coalesce_enabled: true,
            coalesce_window_us: 1500,
        },
        Profile {
            name: "trial_40ms_8mb_coalesce_1500us",
            batch_interval_ms: 40,
            batch_max_bytes: 8 * 1024 * 1024,
            coalesce_enabled: true,
            coalesce_window_us: 1500,
        },
    ];

    for profile in &profiles {
        let (tps, p50, p99, wal_sync_ops, avg_wal_sync_us) = run_profile(profile).await;
        eprintln!(
            "durability_sweep: profile={} tps={:.2} p50_us={} p99_us={} wal_sync_ops={} avg_wal_sync_us={}",
            profile.name, tps, p50, p99, wal_sync_ops, avg_wal_sync_us
        );
    }
}

#[tokio::test]
async fn strict_open_rejects_directory_previously_opened_in_non_strict_mode() {
    let dir = tempdir().expect("temp");
    let mut permissive = AedbConfig::production([7u8; 32]);
    permissive.recovery_mode = RecoveryMode::Permissive;
    permissive.hash_chain_required = false;

    let db = AedbInstance::open_anonymous(permissive, dir.path()).expect("open permissive");
    db.shutdown().await.expect("shutdown permissive");
    drop(db); // release the data-directory lock before reopening

    let strict = AedbConfig::production([7u8; 32]);
    let err = match AedbInstance::open_anonymous(strict, dir.path()) {
        Ok(db) => {
            db.shutdown().await.expect("shutdown unexpected strict db");
            panic!("strict open should fail closed");
        }
        Err(err) => err,
    };
    assert!(
        matches!(err, AedbError::Validation(ref msg) if msg.contains("strict open denied")),
        "unexpected error: {err}"
    );
}

#[tokio::test]
async fn strict_open_rejects_tampered_trust_mode_marker() {
    let dir = tempdir().expect("temp");
    let mut permissive = AedbConfig::production([9u8; 32]);
    permissive.recovery_mode = RecoveryMode::Permissive;
    permissive.hash_chain_required = false;

    let db = AedbInstance::open_anonymous(permissive, dir.path()).expect("open permissive");
    db.shutdown().await.expect("shutdown permissive");
    drop(db); // release the data-directory lock before reopening

    fs::write(
        dir.path().join("trust_mode.json"),
        r#"{"ever_non_strict_recovery":false,"ever_hash_chain_disabled":false}"#,
    )
    .expect("tamper trust mode marker");

    let err = match AedbInstance::open_anonymous(AedbConfig::production([9u8; 32]), dir.path()) {
        Ok(db) => {
            db.shutdown().await.expect("shutdown unexpected strict db");
            panic!("tampered trust marker should fail closed");
        }
        Err(err) => err,
    };
    assert!(
        matches!(err, AedbError::IntegrityError { ref message } if message.contains("trust mode marker hmac mismatch")),
        "unexpected error: {err}"
    );
}

#[tokio::test]
async fn queries_reject_oversized_in_lists_and_like_patterns() {
    let dir = tempdir().expect("temp");
    let db = AedbInstance::open_anonymous(AedbConfig::default(), dir.path()).expect("open");
    db.create_project("p").await.expect("project");
    db.create_scope("p", "app").await.expect("scope");
    create_table(
        &db,
        "p",
        "app",
        "items",
        vec![
            ColumnDef {
                name: "id".into(),
                col_type: ColumnType::Integer,
                nullable: false,
            },
            ColumnDef {
                name: "name".into(),
                col_type: ColumnType::Text,
                nullable: false,
            },
        ],
        vec!["id"],
    )
    .await;

    let oversized_in = db
        .query_with_options(
            "p",
            "app",
            Query::select(&["id"]).from("items").where_(Expr::In(
                "id".into(),
                (0..10_001).map(Value::Integer).collect(),
            )),
            QueryOptions::default(),
        )
        .await
        .expect_err("oversized IN list should be rejected");
    assert!(
        matches!(oversized_in, QueryError::InvalidQuery { reason } if reason.contains("IN list"))
    );

    let oversized_like = db
        .query_with_options(
            "p",
            "app",
            Query::select(&["name"])
                .from("items")
                .where_(Expr::Like("name".into(), "a".repeat(257))),
            QueryOptions::default(),
        )
        .await
        .expect_err("oversized LIKE should be rejected");
    assert!(
        matches!(oversized_like, QueryError::InvalidQuery { reason } if reason.contains("LIKE pattern"))
    );
}

#[tokio::test]
async fn managed_system_tables_reject_direct_user_mutations() {
    let dir = tempdir().expect("temp");
    let db = AedbInstance::open_anonymous(AedbConfig::default(), dir.path()).expect("open");

    let err = db
        .commit(Mutation::Upsert {
            project_id: crate::catalog::SYSTEM_PROJECT_ID.into(),
            scope_id: "app".into(),
            table_name: "reactive_processor_checkpoints".into(),
            primary_key: vec![Value::Text("processor".into())],
            row: Row {
                values: vec![
                    Value::Text("processor".into()),
                    Value::Integer(42),
                    Value::Timestamp(1),
                ],
            },
        })
        .await
        .expect_err("managed system tables must reject direct writes");

    assert!(
        matches!(err, AedbError::Validation(message) if message.contains("managed and read-only"))
    );
}