1
|
|
use crate::TransactionId;
|
2
|
|
use std::time::Duration;
|
3
|
|
|
4
|
|
/// Concurrent Modification Strategy for resolution of conflict on commit.
|
5
|
|
///
|
6
|
1
|
#[derive(PartialEq, Clone, Debug)]
|
7
|
|
pub enum TxStrategy {
|
8
|
|
///
|
9
|
|
/// Last modification received override all the previous modifications
|
10
|
|
///
|
11
|
|
LastWin,
|
12
|
|
|
13
|
|
///
|
14
|
|
/// prepare_commit will fail if the persistent version is more recent of the version when
|
15
|
|
/// the update_record/delete_record is executed
|
16
|
|
///
|
17
|
|
VersionOnWrite,
|
18
|
|
|
19
|
|
///
|
20
|
|
/// prepare_commit will fail if the persistent version is more recent of the version of the
|
21
|
|
/// last read_record_tx, if no read_record_tx was called will fallow the same behaviour of
|
22
|
|
/// VersionOnWrite
|
23
|
|
///
|
24
|
|
VersionOnRead,
|
25
|
|
}
|
26
|
|
impl Default for TxStrategy {
|
27
|
1
|
fn default() -> Self {
|
28
|
|
TxStrategy::LastWin
|
29
|
1
|
}
|
30
|
|
}
|
31
|
|
|
32
|
|
impl TxStrategy {
|
33
|
1
|
pub fn value(&self) -> u8 {
|
34
|
1
|
match *self {
|
35
|
1
|
TxStrategy::LastWin => 1,
|
36
|
1
|
TxStrategy::VersionOnWrite => 2,
|
37
|
1
|
TxStrategy::VersionOnRead => 3,
|
38
|
|
}
|
39
|
1
|
}
|
40
|
1
|
pub fn from_value(val: u8) -> TxStrategy {
|
41
|
1
|
match val {
|
42
|
1
|
1 => TxStrategy::LastWin,
|
43
|
0
|
2 => TxStrategy::VersionOnWrite,
|
44
|
0
|
3 => TxStrategy::VersionOnRead,
|
45
|
0
|
_ => panic!("something went wrong in tx strategy serialization: {}", val),
|
46
|
|
}
|
47
|
1
|
}
|
48
|
|
}
|
49
|
|
|
50
|
|
/// Persy configuration structure.
|
51
|
|
///
|
52
|
|
/// Lock are taken in order, should never go in deadlock so the default timeout is huge.
|
53
|
|
/// Current default values:
|
54
|
|
///
|
55
|
|
/// cache_size = 32M
|
56
|
|
/// transaction_lock_timeout = 1 Day
|
57
|
|
/// concurrent_modification_strategy = LastWin
|
58
|
|
///
|
59
|
1
|
#[derive(Debug, Clone)]
|
60
|
|
pub struct Config {
|
61
|
1
|
cache_size: u64,
|
62
|
1
|
transaction_lock_timeout: Duration,
|
63
|
1
|
tx_strategy: TxStrategy,
|
64
|
|
}
|
65
|
|
|
66
|
|
impl Config {
|
67
|
1
|
pub fn new() -> Config {
|
68
|
1
|
Config {
|
69
|
|
cache_size: 32 * 1024 * 1024,
|
70
|
1
|
transaction_lock_timeout: Duration::new(24 * 60 * 60, 0),
|
71
|
|
tx_strategy: TxStrategy::LastWin,
|
72
|
|
}
|
73
|
1
|
}
|
74
|
|
|
75
|
1
|
pub fn cache_size(&self) -> u64 {
|
76
|
1
|
self.cache_size
|
77
|
1
|
}
|
78
|
|
|
79
|
1
|
pub fn transaction_lock_timeout(&self) -> &Duration {
|
80
|
1
|
&self.transaction_lock_timeout
|
81
|
1
|
}
|
82
|
|
|
83
|
|
pub fn change_cache_size(&mut self, cache_size: u64) {
|
84
|
|
self.cache_size = cache_size;
|
85
|
|
}
|
86
|
|
|
87
|
|
pub fn change_transaction_lock_timeout(&mut self, transaction_lock_timeout: Duration) {
|
88
|
|
self.transaction_lock_timeout = transaction_lock_timeout;
|
89
|
|
}
|
90
|
|
|
91
|
1
|
pub fn tx_strategy(&self) -> &TxStrategy {
|
92
|
1
|
&self.tx_strategy
|
93
|
1
|
}
|
94
|
|
|
95
|
1
|
pub fn change_tx_strategy(&mut self, strategy: TxStrategy) {
|
96
|
1
|
self.tx_strategy = strategy;
|
97
|
1
|
}
|
98
|
|
}
|
99
|
|
|
100
|
|
impl Default for Config {
|
101
|
|
fn default() -> Self {
|
102
|
|
Self::new()
|
103
|
|
}
|
104
|
|
}
|
105
|
|
|
106
|
|
/// Configure the parameters for the transaction on the begin of a new transaction.
|
107
|
|
#[derive(Clone)]
|
108
|
|
pub struct TransactionConfig {
|
109
|
|
pub(crate) tx_strategy: Option<TxStrategy>,
|
110
|
|
pub(crate) background_sync: Option<bool>,
|
111
|
|
pub(crate) transaction_id: Option<TransactionId>,
|
112
|
|
}
|
113
|
|
|
114
|
|
impl TransactionConfig {
|
115
|
1
|
pub fn new() -> Self {
|
116
|
1
|
Self {
|
117
|
|
tx_strategy: None,
|
118
|
|
background_sync: None,
|
119
|
1
|
transaction_id: None,
|
120
|
|
}
|
121
|
1
|
}
|
122
|
|
/// Set the transaction concurrency checks, **experimental** use carefully
|
123
|
|
pub fn set_strategy(mut self, strategy: TxStrategy) -> Self {
|
124
|
|
self.tx_strategy = Some(strategy);
|
125
|
|
self
|
126
|
|
}
|
127
|
|
|
128
|
|
/// Set if the transaction will be fsync-ed in background or on the current thread
|
129
|
|
/// this option is available only if the "background_ops" feature is enabled
|
130
|
|
#[cfg(feature = "background_ops")]
|
131
|
1
|
pub fn set_background_sync(mut self, background: bool) -> Self {
|
132
|
1
|
self.background_sync = Some(background);
|
133
|
1
|
self
|
134
|
1
|
}
|
135
|
|
|
136
|
|
/// Set the transaction id to be used in case of crash recovery.
|
137
|
|
pub fn set_transaction_id(mut self, transaction_id: TransactionId) -> Self {
|
138
|
|
self.transaction_id = Some(transaction_id);
|
139
|
|
self
|
140
|
|
}
|
141
|
|
}
|