#ifndef ROCKSDB_LITE
#include "rocksdb/db.h"
#include "rocksdb/options.h"
#include "rocksdb/slice.h"
#include "rocksdb/utilities/transaction.h"
#include "rocksdb/utilities/transaction_db.h"
using namespace rocksdb;
std::string kDBPath = "/tmp/rocksdb_transaction_example";
int main() {
Options options;
TransactionDBOptions txn_db_options;
options.create_if_missing = true;
TransactionDB* txn_db;
Status s = TransactionDB::Open(options, txn_db_options, kDBPath, &txn_db);
assert(s.ok());
WriteOptions write_options;
ReadOptions read_options;
TransactionOptions txn_options;
std::string value;
Transaction* txn = txn_db->BeginTransaction(write_options);
assert(txn);
s = txn->Get(read_options, "abc", &value);
assert(s.IsNotFound());
s = txn->Put("abc", "def");
assert(s.ok());
s = txn_db->Get(read_options, "abc", &value);
s = txn_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();
s = txn_db->Put(write_options, "abc", "xyz");
assert(s.ok());
read_options.snapshot = snapshot;
s = txn->GetForUpdate(read_options, "abc", &value);
assert(s.IsBusy());
txn->Rollback();
delete txn;
read_options.snapshot = nullptr;
snapshot = nullptr;
txn_options.set_snapshot = true;
txn = txn_db->BeginTransaction(write_options, txn_options);
read_options.snapshot = txn_db->GetSnapshot();
s = txn->Get(read_options, "x", &value);
txn->Put("x", "x");
s = txn_db->Put(write_options, "y", "y");
txn->SetSnapshot();
txn->SetSavePoint();
read_options.snapshot = txn_db->GetSnapshot();
s = txn->GetForUpdate(read_options, "y", &value);
txn->Put("y", "y");
txn->RollbackToSavePoint();
s = txn->Commit();
assert(s.ok());
delete txn;
read_options.snapshot = nullptr;
delete txn_db;
DestroyDB(kDBPath, options);
return 0;
}
#endif