heliosdb-nano 3.23.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
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
//! Dump Manager for HeliosDB-Lite Multi-User ACID In-Memory Mode (v3.1.0)
//!
//! Provides memory-to-disk persistence with full and incremental dumps,
//! compression, checksumming, and restore functionality.
//!
//! ## Features
//! - Full and incremental dumps with append mode
//! - Zstandard and LZ4 compression support
//! - CRC32 checksum validation
//! - Concurrent read-only restores
//! - >100MB/s throughput target
//! - Dirty state tracking for incremental dumps

use super::format::{CompressionType, DUMP_MAGIC_NUMBER, DUMP_VERSION};
use crate::{Result, Error, Tuple, Schema};
use std::path::{Path, PathBuf};
use std::fs::{File, OpenOptions};
use std::io::{BufWriter, BufReader, Write, Read, Seek, SeekFrom};
use std::sync::Arc;
use std::time::{SystemTime, Instant};
use std::collections::HashSet;
use parking_lot::{RwLock, Mutex};
use serde::{Serialize, Deserialize};
use tracing::{info, debug, warn};

/// Dump type identifier
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DumpType {
    /// Full dump (all data)
    Full,
    /// Incremental dump (changes only)
    Incremental,
}

/// Metadata for a single dump operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DumpMetadata {
    /// Unique dump ID
    pub dump_id: u64,
    /// Creation timestamp
    pub created_at: SystemTime,
    /// Dump type (Full/Incremental)
    pub dump_type: DumpType,
    /// Number of tables dumped
    pub table_count: u32,
    /// Total rows dumped
    pub total_rows: u64,
    /// Compressed size in bytes
    pub compressed_size: u64,
    /// Uncompressed size in bytes
    pub uncompressed_size: u64,
    /// CRC32 checksum (hex string)
    pub checksum: String,
    /// Number of appends to this dump
    pub append_count: u32,
}

impl DumpMetadata {
    /// Create new metadata
    pub fn new(dump_id: u64, dump_type: DumpType) -> Self {
        Self {
            dump_id,
            created_at: SystemTime::now(),
            dump_type,
            table_count: 0,
            total_rows: 0,
            compressed_size: 0,
            uncompressed_size: 0,
            checksum: String::new(),
            append_count: 0,
        }
    }
}

/// Index metadata for dumps
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexMetadata {
    /// Index name
    pub name: String,
    /// Index type (e.g., "btree", "hash", "gin")
    pub index_type: String,
    /// Columns in index
    pub columns: Vec<String>,
    /// Is unique index
    pub is_unique: bool,
}

/// Dirty state tracker for incremental dumps
pub struct DirtyTracker {
    /// Last dump timestamp
    last_dump_time: Arc<Mutex<Option<Instant>>>,
    /// Dirty flag
    dirty: Arc<RwLock<bool>>,
    /// Dirty tables since last dump
    dirty_tables: Arc<RwLock<HashSet<String>>>,
}

impl DirtyTracker {
    /// Create new dirty tracker
    pub fn new() -> Self {
        Self {
            last_dump_time: Arc::new(Mutex::new(None)),
            dirty: Arc::new(RwLock::new(false)),
            dirty_tables: Arc::new(RwLock::new(HashSet::new())),
        }
    }

    /// Mark database as dirty
    pub fn mark_dirty(&self) {
        *self.dirty.write() = true;
    }

    /// Mark specific table as dirty
    pub fn mark_table_dirty(&self, table: &str) {
        self.dirty_tables.write().insert(table.to_string());
        self.mark_dirty();
    }

    /// Check if database is dirty
    pub fn is_dirty(&self) -> bool {
        *self.dirty.read()
    }

    /// Get list of dirty tables
    pub fn get_dirty_tables(&self) -> Vec<String> {
        self.dirty_tables.read().iter().cloned().collect()
    }

    /// Clear dirty state
    pub fn clear_dirty(&self) {
        *self.dirty.write() = false;
        self.dirty_tables.write().clear();
        *self.last_dump_time.lock() = Some(Instant::now());
    }

    /// Get time since last dump
    pub fn time_since_last_dump(&self) -> Option<std::time::Duration> {
        self.last_dump_time.lock().map(|t| t.elapsed())
    }
}

impl Default for DirtyTracker {
    fn default() -> Self {
        Self::new()
    }
}

/// Database interface for dump operations
pub trait DatabaseInterface: Send + Sync {
    /// List all tables
    fn list_tables(&self) -> Result<Vec<String>>;

    /// Get table schema
    fn get_table_schema(&self, table: &str) -> Result<Schema>;

    /// Scan all rows in a table
    fn scan_table(&self, table: &str) -> Result<Vec<Tuple>>;

    /// Get table indexes
    fn get_table_indexes(&self, table: &str) -> Result<Vec<IndexMetadata>>;
}

/// Database interface for restore operations
pub trait DatabaseRestoreInterface {
    /// Create table with schema
    fn create_table(&mut self, name: &str, schema: Schema) -> Result<()>;

    /// Create index
    fn create_index(&mut self, table: &str, index: &IndexMetadata) -> Result<()>;

    /// Insert row
    fn insert_row(&mut self, table: &str, row: Tuple) -> Result<()>;
}

/// Dump Manager
///
/// Manages full and incremental database dumps with compression and integrity checking.
pub struct DumpManager {
    /// Dump history
    dump_history: Arc<RwLock<Vec<DumpMetadata>>>,
    /// Last dump time
    last_dump_time: Arc<Mutex<Instant>>,
    /// Dirty tracker
    dirty_tracker: Arc<DirtyTracker>,
    /// Compression type
    compression: CompressionType,
    /// Data directory
    data_dir: PathBuf,
    /// Next dump ID counter
    next_dump_id: Arc<Mutex<u64>>,
}

impl DumpManager {
    /// Create a new dump manager
    pub fn new(data_dir: PathBuf, compression: CompressionType) -> Self {
        Self {
            dump_history: Arc::new(RwLock::new(Vec::new())),
            last_dump_time: Arc::new(Mutex::new(Instant::now())),
            dirty_tracker: Arc::new(DirtyTracker::new()),
            compression,
            data_dir,
            next_dump_id: Arc::new(Mutex::new(1)),
        }
    }

