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
// Copyright (c) 2024-2025 DeepGraph Inc.
// SPDX-License-Identifier: Apache-2.0
//
//! Transaction operation logging for rollback support
//!
//! This module provides a more efficient approach to transaction rollback
//! by logging individual operations rather than keeping full graph snapshots.

use std::collections::HashMap;

use super::state::TransactionId;
use crate::storage::{Edge, Node, Value};

/// Represents an operation that can be undone
#[derive(Debug, Clone)]
pub enum UndoOperation {
    /// A node was inserted - to undo, remove it
    InsertNode { graph_path: String, node_id: String },
    /// A node was updated - to undo, restore old properties
    UpdateNode {
        graph_path: String,
        node_id: String,
        old_properties: HashMap<String, Value>,
        old_labels: Vec<String>,
    },
    /// A node was deleted - to undo, restore it
    DeleteNode {
        graph_path: String,
        node_id: String,
        deleted_node: Node,
    },
    /// An edge was inserted - to undo, remove it
    InsertEdge { graph_path: String, edge_id: String },
    /// An edge was updated - to undo, restore old properties
    UpdateEdge {
        graph_path: String,
        edge_id: String,
        old_properties: HashMap<String, Value>,
        old_label: String,
    },
    /// An edge was deleted - to undo, restore it
    DeleteEdge {
        graph_path: String,
        edge_id: String,
        deleted_edge: Edge,
    },
    /// A batch of operations that must be undone together atomically
    /// Used for multi-property SET operations and other compound statements
    Batch { operations: Vec<UndoOperation> },
}

/// Transaction operation log for a single transaction
#[derive(Debug, Clone)]
#[allow(dead_code)] // ROADMAP v0.3.0 - Transaction undo log for ROLLBACK support
pub struct TransactionLog {
    /// The transaction this log belongs to
    pub transaction_id: TransactionId,
    /// Operations in reverse order (most recent first) for efficient rollback
    pub undo_operations: Vec<UndoOperation>,
    /// Total number of operations logged
    pub operation_count: usize,
    /// Memory usage estimate in bytes
    pub estimated_size_bytes: usize,
}

impl TransactionLog {
    /// Create a new empty transaction log
    pub fn new(transaction_id: TransactionId) -> Self {
        Self {
            transaction_id,
            undo_operations: Vec::new(),
            operation_count: 0,
            estimated_size_bytes: std::mem::size_of::<Self>(),
        }
    }

