oxirs 0.2.4

Command-line interface for OxiRS - import, export, migration, and benchmarking tools
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
//! Point-in-Time Recovery (PITR) Module
//!
//! Transaction log-based recovery system for OxiRS databases.
//! Enables restoration to specific timestamps or transaction IDs.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::fs::{self, File};
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::{Path, PathBuf};

/// Transaction log entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionLogEntry {
    pub transaction_id: u64,
    pub timestamp: DateTime<Utc>,
    pub operation_type: OperationType,
    pub data: Vec<u8>,
    pub checksum: String,
}

/// Types of operations in transaction log
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum OperationType {
    Insert,
    Delete,
    Update,
    BeginTransaction,
    CommitTransaction,
    RollbackTransaction,
}

/// PITR configuration
pub struct PitrConfig {
    pub log_dir: PathBuf,
    pub archive_dir: PathBuf,
    pub max_log_size: u64,
    pub auto_archive: bool,
}

/// Transaction log manager
pub struct TransactionLog {
    config: PitrConfig,
    current_log: Option<BufWriter<File>>,
    next_transaction_id: u64,
}

impl TransactionLog {
    /// Create a new transaction log
    pub fn new(config: PitrConfig) -> Result<Self, Box<dyn std::error::Error>> {
        // Ensure directories exist
        fs::create_dir_all(&config.log_dir)?;
        fs::create_dir_all(&config.archive_dir)?;

        // Load next transaction ID
        let next_transaction_id = Self::get_next_transaction_id(&config.log_dir)?;

        Ok(Self {
            config,
            current_log: None,
            next_transaction_id,
        })
    }

    /// Append a transaction entry to the log
    pub fn append(
        &mut self,
        operation_type: OperationType,
        data: Vec<u8>,
    ) -> Result<u64, Box<dyn std::error::Error>> {
        // Ensure log file is open
        if self.current_log.is_none() {
            self.open_current_log()?;
        }

        let transaction_id = self.next_transaction_id;
        self.next_transaction_id += 1;

        let entry = TransactionLogEntry {
            transaction_id,
            timestamp: Utc::now(),
            operation_type,
            data: data.clone(),
            checksum: Self::calculate_checksum(&data),
        };

        // Serialize and write entry
        let entry_json = serde_json::to_string(&entry)?;
        if let Some(ref mut log) = self.current_log {
            writeln!(log, "{}", entry_json)?;
            log.flush()?;
        }

        // Check if log needs rotation
        if self.should_rotate_log()? {
            self.rotate_log()?;
        }

        Ok(transaction_id)
    }

    /// Recover to a specific point in time
    pub fn recover_to_timestamp(
        &self,
        target_timestamp: DateTime<Utc>,
        _output_dir: &Path,
    ) -> Result<usize, Box<dyn std::error::Error>> {
        println!("Starting Point-in-Time Recovery");
        println!("Target timestamp: {}", target_timestamp.to_rfc3339());

        // Collect all log files (current + archived)
        let mut log_files = self.collect_log_files()?;
        log_files.sort();

        let mut applied_transactions = 0;

        // Replay transactions up to the target timestamp
        for log_file in log_files {
            applied_transactions += self.replay_log_file(&log_file, Some(target_timestamp))?;
        }

        println!(
            "Recovery complete: {} transactions applied",
            applied_transactions
        );
        Ok(applied_transactions)
    }

    /// Recover to a specific transaction ID
    pub fn recover_to_transaction(
        &self,
        target_transaction_id: u64,
        _output_dir: &Path,
    ) -> Result<usize, Box<dyn std::error::Error>> {
        println!("Starting Point-in-Time Recovery");
        println!("Target transaction ID: {}", target_transaction_id);

        let mut log_files = self.collect_log_files()?;
        log_files.sort();

        let mut applied_transactions = 0;

        for log_file in log_files {
            let file = File::open(&log_file)?;
            let reader = BufReader::new(file);

            for line in reader.lines() {
                let line = line?;
                if line.trim().is_empty() {
                    continue;
                }

                let entry: TransactionLogEntry = serde_json::from_str(&line)?;

                if entry.transaction_id > target_transaction_id {
                    // Stop when we exceed target transaction ID
                    println!("Reached target transaction ID");
                    return Ok(applied_transactions);
                }

                // Verify checksum
                if !Self::verify_checksum(&entry) {
                    eprintln!(
                        "Warning: Checksum mismatch for transaction {}",
                        entry.transaction_id
                    );
                    continue;
                }

                // Apply transaction (placeholder - actual implementation would replay to store)
                applied_transactions += 1;
            }
        }

        Ok(applied_transactions)
    }

