0
# Transactions and Undo/Redo
1
2
PyTables provides a complete transaction system with undo/redo capabilities, allowing you to mark specific states, rollback changes, and maintain data integrity through ACID-compliant operations. This system is essential for collaborative workflows and error recovery.
3
4
## Capabilities
5
6
### Transaction Control
7
8
```python { .api }
9
class File:
10
def enable_undo(self, filters=None):
11
"""
12
Enable undo/redo functionality for the file.
13
14
Parameters:
15
- filters (Filters): Compression for undo log
16
"""
17
18
def disable_undo(self):
19
"""Disable undo/redo and remove transaction log."""
20
21
def is_undo_enabled(self):
22
"""
23
Check if undo/redo is enabled.
24
25
Returns:
26
bool: True if undo/redo is enabled
27
"""
28
```
29
30
### Mark Management
31
32
```python { .api }
33
class File:
34
def mark(self, name=None):
35
"""
36
Create a transaction mark at current state.
37
38
Parameters:
39
- name (str): Optional name for the mark
40
41
Returns:
42
int: Mark identifier
43
"""
44
45
def undo(self, mark=None):
46
"""
47
Undo operations back to specified mark.
48
49
Parameters:
50
- mark (int or str): Mark to undo to (None for last mark)
51
"""
52
53
def redo(self, mark=None):
54
"""
55
Redo operations forward to specified mark.
56
57
Parameters:
58
- mark (int or str): Mark to redo to (None for next mark)
59
"""
60
61
def get_current_mark(self):
62
"""
63
Get current transaction mark.
64
65
Returns:
66
int: Current mark identifier
67
"""
68
69
def goto(self, mark):
70
"""
71
Go directly to specified mark.
72
73
Parameters:
74
- mark (int or str): Target mark identifier
75
"""
76
```
77
78
### Mark Information
79
80
```python { .api }
81
class File:
82
def get_marks(self):
83
"""
84
Get list of all available marks.
85
86
Returns:
87
list: Mark identifiers and names
88
"""
89
90
def _undo_log(self):
91
"""Access to internal undo log for advanced operations."""
92
```
93
94
## Usage Examples
95
96
```python
97
import tables as tb
98
import numpy as np
99
100
with tb.open_file("transactional.h5", "w") as h5file:
101
# Enable transactions
102
h5file.enable_undo()
103
104
# Create initial data structure
105
table = h5file.create_table("/", "data", MyDescription)
106
h5file.mark("initial_structure")
107
108
# Add some data
109
for i in range(100):
110
row = table.row
111
row['id'] = i
112
row['value'] = i * 2.0
113
row.append()
114
table.flush()
115
h5file.mark("first_data_batch")
116
117
# Add more data
118
for i in range(100, 200):
119
row = table.row
120
row['id'] = i
121
row['value'] = i * 3.0
122
row.append()
123
table.flush()
124
h5file.mark("second_data_batch")
125
126
# Create additional structures
127
array = h5file.create_array("/", "lookup", np.arange(200))
128
h5file.mark("with_lookup_array")
129
130
# Something went wrong - undo to previous state
131
h5file.undo("second_data_batch")
132
133
# Check current state
134
print(f"Table rows: {table.nrows}") # Should be 100
135
print(f"Array exists: {'lookup' in h5file.root}") # Should be False
136
137
# Redo to restore array
138
h5file.redo("with_lookup_array")
139
140
# List all available marks
141
marks = h5file.get_marks()
142
print(f"Available marks: {marks}")
143
```