Fix #1698 - reduce code duplication in TiKV txn. (#1699)

This commit is contained in:
Finn Bear 2023-03-13 19:54:21 -07:00 committed by GitHub
parent f3e605afde
commit b0811b263e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -32,41 +32,26 @@ impl Datastore {
}
/// Start a new transaction
pub async fn transaction(&self, write: bool, lock: bool) -> Result<Transaction, Error> {
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())),
}
}
}