    /// Create an incremental backup checkpoint
    pub fn create_checkpoint(&self, name: &str) -> Result<PathBuf, Box<dyn std::error::Error>> {
        let checkpoint_file = self
            .config
            .archive_dir
            .join(format!("checkpoint_{}.json", name));

        let checkpoint = CheckpointMetadata {
            name: name.to_string(),
            timestamp: Utc::now(),
            last_transaction_id: self.next_transaction_id - 1,
            log_files: self.collect_log_files()?,
        };

        let mut file = File::create(&checkpoint_file)?;
        serde_json::to_writer_pretty(&mut file, &checkpoint)?;

        println!("Checkpoint created: {}", checkpoint_file.display());
        Ok(checkpoint_file)
    }

    /// List all available checkpoints
    pub fn list_checkpoints(&self) -> Result<Vec<CheckpointMetadata>, Box<dyn std::error::Error>> {
        let mut checkpoints: Vec<CheckpointMetadata> = Vec::new();

        for entry in fs::read_dir(&self.config.archive_dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.extension().and_then(|s| s.to_str()) == Some("json")
                && path
                    .file_name()
                    .and_then(|s| s.to_str())
                    .map(|s| s.starts_with("checkpoint_"))
                    .unwrap_or(false)
            {
                let file = File::open(&path)?;
                if let Ok(checkpoint) = serde_json::from_reader(file) {
                    checkpoints.push(checkpoint);
                }
            }
        }

        checkpoints.sort_by_key(|item| item.timestamp);
        Ok(checkpoints)
    }

    /// Archive old transaction logs
    pub fn archive_logs(&mut self) -> Result<usize, Box<dyn std::error::Error>> {
        // Close current log
        self.close_current_log()?;

        let mut archived = 0;

        // Move all .wal files to archive
        for entry in fs::read_dir(&self.config.log_dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.extension().and_then(|s| s.to_str()) == Some("wal") {
                let filename = path.file_name().expect("file path should have a file name");
                let archive_path = self.config.archive_dir.join(filename);

                fs::rename(&path, &archive_path)?;
                archived += 1;
            }
        }

        println!("Archived {} log file(s)", archived);
        Ok(archived)
    }

    // === Private helper methods ===

    fn open_current_log(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        let log_path = self
            .config
            .log_dir
            .join(format!("transaction_{}.wal", Utc::now().timestamp()));

        let file = File::options().create(true).append(true).open(&log_path)?;

        self.current_log = Some(BufWriter::new(file));
        Ok(())
    }

    fn close_current_log(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        if let Some(mut log) = self.current_log.take() {
            log.flush()?;
        }
        Ok(())
    }

    fn should_rotate_log(&self) -> Result<bool, Box<dyn std::error::Error>> {
        // Rotation based on file size
        for entry in fs::read_dir(&self.config.log_dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.extension().and_then(|s| s.to_str()) == Some("wal") {
                let metadata = fs::metadata(&path)?;
                if metadata.len() >= self.config.max_log_size {
                    return Ok(true);
                }
            }
        }
        Ok(false)
    }

    fn rotate_log(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        // Close current log
        self.close_current_log()?;

        // Archive if enabled
        if self.config.auto_archive {
            self.archive_logs()?;
        }

        // Open new log
        self.open_current_log()?;

        Ok(())
    }

    fn collect_log_files(&self) -> Result<Vec<PathBuf>, Box<dyn std::error::Error>> {
        let mut log_files = Vec::new();

        // Collect from log directory
        if self.config.log_dir.exists() {
            for entry in fs::read_dir(&self.config.log_dir)? {
                let entry = entry?;
                let path = entry.path();
                if path.extension().and_then(|s| s.to_str()) == Some("wal") {
                    log_files.push(path);
                }
            }
        }

        // Collect from archive directory
        if self.config.archive_dir.exists() {
            for entry in fs::read_dir(&self.config.archive_dir)? {
                let entry = entry?;
                let path = entry.path();
                if path.extension().and_then(|s| s.to_str()) == Some("wal") {
                    log_files.push(path);
                }
            }
        }

        Ok(log_files)
    }

    fn replay_log_file(
        &self,
        log_file: &Path,
        until_timestamp: Option<DateTime<Utc>>,
    ) -> Result<usize, Box<dyn std::error::Error>> {
        let file = File::open(log_file)?;
        let reader = BufReader::new(file);
        let mut replayed = 0;

        for line in reader.lines() {
            let line = line?;
            if line.trim().is_empty() {
                continue;
            }

            let entry: TransactionLogEntry = serde_json::from_str(&line)?;

            // Stop if we've passed the target timestamp
            if let Some(target) = until_timestamp {
                if entry.timestamp > target {
                    return Ok(replayed);
                }
            }

            // Verify checksum
            if !Self::verify_checksum(&entry) {
                eprintln!(
                    "Warning: Checksum mismatch for transaction {}",
                    entry.transaction_id
                );
                continue;
            }

            // Apply transaction (placeholder)
            replayed += 1;
        }

        Ok(replayed)
    }

