graphlite 0.0.1

GraphLite - A lightweight ISO GQL Graph Database
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
// Copyright (c) 2024-2025 DeepGraph Inc.
// SPDX-License-Identifier: Apache-2.0
//
//! WAL Recovery Manager for crash recovery and replay operations

use chrono::{DateTime, Utc};
use std::collections::HashMap;

use super::state::{OperationType, TransactionId};
use super::wal::{PersistentWAL, WALEntry, WALEntryType, WALError};
use crate::storage::StorageManager;

/// Recovery Manager for WAL-based crash recovery
#[allow(dead_code)] // ROADMAP v0.3.0 - WAL-based crash recovery manager (3-phase ARIES-style)
pub struct RecoveryManager {
    /// WAL instance to read from
    wal: PersistentWAL,
    /// Tracks recovered transaction states
    recovered_transactions: HashMap<TransactionId, TransactionRecoveryState>,
    /// Storage manager for applying recovered operations
    storage_manager: Option<StorageManager>,
}

/// State of a transaction during recovery
#[derive(Debug, Clone)]
#[allow(dead_code)] // ROADMAP v0.3.0 - Transaction state tracking during recovery
pub struct TransactionRecoveryState {
    pub transaction_id: TransactionId,
    pub status: RecoveryStatus,
    pub operations: Vec<WALEntry>,
    pub start_time: DateTime<Utc>,
    pub end_time: Option<DateTime<Utc>>,
}

/// Status of a transaction during recovery
#[derive(Debug, Clone, PartialEq)]
#[allow(dead_code)] // ROADMAP v0.3.0 - Recovery status enumeration (InProgress/Committed/RolledBack/NeedsAbort)
pub enum RecoveryStatus {
    /// Transaction was started but not completed
    InProgress,
    /// Transaction was successfully committed
    Committed,
    /// Transaction was rolled back
    RolledBack,
    /// Transaction needs to be aborted during recovery
    NeedsAbort,
}

impl RecoveryManager {
    /// Create a new RecoveryManager with database path
    ///
    /// # Arguments
    /// * `db_path` - The database directory path where WAL files are stored
    #[allow(dead_code)] // ROADMAP v0.3.0 - ARIES-style crash recovery (see ROADMAP.md §3)
    pub fn new(db_path: std::path::PathBuf) -> Self {
        let wal = PersistentWAL::new(db_path).expect("Failed to initialize WAL for recovery");

        Self {
            wal,
            recovered_transactions: HashMap::new(),
            storage_manager: None,
        }
    }

    /// Create RecoveryManager with storage manager
    ///
    /// # Arguments
    /// * `db_path` - The database directory path where WAL files are stored
    /// * `storage_manager` - The storage manager for recovery operations
    #[allow(dead_code)] // ROADMAP v0.3.0 - ARIES-style crash recovery (see ROADMAP.md §3)
    pub fn with_storage(db_path: std::path::PathBuf, storage_manager: StorageManager) -> Self {
        let wal = PersistentWAL::new(db_path).expect("Failed to initialize WAL for recovery");

        Self {
            wal,
            recovered_transactions: HashMap::new(),
            storage_manager: Some(storage_manager),
        }
    }

    /// Perform crash recovery by replaying WAL
    #[allow(dead_code)] // ROADMAP v0.3.0 - Main recovery procedure (Analysis→Redo→Undo phases)
    pub fn recover(&mut self) -> Result<RecoveryReport, RecoveryError> {
        let mut report = RecoveryReport::new();

        // Phase 1: Analysis - Scan WAL to determine transaction states
        self.analysis_phase(&mut report)?;

        // Phase 2: Redo - Replay committed transactions
        self.redo_phase(&mut report)?;

        // Phase 3: Undo - Rollback incomplete transactions
        self.undo_phase(&mut report)?;

        Ok(report)
    }

