heliosdb-nano 3.22.2

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
//! Write-Ahead Log (WAL) implementation
//!
//! Provides durability guarantees through write-ahead logging.
//! Uses RocksDB's WriteBatch for atomic operations and built-in WAL support.

use crate::{Error, Result};
use rocksdb::{DB, WriteBatch, WriteOptions};
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::collections::{HashMap, VecDeque};
use std::time::{SystemTime, UNIX_EPOCH, Duration};
use std::thread::{self, JoinHandle};
use tracing::{debug, info, warn, error};
use parking_lot::Mutex;

// Import HA state for replication broadcast (when ha-tier1 is enabled)
#[cfg(feature = "ha-tier1")]
use crate::replication::ha_state::ha_state;

/// Write-Ahead Log operation types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum WalOperation {
    // === DML Operations ===

    /// Insert a tuple into a table
    Insert {
        table: String,
        key: Vec<u8>,
        tuple: Vec<u8>,
    },
    /// Update a tuple in a table
    Update {
        table: String,
        key: Vec<u8>,
        tuple: Vec<u8>,
    },
    /// Delete a tuple from a table
    Delete {
        table: String,
        key: Vec<u8>,
    },
    /// Truncate all rows from a table
    Truncate {
        table: String,
    },

    // === Table DDL ===

    /// Create a new table
    CreateTable {
        table: String,
        schema: Vec<u8>,
    },
    /// Drop a table
    DropTable {
        table: String,
    },
    /// Alter table column storage mode
    AlterColumnStorage {
        table: String,
        column: String,
        /// Serialized ColumnStorageMode
        storage_mode: Vec<u8>,
    },

    // === Index Operations ===

    /// Create an index
    CreateIndex {
        name: String,
        table: String,
        column: String,
        index_type: Option<String>,
        /// Serialized index options
        options: Vec<u8>,
    },
    /// Drop an index
    DropIndex {
        name: String,
    },

    // === Trigger Operations ===

    /// Create a trigger
    CreateTrigger {
        name: String,
        table: String,
        /// Serialized trigger definition
        definition: Vec<u8>,
    },
    /// Drop a trigger
    DropTrigger {
        name: String,
        table: Option<String>,
    },

    // === Function/Procedure Operations ===

    /// Create a function
    CreateFunction {
        name: String,
        /// Serialized function definition
        definition: Vec<u8>,
    },
    /// Drop a function
    DropFunction {
        name: String,
    },
    /// Create a procedure
    CreateProcedure {
        name: String,
        /// Serialized procedure definition
        definition: Vec<u8>,
    },
    /// Drop a procedure
    DropProcedure {
        name: String,
    },

    // === Materialized View Operations ===

    /// Create a materialized view
    CreateMaterializedView {
        name: String,
        /// Serialized view definition
        definition: Vec<u8>,
    },
    /// Drop a materialized view
    DropMaterializedView {
        name: String,
    },
    /// Refresh a materialized view
    RefreshMaterializedView {
        name: String,
        concurrent: bool,
        incremental: bool,
    },

    // === Constraint Operations ===

    /// Add a constraint to a table
    AddConstraint {
        table: String,
        /// Serialized constraint definition
        constraint: Vec<u8>,
    },
    /// Drop a constraint from a table
    DropConstraint {
        table: String,
        constraint_name: String,
    },

    // === Transaction Control ===

    /// Transaction begin
    Begin {
        tx_id: u64,
    },
    /// Transaction commit
    Commit {
        tx_id: u64,
    },
    /// Transaction abort
    Abort {
        tx_id: u64,
    },

    // === HA Replication Operations ===

    /// Update a sequence counter (for HA replication)
    /// Ensures auto-increment values are preserved across failover
    UpdateCounter {
        table_name: String,
        new_value: u64,
    },
}

/// WAL entry with metadata
///
/// Format:
/// - LSN (Log Sequence Number): Monotonically increasing ID
/// - Timestamp: Unix timestamp in microseconds
/// - Operation: The actual operation to log
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WalEntry {
    /// Log Sequence Number (LSN) - unique, monotonic
    pub lsn: u64,
    /// Timestamp in microseconds
    pub timestamp: u64,
    /// Operation to log
    pub operation: WalOperation,
}

impl WalEntry {
    /// Create a new WAL entry
    pub fn new(lsn: u64, operation: WalOperation) -> Self {
        // Get current timestamp - if system clock is before UNIX_EPOCH (extremely unlikely),
        // we fall back to 0 to ensure we can still create the entry
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_micros() as u64)
            .unwrap_or(0);

        Self {
            lsn,
            timestamp,
            operation,
        }
    }

    /// Serialize entry to bytes
    pub fn serialize(&self) -> Result<Vec<u8>> {
        bincode::serialize(self)
            .map_err(|e| Error::storage(format!("Failed to serialize WAL entry: {}", e)))
    }

    /// Deserialize entry from bytes
    pub fn deserialize(data: &[u8]) -> Result<Self> {
        bincode::deserialize(data)
            .map_err(|e| Error::storage(format!("Failed to deserialize WAL entry: {}", e)))
    }
}

