Crate txn_lock

source ·
Expand description

A futures-aware read-write lock which supports transaction-specific versioning

The value to lock must implement Clone since the lock keeps track of past versions after committing. Call finalize(txn_id) to clear past versions which are older than both txn_id and the most recent commit.

Example:

use futures::executor::block_on;
use txn_lock::*;

let lock = TxnLock::new("example", 0, "zero");

assert_eq!(*lock.try_read(0).expect("read"), "zero");
assert_eq!(lock.try_write(1).unwrap_err(), Error::WouldBlock);

{
    let commit = block_on(lock.commit(0)).expect("commit guard");
    assert_eq!(*commit, "zero");
    // this commit guard will block future commits until dropped
}

{
    let mut guard = lock.try_write(1).expect("write lock");
    *guard = "one";
}

assert_eq!(*lock.try_read(0).expect("read past version"), "zero");
assert_eq!(*lock.try_read(1).expect("read current version"), "one");

block_on(lock.commit(1));

assert_eq!(*lock.try_read_exclusive(2).expect("new value"), "one");

lock.rollback(&2);

{
    let mut guard = lock.try_write(3).expect("write lock");
    *guard = "three";
}

assert_eq!(*block_on(lock.finalize(&1)).expect("finalized version"), "one");

assert_eq!(lock.try_read(0).unwrap_err(), Error::Outdated);
assert_eq!(*lock.try_read(3).expect("current value"), "three");

Structs

A futures-aware read-write lock which supports transaction-specific versioning
A RAII guard which blocks commits until dropped, allowing access to the just-committed value
Guard allowing access to a finalized value
A read-only view of a TxnLock at a specific TxnId
An exclusive, upgradable read-only view of a TxnLock at a specific TxnId
Guard allowing access to a rolled-back value
An exclusive mutable view of a TxnLock at a specific TxnId

Enums

An error which may occur when attempting to acquire a transactional lock