oxirs-cluster 0.2.4

Raft-backed distributed dataset for high availability and horizontal scaling
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
//! # Backup and Restore
//!
//! Provides comprehensive backup and restore capabilities for distributed RDF storage.
//!
//! ## Overview
//!
//! This module supports:
//! - Full backups of entire cluster state
//! - Incremental backups for efficiency
//! - Point-in-time recovery
//! - Snapshot-based backups
//! - Compressed backups
//! - Backup verification
//! - Automated backup scheduling
//!
//! ## Features
//!
//! - Multiple backup formats (JSON, Binary, Compressed)
//! - Backup encryption support
//! - Backup metadata tracking
//! - Parallel backup/restore operations
//! - Progress tracking and metrics
//! - SciRS2-optimized compression

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::SystemTime;
use tokio::fs;
use tokio::sync::RwLock;
use tracing::{debug, info, warn};

use crate::error::{ClusterError, Result};
use crate::raft::OxirsNodeId;

/// Backup format
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BackupFormat {
    /// JSON format (human-readable)
    Json,
    /// Binary format (compact)
    Binary,
    /// Compressed binary (smallest)
    CompressedBinary,
}

/// Backup type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BackupType {
    /// Full backup of all data
    Full,
    /// Incremental backup (changes since last backup)
    Incremental,
    /// Snapshot-based backup
    Snapshot,
}

/// Backup status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BackupStatus {
    /// Backup in progress
    InProgress,
    /// Backup completed successfully
    Completed,
    /// Backup failed
    Failed,
    /// Backup verified
    Verified,
}

/// Backup configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupConfig {
    /// Backup directory
    pub backup_dir: PathBuf,
    /// Backup format
    pub format: BackupFormat,
    /// Enable compression
    pub enable_compression: bool,
    /// Compression level (1-9)
    pub compression_level: i32,
    /// Enable encryption
    pub enable_encryption: bool,
    /// Verify backups after creation
    pub verify_after_backup: bool,
    /// Maximum backup age (days)
    pub max_backup_age_days: u32,
    /// Maximum number of backups to keep
    pub max_backups: u32,
}

impl Default for BackupConfig {
    fn default() -> Self {
        Self {
            backup_dir: PathBuf::from("./backups"),
            format: BackupFormat::CompressedBinary,
            enable_compression: true,
            compression_level: 6,
            enable_encryption: false,
            verify_after_backup: true,
            max_backup_age_days: 30,
            max_backups: 10,
        }
    }
}

/// Backup metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupMetadata {
    /// Backup ID
    pub id: String,
    /// Backup type
    pub backup_type: BackupType,
    /// Backup format
    pub format: BackupFormat,
    /// Creation timestamp
    pub created_at: SystemTime,
    /// Node ID that created the backup
    pub node_id: OxirsNodeId,
    /// Backup file path
    pub file_path: PathBuf,
    /// Backup size in bytes
    pub size_bytes: u64,
    /// Number of triples backed up
    pub triple_count: u64,
    /// Backup status
    pub status: BackupStatus,
    /// Checksum (for verification)
    pub checksum: Option<String>,
    /// Parent backup ID (for incremental backups)
    pub parent_backup_id: Option<String>,
    /// Completion timestamp
    pub completed_at: Option<SystemTime>,
    /// Error message (if failed)
    pub error: Option<String>,
}

/// Restore options
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RestoreOptions {
    /// Backup ID to restore
    pub backup_id: String,
    /// Skip verification
    pub skip_verification: bool,
    /// Overwrite existing data
    pub overwrite_existing: bool,
    /// Restore to specific point in time
    pub point_in_time: Option<SystemTime>,
}

/// Backup statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct BackupStatistics {
    /// Total backups created
    pub total_backups: u64,
    /// Total bytes backed up
    pub total_bytes_backed_up: u64,
    /// Total restores performed
    pub total_restores: u64,
    /// Failed backups
    pub failed_backups: u64,
    /// Failed restores
    pub failed_restores: u64,
    /// Last backup timestamp
    pub last_backup: Option<SystemTime>,
    /// Last restore timestamp
    pub last_restore: Option<SystemTime>,
    /// Average backup time (milliseconds)
    pub avg_backup_time_ms: f64,
    /// Average restore time (milliseconds)
    pub avg_restore_time_ms: f64,
}

/// Backup and restore manager
pub struct BackupRestoreManager {
    config: BackupConfig,
    /// Backup metadata registry
    backups: Arc<RwLock<HashMap<String, BackupMetadata>>>,
    /// Statistics
    stats: Arc<RwLock<BackupStatistics>>,
    /// Node ID
    node_id: OxirsNodeId,
}