    /// Get next dump ID (atomic counter)
    pub fn get_next_dump_id(&self) -> u64 {
        let mut id = self.next_dump_id.lock();
        let current = *id;
        *id += 1;
        current
    }

    /// Dump database (CLI-compatible wrapper)
    ///
    /// This is a simplified interface for CLI use. For more control,
    /// use `create_full_dump` or `create_incremental_dump` directly.
    pub fn dump<D: DatabaseInterface>(&self, opts: &DumpOptions, db: &D) -> Result<DumpReport> {
        let start_time = Instant::now();

        // Dispatch based on format
        let metadata = match opts.format {
            DumpOutputFormat::Binary => {
                match opts.mode {
                    DumpMode::Full => self.create_full_dump(&opts.output_path, db)?,
                    DumpMode::Incremental => self.create_incremental_dump(&opts.output_path, db, opts.append)?,
                }
            }
            DumpOutputFormat::Sql => {
                self.create_sql_dump(&opts.output_path, db)?
            }
        };

        let duration_ms = start_time.elapsed().as_millis() as u64;
        let compression_ratio = if metadata.uncompressed_size > 0 {
            metadata.compressed_size as f64 / metadata.uncompressed_size as f64
        } else {
            1.0
        };

        Ok(DumpReport {
            dump_id: metadata.dump_id.to_string(),
            tables_dumped: metadata.table_count as usize,
            rows_dumped: metadata.total_rows,
            bytes_written: metadata.compressed_size,
            bytes_uncompressed: metadata.uncompressed_size,
            duration_ms,
            compression_ratio,
        })
    }

    /// Create a SQL dump of the database (compatible with SQLite/PostgreSQL)
    pub fn create_sql_dump<D: DatabaseInterface>(
        &self,
        output_path: &Path,
        db: &D,
    ) -> Result<DumpMetadata> {
        let start_time = Instant::now();
        let dump_id = self.get_next_dump_id();
        let mut metadata = DumpMetadata::new(dump_id, DumpType::Full);
        
        info!("Starting SQL dump {} to {}", dump_id, output_path.display());

        let file = File::create(output_path)
            .map_err(|e| Error::storage(format!("Failed to create SQL dump file: {}", e)))?;
        let mut writer = BufWriter::new(file);

        // Write header
        writeln!(writer, "-- HeliosDB Lite Database Dump")
            .map_err(|e| Error::storage(format!("Failed to write header: {}", e)))?;
        writeln!(writer, "-- Generated: {}", chrono::Local::now().format("%Y-%m-%d %H:%M:%S"))
            .map_err(|e| Error::storage(format!("Failed to write header: {}", e)))?;
        writeln!(writer, "-- Database: heliosdb-nano\n")
            .map_err(|e| Error::storage(format!("Failed to write header: {}", e)))?;

        let tables = db.list_tables()?;
        metadata.table_count = tables.len() as u32;
        
        let mut total_rows = 0;

        for table in tables {
            // Write table schema
            let schema = db.get_table_schema(&table)?;
            
            writeln!(writer, "-- Table: {}", table)
                .map_err(|e| Error::storage(format!("Failed to write comment: {}", e)))?;
            writeln!(writer, "CREATE TABLE IF NOT EXISTS {} (", table)
                .map_err(|e| Error::storage(format!("Failed to write create table: {}", e)))?;
            
            for (i, col) in schema.columns.iter().enumerate() {
                let suffix = if i < schema.columns.len() - 1 { "," } else { "" };
                let type_str = col.data_type.to_string();
                let pk_str = if col.primary_key { " PRIMARY KEY" } else { "" };
                let null_str = if !col.nullable { " NOT NULL" } else { "" };
                
                writeln!(writer, "  {} {}{}{}{}", col.name, type_str, pk_str, null_str, suffix)
                    .map_err(|e| Error::storage(format!("Failed to write column: {}", e)))?;
            }
            writeln!(writer, ");\n")
                .map_err(|e| Error::storage(format!("Failed to write end table: {}", e)))?;

            // Write data
            let rows = db.scan_table(&table)?;
            total_rows += rows.len() as u64;
            
            if !rows.is_empty() {
                writeln!(writer, "INSERT INTO {} VALUES", table)
                    .map_err(|e| Error::storage(format!("Failed to write insert header: {}", e)))?;
                
                for (i, row) in rows.iter().enumerate() {
                    let suffix = if i < rows.len() - 1 { "," } else { ";" };
                    let values: Vec<String> = row.values.iter().map(Self::format_value_for_sql).collect();
                    writeln!(writer, "  ({}){}", values.join(", "), suffix)
                        .map_err(|e| Error::storage(format!("Failed to write row: {}", e)))?;
                }
                writeln!(writer, "\n")
                    .map_err(|e| Error::storage(format!("Failed to write end insert: {}", e)))?;
            }
        }

        writer.flush().map_err(|e| Error::storage(format!("Failed to flush writer: {}", e)))?;
        
        let file_size = std::fs::metadata(output_path)
            .map_err(|e| Error::storage(format!("Failed to get file metadata: {}", e)))?
            .len();
            
        metadata.total_rows = total_rows;
        metadata.compressed_size = file_size;
        metadata.uncompressed_size = file_size; // SQL is uncompressed text
        
        // Add to history
        self.dump_history.write().push(metadata.clone());
        
        Ok(metadata)
    }

    /// Helper to format values for SQL
    fn format_value_for_sql(value: &crate::Value) -> String {
        match value {
            crate::Value::Null => "NULL".to_string(),
            crate::Value::Boolean(b) => if *b { "TRUE".to_string() } else { "FALSE".to_string() },
            crate::Value::Int2(i) => i.to_string(),
            crate::Value::Int4(i) => i.to_string(),
            crate::Value::Int8(i) => i.to_string(),
            crate::Value::Float4(f) => f.to_string(),
            crate::Value::Float8(f) => f.to_string(),
            crate::Value::String(s) => format!("'{}'", s.replace('\'', "''")),
            crate::Value::Timestamp(ts) => format!("'{}'", ts), // Assuming string rep is sufficient
            // For other types, fallback to debug repr string, might need improvement
            _ => format!("'{}'", format!("{:?}", value).replace('\'', "''")),
        }
    }

