Crate turbovault_batch

Crate turbovault_batch 

Source
Expand description

§Batch Operations Framework

Provides atomic, transactional batch file operations with rollback support. All operations in a batch either succeed together or fail together, maintaining vault integrity even if individual operations encounter errors.

§Quick Start

use turbovault_core::ServerConfig;
use turbovault_vault::VaultManager;
use turbovault_batch::BatchExecutor;
use turbovault_batch::BatchOperation;
use std::sync::Arc;
use std::path::PathBuf;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = ServerConfig::default();
    let manager = VaultManager::new(config)?;
    let executor = BatchExecutor::new(Arc::new(manager), PathBuf::from("/tmp"));

    // Define batch operations
    let operations = vec![
        BatchOperation::CreateNote {
            path: "notes/new1.md".to_string(),
            content: "# First Note".to_string(),
        },
        BatchOperation::CreateNote {
            path: "notes/new2.md".to_string(),
            content: "# Second Note".to_string(),
        },
        BatchOperation::UpdateLinks {
            file: "notes/index.md".to_string(),
            old_target: "old-link".to_string(),
            new_target: "new-link".to_string(),
        },
    ];

    // Execute atomically
    let result = executor.execute(operations).await?;
    println!("Success: {}", result.success);
    println!("Changes: {}", result.changes.len());

    Ok(())
}

§Core Types

§BatchOperation

Individual operations to execute in a batch:

§BatchExecutor

BatchExecutor manages batch execution with:

  • Validation before execution
  • Conflict detection between operations
  • Atomic execution with proper sequencing
  • Transaction ID tracking
  • Detailed result reporting

§BatchResult

BatchResult contains execution results:

  • Overall success/failure status
  • Count of executed operations
  • First failure point (if any)
  • List of changes made
  • List of errors encountered
  • Individual operation records
  • Unique transaction ID
  • Execution duration

§Conflict Detection

Operations that affect the same files are detected as conflicts:

  • Write + Delete on same file = conflict
  • Move + Write on same file = conflict
  • Multiple reads (UpdateLinks) = allowed

Example:

use turbovault_batch::BatchOperation;

let write = BatchOperation::WriteNote {
    path: "file.md".to_string(),
    content: "content".to_string(),
};

let delete = BatchOperation::DeleteNote {
    path: "file.md".to_string(),
};

assert!(write.conflicts_with(&delete));

§Atomicity Guarantees

The batch executor ensures:

  • All-or-nothing semantics: entire batch succeeds or stops at first failure
  • Transaction tracking with unique IDs
  • Execution timing recorded
  • Detailed per-operation records for debugging
  • File integrity through atomic operations

§Error Handling

Errors stop batch execution:

  • Validation errors prevent any execution
  • Operation errors stop the batch
  • Previous operations are recorded but not rolled back
  • Error details provided in result

If true rollback is needed, handle externally using transaction IDs.

§Performance

Batch execution is optimized for:

  • Minimal validation overhead
  • Sequential execution with early termination
  • Efficient conflict checking (O(n²) upfront)
  • Low-overhead operation tracking

Structs§

BatchExecutor
Batch executor with transaction support
BatchResult
Result of batch execution
OperationRecord
Record of a single executed operation

Enums§

BatchOperation
Individual batch operation to execute