Add asserts in transaction example (#6055)

Summary:
The intention of the example for read committed is clearer with these added asserts.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/6055

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

Differential Revision: D18621830

Pulled By: riversand963

fbshipit-source-id: a94b08c5958b589049409ee4fc4d6799e5cbef79
This commit is contained in:
Cheng Chang 2019-11-20 14:17:16 -08:00 committed by Facebook Github Bot
parent 3cd75736a7
commit c0983d0691
1 changed files with 18 additions and 2 deletions

View File

@ -50,17 +50,33 @@ int main() {
// Read a key OUTSIDE this transaction. Does not affect txn. // Read a key OUTSIDE this transaction. Does not affect txn.
s = txn_db->Get(read_options, "abc", &value); s = txn_db->Get(read_options, "abc", &value);
assert(s.IsNotFound());
// Write a key OUTSIDE of this transaction. // Write a key OUTSIDE of this transaction.
// Does not affect txn since this is an unrelated key. If we wrote key 'abc' // Does not affect txn since this is an unrelated key.
// here, the transaction would fail to commit.
s = txn_db->Put(write_options, "xyz", "zzz"); s = txn_db->Put(write_options, "xyz", "zzz");
assert(s.ok());
// Write a key OUTSIDE of this transaction.
// Fail because the key conflicts with the key written in txn.
s = txn_db->Put(write_options, "abc", "def");
assert(s.subcode() == Status::kLockTimeout);
// Value for key "xyz" has been committed, can be read in txn.
s = txn->Get(read_options, "xyz", &value);
assert(s.ok());
assert(value == "zzz");
// Commit transaction // Commit transaction
s = txn->Commit(); s = txn->Commit();
assert(s.ok()); assert(s.ok());
delete txn; delete txn;
// Value is committed, can be read now.
s = txn_db->Get(read_options, "abc", &value);
assert(s.ok());
assert(value == "def");
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
// //
// "Repeatable Read" (Snapshot Isolation) Example // "Repeatable Read" (Snapshot Isolation) Example