TxMap
A concurrent transactional hash map for Rust with fine-grained user-defined locking, internal mutability for easy sharing, and composable transactions.
Features
- Flexible sharding Choose the number of shards (between 8 and 128). Decide how they're locked (Mutex, RwLock or bring your own)
- Transactions Atomic, composable batches of modifications
- Guards/conditions Declarative preconditions that must hold before a transaction runs
- Parameterized transactions Optionally define a parameter type to pass into transaction closures
- Builder API Chain operations to build transactions with a fluent interface
License
Licensed under the MIT License.
Usage
Add txmap to your Cargo.toml:
[]
= "1.1.0"
Creating a TxMap
use *;
// Shard counts available are [8, 16, 32, 64, 128]
let map: = new;
Lock Policy
The lock policy can be set using the with_lock_policy constructor.
Two policies are provided: MutexLockPolicy and RwLockPolicy. The default is MutexLockPolicy.
You can also implement your own policy by implementing LockPolicy.
// Creating a TxMap with a lock policy
let map = ;
Key type requirements
The key type K must implement Hash and Eq. Some functions require Clone.
The value type V has no trait bounds by default. Functions that create default values (e.g., insert_default) require V: Default.
Transactions
Transactions group multiple operations into an atomic unit. They are built using a fluent builder API. Use .into_transaction() to produce a reusable transaction, then .execute() to run it. They can be run as many times as you want within the lifetime of the TxMap it was created from.
Simple transaction
use *;
let db: = new;
db.insert;
db.insert;
// Transfer 50 from alice to bob in one atomic transaction
let result = db.transaction
.modify
.modify
.get_copied
.into_transaction
.execute;
match result
Transaction with guards (preconditions)
Perhaps Alice doesn't have enough funds to make a transfer and you need to prevent a transfer happening.
Guards can be used to veto a transaction if they fail, in which case TxResult::RequirementNotMet is returned.
use *;
let db: = new;
db.insert;
db.insert;
let result = db
.transaction
// Add all your requirements up front
// Requirements cannot be added after modifications
.require
.require
.modify
.modify
.get_copied
.into_transaction
.execute;
match result
Transaction returning a value
As seen already, a completed transaction can optionally return a final value in the result. There are 6 functions like this that can be used
| Final function | Description | Additional bounds required |
|---|---|---|
| none (default) | Returns unit type () | |
get_copied(key) |
Copies a value | V: Copy |
get_all_copied(keys) |
Copies a vec of values | V: Copy |
get_cloned(key) |
Clones a value | V: Clone |
get_all_cloned(keys) |
Clones a vec of values | V: Clone |
get_with(key, |&K, &V| { ... }) |
Transforms a value | |
get_all_with(keys, |&K, &V| { ... }) |
Transforms a vec of values |
Parameterized transactions
A transaction may need to be called with different parameters.
The function with_param::<MyParams>() lets you do this by parameterizing the transaction.
use *;
let db: = new;
db.insert;
db.insert;
let transfer = db
.transaction
// Parameter type is set before requirements
.
.require
.modify
.modify
.get_all_copied
.into_transaction;
// Execute with different parameters
let result1 = transfer.execute;
assert_eq!;
let result2 = transfer.execute;
assert_eq!;
Transaction operations
Transactions are built with a fluent interface, many operations can be chained together atomically into a single transaction. Available transaction operations are as follows
| Transaction operation | Description | Additional required bounds |
|---|---|---|
insert_default |
Insert V::default() for the key. |
K: Clone V: Default |
insert_default_if_absent |
Insert V::default() for the key, only if the key is absent. |
K: Clone V: Default |
insert_with |
Insert a value generated from the key. | K: Clone |
insert_with_if_absent |
Insert a value generated from the key, only if the key is absent. | K: Clone |
modify |
Mutate an existing value in-place. Does nothing if key absent. | |
modify_peek |
Like modify while peeking at other values. |
|
update |
Update a single entry. Return Some(v) to insert/replace, None to remove. |
K: Clone |
update_peek |
Like update while peeking at other values. |
K: Clone |
move_value |
Remove a value from one key and insert it with another key. | K: Clone |
swap_value |
Swap the values of two keys. | K: Clone |
remove |
Remove the given keys. | |
remove_where |
Remove the given keys which also satisfy a condition. |
TxMap operations
All transaction operations are also available on TxMap. In addition there are some operations that require locking the entire map which are only available on TxMap. These are as follows
| TxMap operation | Description |
|---|---|
clear |
Removes all entries |
remove_if |
Removes any entry which satisfies a condition |
retain_only |
Retains only keys specified |
retain_where |
Retains only keys specified which also satisfy a condition |
retain |
Retains any entry which satisfies a condition |