#ifndef ROCKSDB_LITE
#include "rocksdb/db.h"
#include "rocksdb/options.h"
#include "rocksdb/slice.h"
#include "rocksdb/utilities/transaction.h"
#include "rocksdb/utilities/optimistic_transaction_db.h"
using namespace rocksdb;
std::string kDBPath = "/tmp/rocksdb_transaction_example";
int main() {
Options options;
options.create_if_missing = true;
DB* db;
OptimisticTransactionDB* txn_db;
Status s = OptimisticTransactionDB::Open(options, kDBPath, &txn_db);
assert(s.ok());
db = txn_db->GetBaseDB();
WriteOptions write_options;
ReadOptions read_options;
OptimisticTransactionOptions txn_options;
std::string value;
Transaction* txn = txn_db->BeginTransaction(write_options);
assert(txn);
s = txn->Get(read_options, "abc", &value);
assert(s.IsNotFound());
txn->Put("abc", "def");
s = db->Get(read_options, "abc", &value);
s = db->Put(write_options, "xyz", "zzz");
s = txn->Commit();
assert(s.ok());
delete txn;
txn_options.set_snapshot = true;
txn = txn_db->BeginTransaction(write_options, txn_options);
const Snapshot* snapshot = txn->GetSnapshot();
db->Put(write_options, "abc", "xyz");
read_options.snapshot = snapshot;
s = txn->GetForUpdate(read_options, "abc", &value);
assert(value == "def");
s = txn->Commit();
assert(s.IsBusy());
delete txn;
read_options.snapshot = nullptr;
snapshot = nullptr;
txn_options.set_snapshot = true;
txn = txn_db->BeginTransaction(write_options, txn_options);
read_options.snapshot = db->GetSnapshot();
s = txn->Get(read_options, "x", &value);
txn->Put("x", "x");
s = db->Put(write_options, "y", "y");
txn->SetSnapshot();
read_options.snapshot = db->GetSnapshot();
s = txn->GetForUpdate(read_options, "y", &value);
txn->Put("y", "y");
s = txn->Commit();
assert(s.ok());
delete txn;
read_options.snapshot = nullptr;
delete txn_db;
DestroyDB(kDBPath, options);
return 0;
}
#endif