]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/examples/optimistic_transaction_example.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / rocksdb / examples / optimistic_transaction_example.cc
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).
5
6 #ifndef ROCKSDB_LITE
7
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"
13
14 using namespace rocksdb;
15
16 std::string kDBPath = "/tmp/rocksdb_transaction_example";
17
18 int main() {
19 // open DB
20 Options options;
21 options.create_if_missing = true;
22 DB* db;
23 OptimisticTransactionDB* txn_db;
24
25 Status s = OptimisticTransactionDB::Open(options, kDBPath, &txn_db);
26 assert(s.ok());
27 db = txn_db->GetBaseDB();
28
29 WriteOptions write_options;
30 ReadOptions read_options;
31 OptimisticTransactionOptions txn_options;
32 std::string value;
33
34 ////////////////////////////////////////////////////////
35 //
36 // Simple OptimisticTransaction Example ("Read Committed")
37 //
38 ////////////////////////////////////////////////////////
39
40 // Start a transaction
41 Transaction* txn = txn_db->BeginTransaction(write_options);
42 assert(txn);
43
44 // Read a key in this transaction
45 s = txn->Get(read_options, "abc", &value);
46 assert(s.IsNotFound());
47
48 // Write a key in this transaction
49 txn->Put("abc", "def");
50
51 // Read a key OUTSIDE this transaction. Does not affect txn.
52 s = db->Get(read_options, "abc", &value);
53
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");
58
59 // Commit transaction
60 s = txn->Commit();
61 assert(s.ok());
62 delete txn;
63
64 ////////////////////////////////////////////////////////
65 //
66 // "Repeatable Read" (Snapshot Isolation) Example
67 // -- Using a single Snapshot
68 //
69 ////////////////////////////////////////////////////////
70
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);
74
75 const Snapshot* snapshot = txn->GetSnapshot();
76
77 // Write a key OUTSIDE of transaction
78 db->Put(write_options, "abc", "xyz");
79
80 // Read a key using the snapshot
81 read_options.snapshot = snapshot;
82 s = txn->GetForUpdate(read_options, "abc", &value);
83 assert(value == "def");
84
85 // Attempt to commit transaction
86 s = txn->Commit();
87
88 // Transaction could not commit since the write outside of the txn conflicted
89 // with the read!
90 assert(s.IsBusy());
91
92 delete txn;
93 // Clear snapshot from read options since it is no longer valid
94 read_options.snapshot = nullptr;
95 snapshot = nullptr;
96
97 ////////////////////////////////////////////////////////
98 //
99 // "Read Committed" (Monotonic Atomic Views) Example
100 // --Using multiple Snapshots
101 //
102 ////////////////////////////////////////////////////////
103
104 // In this example, we set the snapshot multiple times. This is probably
105 // only necessary if you have very strict isolation requirements to
106 // implement.
107
108 // Set a snapshot at start of transaction
109 txn_options.set_snapshot = true;
110 txn = txn_db->BeginTransaction(write_options, txn_options);
111
112 // Do some reads and writes to key "x"
113 read_options.snapshot = db->GetSnapshot();
114 s = txn->Get(read_options, "x", &value);
115 txn->Put("x", "x");
116
117 // Do a write outside of the transaction to key "y"
118 s = db->Put(write_options, "y", "y");
119
120 // Set a new snapshot in the transaction
121 txn->SetSnapshot();
122 read_options.snapshot = db->GetSnapshot();
123
124 // Do some reads and writes to key "y"
125 s = txn->GetForUpdate(read_options, "y", &value);
126 txn->Put("y", "y");
127
128 // Commit. Since the snapshot was advanced, the write done outside of the
129 // transaction does not prevent this transaction from Committing.
130 s = txn->Commit();
131 assert(s.ok());
132 delete txn;
133 // Clear snapshot from read options since it is no longer valid
134 read_options.snapshot = nullptr;
135
136 // Cleanup
137 delete txn_db;
138 DestroyDB(kDBPath, options);
139 return 0;
140 }
141
142 #endif // ROCKSDB_LITE