Skip to main content

Module transaction

Module transaction 

Source
Expand description

Idempotency + staging-area bookkeeping for file-mutating tools. Two-phase commit transaction manager for file write operations.

TransactionManager implements StagingBackend from brainwires-core.

§Protocol

  1. Stage — calls to TransactionManager::stage write content to a temporary directory with a key-addressed filename. The target path is not touched.

  2. CommitTransactionManager::commit atomically renames each staged file to its target path. On cross-filesystem moves a copy+delete fallback is used. Parent directories are created as needed.

  3. RollbackTransactionManager::rollback deletes all staged files from the temp dir without touching any target path.

A TransactionManager is single-use per transaction: after commit() or rollback() the queue is empty and new stages can be accepted.

§Usage

use std::sync::Arc;
use brainwires_tool_runtime::transaction::TransactionManager;
use brainwires_core::{StagedWrite, ToolContext};

let mgr = Arc::new(TransactionManager::new().unwrap());
let ctx = ToolContext::default().with_staging_backend(mgr.clone());

// … execute file tools against `ctx` — writes are staged, not applied …

mgr.commit().unwrap(); // atomically write all staged files
// or mgr.rollback();  // discard everything

Structs§

TransactionManager
Filesystem-backed two-phase commit transaction manager.