Skip to content

Commit

Permalink
Make Optimistic Tx database stackable
Browse files Browse the repository at this point in the history
Summary:
This change models Optimistic Tx db after Pessimistic TX db. The motivation for this change is to make the ptr polymorphic so it can be held by the same raw or smart ptr.

Currently, due to the inheritance of the Opt Tx db not being rooted in the manner of Pess Tx from a single DB root it is more difficult to write clean code and have clear ownership of the database in cases when options dictate instantiate of plan DB, Pess Tx DB or Opt tx db.
Closes facebook#3566

Differential Revision: D7184502

Pulled By: yiwu-arbug

fbshipit-source-id: 31d06efafd79497bb0c230e971857dba3bd962c3
  • Loading branch information
yuslepukhin authored and facebook-github-bot committed Apr 3, 2018
1 parent b058a33 commit 2a62ca1
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 104 deletions.
15 changes: 5 additions & 10 deletions include/rocksdb/utilities/optimistic_transaction_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "rocksdb/comparator.h"
#include "rocksdb/db.h"
#include "rocksdb/utilities/stackable_db.h"

namespace rocksdb {

Expand All @@ -30,7 +31,7 @@ struct OptimisticTransactionOptions {
const Comparator* cmp = BytewiseComparator();
};

class OptimisticTransactionDB {
class OptimisticTransactionDB : public StackableDB {
public:
// Open an OptimisticTransactionDB similar to DB::Open().
static Status Open(const Options& options, const std::string& dbname,
Expand All @@ -57,18 +58,12 @@ class OptimisticTransactionDB {
OptimisticTransactionOptions(),
Transaction* old_txn = nullptr) = 0;

// Return the underlying Database that was opened
virtual DB* GetBaseDB() = 0;
OptimisticTransactionDB(const OptimisticTransactionDB&) = delete;
void operator=(const OptimisticTransactionDB&) = delete;

protected:
// To Create an OptimisticTransactionDB, call Open()
explicit OptimisticTransactionDB(DB* /*db*/) {}
OptimisticTransactionDB() {}

private:
// No copying allowed
OptimisticTransactionDB(const OptimisticTransactionDB&);
void operator=(const OptimisticTransactionDB&);
explicit OptimisticTransactionDB(DB* db) : StackableDB(db) {}
};

} // namespace rocksdb
Expand Down
12 changes: 6 additions & 6 deletions utilities/transactions/optimistic_transaction_db_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ namespace rocksdb {
class OptimisticTransactionDBImpl : public OptimisticTransactionDB {
public:
explicit OptimisticTransactionDBImpl(DB* db, bool take_ownership = true)
: OptimisticTransactionDB(db), db_(db), db_owner_(take_ownership) {}
: OptimisticTransactionDB(db), db_owner_(take_ownership) {}

~OptimisticTransactionDBImpl() {
// Prevent this stackable from destroying
// base db
if (!db_owner_) {
db_.release();
db_ = nullptr;
}
}

Transaction* BeginTransaction(const WriteOptions& write_options,
const OptimisticTransactionOptions& txn_options,
Transaction* old_txn) override;

DB* GetBaseDB() override { return db_.get(); }

private:
std::unique_ptr<DB> db_;
bool db_owner_;

bool db_owner_;

void ReinitializeTransaction(Transaction* txn,
const WriteOptions& write_options,
Expand Down
Loading

0 comments on commit 2a62ca1

Please sign in to comment.