/// WAL synchronization mode
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WalSyncMode {
    /// Synchronous - fsync on every write (safest, slowest)
    Sync,
    /// Asynchronous - OS-managed flush (faster, less safe)
    Async,
    /// Group commit - batch multiple operations (balanced)
    GroupCommit,
}

/// Pending write for group commit
struct PendingWrite {
    entry: WalEntry,
    result_tx: crossbeam::channel::Sender<Result<u64>>,
}

/// WAL integrity report
#[derive(Debug, Clone)]
pub struct WalIntegrityReport {
    /// Total entries scanned
    pub total_entries: u64,
    /// Corrupted entries found
    pub corrupted_entries: Vec<u64>,
    /// Missing LSN gaps detected
    pub missing_lsns: Vec<(u64, u64)>,
    /// Duplicate LSNs found
    pub duplicate_lsns: Vec<u64>,
    /// Timestamp ordering violations
    pub timestamp_violations: Vec<u64>,
    /// Overall integrity status
    pub is_valid: bool,
}

impl WalIntegrityReport {
    /// Create a new empty report
    fn new() -> Self {
        Self {
            total_entries: 0,
            corrupted_entries: Vec::new(),
            missing_lsns: Vec::new(),
            duplicate_lsns: Vec::new(),
            timestamp_violations: Vec::new(),
            is_valid: true,
        }
    }

    /// Mark report as invalid
    fn mark_invalid(&mut self) {
        self.is_valid = false;
    }
}

/// Replay statistics
#[derive(Debug, Clone)]
pub struct ReplayStats {
    /// Number of operations replayed
    pub operations_replayed: u64,
    /// Number of operations skipped (already applied)
    pub operations_skipped: u64,
    /// Number of errors encountered
    pub errors: u64,
    /// Replay start LSN
    pub start_lsn: u64,
    /// Replay end LSN
    pub end_lsn: u64,
    /// Replay duration in milliseconds
    pub duration_ms: u64,
}

/// Cleanup statistics
#[derive(Debug, Clone)]
pub struct CleanupStats {
    /// Number of entries deleted
    pub entries_deleted: u64,
    /// Bytes freed
    pub bytes_freed: u64,
    /// Oldest LSN remaining
    pub oldest_lsn: u64,
    /// Newest LSN
    pub newest_lsn: u64,
}

/// WAL metrics for monitoring
#[derive(Debug, Clone)]
pub struct WalMetrics {
    /// Current LSN
    pub current_lsn: u64,
    /// Total entries in WAL
    pub entry_count: u64,
    /// Estimated WAL size in bytes
    pub size_bytes: u64,
    /// Oldest entry LSN
    pub oldest_lsn: u64,
    /// Newest entry LSN
    pub newest_lsn: u64,
    /// Oldest entry timestamp
    pub oldest_timestamp: u64,
    /// Newest entry timestamp
    pub newest_timestamp: u64,
    /// WAL sync mode
    pub sync_mode: WalSyncMode,
}

/// Write-Ahead Log
///
/// Provides durability through write-ahead logging. All modifications are
/// logged before being applied to the main database.
///
/// The WAL uses RocksDB's built-in WAL support with WriteBatch for atomic
/// operations. Each entry is assigned a monotonically increasing LSN.
///
/// **Group Commit Optimization**:
/// When in GroupCommit mode, writes are batched together and flushed
/// periodically (default: 10ms) to reduce fsync overhead. This provides
/// 10-100x throughput improvement while maintaining durability with
/// bounded latency increase.
///
/// Recovery process:
/// 1. On startup, check for incomplete WAL entries
/// 2. Replay entries that aren't reflected in the main database
/// 3. Continue normal operation
pub struct WriteAheadLog {
    /// RocksDB instance (shares the same DB as the storage engine)
    db: Arc<DB>,
    /// Current LSN (Log Sequence Number)
    current_lsn: Arc<AtomicU64>,
    /// Synchronization mode
    sync_mode: WalSyncMode,
    /// Write options for WAL operations
    write_opts: WriteOptions,
    /// Pending writes queue for group commit (None if not in GroupCommit mode)
    commit_queue: Option<Arc<Mutex<VecDeque<PendingWrite>>>>,
    /// Background commit thread handle (None if not in GroupCommit mode)
    commit_thread: Option<Arc<Mutex<Option<JoinHandle<()>>>>>,
    /// Batch timeout for group commit (default: 10ms)
    batch_timeout: Duration,
}