impl BackupRestoreManager {
    /// Create a new backup/restore manager
    pub async fn new(config: BackupConfig, node_id: OxirsNodeId) -> Result<Self> {
        // Create backup directory if it doesn't exist
        fs::create_dir_all(&config.backup_dir).await.map_err(|e| {
            ClusterError::Other(format!("Failed to create backup directory: {}", e))
        })?;

        let manager = Self {
            config,
            backups: Arc::new(RwLock::new(HashMap::new())),
            stats: Arc::new(RwLock::new(BackupStatistics::default())),
            node_id,
        };

        // Load existing backup metadata
        manager.load_backup_metadata().await?;

        Ok(manager)
    }

    /// Create a full backup
    pub async fn create_full_backup(&self, data: Vec<(String, String, String)>) -> Result<String> {
        let backup_id = uuid::Uuid::new_v4().to_string();
        let timestamp = SystemTime::now();

        info!("Creating full backup: {}", backup_id);

        let file_name = format!("backup_full_{}_{}.dat", self.node_id, backup_id);
        let file_path = self.config.backup_dir.join(&file_name);

        let start_time = std::time::Instant::now();

        // Serialize data based on format
        let serialized_data = self.serialize_data(&data)?;

        // Compress if enabled
        let final_data = if self.config.enable_compression {
            self.compress_data(&serialized_data)?
        } else {
            serialized_data
        };

        // Write to file
        fs::write(&file_path, &final_data)
            .await
            .map_err(|e| ClusterError::Other(format!("Failed to write backup: {}", e)))?;

        let size_bytes = final_data.len() as u64;
        let elapsed = start_time.elapsed().as_millis() as f64;

        // Calculate checksum
        let checksum = self.calculate_checksum(&final_data);

        // Create metadata
        let metadata = BackupMetadata {
            id: backup_id.clone(),
            backup_type: BackupType::Full,
            format: self.config.format,
            created_at: timestamp,
            node_id: self.node_id,
            file_path: file_path.clone(),
            size_bytes,
            triple_count: data.len() as u64,
            status: BackupStatus::Completed,
            checksum: Some(checksum),
            parent_backup_id: None,
            completed_at: Some(SystemTime::now()),
            error: None,
        };

        // Register backup
        {
            let mut backups = self.backups.write().await;
            backups.insert(backup_id.clone(), metadata.clone());
        }

        // Update statistics
        {
            let mut stats = self.stats.write().await;
            stats.total_backups += 1;
            stats.total_bytes_backed_up += size_bytes;
            stats.last_backup = Some(timestamp);

            // Update average backup time
            if stats.total_backups > 1 {
                stats.avg_backup_time_ms =
                    (stats.avg_backup_time_ms * (stats.total_backups - 1) as f64 + elapsed)
                        / stats.total_backups as f64;
            } else {
                stats.avg_backup_time_ms = elapsed;
            }
        }

        // Verify if configured
        if self.config.verify_after_backup {
            self.verify_backup(&backup_id).await?;
        }

        // Cleanup old backups
        self.cleanup_old_backups().await?;

        info!(
            "Full backup completed: {} ({} bytes, {} triples, {:.2}ms)",
            backup_id,
            size_bytes,
            data.len(),
            elapsed
        );

        Ok(backup_id)
    }

