dbx-core 0.1.0-beta

High-performance file-based database engine with 5-Tier Hybrid Storage
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
//! Database Constructors — factory methods for creating Database instances

use crate::engine::types::BackgroundJob;
use crate::engine::{Database, DeltaVariant, DurabilityLevel, WosVariant};
use crate::error::DbxResult;
use crate::index::HashIndex;
use crate::sql::optimizer::QueryOptimizer;
use crate::sql::parser::SqlParser;
use crate::storage::StorageBackend; // Add this for trait methods
use crate::storage::delta_store::DeltaStore;
use crate::storage::encryption::EncryptionConfig;
use crate::storage::encryption::wos::EncryptedWosBackend;
use crate::storage::memory_wos::InMemoryWosBackend;
use crate::storage::wos::WosBackend;
use crate::transaction::mvcc::manager::TransactionManager; // Fix path
use dashmap::DashMap;
use std::collections::HashMap;
use std::path::Path;
use std::sync::{Arc, RwLock};
use tracing::{info, instrument};

/// Spawn a background worker thread that handles WAL sync and index update jobs.
fn spawn_background_worker(
    rx: std::sync::mpsc::Receiver<BackgroundJob>,
    wal: Option<Arc<crate::wal::WriteAheadLog>>,
    enc_wal: Option<Arc<crate::wal::encrypted_wal::EncryptedWal>>,
    index: Arc<HashIndex>,
) {
    std::thread::spawn(move || {
        while let Ok(job) = rx.recv() {
            match job {
                BackgroundJob::WalSync => {
                    if let Some(w) = &wal {
                        let _ = w.sync();
                    }
                }
                BackgroundJob::EncryptedWalSync => {
                    if let Some(w) = &enc_wal {
                        let _ = w.sync();
                    }
                }
                BackgroundJob::IndexUpdate {
                    table,
                    column,
                    key,
                    row_id,
                } => {
                    let _ = index.update_on_insert(&table, &column, &key, row_id);
                }
            }
        }
    });
}

