1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
8 #include "rocksdb/db.h"
9 #include "rocksdb/options.h"
10 #include "rocksdb/slice.h"
11 #include "rocksdb/utilities/transaction.h"
12 #include "rocksdb/utilities/optimistic_transaction_db.h"
14 using namespace rocksdb
;
16 std::string kDBPath
= "/tmp/rocksdb_transaction_example";
21 options
.create_if_missing
= true;
23 OptimisticTransactionDB
* txn_db
;
25 Status s
= OptimisticTransactionDB::Open(options
, kDBPath
, &txn_db
);
27 db
= txn_db
->GetBaseDB();
29 WriteOptions write_options
;
30 ReadOptions read_options
;
31 OptimisticTransactionOptions txn_options
;
34 ////////////////////////////////////////////////////////
36 // Simple OptimisticTransaction Example ("Read Committed")
38 ////////////////////////////////////////////////////////
40 // Start a transaction
41 Transaction
* txn
= txn_db
->BeginTransaction(write_options
);
44 // Read a key in this transaction
45 s
= txn
->Get(read_options
, "abc", &value
);
46 assert(s
.IsNotFound());
48 // Write a key in this transaction
49 txn
->Put("abc", "def");
51 // Read a key OUTSIDE this transaction. Does not affect txn.
52 s
= db
->Get(read_options
, "abc", &value
);
54 // 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.
57 s
= db
->Put(write_options
, "xyz", "zzz");
64 ////////////////////////////////////////////////////////
66 // "Repeatable Read" (Snapshot Isolation) Example
67 // -- Using a single Snapshot
69 ////////////////////////////////////////////////////////
71 // Set a snapshot at start of transaction by setting set_snapshot=true
72 txn_options
.set_snapshot
= true;
73 txn
= txn_db
->BeginTransaction(write_options
, txn_options
);
75 const Snapshot
* snapshot
= txn
->GetSnapshot();
77 // Write a key OUTSIDE of transaction
78 db
->Put(write_options
, "abc", "xyz");
80 // Read a key using the snapshot
81 read_options
.snapshot
= snapshot
;
82 s
= txn
->GetForUpdate(read_options
, "abc", &value
);
83 assert(value
== "def");
85 // Attempt to commit transaction
88 // Transaction could not commit since the write outside of the txn conflicted
93 // Clear snapshot from read options since it is no longer valid
94 read_options
.snapshot
= nullptr;
97 ////////////////////////////////////////////////////////
99 // "Read Committed" (Monotonic Atomic Views) Example
100 // --Using multiple Snapshots
102 ////////////////////////////////////////////////////////
104 // In this example, we set the snapshot multiple times. This is probably
105 // only necessary if you have very strict isolation requirements to
108 // Set a snapshot at start of transaction
109 txn_options
.set_snapshot
= true;
110 txn
= txn_db
->BeginTransaction(write_options
, txn_options
);
112 // Do some reads and writes to key "x"
113 read_options
.snapshot
= db
->GetSnapshot();
114 s
= txn
->Get(read_options
, "x", &value
);
117 // Do a write outside of the transaction to key "y"
118 s
= db
->Put(write_options
, "y", "y");
120 // Set a new snapshot in the transaction
122 read_options
.snapshot
= db
->GetSnapshot();
124 // Do some reads and writes to key "y"
125 s
= txn
->GetForUpdate(read_options
, "y", &value
);
128 // Commit. Since the snapshot was advanced, the write done outside of the
129 // transaction does not prevent this transaction from Committing.
133 // Clear snapshot from read options since it is no longer valid
134 read_options
.snapshot
= nullptr;
138 DestroyDB(kDBPath
, options
);
142 #endif // ROCKSDB_LITE