    fn get_next_transaction_id(log_dir: &Path) -> Result<u64, Box<dyn std::error::Error>> {
        let mut max_id = 0u64;

        if !log_dir.exists() {
            return Ok(1);
        }

        // Scan all log files to find highest transaction ID
        for entry in fs::read_dir(log_dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.extension().and_then(|s| s.to_str()) == Some("wal") {
                let file = File::open(&path)?;
                let reader = BufReader::new(file);

                for line in reader.lines() {
                    let line = line?;
                    if line.trim().is_empty() {
                        continue;
                    }

                    if let Ok(entry) = serde_json::from_str::<TransactionLogEntry>(&line) {
                        max_id = max_id.max(entry.transaction_id);
                    }
                }
            }
        }

        Ok(max_id + 1)
    }

    fn calculate_checksum(data: &[u8]) -> String {
        use ring::digest;
        let hash = digest::digest(&digest::SHA256, data);
        hex::encode(hash.as_ref())
    }

    fn verify_checksum(entry: &TransactionLogEntry) -> bool {
        let calculated = Self::calculate_checksum(&entry.data);
        calculated == entry.checksum
    }
}

/// Checkpoint metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckpointMetadata {
    pub name: String,
    pub timestamp: DateTime<Utc>,
    pub last_transaction_id: u64,
    pub log_files: Vec<PathBuf>,
}

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

    #[test]
    fn test_transaction_log_creation() {
        let temp_log_dir = temp_dir().join("oxirs_pitr_test_logs");
        let temp_archive_dir = temp_dir().join("oxirs_pitr_test_archives");

        let config = PitrConfig {
            log_dir: temp_log_dir.clone(),
            archive_dir: temp_archive_dir.clone(),
            max_log_size: 10_000_000, // 10MB
            auto_archive: false,
        };

        let log = TransactionLog::new(config).unwrap();
        assert_eq!(log.next_transaction_id, 1);

        // Cleanup
        let _ = fs::remove_dir_all(temp_log_dir);
        let _ = fs::remove_dir_all(temp_archive_dir);
    }

    #[test]
    fn test_transaction_append() {
        let temp_log_dir = temp_dir().join("oxirs_pitr_test_logs2");
        let temp_archive_dir = temp_dir().join("oxirs_pitr_test_archives2");

        let config = PitrConfig {
            log_dir: temp_log_dir.clone(),
            archive_dir: temp_archive_dir.clone(),
            max_log_size: 10_000_000,
            auto_archive: false,
        };

        let mut log = TransactionLog::new(config).unwrap();

        // Append several transactions
        let tx1 = log
            .append(OperationType::Insert, b"test data 1".to_vec())
            .unwrap();
        let tx2 = log
            .append(OperationType::Update, b"test data 2".to_vec())
            .unwrap();
        let tx3 = log
            .append(OperationType::Delete, b"test data 3".to_vec())
            .unwrap();

        assert_eq!(tx1, 1);
        assert_eq!(tx2, 2);
        assert_eq!(tx3, 3);

        // Cleanup
        let _ = fs::remove_dir_all(temp_log_dir);
        let _ = fs::remove_dir_all(temp_archive_dir);
    }

    #[test]
    fn test_checkpoint_creation() {
        let temp_log_dir = temp_dir().join("oxirs_pitr_test_logs3");
        let temp_archive_dir = temp_dir().join("oxirs_pitr_test_archives3");

        let config = PitrConfig {
            log_dir: temp_log_dir.clone(),
            archive_dir: temp_archive_dir.clone(),
            max_log_size: 10_000_000,
            auto_archive: false,
        };

        let mut log = TransactionLog::new(config).unwrap();

        // Add some transactions
        log.append(OperationType::Insert, b"data1".to_vec())
            .unwrap();
        log.append(OperationType::Insert, b"data2".to_vec())
            .unwrap();

        // Create checkpoint
        let checkpoint_path = log.create_checkpoint("test_checkpoint").unwrap();
        assert!(checkpoint_path.exists());

        // List checkpoints
        let checkpoints = log.list_checkpoints().unwrap();
        assert_eq!(checkpoints.len(), 1);
        assert_eq!(checkpoints[0].name, "test_checkpoint");

        // Cleanup
        let _ = fs::remove_dir_all(temp_log_dir);
        let _ = fs::remove_dir_all(temp_archive_dir);
    }
}