Skip to content

Commit c0983d0

Browse files
nccxfacebook-github-bot
authored andcommitted
Add asserts in transaction example (facebook#6055)
Summary: The intention of the example for read committed is clearer with these added asserts. Pull Request resolved: facebook#6055 Test Plan: `cd examples && make transaction_example && ./transaction_example` Differential Revision: D18621830 Pulled By: riversand963 fbshipit-source-id: a94b08c5958b589049409ee4fc4d6799e5cbef79
1 parent 3cd7573 commit c0983d0

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

examples/transaction_example.cc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,33 @@ int main() {
5050

5151
// Read a key OUTSIDE this transaction. Does not affect txn.
5252
s = txn_db->Get(read_options, "abc", &value);
53+
assert(s.IsNotFound());
5354

5455
// Write a key OUTSIDE of this transaction.
55-
// Does not affect txn since this is an unrelated key. If we wrote key 'abc'
56-
// here, the transaction would fail to commit.
56+
// Does not affect txn since this is an unrelated key.
5757
s = txn_db->Put(write_options, "xyz", "zzz");
58+
assert(s.ok());
59+
60+
// Write a key OUTSIDE of this transaction.
61+
// Fail because the key conflicts with the key written in txn.
62+
s = txn_db->Put(write_options, "abc", "def");
63+
assert(s.subcode() == Status::kLockTimeout);
64+
65+
// Value for key "xyz" has been committed, can be read in txn.
66+
s = txn->Get(read_options, "xyz", &value);
67+
assert(s.ok());
68+
assert(value == "zzz");
5869

5970
// Commit transaction
6071
s = txn->Commit();
6172
assert(s.ok());
6273
delete txn;
6374

75+
// Value is committed, can be read now.
76+
s = txn_db->Get(read_options, "abc", &value);
77+
assert(s.ok());
78+
assert(value == "def");
79+
6480
////////////////////////////////////////////////////////
6581
//
6682
// "Repeatable Read" (Snapshot Isolation) Example

0 commit comments

Comments
 (0)