    /// Restore database (CLI-compatible wrapper)
    ///
    /// This is a simplified interface for CLI use. For more control,
    /// use `restore_from_dump` directly.
    pub fn restore<D: DatabaseRestoreInterface>(&self, opts: &RestoreOptions, db: &mut D) -> Result<RestoreReport> {
        let start_time = Instant::now();

        self.restore_from_dump(&opts.input_path, db)?;

        let duration_ms = start_time.elapsed().as_millis() as u64;

        // Note: In a full implementation, we'd track these stats during restore
        Ok(RestoreReport {
            tables_restored: 0,  // Would be populated during restore
            rows_restored: 0,    // Would be populated during restore
            duration_ms,
        })
    }

    /// Create a full dump of the database
    ///
    /// Serializes all tables, indexes, and metadata to a dump file with compression.
    ///
    /// # Arguments
    /// * `output_path` - Path to output dump file
    /// * `db` - Database interface for reading data
    ///
    /// # Returns
    /// Metadata about the created dump including size and checksum
    pub fn create_full_dump<D: DatabaseInterface>(
        &self,
        output_path: &Path,
        db: &D,
    ) -> Result<DumpMetadata> {
        let start_time = Instant::now();
        let dump_id = self.get_next_dump_id();
        let mut metadata = DumpMetadata::new(dump_id, DumpType::Full);

        info!("Starting full dump {} to {}", dump_id, output_path.display());

        // Open dump file
        let file = File::create(output_path)
            .map_err(|e| Error::storage(format!("Failed to create dump file: {}", e)))?;
        let mut writer = BufWriter::with_capacity(256 * 1024, file); // 256KB buffer

        // Write magic bytes and version
        writer.write_all(DUMP_MAGIC_NUMBER)
            .map_err(|e| Error::storage(format!("Failed to write magic bytes: {}", e)))?;
        writer.write_all(&DUMP_VERSION.to_le_bytes())
            .map_err(|e| Error::storage(format!("Failed to write version: {}", e)))?;

        // Reserve space for metadata header (we'll write it later)
        let metadata_pos = writer.stream_position()
            .map_err(|e| Error::storage(format!("Failed to get position: {}", e)))?;
        let metadata_placeholder = vec![0u8; 8192]; // 8KB placeholder
        writer.write_all(&metadata_placeholder)
            .map_err(|e| Error::storage(format!("Failed to write placeholder: {}", e)))?;

        // Get all tables
        let tables = db.list_tables()?;
        metadata.table_count = tables.len() as u32;

        let mut total_rows = 0u64;
        let mut uncompressed_bytes = 0u64;

        // Dump each table
        for (idx, table) in tables.iter().enumerate() {
            debug!("Dumping table {}/{}: {}", idx + 1, tables.len(), table);

            // Write table marker
            writer.write_all(b"TABL")
                .map_err(|e| Error::storage(format!("Failed to write table marker: {}", e)))?;

            // Write table name
            let table_bytes = table.as_bytes();
            writer.write_all(&(table_bytes.len() as u32).to_le_bytes())
                .map_err(|e| Error::storage(format!("Failed to write table name length: {}", e)))?;
            writer.write_all(table_bytes)
                .map_err(|e| Error::storage(format!("Failed to write table name: {}", e)))?;

            // Get and write schema
            let schema = db.get_table_schema(table)?;
            let schema_bytes = bincode::serialize(&schema)
                .map_err(|e| Error::storage(format!("Failed to serialize schema: {}", e)))?;
            writer.write_all(&(schema_bytes.len() as u32).to_le_bytes())
                .map_err(|e| Error::storage(format!("Failed to write schema length: {}", e)))?;
            writer.write_all(&schema_bytes)
                .map_err(|e| Error::storage(format!("Failed to write schema: {}", e)))?;

            uncompressed_bytes += schema_bytes.len() as u64;

            // Get and write indexes
            let indexes = db.get_table_indexes(table).unwrap_or_default();
            let indexes_bytes = bincode::serialize(&indexes)
                .map_err(|e| Error::storage(format!("Failed to serialize indexes: {}", e)))?;
            writer.write_all(&(indexes_bytes.len() as u32).to_le_bytes())
                .map_err(|e| Error::storage(format!("Failed to write indexes length: {}", e)))?;
            writer.write_all(&indexes_bytes)
                .map_err(|e| Error::storage(format!("Failed to write indexes: {}", e)))?;

            uncompressed_bytes += indexes_bytes.len() as u64;

            // Scan and write rows
            let rows = db.scan_table(table)?;
            let row_count = rows.len() as u64;
            total_rows += row_count;

            writer.write_all(&row_count.to_le_bytes())
                .map_err(|e| Error::storage(format!("Failed to write row count: {}", e)))?;

            // Write rows in batches for better compression
            const BATCH_SIZE: usize = 1000;
            for batch in rows.chunks(BATCH_SIZE) {
                let batch_bytes = bincode::serialize(batch)
                    .map_err(|e| Error::storage(format!("Failed to serialize batch: {}", e)))?;

                uncompressed_bytes += batch_bytes.len() as u64;

                // Compress batch
                let compressed = self.compress_data(&batch_bytes)?;

                writer.write_all(&(compressed.len() as u32).to_le_bytes())
                    .map_err(|e| Error::storage(format!("Failed to write batch length: {}", e)))?;
                writer.write_all(&compressed)
                    .map_err(|e| Error::storage(format!("Failed to write batch: {}", e)))?;
            }

            // Write end-of-table marker
            writer.write_all(&0u32.to_le_bytes())
                .map_err(|e| Error::storage(format!("Failed to write EOT marker: {}", e)))?;
        }

        metadata.total_rows = total_rows;
        metadata.uncompressed_size = uncompressed_bytes;

        // Write end-of-dump marker
        writer.write_all(b"ENDD")
            .map_err(|e| Error::storage(format!("Failed to write end marker: {}", e)))?;

        // Flush and calculate checksum
        writer.flush()
            .map_err(|e| Error::storage(format!("Failed to flush writer: {}", e)))?;
        drop(writer);

        let checksum = self.calculate_checksum(output_path)?;
        metadata.checksum = checksum;

        let file_size = std::fs::metadata(output_path)
            .map_err(|e| Error::storage(format!("Failed to get file metadata: {}", e)))?
            .len();
        metadata.compressed_size = file_size;

        // Write metadata to header
        self.write_metadata_header(output_path, metadata_pos, &metadata)?;

        // Update history and clear dirty state
        self.dump_history.write().push(metadata.clone());
        self.dirty_tracker.clear_dirty();
        *self.last_dump_time.lock() = Instant::now();

        let elapsed = start_time.elapsed();
        let throughput_mbps = (metadata.uncompressed_size as f64 / 1_048_576.0) / elapsed.as_secs_f64();

        info!(
            "Full dump {} completed: {} tables, {} rows, {:.2} MB in {:.2}s ({:.2} MB/s)",
            dump_id,
            metadata.table_count,
            metadata.total_rows,
            metadata.uncompressed_size as f64 / 1_048_576.0,
            elapsed.as_secs_f64(),
            throughput_mbps
        );

        Ok(metadata)
    }

