From c0983d069193d59aec7ddc8c02475e0f5081b52f Mon Sep 17 00:00:00 2001 From: Cheng Chang Date: Wed, 20 Nov 2019 14:17:16 -0800 Subject: [PATCH] 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 --- examples/transaction_example.cc | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/examples/transaction_example.cc b/examples/transaction_example.cc index 7274cf7ec..6d12651ad 100644 --- a/examples/transaction_example.cc +++ b/examples/transaction_example.cc @@ -50,17 +50,33 @@ int main() { // Read a key OUTSIDE this transaction. Does not affect txn. s = txn_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. + // Does not affect txn since this is an unrelated key. 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 s = txn->Commit(); assert(s.ok()); 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