    /// Analysis phase: scan WAL to determine transaction states
    fn analysis_phase(&mut self, report: &mut RecoveryReport) -> Result<(), RecoveryError> {
        log::debug!("🔍 Starting WAL analysis phase...");

        // Read all WAL files
        let mut file_number = 1u64;
        loop {
            match self.wal.read_wal_file(file_number) {
                Ok(entries) => {
                    report.total_wal_entries += entries.len();

                    for entry in entries {
                        self.process_entry_analysis(entry)?;
                    }

                    file_number += 1;
                }
                Err(WALError::IOError(msg)) if msg.contains("not found") => {
                    // No more WAL files
                    break;
                }
                Err(e) => {
                    return Err(RecoveryError::WALReadError(e.to_string()));
                }
            }
        }

        // Determine which transactions need recovery
        for (txn_id, state) in &self.recovered_transactions {
            match state.status {
                RecoveryStatus::InProgress => {
                    report.incomplete_transactions.push(*txn_id);
                }
                RecoveryStatus::Committed => {
                    report.committed_transactions.push(*txn_id);
                }
                RecoveryStatus::RolledBack => {
                    report.rolled_back_transactions.push(*txn_id);
                }
                _ => {}
            }
        }

        log::debug!(
            "✅ Analysis phase complete: {} transactions to recover",
            report.incomplete_transactions.len() + report.committed_transactions.len()
        );

        Ok(())
    }

    /// Process a WAL entry during analysis
    fn process_entry_analysis(&mut self, entry: WALEntry) -> Result<(), RecoveryError> {
        let txn_id = entry.transaction_id;

        match entry.entry_type {
            WALEntryType::TransactionBegin => {
                let state = TransactionRecoveryState {
                    transaction_id: txn_id,
                    status: RecoveryStatus::InProgress,
                    operations: Vec::new(),
                    start_time: DateTime::<Utc>::from(entry.timestamp),
                    end_time: None,
                };
                self.recovered_transactions.insert(txn_id, state);
            }

            WALEntryType::TransactionCommit => {
                if let Some(state) = self.recovered_transactions.get_mut(&txn_id) {
                    state.status = RecoveryStatus::Committed;
                    state.end_time = Some(DateTime::<Utc>::from(entry.timestamp));
                }
            }

            WALEntryType::TransactionRollback => {
                if let Some(state) = self.recovered_transactions.get_mut(&txn_id) {
                    state.status = RecoveryStatus::RolledBack;
                    state.end_time = Some(DateTime::<Utc>::from(entry.timestamp));
                }
            }

            WALEntryType::TransactionOperation => {
                if let Some(state) = self.recovered_transactions.get_mut(&txn_id) {
                    state.operations.push(entry);
                }
            }
        }

        Ok(())
    }

    /// Redo phase: replay committed transactions
    fn redo_phase(&mut self, report: &mut RecoveryReport) -> Result<(), RecoveryError> {
        log::debug!("🔄 Starting redo phase...");

        let txn_ids = report.committed_transactions.clone();
        for txn_id in &txn_ids {
            // Clone operations to avoid borrow issues
            let operations = if let Some(state) = self.recovered_transactions.get(txn_id) {
                log::debug!("  Replaying committed transaction: {}", txn_id.id());
                state.operations.clone()
            } else {
                continue;
            };

            for operation in &operations {
                // Apply operation if storage manager is available
                if self.storage_manager.is_some() {
                    self.apply_operation(operation)?;
                }
                report.operations_replayed += 1;
            }
        }

        log::debug!(
            "✅ Redo phase complete: {} operations replayed",
            report.operations_replayed
        );
        Ok(())
    }

    /// Undo phase: rollback incomplete transactions
    fn undo_phase(&mut self, report: &mut RecoveryReport) -> Result<(), RecoveryError> {
        log::debug!("↩️ Starting undo phase...");

        let txn_ids = report.incomplete_transactions.clone();
        for txn_id in &txn_ids {
            // Clone operations to avoid borrow issues
            let operations = if let Some(state) = self.recovered_transactions.get(txn_id) {
                log::debug!("  Rolling back incomplete transaction: {}", txn_id.id());
                state.operations.clone()
            } else {
                continue;
            };

            // Process operations in reverse order for undo
            for operation in operations.iter().rev() {
                // Generate compensating operation if needed
                if self.storage_manager.is_some() {
                    self.undo_operation(operation)?;
                }
                report.operations_undone += 1;
            }
        }

        log::debug!(
            "✅ Undo phase complete: {} operations undone",
            report.operations_undone
        );
        Ok(())
    }