impl WriteAheadLog {
    /// Open or create a WAL
    ///
    /// The WAL uses RocksDB's built-in WAL functionality. This implementation
    /// adds logical WAL entries on top of RocksDB's physical WAL.
    pub fn open(db: Arc<DB>, sync_mode: WalSyncMode) -> Result<Self> {
        // Configure write options based on sync mode
        let mut write_opts = WriteOptions::default();
        match sync_mode {
            WalSyncMode::Sync => {
                write_opts.set_sync(true);
                info!("WAL initialized in synchronous mode (fsync on every write)");
            }
            WalSyncMode::Async => {
                write_opts.set_sync(false);
                write_opts.disable_wal(false); // Enable WAL but async
                info!("WAL initialized in asynchronous mode");
            }
            WalSyncMode::GroupCommit => {
                write_opts.set_sync(false);
                write_opts.disable_wal(false);
                info!("WAL initialized in group commit mode");
            }
        }

        // Recover the last LSN from the database
        let current_lsn = Self::recover_last_lsn(&db)?;
        info!("WAL recovered with LSN starting at {}", current_lsn);

        // Initialize group commit structures if needed
        let (commit_queue, commit_thread) = if sync_mode == WalSyncMode::GroupCommit {
            let queue = Arc::new(Mutex::new(VecDeque::new()));
            (Some(queue), Some(Arc::new(Mutex::new(None))))
        } else {
            (None, None)
        };

        let batch_timeout = Duration::from_millis(10); // 10ms batch window

        let wal = Self {
            db: Arc::clone(&db),
            current_lsn: Arc::new(AtomicU64::new(current_lsn)),
            sync_mode,
            write_opts,
            commit_queue: commit_queue.clone(),
            commit_thread: commit_thread.clone(),
            batch_timeout,
        };

        // Start group commit thread if in GroupCommit mode
        if sync_mode == WalSyncMode::GroupCommit {
            if let Some(queue) = commit_queue {
                let db_clone = Arc::clone(&db);
                let current_lsn_clone = Arc::clone(&wal.current_lsn);
                let batch_timeout = wal.batch_timeout;

                let handle = thread::spawn(move || {
                    match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                        Self::group_commit_loop(db_clone, queue, current_lsn_clone, batch_timeout);
                    })) {
                        Ok(()) => {}
                        Err(panic_info) => {
                            let msg = if let Some(s) = panic_info.downcast_ref::<&str>() {
                                s.to_string()
                            } else if let Some(s) = panic_info.downcast_ref::<String>() {
                                s.clone()
                            } else {
                                "unknown panic".to_string()
                            };
                            error!("WAL group commit thread panicked: {}", msg);
                        }
                    }
                });

