persy/
config.rs

1use crate::TransactionId;
2use std::time::Duration;
3
4/// Concurrent Modification Strategy for resolution of conflict on commit.
5///
6#[derive(PartialEq, Clone, Debug, Eq, Default)]
7pub enum TxStrategy {
8    ///
9    /// Last modification received override all the previous modifications
10    ///
11    #[default]
12    LastWin,
13
14    ///
15    /// prepare_commit will fail if the persistent version is more recent of the version when
16    /// the [`update`] or [`delete`] is executed
17    ///
18    /// [`update`]: struct.Transaction.html#method.update
19    /// [`delete`]: struct.Transaction.html#method.delete
20    ///
21    VersionOnWrite,
22
23    ///
24    /// prepare_commit will fail if the persistent version is more recent of the version of the
25    /// last [`read`] on the tx, if no [`read`] on the tx was called will follow the same behavior of
26    /// VersionOnWrite
27    ///
28    /// [`read`]: struct.Transaction.html#method.read
29    ///
30    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/// Persy configuration structure.
52///
53/// Lock are taken in order, should never go in deadlock so the default timeout is huge.
54/// Current default values:
55///
56/// cache_size = 32M
57/// cache_age_limit = 1 Day
58/// transaction_lock_timeout = 1 Day
59/// concurrent_modification_strategy = LastWin
60///
61#[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/// Configure the parameters for the transaction on the begin of a new transaction.
119#[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    /// Set the transaction concurrency checks, **experimental** use carefully
135    pub fn set_strategy(mut self, strategy: TxStrategy) -> Self {
136        self.tx_strategy = Some(strategy);
137        self
138    }
139
140    /// Set if the transaction will be fsync-ed in background or on the current thread
141    /// this option is available only if the "background_ops" feature is enabled
142    #[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    /// Set the transaction id to be used in case of crash recovery.
149    ///
150    /// The id must be maximum 512 bytes of length
151    pub fn set_transaction_id(mut self, transaction_id: TransactionId) -> Self {
152        self.transaction_id = Some(transaction_id);
153        self
154    }
155}