TxMap
A concurrent transactional hash map for Rust with fine-grained user-defined locking, internal mutability for easy sharing, and composable transactions.
Features
- Proven performance One of the fastest concurrent map implementations available
- Flexible sharding Choose the number of shards (between 8 and 128). Decide how they're locked (Mutex, RwLock or bring your own)
- Immediate Transactions Immediately execute an atomic, composable batch of modifications
- Parameterized Transactions Create re-usable transactions for faster parameterized execution
- Guards/conditions Declarative preconditions that must hold before a transaction runs
- Fluent API Chain operations to build or execute transactions with a fluent interface
- Optional serde support Use the
serdefeature to enable it
Benchmarks
Benchmarks for txmap and other similar crates can be found here
License
Licensed under the MIT License.
Usage
Add txmap to your Cargo.toml:
[]
= "2.2.1"
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 use 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.
Immediate Transactions
You may need to run a one-off transaction, for these cases use map.immediate_tx().
use *;
// Create a state struct for the transaction, it must implement Default
let db: = new;
db.insert;
db.insert;
// Use the state struct as the generic type
// The transaction will create an instance to use as the mutable state and return it wrapped in a TxResult
let result = db.
// Transfer 50 from alice to bob in one atomic transaction
.modify
.modify
.execute;
assert!;
Parameterized transactions
Some transaction might need to be run many times, or with different parameters. For these cases create a prepared transaction with map.prepared_tx().
Prepared transactions need a transaction schema, created via the macro tx_schema.
use *;
// The example below will create 4 types to use in the transaction
// 1. Transfer: Contains constants for SCHEMA and all key handles used in the transaction, (from and to)
// 2. TransferKeys: Used to pass in the actual keys for each execution
// 3. TransferParams: Used to pass in the real parameters for each execution
// 4. TransferState: Used by the transaction to store state and is returned as part of the final result
tx_schema!
let db: = new;
db.insert;
db.insert;
let transfer_tx = db
.prepared_tx // pass in the SCHEMA constant
// prepared transactions also pass in your parameters to all closures
.modify
.modify
.into_transaction;
// Execute with different parameters
// Use the ...Keys and ...Params structs created by the `tx_schema` macro
let result1 = transfer_tx.execute;
assert_eq!;
let result2 = transfer_tx.execute;
assert_eq!;
Transaction with guards (preconditions)
Perhaps Alice doesn't have enough funds to make a transfer and you need to prevent a transfer if it would cause a negative balance.
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
.
// Add all your requirements up front
// Requirements cannot be added after modifications
.require
.require
.modify
.modify
.execute;
match result
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 |
|---|---|---|
get |
Reads a value, allows updating state without making any changes. | |
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. | |
move_value |
Remove a value from one key and insert it with another key. | K: Clone |
remove |
Remove the given key. | |
remove_if |
Remove the given key if it also satisfies a condition. | |
swap_value |
Swap the values of two keys. | K: Clone |
update |
Update a single entry. Return Some(v) to insert/replace, None to remove. |
K: Clone |
TxMap operations
All transaction operations (or variations of them) are also available on TxMap. There are also some additional operations which require map level locking and therefore are only available on TxMap. These are as follows
| TxMap operation | Description |
|---|---|
clear |
Removes all entries |
len |
Returns how many entries the map contains |
is_empty |
Returns if the map is empty |
fold |
Performs a fold on all the entries |
retain |
Retains any entry which satisfies a condition |