                if let Some(thread_handle) = &commit_thread {
                    *thread_handle.lock() = Some(handle);
                }
            }
        }

        Ok(wal)
    }

    /// Recover the last LSN from the database
    fn recover_last_lsn(db: &DB) -> Result<u64> {
        // Check if we have a stored LSN marker
        match db.get(b"wal:last_lsn") {
            Ok(Some(data)) => {
                let lsn = u64::from_le_bytes(
                    data.as_slice()
                        .try_into()
                        .map_err(|_| Error::storage("Invalid LSN format"))?,
                );
                debug!("Recovered last LSN: {}", lsn);
                Ok(lsn)
            }
            Ok(None) => {
                debug!("No previous LSN found, starting from 0");
                Ok(0)
            }
            Err(e) => {
                warn!("Failed to recover LSN: {}, starting from 0", e);
                Ok(0)
            }
        }
    }

    /// Get next LSN
    fn next_lsn(&self) -> u64 {
        self.current_lsn.fetch_add(1, Ordering::SeqCst) + 1
    }

    /// Append an entry to the WAL
    ///
    /// Returns the LSN of the appended entry.
    ///
    /// The entry is written to RocksDB with a special WAL prefix.
    /// If sync_mode is Sync, the write is fsynced immediately.
    /// If sync_mode is GroupCommit, the write is queued and batched.
    pub fn append(&self, operation: WalOperation) -> Result<u64> {
        // Use group commit path if enabled
        if self.sync_mode == WalSyncMode::GroupCommit {
            return self.append_group_commit(operation);
        }

        // Original synchronous/async path
        let lsn = self.next_lsn();
        let entry = WalEntry::new(lsn, operation);

        // Serialize the entry
        let data = entry.serialize()?;

        // Create a batch for atomic write
        let mut batch = WriteBatch::default();

        // Write the WAL entry with key: wal:entries:{lsn}
        let key = format!("wal:entries:{:020}", lsn);
        batch.put(key.as_bytes(), &data);

        // Update last LSN marker
        batch.put(b"wal:last_lsn", lsn.to_le_bytes());

        // Write atomically
        self.db
            .write_opt(batch, &self.write_opts)
            .map_err(|e| Error::storage(format!("Failed to append WAL entry: {}", e)))?;

        // Broadcast to standbys for replication (if HA is enabled and we're primary)
        #[cfg(feature = "ha-tier1")]
        {
            if let Some(replicated_lsn) = ha_state().broadcast_wal_operation(lsn, &entry.operation) {
                debug!("WAL entry {} replicated to standbys", replicated_lsn);

                // In sync or semi-sync mode, wait for at least 1 standby to ACK
                // This is the "faster-safe" approach: wait for first ACK, not all
                // Uses existing wait_for_sync() which already implements first-ACK semantics
                const DEFAULT_SYNC_TIMEOUT_MS: u64 = 10000; // 10 second timeout

                if let Err(e) = ha_state().wait_for_sync(replicated_lsn, DEFAULT_SYNC_TIMEOUT_MS) {
                    // Log warning but don't fail - data is safely written locally
                    // This allows the system to continue in degraded mode
                    warn!("Sync replication wait for LSN {}: {}", replicated_lsn, e);
                }
            }
        }

        debug!("Appended WAL entry with LSN {}", lsn);
        Ok(lsn)
    }

    /// Append a WAL entry without fsync (for DDL operations where metadata
    /// is already crash-safe in RocksDB). Saves ~1ms per DDL operation.
    pub fn append_nosync(&self, operation: WalOperation) -> Result<u64> {
        let lsn = self.next_lsn();
        let entry = WalEntry::new(lsn, operation);
        let data = entry.serialize()?;

        let mut batch = WriteBatch::default();
        let key = format!("wal:entries:{:020}", lsn);
        batch.put(key.as_bytes(), &data);
        batch.put(b"wal:last_lsn", lsn.to_le_bytes());

        // Write without fsync
        let mut nosync_opts = WriteOptions::default();
        nosync_opts.set_sync(false);
        self.db
            .write_opt(batch, &nosync_opts)
            .map_err(|e| Error::storage(format!("Failed to append WAL entry: {}", e)))?;

        Ok(lsn)
    }

    /// Append a WAL entry and wait for synchronous replication acknowledgement
    ///
    /// This method appends the operation to WAL and then blocks until standbys
    /// have acknowledged receipt (semi-sync) or application (sync) of the entry,
    /// based on the configured sync mode.
    ///
    /// # Arguments
    /// * `operation` - The WAL operation to append
    ///
    /// # Returns
    /// * `Ok(lsn)` - The LSN of the appended entry
    /// * `Err` - If WAL append fails or sync wait times out
    pub fn append_sync(&self, operation: WalOperation) -> Result<u64> {
        // First, append the entry normally
        let lsn = self.append(operation)?;

        // Then wait for synchronous replication based on configured mode
        #[cfg(feature = "ha-tier1")]
        {
            use crate::replication::ha_state::ha_state;

            // Default timeout of 30 seconds for sync wait
            const DEFAULT_SYNC_TIMEOUT_MS: u64 = 30000;

            if let Err(e) = ha_state().wait_for_sync(lsn, DEFAULT_SYNC_TIMEOUT_MS) {
                // Log warning but don't fail the append - the data is safely written locally
                // This allows the system to continue operating in degraded mode
                warn!("Sync replication timeout for LSN {}: {}", lsn, e);
            }
        }

        Ok(lsn)
    }

    /// Append entry using group commit
    fn append_group_commit(&self, operation: WalOperation) -> Result<u64> {
        let lsn = self.next_lsn();
        let entry = WalEntry::new(lsn, operation);

        // Create channel for receiving result
        let (tx, rx) = crossbeam::channel::bounded(1);

        // Queue the write
        if let Some(queue) = &self.commit_queue {
            let pending = PendingWrite {
                entry,
                result_tx: tx,
            };
            queue.lock().push_back(pending);
        } else {
            return Err(Error::storage("Group commit queue not initialized"));
        }

        // Wait for batch commit to complete
        match rx.recv() {
            Ok(result) => result,
            Err(e) => Err(Error::storage(format!("Group commit failed: {}", e))),
        }
    }

    /// Flush WAL to disk
    ///
    /// Forces an fsync of the WAL. Only needed in Async or GroupCommit modes.
    /// In Sync mode, this is a no-op as every write is already fsynced.
    pub fn flush(&self) -> Result<()> {
        match self.sync_mode {
            WalSyncMode::Sync => {
                // Already synced on every write
                Ok(())
            }
            WalSyncMode::Async | WalSyncMode::GroupCommit => {
                self.db
                    .flush_wal(true)
                    .map_err(|e| Error::storage(format!("Failed to flush WAL: {}", e)))?;
                debug!("WAL flushed to disk");
                Ok(())
            }
        }
    }

    /// Replay WAL entries for crash recovery
    ///
    /// Returns all WAL entries in LSN order.
    /// The caller is responsible for applying these entries to restore state.
    ///
    /// Note: This is a simplified implementation. A production WAL would:
    /// - Track which entries have been checkpointed
    /// - Only replay entries after the last checkpoint
    /// - Handle partial writes during crashes
    pub fn replay(&self) -> Result<Vec<WalEntry>> {
        info!("Starting WAL replay for crash recovery");
        let mut entries = Vec::new();
        let prefix = b"wal:entries:";

        // Iterate over all WAL entries
        let iter = self.db.prefix_iterator(prefix);
        for item in iter {
            let (key, value) = item
                .map_err(|e| Error::storage(format!("WAL replay iterator error: {}", e)))?;

            // Skip if not a WAL entry
            if !key.starts_with(prefix) {
                break;
            }

            // Deserialize entry
            let entry = WalEntry::deserialize(&value)?;
            debug!("Replaying WAL entry with LSN {}", entry.lsn);
            entries.push(entry);
        }

        info!("WAL replay complete, {} entries recovered", entries.len());
        Ok(entries)
    }

    /// Truncate WAL up to a given LSN (for checkpointing)
    ///
    /// Removes WAL entries older than the specified LSN.
    /// This should only be called after a successful checkpoint.
    pub fn truncate(&self, up_to_lsn: u64) -> Result<()> {
        info!("Truncating WAL entries up to LSN {}", up_to_lsn);
        let mut batch = WriteBatch::default();
        let prefix = b"wal:entries:";

        // Find entries to delete
        let iter = self.db.prefix_iterator(prefix);
        let mut deleted_count = 0;

        for item in iter {
            let (key, value) = item
                .map_err(|e| Error::storage(format!("WAL truncate iterator error: {}", e)))?;

            if !key.starts_with(prefix) {
                break;
            }

            // Parse LSN from entry
            let entry = WalEntry::deserialize(&value)?;
            if entry.lsn <= up_to_lsn {
                batch.delete(&key);
                deleted_count += 1;
            }
        }

        // Apply deletions
        self.db
            .write(batch)
            .map_err(|e| Error::storage(format!("Failed to truncate WAL: {}", e)))?;

        info!("Truncated {} WAL entries", deleted_count);
        Ok(())
    }

    /// Get current LSN
    pub fn current_lsn(&self) -> u64 {
        self.current_lsn.load(Ordering::SeqCst)
    }

    /// Increment LSN without appending a WAL entry
    ///
    /// Used by transaction commit to track operations even when
    /// the transaction system bypasses WAL for durability.
    /// Returns the new LSN value.
    pub fn increment_lsn(&self) -> u64 {
        self.current_lsn.fetch_add(1, Ordering::SeqCst) + 1
    }

    /// Get synchronization mode
    pub fn sync_mode(&self) -> WalSyncMode {
        self.sync_mode
    }

    /// Change synchronization mode
    ///
    /// Note: This reconfigures write options but doesn't affect in-flight writes.
    /// Changing to/from GroupCommit mode is not supported after initialization.
    pub fn set_sync_mode(&mut self, mode: WalSyncMode) {
        // Warn if trying to change to/from GroupCommit mode
        if (self.sync_mode == WalSyncMode::GroupCommit || mode == WalSyncMode::GroupCommit)
            && self.sync_mode != mode
        {
            warn!("Cannot dynamically change to/from GroupCommit mode. This change may not take full effect.");
        }

        self.sync_mode = mode;
        match mode {
            WalSyncMode::Sync => {
                self.write_opts.set_sync(true);
            }
            WalSyncMode::Async | WalSyncMode::GroupCommit => {
                self.write_opts.set_sync(false);
                self.write_opts.disable_wal(false);
            }
        }
        info!("WAL sync mode changed to {:?}", mode);
    }

    /// Group commit background thread loop
    ///
    /// Periodically flushes batched writes to reduce fsync overhead.
    /// Runs until the WAL is dropped or an unrecoverable error occurs.
    fn group_commit_loop(
        db: Arc<DB>,
        queue: Arc<Mutex<VecDeque<PendingWrite>>>,
        _current_lsn: Arc<AtomicU64>,
        batch_timeout: Duration,
    ) {
        info!("Group commit thread started (batch timeout: {:?})", batch_timeout);

        loop {
            // Sleep for batch timeout
            thread::sleep(batch_timeout);

            // Drain queue
            let pending: Vec<PendingWrite> = {
                let mut q = queue.lock();
                if q.is_empty() {
                    continue;
                }
                q.drain(..).collect()
            };

            if pending.is_empty() {
                continue;
            }

            debug!("Group commit: processing {} pending writes", pending.len());

            // Build batch
            let mut batch = WriteBatch::default();
            let mut last_lsn = 0u64;

            for write in &pending {
                let lsn = write.entry.lsn;
                last_lsn = last_lsn.max(lsn);

                // Serialize entry
                match write.entry.serialize() {
                    Ok(data) => {
                        let key = format!("wal:entries:{:020}", lsn);
                        batch.put(key.as_bytes(), &data);
                    }
                    Err(e) => {
                        // Send error to waiter
                        let _ = write.result_tx.send(Err(e));
                        continue;
                    }
                }
            }

            // Update last LSN marker
            batch.put(b"wal:last_lsn", last_lsn.to_le_bytes());

            // Flush batch with fsync
            let mut write_opts = WriteOptions::default();
            write_opts.set_sync(true);

            match db.write_opt(batch, &write_opts) {
                Ok(()) => {
                    // Broadcast to standbys for replication (if HA is enabled)
                    #[cfg(feature = "ha-tier1")]
                    {
                        for write in &pending {
                            ha_state().broadcast_wal_operation(write.entry.lsn, &write.entry.operation);
                        }
                    }

                    // Notify all waiters of success
                    for write in pending {
                        let _ = write.result_tx.send(Ok(write.entry.lsn));
                    }
                    debug!("Group commit: successfully flushed {} writes", last_lsn);
                }
                Err(e) => {
                    let err_msg = format!("Group commit batch write failed: {}", e);
                    // Notify all waiters of failure
                    for write in pending {
                        let _ = write.result_tx.send(Err(Error::storage(err_msg.clone())));
                    }
                    error!("Group commit failed: {}", e);
                }
            }
        }
    }

    /// Verify WAL integrity on startup
    ///
    /// Performs comprehensive integrity checks:
    /// - Validates all entries can be deserialized
    /// - Checks for LSN sequence continuity
    /// - Detects duplicate LSNs
    /// - Validates timestamp ordering
    /// - Reports corruption details
    pub fn verify_integrity(&self) -> Result<WalIntegrityReport> {
        info!("Starting WAL integrity verification");
        let mut report = WalIntegrityReport::new();
        let prefix = b"wal:entries:";

        let mut lsn_map: HashMap<u64, usize> = HashMap::new();
        let mut last_lsn: Option<u64> = None;
        let mut last_timestamp: u64 = 0;
        let mut entry_count = 0u64;

        // Scan all entries
        let iter = self.db.prefix_iterator(prefix);
        for item in iter {
            match item {
                Ok((key, value)) => {
                    if !key.starts_with(prefix) {
                        break;
                    }

                    entry_count += 1;

                    // Try to deserialize
                    match WalEntry::deserialize(&value) {
                        Ok(entry) => {
                            // Check for duplicate LSN
                            if let Some(count) = lsn_map.get_mut(&entry.lsn) {
                                *count += 1;
                                report.duplicate_lsns.push(entry.lsn);
                                report.mark_invalid();
                                warn!("Duplicate LSN detected: {}", entry.lsn);
                            } else {
                                lsn_map.insert(entry.lsn, 1);
                            }

                            // Check for LSN gaps
                            if let Some(prev_lsn) = last_lsn {
                                if entry.lsn != prev_lsn + 1 {
                                    report.missing_lsns.push((prev_lsn + 1, entry.lsn - 1));
                                    report.mark_invalid();
                                    warn!("LSN gap detected: {} to {}", prev_lsn + 1, entry.lsn - 1);
                                }
                            }

                            // Check timestamp ordering
                            if entry.timestamp < last_timestamp {
                                report.timestamp_violations.push(entry.lsn);
                                warn!("Timestamp ordering violation at LSN {}", entry.lsn);
                                // Don't mark as invalid - timestamps can be out of order in distributed systems
                            }

                            last_lsn = Some(entry.lsn);
                            last_timestamp = entry.timestamp;
                        }
                        Err(e) => {
                            // Corruption detected
                            if let Some(lsn) = last_lsn {
                                report.corrupted_entries.push(lsn + 1);
                            } else {
                                report.corrupted_entries.push(0);
                            }
                            report.mark_invalid();
                            error!("Corrupted WAL entry detected: {}", e);
                        }
                    }
                }
                Err(e) => {
                    error!("Error reading WAL entry: {}", e);
                    report.mark_invalid();
                }
            }
        }

        report.total_entries = entry_count;

        if report.is_valid {
            info!("WAL integrity verification passed: {} entries verified", entry_count);
        } else {
            error!(
                "WAL integrity verification FAILED: {} corrupted, {} gaps, {} duplicates",
                report.corrupted_entries.len(),
                report.missing_lsns.len(),
                report.duplicate_lsns.len()
            );
        }

        Ok(report)
    }

    /// Replay WAL operations after crash with detailed statistics
    ///
    /// This enhanced replay method:
    /// - Tracks replay statistics
    /// - Handles partial writes gracefully
    /// - Verifies data consistency
    /// - Provides progress reporting
    pub fn replay_with_stats(&self, from_lsn: u64) -> Result<ReplayStats> {
        let start_time = SystemTime::now();
        info!("Starting WAL replay from LSN {}", from_lsn);

        let mut stats = ReplayStats {
            operations_replayed: 0,
            operations_skipped: 0,
            errors: 0,
            start_lsn: from_lsn,
            end_lsn: from_lsn,
            duration_ms: 0,
        };

        let prefix = b"wal:entries:";
        let iter = self.db.prefix_iterator(prefix);

        for item in iter {
            match item {
                Ok((key, value)) => {
                    if !key.starts_with(prefix) {
                        break;
                    }

                    match WalEntry::deserialize(&value) {
                        Ok(entry) => {
                            if entry.lsn < from_lsn {
                                stats.operations_skipped += 1;
                                continue;
                            }

                            stats.end_lsn = entry.lsn;

                            // In a production system, we would re-apply operations here
                            // For now, we just count them as this is delegated to RocksDB's WAL
                            debug!("Replaying operation at LSN {}: {:?}", entry.lsn, entry.operation);
                            stats.operations_replayed += 1;
                        }
                        Err(e) => {
                            error!("Failed to deserialize entry during replay: {}", e);
                            stats.errors += 1;
                        }
                    }
                }
                Err(e) => {
                    error!("Error reading entry during replay: {}", e);
                    stats.errors += 1;
                }
            }
        }

        if let Ok(elapsed) = start_time.elapsed() {
            stats.duration_ms = elapsed.as_millis() as u64;
        }

        info!(
            "WAL replay complete: {} operations replayed, {} skipped, {} errors in {}ms",
            stats.operations_replayed, stats.operations_skipped, stats.errors, stats.duration_ms
        );

        Ok(stats)
    }

    /// Rotate WAL log files
    ///
    /// Creates a new WAL segment when size limit is reached.
    /// This is a logical rotation since we use RocksDB's WAL.
    ///
    /// For production, this would:
    /// - Create a new WAL file
    /// - Archive the old WAL file
    /// - Update the active WAL pointer
    pub fn rotate(&self) -> Result<()> {
        info!("Rotating WAL");

        // Get current metrics to determine if rotation is needed
        let metrics = self.metrics()?;

        // Force a flush and checkpoint
        self.flush()?;

        // In RocksDB, rotation is handled internally
        // We can trigger compaction to help manage WAL files
        self.db
            .flush_wal(true)
            .map_err(|e| Error::storage(format!("Failed to rotate WAL: {}", e)))?;

        info!("WAL rotated successfully at LSN {}", metrics.current_lsn);
        Ok(())
    }

    /// Clean up old WAL files based on retention policy
    ///
    /// Deletes WAL entries older than the retention period.
    /// Keeps minimum required entries for recovery.
    ///
    /// # Arguments
    /// * `retention_seconds` - Maximum age of WAL entries to keep
    /// * `min_entries_to_keep` - Minimum number of entries to always retain
    pub fn cleanup_old_logs(&self, retention_seconds: u64, min_entries_to_keep: u64) -> Result<CleanupStats> {
        info!(
            "Starting WAL cleanup (retention: {}s, min entries: {})",
            retention_seconds, min_entries_to_keep
        );

        let mut stats = CleanupStats {
            entries_deleted: 0,
            bytes_freed: 0,
            oldest_lsn: 0,
            newest_lsn: 0,
        };

        let current_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);

        let cutoff_time = current_time.saturating_sub(retention_seconds);

        let cutoff_time_micros = cutoff_time * 1_000_000;

        let prefix = b"wal:entries:";
        let mut entries_to_delete = Vec::new();
        let mut total_entries = 0u64;

        // First pass: identify entries to delete
        let iter = self.db.prefix_iterator(prefix);
        for item in iter {
            match item {
                Ok((key, value)) => {
                    if !key.starts_with(prefix) {
                        break;
                    }

                    total_entries += 1;

                    match WalEntry::deserialize(&value) {
                        Ok(entry) => {
                            if stats.oldest_lsn == 0 {
                                stats.oldest_lsn = entry.lsn;
                            }
                            stats.newest_lsn = entry.lsn;

                            // Only delete if:
                            // 1. Entry is older than retention period
                            // 2. We have enough entries remaining
                            if entry.timestamp < cutoff_time_micros {
                                let remaining = total_entries - entries_to_delete.len() as u64;
                                if remaining > min_entries_to_keep {
                                    entries_to_delete.push((key.to_vec(), value.len()));
                                }
                            }
                        }
                        Err(e) => {
                            warn!("Failed to deserialize entry during cleanup: {}", e);
                        }
                    }
                }
                Err(e) => {
                    error!("Error reading entry during cleanup: {}", e);
                }
            }
        }

        // Second pass: delete identified entries
        if !entries_to_delete.is_empty() {
            let mut batch = WriteBatch::default();
            for (key, size) in &entries_to_delete {
                batch.delete(key);
                stats.bytes_freed += *size as u64;
            }

            self.db
                .write(batch)
                .map_err(|e| Error::storage(format!("Failed to cleanup WAL: {}", e)))?;

            stats.entries_deleted = entries_to_delete.len() as u64;
        }

        info!(
            "WAL cleanup complete: {} entries deleted, {} bytes freed",
            stats.entries_deleted, stats.bytes_freed
        );

        Ok(stats)
    }

    /// Get WAL metrics for monitoring
    ///
    /// Returns detailed metrics including:
    /// - Current LSN and entry count
    /// - Size estimation
    /// - Oldest and newest entries
    /// - Sync mode
    pub fn metrics(&self) -> Result<WalMetrics> {
        let prefix = b"wal:entries:";
        let mut metrics = WalMetrics {
            current_lsn: self.current_lsn(),
            entry_count: 0,
            size_bytes: 0,
            oldest_lsn: 0,
            newest_lsn: 0,
            oldest_timestamp: 0,
            newest_timestamp: 0,
            sync_mode: self.sync_mode,
        };

        let iter = self.db.prefix_iterator(prefix);
        for item in iter {
            match item {
                Ok((key, value)) => {
                    if !key.starts_with(prefix) {
                        break;
                    }

                    metrics.entry_count += 1;
                    metrics.size_bytes += key.len() as u64 + value.len() as u64;

                    if let Ok(entry) = WalEntry::deserialize(&value) {
                        if metrics.oldest_lsn == 0 {
                            metrics.oldest_lsn = entry.lsn;
                            metrics.oldest_timestamp = entry.timestamp;
                        }
                        metrics.newest_lsn = entry.lsn;
                        metrics.newest_timestamp = entry.timestamp;
                    }
                }
                Err(e) => {
                    warn!("Error reading entry for metrics: {}", e);
                }
            }
        }

        debug!(
            "WAL metrics: {} entries, {} bytes, LSN range {}-{}",
            metrics.entry_count, metrics.size_bytes, metrics.oldest_lsn, metrics.newest_lsn
        );

        Ok(metrics)
    }
}