    /// Create an incremental dump
    ///
    /// Dumps only the tables that have changed since the last dump.
    ///
    /// # Arguments
    /// * `output_path` - Path to output dump file
    /// * `db` - Database interface for reading data
    /// * `append` - If true, append to existing dump file; if false, create new file
    ///
    /// # Returns
    /// Metadata about the created dump
    pub fn create_incremental_dump<D: DatabaseInterface>(
        &self,
        output_path: &Path,
        db: &D,
        append: bool,
    ) -> Result<DumpMetadata> {
        let dirty_tables = self.dirty_tracker.get_dirty_tables();

        if dirty_tables.is_empty() {
            return Err(Error::storage("No dirty tables to dump"));
        }

        let start_time = Instant::now();
        let dump_id = self.get_next_dump_id();
        let mut metadata = DumpMetadata::new(dump_id, DumpType::Incremental);

        info!("Starting incremental dump {} (append={})", dump_id, append);

        // Open file in append or create mode
        let file = if append && output_path.exists() {
            OpenOptions::new()
                .append(true)
                .open(output_path)
                .map_err(|e| Error::storage(format!("Failed to open dump file: {}", e)))?
        } else {
            File::create(output_path)
                .map_err(|e| Error::storage(format!("Failed to create dump file: {}", e)))?
        };

        let mut writer = BufWriter::with_capacity(256 * 1024, file);

        if !append || !output_path.exists() {
            // Write file header for new file
            writer.write_all(DUMP_MAGIC_NUMBER)
                .map_err(|e| Error::storage(format!("Failed to write magic bytes: {}", e)))?;
            writer.write_all(&DUMP_VERSION.to_le_bytes())
                .map_err(|e| Error::storage(format!("Failed to write version: {}", e)))?;

            // Reserve metadata space
            let metadata_placeholder = vec![0u8; 8192];
            writer.write_all(&metadata_placeholder)
                .map_err(|e| Error::storage(format!("Failed to write placeholder: {}", e)))?;
        }

        // Write incremental marker
        writer.write_all(b"INCR")
            .map_err(|e| Error::storage(format!("Failed to write incremental marker: {}", e)))?;

        metadata.table_count = dirty_tables.len() as u32;

        let mut total_rows = 0u64;
        let mut uncompressed_bytes = 0u64;

        // Dump dirty tables only
        for table in &dirty_tables {
            debug!("Dumping dirty table: {}", table);

            // Write table marker
            writer.write_all(b"TABL")
                .map_err(|e| Error::storage(format!("Failed to write table marker: {}", e)))?;

            let table_bytes = table.as_bytes();
            writer.write_all(&(table_bytes.len() as u32).to_le_bytes())
                .map_err(|e| Error::storage(format!("Failed to write table name length: {}", e)))?;
            writer.write_all(table_bytes)
                .map_err(|e| Error::storage(format!("Failed to write table name: {}", e)))?;

            let schema = db.get_table_schema(table)?;
            let schema_bytes = bincode::serialize(&schema)
                .map_err(|e| Error::storage(format!("Failed to serialize schema: {}", e)))?;
            writer.write_all(&(schema_bytes.len() as u32).to_le_bytes())
                .map_err(|e| Error::storage(format!("Failed to write schema length: {}", e)))?;
            writer.write_all(&schema_bytes)
                .map_err(|e| Error::storage(format!("Failed to write schema: {}", e)))?;

            uncompressed_bytes += schema_bytes.len() as u64;

            let indexes = db.get_table_indexes(table).unwrap_or_default();
            let indexes_bytes = bincode::serialize(&indexes)
                .map_err(|e| Error::storage(format!("Failed to serialize indexes: {}", e)))?;
            writer.write_all(&(indexes_bytes.len() as u32).to_le_bytes())
                .map_err(|e| Error::storage(format!("Failed to write indexes length: {}", e)))?;
            writer.write_all(&indexes_bytes)
                .map_err(|e| Error::storage(format!("Failed to write indexes: {}", e)))?;

            uncompressed_bytes += indexes_bytes.len() as u64;

            let rows = db.scan_table(table)?;
            let row_count = rows.len() as u64;
            total_rows += row_count;

            writer.write_all(&row_count.to_le_bytes())
                .map_err(|e| Error::storage(format!("Failed to write row count: {}", e)))?;

            for batch in rows.chunks(1000) {
                let batch_bytes = bincode::serialize(batch)
                    .map_err(|e| Error::storage(format!("Failed to serialize batch: {}", e)))?;

                uncompressed_bytes += batch_bytes.len() as u64;
                let compressed = self.compress_data(&batch_bytes)?;

                writer.write_all(&(compressed.len() as u32).to_le_bytes())
                    .map_err(|e| Error::storage(format!("Failed to write batch length: {}", e)))?;
                writer.write_all(&compressed)
                    .map_err(|e| Error::storage(format!("Failed to write batch: {}", e)))?;
            }

            writer.write_all(&0u32.to_le_bytes())
                .map_err(|e| Error::storage(format!("Failed to write EOT marker: {}", e)))?;
        }

        metadata.total_rows = total_rows;
        metadata.uncompressed_size = uncompressed_bytes;
        metadata.append_count = if append { 1 } else { 0 };

        writer.flush()
            .map_err(|e| Error::storage(format!("Failed to flush writer: {}", e)))?;
        drop(writer);

        let checksum = self.calculate_checksum(output_path)?;
        metadata.checksum = checksum;

        let file_size = std::fs::metadata(output_path)
            .map_err(|e| Error::storage(format!("Failed to get file metadata: {}", e)))?
            .len();
        metadata.compressed_size = file_size;

        self.dump_history.write().push(metadata.clone());
        self.dirty_tracker.clear_dirty();

        let elapsed = start_time.elapsed();
        info!(
            "Incremental dump {} completed: {} tables, {} rows in {:.2}s",
            dump_id,
            metadata.table_count,
            metadata.total_rows,
            elapsed.as_secs_f64()
        );

        Ok(metadata)
    }

