1use crate::TransactionId;
2use std::time::Duration;
3
4#[derive(PartialEq, Clone, Debug, Eq, Default)]
7pub enum TxStrategy {
8 #[default]
12 LastWin,
13
14 VersionOnWrite,
22
23 VersionOnRead,
31}
32
33impl TxStrategy {
34 pub fn value(&self) -> u8 {
35 match *self {
36 TxStrategy::LastWin => 1,
37 TxStrategy::VersionOnWrite => 2,
38 TxStrategy::VersionOnRead => 3,
39 }
40 }
41 pub fn from_value(val: u8) -> TxStrategy {
42 match val {
43 1 => TxStrategy::LastWin,
44 2 => TxStrategy::VersionOnWrite,
45 3 => TxStrategy::VersionOnRead,
46 _ => panic!("something went wrong in tx strategy serialization: {}", val),
47 }
48 }
49}
50
51#[derive(Debug, Clone)]
62pub struct Config {
63 cache_size: u64,
64 cache_age_limit: Duration,
65 transaction_lock_timeout: Duration,
66 tx_strategy: TxStrategy,
67}
68
69impl Config {
70 pub fn new() -> Config {
71 Config {
72 cache_size: 32 * 1024 * 1024,
73 transaction_lock_timeout: Duration::new(24 * 60 * 60, 0),
74 cache_age_limit: Duration::from_secs(60 * 60 * 24),
75 tx_strategy: TxStrategy::LastWin,
76 }
77 }
78
79 pub fn cache_size(&self) -> u64 {
80 self.cache_size
81 }
82
83 pub fn cache_age_limit(&self) -> Duration {
84 self.cache_age_limit
85 }
86
87 pub fn transaction_lock_timeout(&self) -> &Duration {
88 &self.transaction_lock_timeout
89 }
90
91 pub fn change_cache_size(&mut self, cache_size: u64) {
92 self.cache_size = cache_size;
93 }
94
95 pub fn change_cache_age_limit(&mut self, cache_age_limit: Duration) {
96 self.cache_age_limit = cache_age_limit;
97 }
98
99 pub fn change_transaction_lock_timeout(&mut self, transaction_lock_timeout: Duration) {
100 self.transaction_lock_timeout = transaction_lock_timeout;
101 }
102
103 pub fn tx_strategy(&self) -> &TxStrategy {
104 &self.tx_strategy
105 }
106
107 pub fn change_tx_strategy(&mut self, strategy: TxStrategy) {
108 self.tx_strategy = strategy;
109 }
110}
111
112impl Default for Config {
113 fn default() -> Self {
114 Self::new()
115 }
116}
117
118#[derive(Clone, Default)]
120pub struct TransactionConfig {
121 pub(crate) tx_strategy: Option<TxStrategy>,
122 pub(crate) background_sync: Option<bool>,
123 pub(crate) transaction_id: Option<TransactionId>,
124}
125
126impl TransactionConfig {
127 pub fn new() -> Self {
128 Self {
129 tx_strategy: None,
130 background_sync: None,
131 transaction_id: None,
132 }
133 }
134 pub fn set_strategy(mut self, strategy: TxStrategy) -> Self {
136 self.tx_strategy = Some(strategy);
137 self
138 }
139
140 #[cfg(feature = "background_ops")]
143 pub fn set_background_sync(mut self, background: bool) -> Self {
144 self.background_sync = Some(background);
145 self
146 }
147
148 pub fn set_transaction_id(mut self, transaction_id: TransactionId) -> Self {
152 self.transaction_id = Some(transaction_id);
153 self
154 }
155}