// Legacy compatibility - keeping old enum for backward compatibility
/// WAL entry type (legacy)
#[derive(Debug, Clone)]
pub enum WalEntryLegacy {
    /// Put operation
    Put { key: Vec<u8>, value: Vec<u8> },
    /// Delete operation
    Delete { key: Vec<u8> },
    /// Transaction begin
    Begin { tx_id: u64 },
    /// Transaction commit
    Commit { tx_id: u64 },
    /// Transaction abort
    Abort { tx_id: u64 },
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use rocksdb::Options;
    use tempfile::TempDir;

    fn create_test_db() -> (TempDir, Arc<DB>) {
        let temp_dir = TempDir::new().unwrap();
        let mut opts = Options::default();
        opts.create_if_missing(true);
        let db = DB::open(&opts, temp_dir.path()).unwrap();
        (temp_dir, Arc::new(db))
    }

    #[test]
    fn test_wal_basic_operations() {
        let (_temp, db) = create_test_db();
        let wal = WriteAheadLog::open(db, WalSyncMode::Sync).unwrap();

        // Append entries
        let lsn1 = wal
            .append(WalOperation::Insert {
                table: "users".to_string(),
                key: b"data:users:1".to_vec(),
                tuple: vec![1, 2, 3],
            })
            .unwrap();

        let lsn2 = wal
            .append(WalOperation::Delete {
                table: "users".to_string(),
                key: vec![4, 5, 6],
            })
            .unwrap();

        assert_eq!(lsn1, 1);
        assert_eq!(lsn2, 2);
        assert_eq!(wal.current_lsn(), 2);
    }