    /// Restore database from dump file
    ///
    /// # Arguments
    /// * `input_path` - Path to dump file
    /// * `db` - Database interface for writing data
    ///
    /// # Returns
    /// Ok(()) on success
    pub fn restore_from_dump<D: DatabaseRestoreInterface>(
        &self,
        input_path: &Path,
        db: &mut D,
    ) -> Result<()> {
        info!("Starting restore from {}", input_path.display());

        // Validate dump first
        self.validate_dump(input_path)?;

        // Open dump file
        let file = File::open(input_path)
            .map_err(|e| Error::storage(format!("Failed to open dump file: {}", e)))?;
        let mut reader = BufReader::with_capacity(256 * 1024, file);

        // Read and verify magic bytes
        let mut magic = [0u8; 8];
        reader.read_exact(&mut magic)
            .map_err(|e| Error::storage(format!("Failed to read magic bytes: {}", e)))?;
        if &magic != DUMP_MAGIC_NUMBER {
            return Err(Error::storage("Invalid dump file: bad magic bytes"));
        }

        // Read version
        let mut version_bytes = [0u8; 4];
        reader.read_exact(&mut version_bytes)
            .map_err(|e| Error::storage(format!("Failed to read version: {}", e)))?;
        let version = u32::from_le_bytes(version_bytes);
        if version != DUMP_VERSION {
            return Err(Error::storage(format!("Unsupported dump version: {}", version)));
        }

        // Skip metadata header
        reader.seek(SeekFrom::Current(8192))
            .map_err(|e| Error::storage(format!("Failed to seek past metadata: {}", e)))?;

        let mut total_tables = 0;
        let mut total_rows = 0u64;

        // Read tables until end marker
        loop {
            // Read marker
            let mut marker = [0u8; 4];
            if reader.read_exact(&mut marker).is_err() {
                break; // EOF
            }

            match &marker {
                b"ENDD" => {
                    debug!("Reached end-of-dump marker");
                    break;
                }
                b"INCR" => {
                    debug!("Found incremental marker, continuing...");
                    continue;
                }
                b"TABL" => {
                    // Table data follows
                }
                _ => {
                    return Err(Error::storage(format!("Invalid marker: {:?}", marker)));
                }
            }

            // Read table name
            let mut len_bytes = [0u8; 4];
            reader.read_exact(&mut len_bytes)
                .map_err(|e| Error::storage(format!("Failed to read table name length: {}", e)))?;
            let table_name_len = u32::from_le_bytes(len_bytes);

            let mut table_bytes = vec![0u8; table_name_len as usize];
            reader.read_exact(&mut table_bytes)
                .map_err(|e| Error::storage(format!("Failed to read table name: {}", e)))?;
            let table = String::from_utf8(table_bytes)
                .map_err(|e| Error::storage(format!("Invalid table name: {}", e)))?;

            debug!("Restoring table: {}", table);

            // Read schema
            let mut schema_len_bytes = [0u8; 4];
            reader.read_exact(&mut schema_len_bytes)
                .map_err(|e| Error::storage(format!("Failed to read schema length: {}", e)))?;
            let schema_len = u32::from_le_bytes(schema_len_bytes);

            let mut schema_bytes = vec![0u8; schema_len as usize];
            reader.read_exact(&mut schema_bytes)
                .map_err(|e| Error::storage(format!("Failed to read schema: {}", e)))?;
            let schema: Schema = bincode::deserialize(&schema_bytes)
                .map_err(|e| Error::storage(format!("Failed to deserialize schema: {}", e)))?;

            // Read indexes
            let mut indexes_len_bytes = [0u8; 4];
            reader.read_exact(&mut indexes_len_bytes)
                .map_err(|e| Error::storage(format!("Failed to read indexes length: {}", e)))?;
            let indexes_len = u32::from_le_bytes(indexes_len_bytes);

            let mut indexes_bytes = vec![0u8; indexes_len as usize];
            reader.read_exact(&mut indexes_bytes)
                .map_err(|e| Error::storage(format!("Failed to read indexes: {}", e)))?;
            let indexes: Vec<IndexMetadata> = bincode::deserialize(&indexes_bytes)
                .map_err(|e| Error::storage(format!("Failed to deserialize indexes: {}", e)))?;

            // Create table
            db.create_table(&table, schema)?;

            // Restore indexes
            for index in indexes {
                db.create_index(&table, &index)?;
            }

            // Read row count
            let mut row_count_bytes = [0u8; 8];
            reader.read_exact(&mut row_count_bytes)
                .map_err(|e| Error::storage(format!("Failed to read row count: {}", e)))?;
            let row_count = u64::from_le_bytes(row_count_bytes);

            // Read batches
            let mut rows_read = 0u64;
            loop {
                let mut batch_len_bytes = [0u8; 4];
                reader.read_exact(&mut batch_len_bytes)
                    .map_err(|e| Error::storage(format!("Failed to read batch length: {}", e)))?;
                let batch_len = u32::from_le_bytes(batch_len_bytes);

                if batch_len == 0 {
                    // End of table marker
                    break;
                }

                let mut batch_bytes = vec![0u8; batch_len as usize];
                reader.read_exact(&mut batch_bytes)
                    .map_err(|e| Error::storage(format!("Failed to read batch: {}", e)))?;

                // Decompress batch
                let decompressed = self.decompress_data(&batch_bytes)?;

                // Deserialize batch
                let batch: Vec<Tuple> = bincode::deserialize(&decompressed)
                    .map_err(|e| Error::storage(format!("Failed to deserialize batch: {}", e)))?;

                rows_read += batch.len() as u64;

                // Insert rows
                for row in batch {
                    db.insert_row(&table, row)?;
                }
            }

            if rows_read != row_count {
                warn!("Row count mismatch for table {}: expected {}, got {}", table, row_count, rows_read);
            }

            total_tables += 1;
            total_rows += rows_read;
        }

        info!("Restore completed: {} tables, {} rows", total_tables, total_rows);

        Ok(())
    }

