diff --git a/lib/src/kvs/tikv/mod.rs b/lib/src/kvs/tikv/mod.rs index 0def3b3c..87003aca 100644 --- a/lib/src/kvs/tikv/mod.rs +++ b/lib/src/kvs/tikv/mod.rs @@ -32,41 +32,26 @@ impl Datastore { } /// Start a new transaction pub async fn transaction(&self, write: bool, lock: bool) -> Result { - match lock { - false => { - // Set the behaviour when dropping an unfinished transaction - let mut opt = TransactionOptions::new_optimistic().drop_check(CheckLevel::Warn); - // Set this transaction as read only if possible - if !write { - opt = opt.read_only(); - } - // Create a new optimistic transaction - match self.db.begin_with_options(opt).await { - Ok(tx) => Ok(Transaction { - ok: false, - rw: write, - tx, - }), - Err(e) => Err(Error::Tx(e.to_string())), - } - } - true => { - // Set the behaviour when dropping an unfinished transaction - let mut opt = TransactionOptions::new_pessimistic().drop_check(CheckLevel::Warn); - // Set this transaction as read only if possible - if !write { - opt = opt.read_only(); - } - // Create a new pessimistic transaction - match self.db.begin_with_options(opt).await { - Ok(tx) => Ok(Transaction { - ok: false, - rw: write, - tx, - }), - Err(e) => Err(Error::Tx(e.to_string())), - } - } + // Set whether this should be an optimistic or pessimistic transaction + let mut opt = if lock { + TransactionOptions::new_pessimistic() + } else { + TransactionOptions::new_optimistic() + }; + // Set the behaviour when dropping an unfinished transaction + opt = opt.drop_check(CheckLevel::Warn); + // Set this transaction as read only if possible + if !write { + opt = opt.read_only(); + } + // Create a new distributed transaction + match self.db.begin_with_options(opt).await { + Ok(tx) => Ok(Transaction { + ok: false, + rw: write, + tx, + }), + Err(e) => Err(Error::Tx(e.to_string())), } } }