You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.3 KiB
C

2 years ago
//
// Created by Ziyang Hu on 2022/7/3.
//
#ifndef COZOROCKS_TX_H
#define COZOROCKS_TX_H
#include "common.h"
2 years ago
#include "slice.h"
#include "status.h"
#include "iter.h"
2 years ago
2 years ago
struct TxBridge {
OptimisticTransactionDB *odb;
TransactionDB *tdb;
unique_ptr<Transaction> tx;
unique_ptr<WriteOptions> w_opts;
unique_ptr<ReadOptions> r_opts;
unique_ptr<OptimisticTransactionOptions> o_tx_opts;
unique_ptr<TransactionOptions> p_tx_opts;
2 years ago
2 years ago
TxBridge(OptimisticTransactionDB *odb_) :
odb(odb_),
tdb(nullptr),
tx(),
w_opts(new WriteOptions),
r_opts(new ReadOptions),
o_tx_opts(new OptimisticTransactionOptions),
p_tx_opts(nullptr) {
r_opts->ignore_range_deletions = true;
}
2 years ago
2 years ago
TxBridge(TransactionDB *tdb_) :
odb(nullptr),
tdb(tdb_),
tx(),
w_opts(new WriteOptions),
r_opts(new ReadOptions),
o_tx_opts(nullptr),
p_tx_opts(new TransactionOptions) {
r_opts->ignore_range_deletions = true;
}
2 years ago
inline WriteOptions &get_w_opts() {
2 years ago
return *w_opts;
}
// inline ReadOptions &get_r_opts() {
// return *r_opts;
// }
inline void verify_checksums(bool val) {
r_opts->verify_checksums = val;
}
inline void fill_cache(bool val) {
r_opts->fill_cache = val;
}
inline unique_ptr<IterBridge> iterator() {
return make_unique<IterBridge>(&*tx);
};
inline void set_snapshot(bool val) {
2 years ago
if (tx != nullptr) {
if (val) {
tx->SetSnapshot();
}
2 years ago
} else if (o_tx_opts != nullptr) {
o_tx_opts->set_snapshot = val;
2 years ago
} else if (p_tx_opts != nullptr) {
p_tx_opts->set_snapshot = val;
2 years ago
}
}
inline void clear_snapshot() {
tx->ClearSnapshot();
}
inline DB *get_db() const {
if (tdb != nullptr) {
return tdb;
} else {
return odb;
}
}
void start();
inline unique_ptr<PinnableSlice> get(RustBytes key, bool for_update, RdbStatus &status) {
Slice key_ = convert_slice(key);
auto ret = make_unique<PinnableSlice>();
if (for_update) {
auto s = tx->GetForUpdate(*r_opts, get_db()->DefaultColumnFamily(), key_, &*ret);
write_status(s, status);
} else {
auto s = tx->Get(*r_opts, key_, &*ret);
write_status(s, status);
}
return ret;
}
inline void put(RustBytes key, RustBytes val, RdbStatus &status) {
write_status(tx->Put(convert_slice(key), convert_slice(val)), status);
}
inline void del(RustBytes key, RdbStatus &status) {
write_status(tx->Delete(convert_slice(key)), status);
}
inline void commit(RdbStatus &status) {
write_status(tx->Commit(), status);
}
inline void rollback(RdbStatus &status) {
write_status(tx->Rollback(), status);
}
inline void rollback_to_savepoint(RdbStatus &status) {
write_status(tx->RollbackToSavePoint(), status);
}
inline void pop_savepoint(RdbStatus &status) {
write_status(tx->PopSavePoint(), status);
}
inline void set_savepoint() {
tx->SetSavePoint();
}
2 years ago
};
#endif //COZOROCKS_TX_H