Atomic Operations
This section documents the atomic operations and transaction management functions used throughout the NDF Studio backend.
backend.core.atomic_ops
Atomic Operations Module for NDF Studio
This module provides atomic file operations and transaction support to ensure data integrity across all NDF Studio operations. It implements:
- Atomic file writes using temporary files and atomic renames
- Transaction contexts for multi-file operations
- Rollback mechanisms for failed operations
- Validation and consistency checks
- Backup and restore functionality
All data modifications in NDF Studio should go through this module to ensure atomicity and data integrity.
Classes
AtomicityError
ConsistencyError
Functions
atomic_write(file_path: Path, mode='w', encoding='utf-8')
Atomic file write using temporary file and rename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Path
|
Path to the target file |
required |
mode
|
File open mode (default: 'w') |
'w'
|
|
encoding
|
File encoding (default: 'utf-8') |
'utf-8'
|
Yields:
| Type | Description |
|---|---|
|
File object for writing |
Raises:
| Type | Description |
|---|---|
AtomicityError
|
If the atomic write fails |
Source code in backend/core/atomic_ops.py
save_json_file_atomic(file_path: Path, data: Dict[str, Any], backup_dir: Optional[Path] = None) -> None
Atomically save JSON data to a file with backup and rollback capability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Path
|
Path to the target file |
required |
data
|
Dict[str, Any]
|
JSON data to save |
required |
backup_dir
|
Optional[Path]
|
Directory to store backups (optional) |
None
|
Source code in backend/core/atomic_ops.py
load_json_file(file_path: Path) -> Dict[str, Any]
Load JSON data from a file with error handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Path
|
Path to the JSON file |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary containing the JSON data |
Raises:
| Type | Description |
|---|---|
AtomicityError
|
If file cannot be loaded |
Source code in backend/core/atomic_ops.py
atomic_registry_save(user_id: str, registry_type: str, registry_data: Dict[str, Any], backup_dir: Optional[Path] = None) -> None
Atomically save registry data with proper error handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
str
|
User identifier |
required |
registry_type
|
str
|
Type of registry (node, relation, attribute) |
required |
registry_data
|
Dict[str, Any]
|
Registry data to save |
required |
backup_dir
|
Optional[Path]
|
Directory for backups |
None
|
Source code in backend/core/atomic_ops.py
graph_transaction(user_id: str, graph_id: str, operation_name: str) -> Generator[Path, None, None]
Context manager for atomic graph operations with backup and rollback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
str
|
User identifier |
required |
graph_id
|
str
|
Graph identifier |
required |
operation_name
|
str
|
Name of the operation for logging |
required |
Yields:
| Type | Description |
|---|---|
Path
|
Path to backup directory |
Raises:
| Type | Description |
|---|---|
AtomicityError
|
If transaction fails |
Source code in backend/core/atomic_ops.py
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 | |
validate_data_consistency(user_id: str) -> Dict[str, Any]
Validate data consistency across registries and files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
str
|
User identifier |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with validation results |
Source code in backend/core/atomic_ops.py
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 | |
cleanup_old_backups(user_id: str, max_age_hours: int = 24) -> int
Clean up old backup directories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
str
|
User identifier |
required |
max_age_hours
|
int
|
Maximum age of backups to keep |
24
|
Returns:
| Type | Description |
|---|---|
int
|
Number of backups cleaned up |
Source code in backend/core/atomic_ops.py
atomic_node_save(user_id: str, node_id: str, node_data: Dict[str, Any]) -> None
Atomically save a node file.
Source code in backend/core/atomic_ops.py
atomic_relation_save(user_id: str, relation_id: str, relation_data: Dict[str, Any]) -> None
Atomically save a relation file.
Source code in backend/core/atomic_ops.py
atomic_attribute_save(user_id: str, attribute_id: str, attribute_data: Dict[str, Any]) -> None
Atomically save an attribute file.
Source code in backend/core/atomic_ops.py
atomic_composed_save(user_id: str, graph_id: str, composed_data: Dict[str, Any], format_type: str = 'json') -> None
Atomically save a composed file.