    /// Add an undo operation to the log
    pub fn log_operation(&mut self, operation: UndoOperation) {
        // Estimate memory usage
        let op_size = match &operation {
            UndoOperation::InsertNode {
                graph_path,
                node_id,
            } => std::mem::size_of::<UndoOperation>() + graph_path.len() + node_id.len(),
            UndoOperation::UpdateNode {
                graph_path,
                node_id,
                old_properties,
                old_labels,
            } => {
                let props_size = old_properties
                    .iter()
                    .map(|(k, v)| k.len() + estimate_value_size(v))
                    .sum::<usize>();
                let labels_size = old_labels.iter().map(|l| l.len()).sum::<usize>();
                std::mem::size_of::<UndoOperation>()
                    + graph_path.len()
                    + node_id.len()
                    + props_size
                    + labels_size
            }
            UndoOperation::DeleteNode {
                graph_path,
                node_id,
                deleted_node,
            } => {
                std::mem::size_of::<UndoOperation>()
                    + graph_path.len()
                    + node_id.len()
                    + estimate_node_size(deleted_node)
            }
            UndoOperation::InsertEdge {
                graph_path,
                edge_id,
            } => std::mem::size_of::<UndoOperation>() + graph_path.len() + edge_id.len(),
            UndoOperation::UpdateEdge {
                graph_path,
                edge_id,
                old_properties,
                old_label,
            } => {
                let props_size = old_properties
                    .iter()
                    .map(|(k, v)| k.len() + estimate_value_size(v))
                    .sum::<usize>();
                let label_size = old_label.len();
                std::mem::size_of::<UndoOperation>()
                    + graph_path.len()
                    + edge_id.len()
                    + props_size
                    + label_size
            }
            UndoOperation::DeleteEdge {
                graph_path,
                edge_id,
                deleted_edge,
            } => {
                std::mem::size_of::<UndoOperation>()
                    + graph_path.len()
                    + edge_id.len()
                    + estimate_edge_size(deleted_edge)
            }
            UndoOperation::Batch { operations } => {
                // For batch operations, sum up the size of all individual operations
                let mut total_size = std::mem::size_of::<UndoOperation>();
                for op in operations {
                    total_size += match op {
                        UndoOperation::InsertNode {
                            graph_path,
                            node_id,
                        } => {
                            std::mem::size_of::<UndoOperation>() + graph_path.len() + node_id.len()
                        }
                        UndoOperation::UpdateNode {
                            graph_path,
                            node_id,
                            old_properties,
                            old_labels,
                        } => {
                            let props_size = old_properties
                                .iter()
                                .map(|(k, v)| k.len() + estimate_value_size(v))
                                .sum::<usize>();
                            let labels_size = old_labels.iter().map(|l| l.len()).sum::<usize>();
                            std::mem::size_of::<UndoOperation>()
                                + graph_path.len()
                                + node_id.len()
                                + props_size
                                + labels_size
                        }
                        UndoOperation::DeleteNode {
                            graph_path,
                            node_id,
                            deleted_node,
                        } => {
                            std::mem::size_of::<UndoOperation>()
                                + graph_path.len()
                                + node_id.len()
                                + estimate_node_size(deleted_node)
                        }
                        UndoOperation::InsertEdge {
                            graph_path,
                            edge_id,
                        } => {
                            std::mem::size_of::<UndoOperation>() + graph_path.len() + edge_id.len()
                        }
                        UndoOperation::UpdateEdge {
                            graph_path,
                            edge_id,
                            old_properties,
                            old_label,
                        } => {
                            let props_size = old_properties
                                .iter()
                                .map(|(k, v)| k.len() + estimate_value_size(v))
                                .sum::<usize>();
                            let label_size = old_label.len();
                            std::mem::size_of::<UndoOperation>()
                                + graph_path.len()
                                + edge_id.len()
                                + props_size
                                + label_size
                        }
                        UndoOperation::DeleteEdge {
                            graph_path,
                            edge_id,
                            deleted_edge,
                        } => {
                            std::mem::size_of::<UndoOperation>()
                                + graph_path.len()
                                + edge_id.len()
                                + estimate_edge_size(deleted_edge)
                        }
                        UndoOperation::Batch { .. } => {
                            // Nested batches - just use base size to avoid infinite recursion
                            std::mem::size_of::<UndoOperation>()
                        }
                    };
                }
                total_size
            }
        };

        self.undo_operations.push(operation);
        self.operation_count += 1;
        self.estimated_size_bytes += op_size;
    }

    /// Get the operations in rollback order (most recent first)
    #[allow(dead_code)] // ROADMAP v0.3.0 - Rollback operation iteration for ROLLBACK execution
    pub fn get_rollback_operations(&self) -> impl Iterator<Item = &UndoOperation> {
        self.undo_operations.iter().rev()
    }

    /// Clear the log (used after commit or rollback)
    #[allow(dead_code)] // ROADMAP v0.3.0 - Clear transaction log after COMMIT/ROLLBACK completion
    pub fn clear(&mut self) {
        self.undo_operations.clear();
        self.operation_count = 0;
        self.estimated_size_bytes = std::mem::size_of::<Self>();
    }

    /// Check if the log is empty
    #[allow(dead_code)] // ROADMAP v0.3.0 - Check if transaction has any undo operations for ROLLBACK decision
    pub fn is_empty(&self) -> bool {
        self.undo_operations.is_empty()
    }

    /// Get statistics about the log
    #[allow(dead_code)] // ROADMAP v0.6.0 - Transaction log statistics for monitoring and observability
    pub fn stats(&self) -> TransactionLogStats {
        TransactionLogStats {
            transaction_id: self.transaction_id,
            operation_count: self.operation_count,
            estimated_size_bytes: self.estimated_size_bytes,
        }
    }
}

/// Statistics about a transaction log
#[derive(Debug, Clone)]
#[allow(dead_code)] // ROADMAP v0.6.0 - Transaction statistics for monitoring
pub struct TransactionLogStats {
    pub transaction_id: TransactionId,
    pub operation_count: usize,
    pub estimated_size_bytes: usize,
}

