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.

138 lines
3.7 KiB
C

// Copyright 2022, The Cozo Project Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
2 years ago
#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;
ColumnFamilyHandle * cf_handle;
2 years ago
explicit TxBridge(TransactionDB *tdb_, ColumnFamilyHandle * cf_handle_) :
2 years ago
odb(nullptr),
tdb(tdb_),
tx(),
w_opts(new WriteOptions),
r_opts(new ReadOptions),
o_tx_opts(nullptr),
p_tx_opts(new TransactionOptions),
cf_handle(cf_handle_) {
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() const {
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();
}
[[nodiscard]] inline DB *get_db() const {
2 years ago
if (tdb != nullptr) {
return tdb;
} else {
return odb;
}
}
void start();
inline unique_ptr<PinnableSlice> get(RustBytes key, bool for_update, RocksDbStatus &status) const {
2 years ago
Slice key_ = convert_slice(key);
auto ret = make_unique<PinnableSlice>();
if (for_update) {
auto s = tx->GetForUpdate(*r_opts, cf_handle, key_, &*ret);
2 years ago
write_status(s, status);
} else {
auto s = tx->Get(*r_opts, key_, &*ret);
2 years ago
write_status(s, status);
}
return ret;
}
inline void exists(RustBytes key, bool for_update, RocksDbStatus &status) const {
Slice key_ = convert_slice(key);
auto ret = PinnableSlice();
if (for_update) {
auto s = tx->GetForUpdate(*r_opts, cf_handle, key_, &ret);
write_status(s, status);
} else {
auto s = tx->Get(*r_opts, key_, &ret);
write_status(s, status);
}
}
inline void put(RustBytes key, RustBytes val, RocksDbStatus &status) const {
write_status(tx->Put(convert_slice(key), convert_slice(val)), status);
2 years ago
}
inline void del(RustBytes key, RocksDbStatus &status) const {
write_status(tx->Delete(convert_slice(key)), status);
2 years ago
}
2 years ago
inline void commit(RocksDbStatus &status) {
2 years ago
write_status(tx->Commit(), status);
}
2 years ago
inline void rollback(RocksDbStatus &status) {
2 years ago
write_status(tx->Rollback(), status);
}
2 years ago
inline void rollback_to_savepoint(RocksDbStatus &status) {
2 years ago
write_status(tx->RollbackToSavePoint(), status);
}
2 years ago
inline void pop_savepoint(RocksDbStatus &status) {
2 years ago
write_status(tx->PopSavePoint(), status);
}
inline void set_savepoint() {
tx->SetSavePoint();
}
2 years ago
};
#endif //COZOROCKS_TX_H