    /// Apply a recovered operation
    fn apply_operation(&mut self, entry: &WALEntry) -> Result<(), RecoveryError> {
        // This would integrate with the actual storage operations
        match entry.operation_type {
            Some(OperationType::Insert)
            | Some(OperationType::Update)
            | Some(OperationType::Delete) => {
                // Apply data modification
                // Note: Actual implementation would parse entry.description
                // and apply to storage_manager
                log::debug!(
                    "    Applying: {:?} - {}",
                    entry.operation_type,
                    entry.description
                );
            }

            Some(OperationType::CreateTable)
            | Some(OperationType::CreateGraph)
            | Some(OperationType::DropTable)
            | Some(OperationType::DropGraph) => {
                // Apply schema changes
                log::debug!("    Applying schema change: {:?}", entry.operation_type);
            }

            _ => {
                // Skip non-modifying operations
            }
        }

        Ok(())
    }

    /// Undo a recovered operation
    fn undo_operation(&mut self, entry: &WALEntry) -> Result<(), RecoveryError> {
        // Generate and apply compensating operation
        match entry.operation_type {
            Some(OperationType::Insert) => {
                // Compensate with delete
                log::debug!("    Undoing insert: {}", entry.description);
            }

            Some(OperationType::Delete) => {
                // Compensate with insert (if we have before-image)
                log::debug!("    Undoing delete: {}", entry.description);
            }

            Some(OperationType::Update) => {
                // Compensate with reverse update (if we have before-image)
                log::debug!("    Undoing update: {}", entry.description);
            }

            _ => {
                // Some operations may not need compensation
            }
        }

        Ok(())
    }

    /// Check if a specific transaction was recovered
    #[allow(dead_code)] // ROADMAP v0.3.0 - ARIES-style crash recovery (see ROADMAP.md §3)
    pub fn was_transaction_recovered(&self, txn_id: &TransactionId) -> bool {
        self.recovered_transactions.contains_key(txn_id)
    }

    /// Get the recovery state of a transaction
    #[allow(dead_code)] // ROADMAP v0.3.0 - ARIES-style crash recovery (see ROADMAP.md §3)
    pub fn get_transaction_state(
        &self,
        txn_id: &TransactionId,
    ) -> Option<&TransactionRecoveryState> {
        self.recovered_transactions.get(txn_id)
    }
}

/// Report of recovery operations
#[derive(Debug)]
#[allow(dead_code)] // ROADMAP v0.6.0 - Recovery statistics reporting for observability
pub struct RecoveryReport {
    pub total_wal_entries: usize,
    pub committed_transactions: Vec<TransactionId>,
    pub rolled_back_transactions: Vec<TransactionId>,
    pub incomplete_transactions: Vec<TransactionId>,
    pub operations_replayed: usize,
    pub operations_undone: usize,
    pub recovery_time_ms: u64,
}

impl RecoveryReport {
    fn new() -> Self {
        Self {
            total_wal_entries: 0,
            committed_transactions: Vec::new(),
            rolled_back_transactions: Vec::new(),
            incomplete_transactions: Vec::new(),
            operations_replayed: 0,
            operations_undone: 0,
            recovery_time_ms: 0,
        }
    }
}

/// Recovery-specific errors
#[derive(Debug)]
pub enum RecoveryError {
    WALReadError(String),
    #[allow(dead_code)]
    // ROADMAP v0.3.0 - ARIES-style crash recovery error handling (see ROADMAP.md §3)
    OperationReplayError(String),
    #[allow(dead_code)]
    // ROADMAP v0.3.0 - ARIES-style crash recovery error handling (see ROADMAP.md §3)
    StorageError(String),
}

impl std::fmt::Display for RecoveryError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RecoveryError::WALReadError(msg) => write!(f, "WAL Read Error: {}", msg),
            RecoveryError::OperationReplayError(msg) => {
                write!(f, "Operation Replay Error: {}", msg)
            }
            RecoveryError::StorageError(msg) => write!(f, "Storage Error: {}", msg),
        }
    }
}

impl std::error::Error for RecoveryError {}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_recovery_manager_creation() {
        let temp_dir = TempDir::new().unwrap();
        let manager = RecoveryManager::new(temp_dir.path().to_path_buf());
        assert!(manager.recovered_transactions.is_empty());
    }

    #[test]
    fn test_empty_recovery() {
        // Create temp directory for WAL
        let temp_dir = TempDir::new().unwrap();

        // Create manager with explicit path
        let mut manager = RecoveryManager::new(temp_dir.path().to_path_buf());

        let report = manager.recover().unwrap();
        assert_eq!(report.total_wal_entries, 0);
        assert_eq!(report.committed_transactions.len(), 0);
        assert_eq!(report.incomplete_transactions.len(), 0);
    }
}