/// Estimate memory usage of a Value
fn estimate_value_size(value: &Value) -> usize {
    match value {
        Value::Null => 0,
        Value::Boolean(_) => 1,
        Value::Number(_) => 8,
        Value::String(s) => s.len(),
        Value::List(list) | Value::Array(list) => list.iter().map(estimate_value_size).sum(),
        Value::Vector(vec) => vec.len() * 4, // f32 is 4 bytes
        _ => 64, // Rough estimate for complex types like DateTime, TimeWindow, Path
    }
}

/// Estimate memory usage of a Node
fn estimate_node_size(node: &Node) -> usize {
    let id_size = node.id.len();
    let labels_size = node.labels.iter().map(|l| l.len()).sum::<usize>();
    let props_size = node
        .properties
        .iter()
        .map(|(k, v)| k.len() + estimate_value_size(v))
        .sum::<usize>();

    std::mem::size_of::<Node>() + id_size + labels_size + props_size
}

/// Estimate memory usage of an Edge
fn estimate_edge_size(edge: &Edge) -> usize {
    let id_size = edge.id.len();
    let from_size = edge.from_node.len();
    let to_size = edge.to_node.len();
    let label_size = edge.label.len();
    let props_size = edge
        .properties
        .iter()
        .map(|(k, v)| k.len() + estimate_value_size(v))
        .sum::<usize>();

    std::mem::size_of::<Edge>() + id_size + from_size + to_size + label_size + props_size
}

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

    #[test]
    fn test_transaction_log_creation() {
        let txn_id = TransactionId::new();
        let log = TransactionLog::new(txn_id);

        assert_eq!(log.transaction_id, txn_id);
        assert!(log.is_empty());
        assert_eq!(log.operation_count, 0);
    }

    #[test]
    fn test_log_operations() {
        let txn_id = TransactionId::new();
        let mut log = TransactionLog::new(txn_id);

        // Log an insert operation
        log.log_operation(UndoOperation::InsertNode {
            graph_path: "/test_graph".to_string(),
            node_id: "node1".to_string(),
        });

        assert!(!log.is_empty());
        assert_eq!(log.operation_count, 1);
        assert!(log.estimated_size_bytes > std::mem::size_of::<TransactionLog>());
    }

    #[test]
    fn test_rollback_order() {
        let txn_id = TransactionId::new();
        let mut log = TransactionLog::new(txn_id);

        // Log operations in order: insert, update, delete
        log.log_operation(UndoOperation::InsertNode {
            graph_path: "/test_graph".to_string(),
            node_id: "node1".to_string(),
        });
        log.log_operation(UndoOperation::UpdateNode {
            graph_path: "/test_graph".to_string(),
            node_id: "node1".to_string(),
            old_properties: HashMap::new(),
            old_labels: vec![],
        });
        log.log_operation(UndoOperation::DeleteNode {
            graph_path: "/test_graph".to_string(),
            node_id: "node1".to_string(),
            deleted_node: Node {
                id: "node1".to_string(),
                labels: vec![],
                properties: HashMap::new(),
            },
        });

        // Rollback should be in reverse order: delete, update, insert
        let rollback_ops: Vec<_> = log.get_rollback_operations().collect();
        assert_eq!(rollback_ops.len(), 3);

        match rollback_ops[0] {
            UndoOperation::DeleteNode {
                graph_path,
                node_id,
                ..
            } => {
                assert_eq!(graph_path, "/test_graph");
                assert_eq!(node_id, "node1");
            }
            _ => panic!("Expected DeleteNode operation first"),
        }
        match rollback_ops[1] {
            UndoOperation::UpdateNode {
                graph_path,
                node_id,
                ..
            } => {
                assert_eq!(graph_path, "/test_graph");
                assert_eq!(node_id, "node1");
            }
            _ => panic!("Expected UpdateNode operation second"),
        }
        match rollback_ops[2] {
            UndoOperation::InsertNode {
                graph_path,
                node_id,
            } => {
                assert_eq!(graph_path, "/test_graph");
                assert_eq!(node_id, "node1");
            }
            _ => panic!("Expected InsertNode operation third"),
        }
    }
}