    #[test]
    fn test_wal_replay() {
        let (_temp, db) = create_test_db();
        let wal = WriteAheadLog::open(Arc::clone(&db), WalSyncMode::Sync).unwrap();

        // Append some entries
        wal.append(WalOperation::CreateTable {
            table: "test".to_string(),
            schema: vec![7, 8, 9],
        })
        .unwrap();

        wal.append(WalOperation::Insert {
            table: "test".to_string(),
            key: b"data:test:1".to_vec(),
            tuple: vec![1, 2, 3],
        })
        .unwrap();

        // Replay
        let entries = wal.replay().unwrap();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].lsn, 1);
        assert_eq!(entries[1].lsn, 2);
    }

    #[test]
    fn test_wal_truncate() {
        let (_temp, db) = create_test_db();
        let wal = WriteAheadLog::open(Arc::clone(&db), WalSyncMode::Sync).unwrap();

        // Append multiple entries
        for i in 0..5 {
            wal.append(WalOperation::Insert {
                table: "test".to_string(),
                key: format!("data:test:{}", i).into_bytes(),
                tuple: vec![i],
            })
            .unwrap();
        }

        // Truncate first 3 entries
        wal.truncate(3).unwrap();

        // Replay should only return entries 4 and 5
        let entries = wal.replay().unwrap();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].lsn, 4);
        assert_eq!(entries[1].lsn, 5);
    }

    #[test]
    fn test_wal_recovery() {
        let (temp, db) = create_test_db();

        // Create WAL and append entries
        {
            let wal = WriteAheadLog::open(Arc::clone(&db), WalSyncMode::Sync).unwrap();
            wal.append(WalOperation::Insert {
                table: "test".to_string(),
                key: b"data:test:1".to_vec(),
                tuple: vec![1, 2, 3],
            })
            .unwrap();
        }

        // "Crash" - drop the WAL
        // Reopen and check LSN is recovered
        let wal = WriteAheadLog::open(Arc::clone(&db), WalSyncMode::Sync).unwrap();
        assert_eq!(wal.current_lsn(), 1);

        // Next entry should have LSN 2
        let lsn = wal
            .append(WalOperation::Insert {
                table: "test".to_string(),
                key: b"data:test:2".to_vec(),
                tuple: vec![4, 5, 6],
            })
            .unwrap();
        assert_eq!(lsn, 2);
    }

    #[test]
    fn test_wal_sync_modes() {
        let (_temp, db) = create_test_db();

        // Test all sync modes
        for mode in [WalSyncMode::Sync, WalSyncMode::Async, WalSyncMode::GroupCommit] {
            let wal = WriteAheadLog::open(Arc::clone(&db), mode).unwrap();
            assert_eq!(wal.sync_mode(), mode);

            let lsn = wal
                .append(WalOperation::Insert {
                    table: "test".to_string(),
                    key: b"data:test:1".to_vec(),
                    tuple: vec![1, 2, 3],
                })
                .unwrap();
            assert!(lsn > 0);

            wal.flush().unwrap();
        }
    }
}