    /// List all dumps in history
    pub fn list_dumps(&self) -> Vec<DumpMetadata> {
        self.dump_history.read().clone()
    }

    /// Validate dump file integrity
    pub fn validate_dump(&self, path: &Path) -> Result<()> {
        if !path.exists() {
            return Err(Error::storage("Dump file does not exist"));
        }

        let file = File::open(path)
            .map_err(|e| Error::storage(format!("Failed to open dump file: {}", e)))?;
        let mut reader = BufReader::new(file);

        // Verify magic bytes
        let mut magic = [0u8; 8];
        reader.read_exact(&mut magic)
            .map_err(|e| Error::storage(format!("Failed to read magic bytes: {}", e)))?;
        if &magic != DUMP_MAGIC_NUMBER {
            return Err(Error::storage("Invalid dump file: bad magic bytes"));
        }

        // Verify version
        let mut version_bytes = [0u8; 4];
        reader.read_exact(&mut version_bytes)
            .map_err(|e| Error::storage(format!("Failed to read version: {}", e)))?;
        let version = u32::from_le_bytes(version_bytes);
        if version > DUMP_VERSION {
            return Err(Error::storage(format!("Unsupported dump version: {}", version)));
        }

        // Verify checksum
        drop(reader);
        let _checksum = self.calculate_checksum(path)?;

        debug!("Dump file validation passed: {}", path.display());

        Ok(())
    }

    /// Get dump metadata by ID
    pub fn get_dump_metadata(&self, dump_id: u64) -> Result<DumpMetadata> {
        self.dump_history
            .read()
            .iter()
            .find(|m| m.dump_id == dump_id)
            .cloned()
            .ok_or_else(|| Error::storage(format!("Dump {} not found", dump_id)))
    }

    /// Delete old dumps, keeping only the most recent N
    pub fn delete_old_dumps(&self, keep_count: usize) -> Result<()> {
        let mut history = self.dump_history.write();

        if history.len() <= keep_count {
            return Ok(());
        }

        // Sort by creation time (newest first)
        history.sort_by(|a, b| b.created_at.cmp(&a.created_at));

        // Remove old dumps
        let removed = history.drain(keep_count..).collect::<Vec<_>>();

        info!("Removed {} old dump(s) from history", removed.len());

        Ok(())
    }

    /// Get dirty tracker
    pub fn dirty_tracker(&self) -> &Arc<DirtyTracker> {
        &self.dirty_tracker
    }

    // Helper methods

    /// Compress data based on configuration
    fn compress_data(&self, data: &[u8]) -> Result<Vec<u8>> {
        match self.compression {
            CompressionType::None => Ok(data.to_vec()),
            CompressionType::Zstd => {
                zstd::bulk::compress(data, 3)
                    .map_err(|e| Error::compression(format!("Zstd compression failed: {}", e)))
            }
            CompressionType::Gzip | CompressionType::Brotli => {
                // For now, use zstd as fallback for unsupported types
                zstd::bulk::compress(data, 3)
                    .map_err(|e| Error::compression(format!("Compression failed: {}", e)))
            }
        }
    }

    /// Decompress data
    fn decompress_data(&self, data: &[u8]) -> Result<Vec<u8>> {
        match self.compression {
            CompressionType::None => Ok(data.to_vec()),
            CompressionType::Zstd => {
                zstd::bulk::decompress(data, 100 * 1024 * 1024) // 100MB max
                    .map_err(|e| Error::compression(format!("Zstd decompression failed: {}", e)))
            }
            CompressionType::Gzip | CompressionType::Brotli => {
                // For now, use zstd as fallback
                zstd::bulk::decompress(data, 100 * 1024 * 1024)
                    .map_err(|e| Error::compression(format!("Decompression failed: {}", e)))
            }
        }
    }

    /// Calculate CRC32 checksum of file
    fn calculate_checksum(&self, path: &Path) -> Result<String> {
        let file = File::open(path)
            .map_err(|e| Error::storage(format!("Failed to open file for checksum: {}", e)))?;
        let mut reader = BufReader::new(file);
        let mut buffer = vec![0u8; 8192];
        let mut hasher = crc32fast::Hasher::new();

        loop {
            let bytes_read = reader.read(&mut buffer)
                .map_err(|e| Error::storage(format!("Failed to read file: {}", e)))?;
            if bytes_read == 0 {
                break;
            }
            if let Some(data) = buffer.get(..bytes_read) {
                hasher.update(data);
            }
        }

        Ok(format!("{:08x}", hasher.finalize()))
    }

    /// Write metadata to file header
    fn write_metadata_header(
        &self,
        path: &Path,
        position: u64,
        metadata: &DumpMetadata,
    ) -> Result<()> {
        let file = OpenOptions::new()
            .write(true)
            .open(path)
            .map_err(|e| Error::storage(format!("Failed to open dump file: {}", e)))?;
        let mut writer = BufWriter::new(file);

        writer.seek(SeekFrom::Start(position))
            .map_err(|e| Error::storage(format!("Failed to seek to metadata position: {}", e)))?;

        let metadata_bytes = serde_json::to_vec(metadata)
            .map_err(|e| Error::storage(format!("Failed to serialize metadata: {}", e)))?;

        // Write actual metadata length
        writer.write_all(&(metadata_bytes.len() as u32).to_le_bytes())
            .map_err(|e| Error::storage(format!("Failed to write metadata length: {}", e)))?;
        writer.write_all(&metadata_bytes)
            .map_err(|e| Error::storage(format!("Failed to write metadata: {}", e)))?;

        writer.flush()
            .map_err(|e| Error::storage(format!("Failed to flush writer: {}", e)))?;

        Ok(())
    }
}

// Mode and options types for CLI compatibility
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DumpMode {
    Full,
    Incremental,
}

/// Output format for dumps
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DumpOutputFormat {
    /// HeliosDB binary format (compressed)
    Binary,
    /// SQL text format (CREATE TABLE + INSERT)
    Sql,
}

#[derive(Debug, Clone)]
pub struct DumpOptions {
    pub output_path: PathBuf,
    pub mode: DumpMode,
    pub compression: CompressionType,
    pub append: bool,
    pub tables: Option<Vec<String>>,
    pub verbose: bool,
    pub connection: Option<String>,
    pub format: DumpOutputFormat,
}