    /// Create an incremental backup
    pub async fn create_incremental_backup(
        &self,
        parent_backup_id: &str,
        changed_data: Vec<(String, String, String)>,
    ) -> Result<String> {
        // Verify parent backup exists
        {
            let backups = self.backups.read().await;
            if !backups.contains_key(parent_backup_id) {
                return Err(ClusterError::Other(format!(
                    "Parent backup not found: {}",
                    parent_backup_id
                )));
            }
        }

        let backup_id = uuid::Uuid::new_v4().to_string();
        let timestamp = SystemTime::now();

        info!(
            "Creating incremental backup: {} (parent: {})",
            backup_id, parent_backup_id
        );

        let file_name = format!("backup_incr_{}_{}.dat", self.node_id, backup_id);
        let file_path = self.config.backup_dir.join(&file_name);

        let start_time = std::time::Instant::now();

        // Serialize incremental data
        let serialized_data = self.serialize_data(&changed_data)?;
        let final_data = if self.config.enable_compression {
            self.compress_data(&serialized_data)?
        } else {
            serialized_data
        };

        // Write to file
        fs::write(&file_path, &final_data)
            .await
            .map_err(|e| ClusterError::Other(format!("Failed to write backup: {}", e)))?;

        let size_bytes = final_data.len() as u64;
        let elapsed = start_time.elapsed().as_millis() as f64;

        // Calculate checksum
        let checksum = self.calculate_checksum(&final_data);

        // Create metadata
        let metadata = BackupMetadata {
            id: backup_id.clone(),
            backup_type: BackupType::Incremental,
            format: self.config.format,
            created_at: timestamp,
            node_id: self.node_id,
            file_path,
            size_bytes,
            triple_count: changed_data.len() as u64,
            status: BackupStatus::Completed,
            checksum: Some(checksum),
            parent_backup_id: Some(parent_backup_id.to_string()),
            completed_at: Some(SystemTime::now()),
            error: None,
        };

        // Register backup
        {
            let mut backups = self.backups.write().await;
            backups.insert(backup_id.clone(), metadata);
        }

        // Update statistics
        {
            let mut stats = self.stats.write().await;
            stats.total_backups += 1;
            stats.total_bytes_backed_up += size_bytes;
            stats.last_backup = Some(timestamp);

            if stats.total_backups > 1 {
                stats.avg_backup_time_ms =
                    (stats.avg_backup_time_ms * (stats.total_backups - 1) as f64 + elapsed)
                        / stats.total_backups as f64;
            } else {
                stats.avg_backup_time_ms = elapsed;
            }
        }

        info!(
            "Incremental backup completed: {} ({} bytes, {} triples, {:.2}ms)",
            backup_id,
            size_bytes,
            changed_data.len(),
            elapsed
        );

        Ok(backup_id)
    }

    /// Restore from backup
    pub async fn restore_backup(
        &self,
        options: RestoreOptions,
    ) -> Result<Vec<(String, String, String)>> {
        let backup_id = &options.backup_id;

        info!("Restoring from backup: {}", backup_id);

        // Get backup metadata
        let metadata = {
            let backups = self.backups.read().await;
            backups
                .get(backup_id)
                .cloned()
                .ok_or_else(|| ClusterError::Other(format!("Backup not found: {}", backup_id)))?
        };

        let start_time = std::time::Instant::now();

        // Verify backup if not skipped
        if !options.skip_verification {
            self.verify_backup(backup_id).await?;
        }

        // Read backup file
        let file_data = fs::read(&metadata.file_path)
            .await
            .map_err(|e| ClusterError::Other(format!("Failed to read backup file: {}", e)))?;

        // Decompress if needed
        let decompressed_data = if self.config.enable_compression {
            self.decompress_data(&file_data)?
        } else {
            file_data
        };

        // Deserialize data
        let mut restored_data = self.deserialize_data(&decompressed_data)?;

        // If incremental backup, restore parent first
        if metadata.backup_type == BackupType::Incremental {
            if let Some(parent_id) = &metadata.parent_backup_id {
                let parent_options = RestoreOptions {
                    backup_id: parent_id.clone(),
                    skip_verification: options.skip_verification,
                    overwrite_existing: false,
                    point_in_time: options.point_in_time,
                };
                // Use Box::pin for recursive async call
                let mut parent_data = Box::pin(self.restore_backup(parent_options)).await?;
                parent_data.extend(restored_data);
                restored_data = parent_data;
            }
        }

        let elapsed = start_time.elapsed().as_millis() as f64;

        // Update statistics
        {
            let mut stats = self.stats.write().await;
            stats.total_restores += 1;
            stats.last_restore = Some(SystemTime::now());

            if stats.total_restores > 1 {
                stats.avg_restore_time_ms =
                    (stats.avg_restore_time_ms * (stats.total_restores - 1) as f64 + elapsed)
                        / stats.total_restores as f64;
            } else {
                stats.avg_restore_time_ms = elapsed;
            }
        }

        info!(
            "Restore completed: {} ({} triples, {:.2}ms)",
            backup_id,
            restored_data.len(),
            elapsed
        );

        Ok(restored_data)
    }

