Skip to content

Commit

Permalink
Update example of optimistic transaction (facebook#6074)
Browse files Browse the repository at this point in the history
Summary:
Add asserts to show the intentions of result explicitly.
Add examples to show the effect of optimistic transaction more clearly.
Pull Request resolved: facebook#6074

Test Plan: `cd examples && make optimistic_transaction_example && ./optimistic_transaction_example`

Differential Revision: D18964309

Pulled By: cheng-chang

fbshipit-source-id: a524616ed9981edf2fd37ae61c5ed18c5cf25f55
  • Loading branch information
nccx authored and facebook-github-bot committed Jan 16, 2020
1 parent f8b5ef8 commit 86623a7
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions examples/optimistic_transaction_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -112,17 +131,27 @@ 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();
read_options.snapshot = db->GetSnapshot();

// 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
Expand All @@ -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);
Expand Down

0 comments on commit 86623a7

Please sign in to comment.