mirror of https://github.com/facebook/rocksdb.git
Update example of optimistic transaction (#6074)
Summary: Add asserts to show the intentions of result explicitly. Add examples to show the effect of optimistic transaction more clearly. Pull Request resolved: https://github.com/facebook/rocksdb/pull/6074 Test Plan: `cd examples && make optimistic_transaction_example && ./optimistic_transaction_example` Differential Revision: D18964309 Pulled By: cheng-chang fbshipit-source-id: a524616ed9981edf2fd37ae61c5ed18c5cf25f55
This commit is contained in:
parent
f8b5ef85ec
commit
86623a7153
|
@ -46,21 +46,34 @@ int main() {
|
|||
assert(s.IsNotFound());
|
||||
|
||||
// Write a key in this transaction
|
||||
txn->Put("abc", "def");
|
||||
s = txn->Put("abc", "xyz");
|
||||
assert(s.ok());
|
||||
|
||||
// Read a key OUTSIDE this transaction. Does not affect txn.
|
||||
s = db->Get(read_options, "abc", &value);
|
||||
assert(s.IsNotFound());
|
||||
|
||||
// Write a key OUTSIDE of this transaction.
|
||||
// Does not affect txn since this is an unrelated key. If we wrote key 'abc'
|
||||
// here, the transaction would fail to commit.
|
||||
s = db->Put(write_options, "xyz", "zzz");
|
||||
assert(s.ok());
|
||||
s = db->Put(write_options, "abc", "def");
|
||||
assert(s.ok());
|
||||
|
||||
// Commit transaction
|
||||
s = txn->Commit();
|
||||
assert(s.ok());
|
||||
assert(s.IsBusy());
|
||||
delete txn;
|
||||
|
||||
s = db->Get(read_options, "xyz", &value);
|
||||
assert(s.ok());
|
||||
assert(value == "zzz");
|
||||
|
||||
s = db->Get(read_options, "abc", &value);
|
||||
assert(s.ok());
|
||||
assert(value == "def");
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
//
|
||||
// "Repeatable Read" (Snapshot Isolation) Example
|
||||
|
@ -75,11 +88,13 @@ int main() {
|
|||
const Snapshot* snapshot = txn->GetSnapshot();
|
||||
|
||||
// Write a key OUTSIDE of transaction
|
||||
db->Put(write_options, "abc", "xyz");
|
||||
s = db->Put(write_options, "abc", "xyz");
|
||||
assert(s.ok());
|
||||
|
||||
// Read a key using the snapshot
|
||||
read_options.snapshot = snapshot;
|
||||
s = txn->GetForUpdate(read_options, "abc", &value);
|
||||
assert(s.ok());
|
||||
assert(value == "def");
|
||||
|
||||
// Attempt to commit transaction
|
||||
|
@ -94,6 +109,10 @@ int main() {
|
|||
read_options.snapshot = nullptr;
|
||||
snapshot = nullptr;
|
||||
|
||||
s = db->Get(read_options, "abc", &value);
|
||||
assert(s.ok());
|
||||
assert(value == "xyz");
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
//
|
||||
// "Read Committed" (Monotonic Atomic Views) Example
|
||||
|
@ -112,10 +131,18 @@ int main() {
|
|||
// Do some reads and writes to key "x"
|
||||
read_options.snapshot = db->GetSnapshot();
|
||||
s = txn->Get(read_options, "x", &value);
|
||||
txn->Put("x", "x");
|
||||
assert(s.IsNotFound());
|
||||
s = txn->Put("x", "x");
|
||||
assert(s.ok());
|
||||
|
||||
// The transaction hasn't committed, so the write is not visible
|
||||
// outside of txn.
|
||||
s = db->Get(read_options, "x", &value);
|
||||
assert(s.IsNotFound());
|
||||
|
||||
// Do a write outside of the transaction to key "y"
|
||||
s = db->Put(write_options, "y", "y");
|
||||
s = db->Put(write_options, "y", "z");
|
||||
assert(s.ok());
|
||||
|
||||
// Set a new snapshot in the transaction
|
||||
txn->SetSnapshot();
|
||||
|
@ -123,6 +150,8 @@ int main() {
|
|||
|
||||
// Do some reads and writes to key "y"
|
||||
s = txn->GetForUpdate(read_options, "y", &value);
|
||||
assert(s.ok());
|
||||
assert(value == "z");
|
||||
txn->Put("y", "y");
|
||||
|
||||
// Commit. Since the snapshot was advanced, the write done outside of the
|
||||
|
@ -133,6 +162,15 @@ int main() {
|
|||
// Clear snapshot from read options since it is no longer valid
|
||||
read_options.snapshot = nullptr;
|
||||
|
||||
// txn is committed, read the latest values.
|
||||
s = db->Get(read_options, "x", &value);
|
||||
assert(s.ok());
|
||||
assert(value == "x");
|
||||
|
||||
s = db->Get(read_options, "y", &value);
|
||||
assert(s.ok());
|
||||
assert(value == "y");
|
||||
|
||||
// Cleanup
|
||||
delete txn_db;
|
||||
DestroyDB(kDBPath, options);
|
||||
|
|
Loading…
Reference in New Issue