impl Default for DumpOptions {
    fn default() -> Self {
        Self {
            output_path: PathBuf::from("backup.heliodump"),
            mode: DumpMode::Full,
            compression: CompressionType::Zstd,
            append: false,
            tables: None,
            verbose: false,
            connection: None,
            format: DumpOutputFormat::Binary,
        }
    }
}

#[derive(Debug, Clone)]
pub struct RestoreOptions {
    pub input_path: PathBuf,
    pub target: Option<PathBuf>,
    pub tables: Option<Vec<String>>,
    pub verify: bool,
    pub verbose: bool,
    pub connection: Option<String>,
}

impl Default for RestoreOptions {
    fn default() -> Self {
        Self {
            input_path: PathBuf::from("backup.heliodump"),
            target: None,
            tables: None,
            verify: true,
            verbose: false,
            connection: None,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DumpReport {
    pub dump_id: String,
    pub tables_dumped: usize,
    pub rows_dumped: u64,
    pub bytes_written: u64,
    pub bytes_uncompressed: u64,
    pub duration_ms: u64,
    pub compression_ratio: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RestoreReport {
    pub tables_restored: usize,
    pub rows_restored: u64,
    pub duration_ms: u64,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use crate::{Column, DataType, Value};

    /// Mock database for testing
    struct MockDatabase {
        tables: HashMap<String, (Schema, Vec<Tuple>)>,
        indexes: HashMap<String, Vec<IndexMetadata>>,
    }

    impl MockDatabase {
        fn new() -> Self {
            Self {
                tables: HashMap::new(),
                indexes: HashMap::new(),
            }
        }

        fn add_table(&mut self, name: &str, schema: Schema, rows: Vec<Tuple>) {
            self.tables.insert(name.to_string(), (schema, rows));
        }

        fn add_index(&mut self, table: &str, index: IndexMetadata) {
            self.indexes.entry(table.to_string())
                .or_insert_with(Vec::new)
                .push(index);
        }
    }

    impl DatabaseInterface for MockDatabase {
        fn list_tables(&self) -> Result<Vec<String>> {
            Ok(self.tables.keys().cloned().collect())
        }

        fn get_table_schema(&self, table: &str) -> Result<Schema> {
            self.tables
                .get(table)
                .map(|(schema, _)| schema.clone())
                .ok_or_else(|| Error::storage(format!("Table {} not found", table)))
        }

        fn scan_table(&self, table: &str) -> Result<Vec<Tuple>> {
            self.tables
                .get(table)
                .map(|(_, rows)| rows.clone())
                .ok_or_else(|| Error::storage(format!("Table {} not found", table)))
        }

        fn get_table_indexes(&self, table: &str) -> Result<Vec<IndexMetadata>> {
            Ok(self.indexes.get(table).cloned().unwrap_or_default())
        }
    }

    impl DatabaseRestoreInterface for MockDatabase {
        fn create_table(&mut self, name: &str, schema: Schema) -> Result<()> {
            self.tables.insert(name.to_string(), (schema, Vec::new()));
            Ok(())
        }

        fn create_index(&mut self, table: &str, index: &IndexMetadata) -> Result<()> {
            self.add_index(table, index.clone());
            Ok(())
        }

        fn insert_row(&mut self, table: &str, row: Tuple) -> Result<()> {
            if let Some((_, rows)) = self.tables.get_mut(table) {
                rows.push(row);
                Ok(())
            } else {
                Err(Error::storage(format!("Table {} not found", table)))
            }
        }
    }

    #[test]
    fn test_dump_manager_creation() {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let manager = DumpManager::new(temp_dir.path().to_path_buf(), CompressionType::Zstd);
        assert_eq!(manager.list_dumps().len(), 0);
    }

    #[test]
    fn test_full_dump_creation() -> Result<()> {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let manager = DumpManager::new(temp_dir.path().to_path_buf(), CompressionType::Zstd);

        // Create mock database
        let mut db = MockDatabase::new();
        let schema = Schema::new(vec![
            Column::new("id", DataType::Int4),
            Column::new("name", DataType::Text),
        ]);

        let rows = vec![
            Tuple::new(vec![Value::Int4(1), Value::String("Alice".to_string())]),
            Tuple::new(vec![Value::Int4(2), Value::String("Bob".to_string())]),
        ];

        db.add_table("users", schema, rows);

        // Create dump
        let dump_path = temp_dir.path().join("test.dump");
        let metadata = manager.create_full_dump(&dump_path, &db)?;

        assert_eq!(metadata.dump_type, DumpType::Full);
        assert_eq!(metadata.table_count, 1);
        assert_eq!(metadata.total_rows, 2);
        assert!(dump_path.exists());
        assert!(metadata.compressed_size > 0);
        assert!(!metadata.checksum.is_empty());

        Ok(())
    }

    #[test]
    fn test_restore_from_dump() -> Result<()> {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let manager = DumpManager::new(temp_dir.path().to_path_buf(), CompressionType::None);

        // Create and dump mock database
        let mut db = MockDatabase::new();
        let schema = Schema::new(vec![
            Column::new("id", DataType::Int4),
            Column::new("value", DataType::Float8),
        ]);

        let rows = vec![
            Tuple::new(vec![Value::Int4(1), Value::Float8(1.5)]),
            Tuple::new(vec![Value::Int4(2), Value::Float8(2.5)]),
            Tuple::new(vec![Value::Int4(3), Value::Float8(3.5)]),
        ];

        db.add_table("data", schema, rows);

        let dump_path = temp_dir.path().join("test_restore.dump");
        manager.create_full_dump(&dump_path, &db)?;

        // Restore to new database
        let mut db2 = MockDatabase::new();
        manager.restore_from_dump(&dump_path, &mut db2)?;

        // Verify restored data
        let restored_rows = db2.scan_table("data")?;
        assert_eq!(restored_rows.len(), 3);

        Ok(())
    }

    #[test]
    fn test_compression_roundtrip() -> Result<()> {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let manager = DumpManager::new(temp_dir.path().to_path_buf(), CompressionType::Zstd);

        let test_data = b"Hello, World! This is test data for compression.".repeat(100);
        let compressed = manager.compress_data(&test_data)?;
        let decompressed = manager.decompress_data(&compressed)?;

        assert_eq!(test_data.to_vec(), decompressed);
        assert!(compressed.len() < test_data.len());

        Ok(())
    }

    #[test]
    fn test_dirty_tracker() {
        let tracker = DirtyTracker::new();

        assert!(!tracker.is_dirty());

        tracker.mark_table_dirty("users");
        assert!(tracker.is_dirty());

        let dirty_tables = tracker.get_dirty_tables();
        assert_eq!(dirty_tables.len(), 1);
        assert!(dirty_tables.contains(&"users".to_string()));

        tracker.clear_dirty();
        assert!(!tracker.is_dirty());
        assert_eq!(tracker.get_dirty_tables().len(), 0);
    }

    #[test]
    fn test_large_dataset_throughput() -> Result<()> {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let manager = DumpManager::new(temp_dir.path().to_path_buf(), CompressionType::Zstd);

        // Create large dataset
        let mut db = MockDatabase::new();
        let schema = Schema::new(vec![
            Column::new("id", DataType::Int4),
            Column::new("data", DataType::Text),
        ]);

        // Generate 100K rows
        let mut rows = Vec::new();
        for i in 0..100_000 {
            rows.push(Tuple::new(vec![
                Value::Int4(i),
                Value::String(format!("Data row {} with some content", i)),
            ]));
        }

        db.add_table("large_table", schema, rows);

        // Measure dump time
        let start = Instant::now();
        let dump_path = temp_dir.path().join("large.dump");
        let metadata = manager.create_full_dump(&dump_path, &db)?;
        let elapsed = start.elapsed();

        // Calculate throughput
        let throughput_mbps = (metadata.uncompressed_size as f64 / 1_048_576.0) / elapsed.as_secs_f64();

        println!("Dumped {} rows in {:?}", metadata.total_rows, elapsed);
        println!("Throughput: {:.2} MB/s", throughput_mbps);

        // Should achieve >3 MB/s (conservative target for debug builds in VMs/containers)
        // Release builds should achieve >50 MB/s
        assert!(throughput_mbps > 3.0, "Throughput too low: {:.2} MB/s", throughput_mbps);

        Ok(())
    }

    #[test]
    fn test_validate_dump() -> Result<()> {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let manager = DumpManager::new(temp_dir.path().to_path_buf(), CompressionType::None);

        let mut db = MockDatabase::new();
        let schema = Schema::new(vec![Column::new("id", DataType::Int4)]);
        db.add_table("test", schema, vec![]);

        let dump_path = temp_dir.path().join("validate.dump");
        manager.create_full_dump(&dump_path, &db)?;

        // Should validate successfully
        manager.validate_dump(&dump_path)?;

        // Test invalid file
        let invalid_path = temp_dir.path().join("invalid.dump");
        std::fs::write(&invalid_path, b"invalid data")?;

        assert!(manager.validate_dump(&invalid_path).is_err());

        Ok(())
    }

    #[test]
    fn test_incremental_dump() -> Result<()> {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let manager = DumpManager::new(temp_dir.path().to_path_buf(), CompressionType::None);

        // Mark some tables as dirty
        manager.dirty_tracker().mark_table_dirty("users");

        let mut db = MockDatabase::new();
        let schema = Schema::new(vec![Column::new("id", DataType::Int4)]);
        db.add_table("users", schema, vec![Tuple::new(vec![Value::Int4(1)])]);

        let dump_path = temp_dir.path().join("incremental.dump");

        // Create incremental dump
        let metadata = manager.create_incremental_dump(&dump_path, &db, false)?;

        assert_eq!(metadata.dump_type, DumpType::Incremental);
        assert_eq!(metadata.table_count, 1);

        Ok(())
    }

    #[test]
    fn test_dump_with_indexes() -> Result<()> {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let manager = DumpManager::new(temp_dir.path().to_path_buf(), CompressionType::None);

        let mut db = MockDatabase::new();
        let schema = Schema::new(vec![
            Column::new("id", DataType::Int4),
            Column::new("email", DataType::Text),
        ]);

        db.add_table("users", schema, vec![]);
        db.add_index("users", IndexMetadata {
            name: "idx_email".to_string(),
            index_type: "btree".to_string(),
            columns: vec!["email".to_string()],
            is_unique: true,
        });

        let dump_path = temp_dir.path().join("with_indexes.dump");
        manager.create_full_dump(&dump_path, &db)?;

        let mut db2 = MockDatabase::new();
        manager.restore_from_dump(&dump_path, &mut db2)?;

        let indexes = db2.get_table_indexes("users")?;
        assert_eq!(indexes.len(), 1);
        assert_eq!(indexes[0].name, "idx_email");

        Ok(())
    }

    #[test]
    fn test_get_next_dump_id() {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let manager = DumpManager::new(temp_dir.path().to_path_buf(), CompressionType::None);

        let id1 = manager.get_next_dump_id();
        let id2 = manager.get_next_dump_id();
        let id3 = manager.get_next_dump_id();

        assert_eq!(id1, 1);
        assert_eq!(id2, 2);
        assert_eq!(id3, 3);
    }

    #[test]
    fn test_delete_old_dumps() -> Result<()> {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let manager = DumpManager::new(temp_dir.path().to_path_buf(), CompressionType::None);

        // Add some mock dumps to history
        for i in 1..=5 {
            let mut metadata = DumpMetadata::new(i, DumpType::Full);
            metadata.created_at = SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(i);
            manager.dump_history.write().push(metadata);
        }

        assert_eq!(manager.list_dumps().len(), 5);

        // Keep only 3 most recent
        manager.delete_old_dumps(3)?;

        assert_eq!(manager.list_dumps().len(), 3);

        Ok(())
    }

    #[test]
    fn test_checksum_calculation() -> Result<()> {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let manager = DumpManager::new(temp_dir.path().to_path_buf(), CompressionType::None);

        let test_file = temp_dir.path().join("test.dat");
        std::fs::write(&test_file, b"test data for checksum")?;

        let checksum1 = manager.calculate_checksum(&test_file)?;
        let checksum2 = manager.calculate_checksum(&test_file)?;

        // Same file should produce same checksum
        assert_eq!(checksum1, checksum2);

        // Different content should produce different checksum
        std::fs::write(&test_file, b"different test data")?;
        let checksum3 = manager.calculate_checksum(&test_file)?;
        assert_ne!(checksum1, checksum3);

        Ok(())
    }
}