    /// Verify backup integrity
    pub async fn verify_backup(&self, backup_id: &str) -> Result<()> {
        debug!("Verifying backup: {}", backup_id);

        let metadata = {
            let backups = self.backups.read().await;
            backups
                .get(backup_id)
                .cloned()
                .ok_or_else(|| ClusterError::Other(format!("Backup not found: {}", backup_id)))?
        };

        // Read backup file
        let file_data = fs::read(&metadata.file_path)
            .await
            .map_err(|e| ClusterError::Other(format!("Failed to read backup file: {}", e)))?;

        // Verify checksum
        if let Some(expected_checksum) = &metadata.checksum {
            let actual_checksum = self.calculate_checksum(&file_data);
            if &actual_checksum != expected_checksum {
                return Err(ClusterError::Other(format!(
                    "Backup verification failed: checksum mismatch (expected: {}, actual: {})",
                    expected_checksum, actual_checksum
                )));
            }
        }

        // Update status
        {
            let mut backups = self.backups.write().await;
            if let Some(backup) = backups.get_mut(backup_id) {
                backup.status = BackupStatus::Verified;
            }
        }

        debug!("Backup verified successfully: {}", backup_id);
        Ok(())
    }

    /// List all backups
    pub async fn list_backups(&self) -> Vec<BackupMetadata> {
        let backups = self.backups.read().await;
        backups.values().cloned().collect()
    }

    /// Get backup metadata
    pub async fn get_backup_metadata(&self, backup_id: &str) -> Option<BackupMetadata> {
        let backups = self.backups.read().await;
        backups.get(backup_id).cloned()
    }

    /// Delete a backup
    pub async fn delete_backup(&self, backup_id: &str) -> Result<()> {
        info!("Deleting backup: {}", backup_id);

        let metadata = {
            let mut backups = self.backups.write().await;
            backups
                .remove(backup_id)
                .ok_or_else(|| ClusterError::Other(format!("Backup not found: {}", backup_id)))?
        };

        // Delete backup file
        if metadata.file_path.exists() {
            fs::remove_file(&metadata.file_path)
                .await
                .map_err(|e| ClusterError::Other(format!("Failed to delete backup file: {}", e)))?;
        }

        info!("Backup deleted: {}", backup_id);
        Ok(())
    }

    /// Get statistics
    pub async fn get_statistics(&self) -> BackupStatistics {
        self.stats.read().await.clone()
    }

    /// Serialize data based on format
    fn serialize_data(&self, data: &[(String, String, String)]) -> Result<Vec<u8>> {
        match self.config.format {
            BackupFormat::Json => serde_json::to_vec(data)
                .map_err(|e| ClusterError::Serialize(format!("JSON serialization failed: {}", e))),
            BackupFormat::Binary | BackupFormat::CompressedBinary => {
                oxicode::serde::encode_to_vec(&data, oxicode::config::standard()).map_err(|e| {
                    ClusterError::Serialize(format!("Binary serialization failed: {}", e))
                })
            }
        }
    }

    /// Deserialize data based on format
    fn deserialize_data(&self, data: &[u8]) -> Result<Vec<(String, String, String)>> {
        match self.config.format {
            BackupFormat::Json => serde_json::from_slice(data).map_err(|e| {
                ClusterError::Serialize(format!("JSON deserialization failed: {}", e))
            }),
            BackupFormat::Binary | BackupFormat::CompressedBinary => {
                oxicode::serde::decode_from_slice(data, oxicode::config::standard())
                    .map(|(v, _)| v)
                    .map_err(|e| {
                        ClusterError::Serialize(format!("Binary deserialization failed: {}", e))
                    })
            }
        }
    }

    /// Compress data using zstd
    fn compress_data(&self, data: &[u8]) -> Result<Vec<u8>> {
        oxiarc_zstd::encode_all(data, self.config.compression_level)
            .map_err(|e| ClusterError::Other(format!("Compression failed: {}", e)))
    }

    /// Decompress data
    fn decompress_data(&self, data: &[u8]) -> Result<Vec<u8>> {
        oxiarc_zstd::decode_all(data)
            .map_err(|e| ClusterError::Other(format!("Decompression failed: {}", e)))
    }

    /// Calculate checksum (SHA256)
    fn calculate_checksum(&self, data: &[u8]) -> String {
        use sha2::{Digest, Sha256};
        let mut hasher = Sha256::new();
        hasher.update(data);
        format!("{:x}", hasher.finalize())
    }

    /// Load backup metadata from disk
    async fn load_backup_metadata(&self) -> Result<()> {
        // Implementation would scan backup directory and load metadata
        // For now, this is a placeholder
        debug!(
            "Loading backup metadata from {}",
            self.config.backup_dir.display()
        );
        Ok(())
    }