impl Database {
    /// 데이터베이스를 열거나 생성합니다.
    ///
    /// 지정된 경로에 데이터베이스를 생성하거나 기존 데이터베이스를 엽니다.
    /// WOS (sled)를 통해 영구 저장소를 제공합니다.
    ///
    /// # 인자
    ///
    /// * `path` - 데이터베이스 디렉토리 경로
    ///
    /// # 예제
    ///
    /// ```rust
    /// use dbx_core::Database;
    /// use std::path::Path;
    ///
    /// # fn main() -> dbx_core::DbxResult<()> {
    /// let db = Database::open(Path::new("./data"))?;
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip(path))]
    pub fn open(path: &Path) -> DbxResult<Arc<Self>> {
        info!("Opening database at {:?}", path);
        let wos_path = path.join("wos");
        std::fs::create_dir_all(&wos_path)?;

        // Initialize WAL
        let wal_path = path.join("wal.log");
        let wal = Arc::new(crate::wal::WriteAheadLog::open(&wal_path)?);

        let wos_backend = Arc::new(WosBackend::open(&wos_path)?);
        let db_index = Arc::new(HashIndex::new());

        // Load persisted metadata (schemas, indexes, and triggers)
        let loaded_schemas = crate::engine::metadata::load_all_schemas(&wos_backend)?;
        let loaded_indexes = crate::engine::metadata::load_all_indexes(&wos_backend)?;
        let loaded_triggers = crate::engine::metadata::load_all_triggers(&wos_backend)?;
        let loaded_procedures = crate::engine::metadata::load_all_procedures(&wos_backend)?;
        let loaded_schedules = crate::engine::metadata::load_all_schedules(&wos_backend)?;

        info!(
            "Loaded {} schemas, {} indexes, {} triggers, {} procedures, and {} schedules from persistent storage",
            loaded_schemas.len(),
            loaded_indexes.len(),
            loaded_triggers.len(),
            loaded_procedures.len(),
            loaded_schedules.len()
        );

        let (tx, rx) = std::sync::mpsc::channel::<BackgroundJob>();
        spawn_background_worker(rx, Some(wal.clone()), None, Arc::clone(&db_index));

        let db = Self {
            delta: DeltaVariant::RowBased(Arc::new(DeltaStore::new())),
            memory_wos: WosVariant::InMemory(Arc::new(InMemoryWosBackend::new())),
            file_wos: Some(WosVariant::Plain(Arc::clone(&wos_backend))),
            table_persistence: DashMap::new(),
            schemas: Arc::new(RwLock::new(HashMap::new())),
            tables: RwLock::new(HashMap::new()),
            table_schemas: Arc::new(RwLock::new(loaded_schemas)),
            index: db_index,
            row_counters: Arc::new(DashMap::new()),
            sql_parser: SqlParser::new(),
            sql_optimizer: QueryOptimizer::new(),
            wal: Some(wal),
            encrypted_wal: None,

            encryption: RwLock::new(None),
            tx_manager: Arc::new(TransactionManager::new()),
            columnar_cache: Arc::new(crate::storage::columnar_cache::ColumnarCache::new()),
            gpu_manager: crate::storage::gpu::GpuManager::try_new().map(Arc::new),
            job_sender: Some(tx),
            durability: DurabilityLevel::Lazy,
            index_registry: RwLock::new(loaded_indexes),
            automation_engine: Arc::new(crate::automation::ExecutionEngine::new()),
            trigger_registry: crate::engine::automation_api::TriggerRegistry::new(),
            trigger_executor: Arc::new(RwLock::new(crate::automation::TriggerExecutor::new())),
            procedure_executor: Arc::new(RwLock::new(crate::automation::ProcedureExecutor::new())),
            schedule_executor: Arc::new(RwLock::new(crate::automation::ScheduleExecutor::new())),
            parallel_engine: Arc::new(
                crate::engine::parallel_engine::ParallelExecutionEngine::new_auto()
                    .expect("Failed to create parallel engine"),
            ),
        };

        // Perform crash recovery
        let apply_fn = |record: &crate::wal::WalRecord| -> DbxResult<()> {
            match record {
                crate::wal::WalRecord::Insert {
                    table,
                    key,
                    value,
                    ts: _,
                } => {
                    db.delta.insert(table, key, value)?;
                }
                crate::wal::WalRecord::Delete { table, key, ts: _ } => {
                    db.delta.delete(table, key)?;
                }
                crate::wal::WalRecord::Batch { table, rows, ts: _ } => {
                    db.delta.insert_batch(table, rows.clone())?;
                }
                _ => {}
            }
            Ok(())
        };

        let recovered_count =
            crate::wal::checkpoint::CheckpointManager::recover(&wal_path, apply_fn)?;
        if recovered_count > 0 {
            info!("Recovered {} WAL records", recovered_count);
            // Flush recovered data to WOS to prevent duplicate inserts
            info!("Flushing recovered WAL data to WOS");
            db.flush()?;
        }

        // Auto-register loaded SQL triggers
        if !loaded_triggers.is_empty() {
            info!(
                "Auto-registering {} persisted triggers",
                loaded_triggers.len()
            );
            let mut executor = db.trigger_executor.write().unwrap();
            executor.register_all(loaded_triggers);
        }

        // Auto-register loaded SQL procedures
        if !loaded_procedures.is_empty() {
            info!(
                "Auto-registering {} persisted procedures",
                loaded_procedures.len()
            );
            let mut executor = db.procedure_executor.write().unwrap();
            executor.register_all(loaded_procedures);
        }

        // Auto-register loaded SQL schedules
        if !loaded_schedules.is_empty() {
            info!(
                "Auto-registering {} persisted schedules",
                loaded_schedules.len()
            );
            let executor = db.schedule_executor.write().unwrap();
            for (_, schedule) in loaded_schedules {
                let _ = executor.register(schedule);
            }
        }

        info!("Database opened successfully");

        // Wrap in Arc
        let db_arc = Arc::new(db);

        // Start background scheduler
        let db_weak = Arc::downgrade(&db_arc);
        db_arc
            .schedule_executor
            .write()
            .unwrap()
            .start_scheduler(db_weak)?;

        Ok(db_arc)
    }

    /// 암호화된 데이터베이스를 열거나 생성합니다.
    ///
    /// 지정된 경로에 암호화된 데이터베이스를 생성하거나 기존 암호화 DB를 엽니다.
    /// WAL과 WOS 모두 암호화됩니다.
    ///
    /// # 인자
    ///
    /// * `path` - 데이터베이스 디렉토리 경로
    /// * `encryption` - 암호화 설정 (패스워드 또는 raw key 기반)
    ///
    /// # 예제
    ///
    /// ```rust,no_run
    /// use dbx_core::Database;
    /// use dbx_core::storage::encryption::EncryptionConfig;
    /// use std::path::Path;
    ///
    /// let enc = EncryptionConfig::from_password("my-secret-password");
    /// let db = Database::open_encrypted(Path::new("./data"), enc).unwrap();
    /// ```
    #[instrument(skip(path, encryption))]
    pub fn open_encrypted(path: &Path, encryption: EncryptionConfig) -> DbxResult<Self> {
        info!("Opening encrypted database at {:?}", path);
        let wos_path = path.join("wos");
        std::fs::create_dir_all(&wos_path)?;

        // Initialize encrypted WAL
        let wal_path = path.join("wal.enc.log");
        let encrypted_wal = Arc::new(crate::wal::encrypted_wal::EncryptedWal::open(
            &wal_path,
            encryption.clone(),
        )?);

        // Initialize encrypted WOS
        let enc_wos = Arc::new(EncryptedWosBackend::open(&wos_path, encryption.clone())?);
        let db_index = Arc::new(HashIndex::new());

        let (tx, rx) = std::sync::mpsc::channel::<BackgroundJob>();
        spawn_background_worker(
            rx,
            None,
            Some(Arc::clone(&encrypted_wal)),
            Arc::clone(&db_index),
        );

        let db = Self {
            delta: DeltaVariant::RowBased(Arc::new(DeltaStore::new())),
            memory_wos: WosVariant::InMemory(Arc::new(InMemoryWosBackend::new())),
            file_wos: Some(WosVariant::Encrypted(Arc::clone(&enc_wos))),
            table_persistence: DashMap::new(),
            schemas: Arc::new(RwLock::new(HashMap::new())),
            tables: RwLock::new(HashMap::new()),
            table_schemas: Arc::new(RwLock::new(HashMap::new())),
            index: db_index,
            row_counters: Arc::new(DashMap::new()),
            sql_parser: SqlParser::new(),
            sql_optimizer: QueryOptimizer::new(),
            wal: None,
            encrypted_wal: Some(Arc::clone(&encrypted_wal)),

            encryption: RwLock::new(Some(encryption)),
            tx_manager: Arc::new(TransactionManager::new()),
            columnar_cache: Arc::new(crate::storage::columnar_cache::ColumnarCache::new()),
            gpu_manager: crate::storage::gpu::GpuManager::try_new().map(Arc::new),
            job_sender: Some(tx),
            durability: DurabilityLevel::Lazy,
            index_registry: RwLock::new(HashMap::new()),
            automation_engine: Arc::new(crate::automation::ExecutionEngine::new()),
            trigger_registry: crate::engine::automation_api::TriggerRegistry::new(),
            trigger_executor: Arc::new(RwLock::new(crate::automation::TriggerExecutor::new())),
            procedure_executor: Arc::new(RwLock::new(crate::automation::ProcedureExecutor::new())),
            schedule_executor: Arc::new(RwLock::new(crate::automation::ScheduleExecutor::new())),
            parallel_engine: Arc::new(
                crate::engine::parallel_engine::ParallelExecutionEngine::new_auto()
                    .expect("Failed to create parallel engine"),
            ),
        };

        // Perform crash recovery from encrypted WAL
        let records = encrypted_wal.replay()?;
        let mut recovered_count = 0;
        for record in &records {
            match record {
                crate::wal::WalRecord::Insert {
                    table,
                    key,
                    value,
                    ts: _,
                } => {
                    db.delta.insert(table, key, value)?;
                    recovered_count += 1;
                }
                crate::wal::WalRecord::Delete { table, key, ts: _ } => {
                    db.delta.delete(table, key)?;
                    recovered_count += 1;
                }
                crate::wal::WalRecord::Batch { table, rows, ts: _ } => {
                    db.delta.insert_batch(table, rows.clone())?;
                    recovered_count += rows.len();
                }
                _ => {}
            }
        }
        if recovered_count > 0 {
            info!("Recovered {} encrypted WAL records", recovered_count);
        }

        info!("Encrypted database opened successfully");
        Ok(db)
    }

    /// 인메모리 데이터베이스를 생성합니다.
    ///
    /// 테스트 및 임시 데이터 저장용으로 사용됩니다. 영구 저장되지 않습니다.
    ///
    /// # 예제
    ///
    /// ```rust
    /// use dbx_core::Database;
    ///
    /// # fn main() -> dbx_core::DbxResult<()> {
    /// let db = Database::open_in_memory()?;
    /// db.insert("cache", b"key1", b"value1")?;
    /// # Ok(())
    /// # }
    /// ```
    #[instrument]
    pub fn open_in_memory() -> DbxResult<Self> {
        info!("Creating in-memory database");
        let db_index = Arc::new(HashIndex::new());
        let (tx, rx) = std::sync::mpsc::channel::<BackgroundJob>();
        spawn_background_worker(rx, None, None, Arc::clone(&db_index));

        Ok(Self {
            delta: DeltaVariant::RowBased(Arc::new(DeltaStore::new())),
            memory_wos: WosVariant::InMemory(Arc::new(InMemoryWosBackend::new())),
            file_wos: None,
            table_persistence: DashMap::new(),
            schemas: Arc::new(RwLock::new(HashMap::new())),
            tables: RwLock::new(HashMap::new()),
            table_schemas: Arc::new(RwLock::new(HashMap::new())),
            index: db_index,
            row_counters: Arc::new(DashMap::new()),
            sql_parser: SqlParser::new(),
            sql_optimizer: QueryOptimizer::new(),
            wal: None,
            encrypted_wal: None,

            encryption: RwLock::new(None),
            tx_manager: Arc::new(TransactionManager::new()),
            columnar_cache: Arc::new(crate::storage::columnar_cache::ColumnarCache::new()),
            gpu_manager: crate::storage::gpu::GpuManager::try_new().map(Arc::new),
            job_sender: Some(tx),
            durability: DurabilityLevel::Lazy,
            index_registry: RwLock::new(HashMap::new()),
            automation_engine: Arc::new(crate::automation::ExecutionEngine::new()),
            trigger_registry: crate::engine::automation_api::TriggerRegistry::new(),
            trigger_executor: Arc::new(RwLock::new(crate::automation::TriggerExecutor::new())),
            procedure_executor: Arc::new(RwLock::new(crate::automation::ProcedureExecutor::new())),
            schedule_executor: Arc::new(RwLock::new(crate::automation::ScheduleExecutor::new())),
            parallel_engine: Arc::new(
                crate::engine::parallel_engine::ParallelExecutionEngine::new_auto()
                    .expect("Failed to create parallel engine"),
            ),
        })
    }

    /// 암호화된 인메모리 데이터베이스를 생성합니다.
    ///
    /// 테스트 및 임시 데이터 저장용으로, 메모리 상에서 value가 암호화됩니다.
    ///
    /// # 예제
    ///
    /// ```rust
    /// use dbx_core::Database;
    /// use dbx_core::storage::encryption::EncryptionConfig;
    ///
    /// # fn main() -> dbx_core::DbxResult<()> {
    /// let enc = EncryptionConfig::from_password("secret");
    /// let db = Database::open_in_memory_encrypted(enc)?;
    /// db.insert("users", b"user:1", b"Alice")?;
    /// let val = db.get("users", b"user:1")?;
    /// assert_eq!(val, Some(b"Alice".to_vec()));
    /// # Ok(())
    /// # }
    /// ```
    pub fn open_in_memory_encrypted(encryption: EncryptionConfig) -> DbxResult<Self> {
        let db_index = Arc::new(HashIndex::new());
        let (tx, rx) = std::sync::mpsc::channel::<BackgroundJob>();
        spawn_background_worker(rx, None, None, Arc::clone(&db_index));

        Ok(Self {
            delta: DeltaVariant::RowBased(Arc::new(DeltaStore::new())),
            memory_wos: WosVariant::Encrypted(Arc::new(EncryptedWosBackend::open_temporary(
                encryption.clone(),
            )?)),
            file_wos: None,
            table_persistence: DashMap::new(),
            schemas: Arc::new(RwLock::new(HashMap::new())),
            tables: RwLock::new(HashMap::new()),
            table_schemas: Arc::new(RwLock::new(HashMap::new())),
            index: db_index,
            row_counters: Arc::new(DashMap::new()),
            sql_parser: SqlParser::new(),
            sql_optimizer: QueryOptimizer::new(),
            wal: None,
            encrypted_wal: None,

            encryption: RwLock::new(Some(encryption)),
            tx_manager: Arc::new(TransactionManager::new()),
            columnar_cache: Arc::new(crate::storage::columnar_cache::ColumnarCache::new()),
            gpu_manager: crate::storage::gpu::GpuManager::try_new().map(Arc::new),
            job_sender: Some(tx),
            durability: DurabilityLevel::Lazy,
            index_registry: RwLock::new(HashMap::new()),
            automation_engine: Arc::new(crate::automation::ExecutionEngine::new()),
            trigger_registry: crate::engine::automation_api::TriggerRegistry::new(),
            trigger_executor: Arc::new(RwLock::new(crate::automation::TriggerExecutor::new())),
            procedure_executor: Arc::new(RwLock::new(crate::automation::ProcedureExecutor::new())),
            schedule_executor: Arc::new(RwLock::new(crate::automation::ScheduleExecutor::new())),
            parallel_engine: Arc::new(
                crate::engine::parallel_engine::ParallelExecutionEngine::new_auto()
                    .expect("Failed to create parallel engine"),
            ),
        })
    }

    /// 최대 안전성 설정으로 데이터베이스를 엽니다 (Full durability).
    ///
    /// 금융, 의료 등 데이터 손실이 절대 허용되지 않는 경우 사용합니다.
    /// 모든 쓰기 작업마다 fsync를 수행하여 최대 안전성을 보장하지만,
    /// 성능은 기본 설정(Lazy)보다 느립니다.
    ///
    /// # 인자
    ///
    /// * `path` - 데이터베이스 파일 경로
    pub fn open_safe(path: impl AsRef<Path>) -> DbxResult<Arc<Self>> {
        Self::open_with_durability(path, DurabilityLevel::Full)
    }

    /// 최고 성능 설정으로 데이터베이스를 엽니다 (No durability).
    ///
    /// WAL을 사용하지 않아 최고 성능을 제공하지만,
    /// 크래시 시 데이터 손실 가능성이 있습니다.
    /// 캐시, 임시 데이터, 벤치마크 등에 적합합니다.
    ///
    /// # 인자
    ///
    /// * `path` - 데이터베이스 파일 경로
    ///
    pub fn open_fast(path: impl AsRef<Path>) -> DbxResult<Arc<Self>> {
        Self::open_with_durability(path, DurabilityLevel::None)
    }

    /// 지정된 durability 설정으로 데이터베이스를 엽니다.
    ///
    /// # 인자
    ///
    /// * `path` - 데이터베이스 파일 경로
    /// * `durability` - 내구성 수준
    pub fn open_with_durability(
        path: impl AsRef<Path>,
        durability: DurabilityLevel,
    ) -> DbxResult<Arc<Self>> {
        info!(
            "Opening database at {:?} with durability {:?}",
            path.as_ref(),
            durability
        );
        let path = path.as_ref();
        let wos_path = path.join("wos");
        std::fs::create_dir_all(&wos_path)?;

        // Initialize WAL
        let wal_path = path.join("wal.log");
        let wal = Arc::new(crate::wal::WriteAheadLog::open(&wal_path)?);

        let wos_backend = Arc::new(WosBackend::open(&wos_path)?);
        let db_index = Arc::new(HashIndex::new());

        // Load persisted metadata
        let loaded_schemas = crate::engine::metadata::load_all_schemas(&wos_backend)?;
        let loaded_indexes = crate::engine::metadata::load_all_indexes(&wos_backend)?;
        let loaded_triggers = crate::engine::metadata::load_all_triggers(&wos_backend)?;
        let loaded_procedures = crate::engine::metadata::load_all_procedures(&wos_backend)?;
        let loaded_schedules = crate::engine::metadata::load_all_schedules(&wos_backend)?;

        let (tx, rx) = std::sync::mpsc::channel::<BackgroundJob>();
        spawn_background_worker(rx, Some(wal.clone()), None, Arc::clone(&db_index));

        let db = Self {
            delta: DeltaVariant::RowBased(Arc::new(DeltaStore::new())),
            memory_wos: WosVariant::InMemory(Arc::new(InMemoryWosBackend::new())),
            file_wos: Some(WosVariant::Plain(Arc::clone(&wos_backend))),
            table_persistence: DashMap::new(),
            schemas: Arc::new(RwLock::new(HashMap::new())),
            tables: RwLock::new(HashMap::new()),
            table_schemas: Arc::new(RwLock::new(loaded_schemas)),
            index: db_index,
            row_counters: Arc::new(DashMap::new()),
            sql_parser: SqlParser::new(),
            sql_optimizer: QueryOptimizer::new(),
            wal: Some(wal),
            encrypted_wal: None,
            encryption: RwLock::new(None),
            tx_manager: Arc::new(TransactionManager::new()),
            columnar_cache: Arc::new(crate::storage::columnar_cache::ColumnarCache::new()),
            gpu_manager: crate::storage::gpu::GpuManager::try_new().map(Arc::new),
            job_sender: Some(tx),
            durability, // ← set BEFORE Arc wrapping
            index_registry: RwLock::new(loaded_indexes),
            automation_engine: Arc::new(crate::automation::ExecutionEngine::new()),
            trigger_registry: crate::engine::automation_api::TriggerRegistry::new(),
            trigger_executor: Arc::new(RwLock::new(crate::automation::TriggerExecutor::new())),
            procedure_executor: Arc::new(RwLock::new(crate::automation::ProcedureExecutor::new())),
            schedule_executor: Arc::new(RwLock::new(crate::automation::ScheduleExecutor::new())),
            parallel_engine: Arc::new(
                crate::engine::parallel_engine::ParallelExecutionEngine::new_auto()
                    .expect("Failed to create parallel engine"),
            ),
        };

        // Crash recovery
        let apply_fn = |record: &crate::wal::WalRecord| -> DbxResult<()> {
            match record {
                crate::wal::WalRecord::Insert {
                    table,
                    key,
                    value,
                    ts: _,
                } => {
                    db.delta.insert(table, key, value)?;
                }
                crate::wal::WalRecord::Delete { table, key, ts: _ } => {
                    db.delta.delete(table, key)?;
                }
                crate::wal::WalRecord::Batch { table, rows, ts: _ } => {
                    db.delta.insert_batch(table, rows.clone())?;
                }
                _ => {}
            }
            Ok(())
        };
        let recovered_count =
            crate::wal::checkpoint::CheckpointManager::recover(&wal_path, apply_fn)?;
        if recovered_count > 0 {
            info!("Recovered {} WAL records", recovered_count);
            db.flush()?;
        }

        // Auto-register triggers, procedures, schedules
        if !loaded_triggers.is_empty() {
            db.trigger_executor
                .write()
                .unwrap()
                .register_all(loaded_triggers);
        }
        if !loaded_procedures.is_empty() {
            db.procedure_executor
                .write()
                .unwrap()
                .register_all(loaded_procedures);
        }
        if !loaded_schedules.is_empty() {
            let executor = db.schedule_executor.write().unwrap();
            for (_, schedule) in loaded_schedules {
                let _ = executor.register(schedule);
            }
        }

        info!(
            "Database opened successfully with durability {:?}",
            durability
        );

        let db_arc = Arc::new(db);
        let db_weak = Arc::downgrade(&db_arc);
        db_arc
            .schedule_executor
            .write()
            .unwrap()
            .start_scheduler(db_weak)?;

        Ok(db_arc)
    }
}