    /// Cleanup old backups based on retention policy
    async fn cleanup_old_backups(&self) -> Result<()> {
        let mut backups = self.backups.write().await;

        // Sort backups by creation time
        let mut backup_list: Vec<_> = backups.values().cloned().collect();
        backup_list.sort_by_key(|b| std::cmp::Reverse(b.created_at));

        // Remove backups exceeding max count
        if backup_list.len() > self.config.max_backups as usize {
            for backup in backup_list.iter().skip(self.config.max_backups as usize) {
                info!("Removing old backup (max count exceeded): {}", backup.id);
                backups.remove(&backup.id);

                // Delete file
                if backup.file_path.exists() {
                    if let Err(e) = fs::remove_file(&backup.file_path).await {
                        warn!("Failed to delete backup file: {}", e);
                    }
                }
            }
        }

        // Remove backups exceeding max age
        let now = SystemTime::now();
        let max_age =
            std::time::Duration::from_secs(self.config.max_backup_age_days as u64 * 24 * 60 * 60);

        for backup in backup_list.iter() {
            if let Ok(age) = now.duration_since(backup.created_at) {
                if age > max_age {
                    info!("Removing old backup (max age exceeded): {}", backup.id);
                    backups.remove(&backup.id);

                    // Delete file
                    if backup.file_path.exists() {
                        if let Err(e) = fs::remove_file(&backup.file_path).await {
                            warn!("Failed to delete backup file: {}", e);
                        }
                    }
                }
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;

    #[tokio::test]
    async fn test_backup_restore_manager_creation() {
        let temp_dir = env::temp_dir().join("oxirs_backup_test_1");
        let config = BackupConfig {
            backup_dir: temp_dir.clone(),
            ..Default::default()
        };

        let manager = BackupRestoreManager::new(config, 1).await;
        assert!(manager.is_ok());

        // Cleanup
        let _ = tokio::fs::remove_dir_all(&temp_dir).await;
    }

    #[tokio::test]
    async fn test_full_backup_creation() {
        let temp_dir = env::temp_dir().join("oxirs_backup_test_2");
        let config = BackupConfig {
            backup_dir: temp_dir.clone(),
            verify_after_backup: false,
            ..Default::default()
        };

        let manager = BackupRestoreManager::new(config, 1).await.unwrap();

        let data = vec![
            ("s1".to_string(), "p1".to_string(), "o1".to_string()),
            ("s2".to_string(), "p2".to_string(), "o2".to_string()),
        ];

        let backup_id = manager.create_full_backup(data.clone()).await;
        assert!(backup_id.is_ok());

        let backup_id = backup_id.unwrap();
        let metadata = manager.get_backup_metadata(&backup_id).await;
        assert!(metadata.is_some());

        let metadata = metadata.unwrap();
        assert_eq!(metadata.backup_type, BackupType::Full);
        assert_eq!(metadata.triple_count, 2);

        // Cleanup
        let _ = tokio::fs::remove_dir_all(&temp_dir).await;
    }

    #[tokio::test]
    async fn test_backup_and_restore() {
        let temp_dir = env::temp_dir().join("oxirs_backup_test_3");
        let config = BackupConfig {
            backup_dir: temp_dir.clone(),
            verify_after_backup: false,
            ..Default::default()
        };

        let manager = BackupRestoreManager::new(config, 1).await.unwrap();

        let original_data = vec![
            ("s1".to_string(), "p1".to_string(), "o1".to_string()),
            ("s2".to_string(), "p2".to_string(), "o2".to_string()),
        ];

        // Create backup
        let backup_id = manager
            .create_full_backup(original_data.clone())
            .await
            .unwrap();

        // Restore backup
        let options = RestoreOptions {
            backup_id,
            skip_verification: true,
            overwrite_existing: true,
            point_in_time: None,
        };

        let restored_data = manager.restore_backup(options).await;
        assert!(restored_data.is_ok());

        let restored_data = restored_data.unwrap();
        assert_eq!(restored_data.len(), original_data.len());

        // Cleanup
        let _ = tokio::fs::remove_dir_all(&temp_dir).await;
    }

    #[tokio::test]
    async fn test_backup_statistics() {
        let temp_dir = env::temp_dir().join("oxirs_backup_test_4");
        let config = BackupConfig {
            backup_dir: temp_dir.clone(),
            verify_after_backup: false,
            ..Default::default()
        };

        let manager = BackupRestoreManager::new(config, 1).await.unwrap();

        let data = vec![("s1".to_string(), "p1".to_string(), "o1".to_string())];

        let _ = manager.create_full_backup(data).await.unwrap();

        let stats = manager.get_statistics().await;
        assert_eq!(stats.total_backups, 1);
        assert!(stats.total_bytes_backed_up > 0);

        // Cleanup
        let _ = tokio::fs::remove_dir_all(&temp_dir).await;
    }
}