]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
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/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 | TransactionDBOptions txn_db_options; | |
22 | options.create_if_missing = true; | |
23 | TransactionDB* txn_db; | |
24 | ||
25 | Status s = TransactionDB::Open(options, txn_db_options, kDBPath, &txn_db); | |
26 | assert(s.ok()); | |
27 | ||
28 | WriteOptions write_options; | |
29 | ReadOptions read_options; | |
30 | TransactionOptions txn_options; | |
31 | std::string value; | |
32 | ||
33 | //////////////////////////////////////////////////////// | |
34 | // | |
35 | // Simple Transaction Example ("Read Committed") | |
36 | // | |
37 | //////////////////////////////////////////////////////// | |
38 | ||
39 | // Start a transaction | |
40 | Transaction* txn = txn_db->BeginTransaction(write_options); | |
41 | assert(txn); | |
42 | ||
43 | // Read a key in this transaction | |
44 | s = txn->Get(read_options, "abc", &value); | |
45 | assert(s.IsNotFound()); | |
46 | ||
47 | // Write a key in this transaction | |
48 | s = txn->Put("abc", "def"); | |
49 | assert(s.ok()); | |
50 | ||
51 | // Read a key OUTSIDE this transaction. Does not affect txn. | |
52 | s = txn_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 = txn_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 | s = txn_db->Put(write_options, "abc", "xyz"); | |
79 | assert(s.ok()); | |
80 | ||
81 | // Attempt to read a key using the snapshot. This will fail since | |
82 | // the previous write outside this txn conflicts with this read. | |
83 | read_options.snapshot = snapshot; | |
84 | s = txn->GetForUpdate(read_options, "abc", &value); | |
85 | assert(s.IsBusy()); | |
86 | ||
87 | txn->Rollback(); | |
88 | ||
89 | delete txn; | |
90 | // Clear snapshot from read options since it is no longer valid | |
91 | read_options.snapshot = nullptr; | |
92 | snapshot = nullptr; | |
93 | ||
94 | //////////////////////////////////////////////////////// | |
95 | // | |
96 | // "Read Committed" (Monotonic Atomic Views) Example | |
97 | // --Using multiple Snapshots | |
98 | // | |
99 | //////////////////////////////////////////////////////// | |
100 | ||
101 | // In this example, we set the snapshot multiple times. This is probably | |
102 | // only necessary if you have very strict isolation requirements to | |
103 | // implement. | |
104 | ||
105 | // Set a snapshot at start of transaction | |
106 | txn_options.set_snapshot = true; | |
107 | txn = txn_db->BeginTransaction(write_options, txn_options); | |
108 | ||
109 | // Do some reads and writes to key "x" | |
110 | read_options.snapshot = txn_db->GetSnapshot(); | |
111 | s = txn->Get(read_options, "x", &value); | |
112 | txn->Put("x", "x"); | |
113 | ||
114 | // Do a write outside of the transaction to key "y" | |
115 | s = txn_db->Put(write_options, "y", "y"); | |
116 | ||
117 | // Set a new snapshot in the transaction | |
118 | txn->SetSnapshot(); | |
119 | txn->SetSavePoint(); | |
120 | read_options.snapshot = txn_db->GetSnapshot(); | |
121 | ||
122 | // Do some reads and writes to key "y" | |
123 | // Since the snapshot was advanced, the write done outside of the | |
124 | // transaction does not conflict. | |
125 | s = txn->GetForUpdate(read_options, "y", &value); | |
126 | txn->Put("y", "y"); | |
127 | ||
128 | // Decide we want to revert the last write from this transaction. | |
129 | txn->RollbackToSavePoint(); | |
130 | ||
131 | // Commit. | |
132 | s = txn->Commit(); | |
133 | assert(s.ok()); | |
134 | delete txn; | |
135 | // Clear snapshot from read options since it is no longer valid | |
136 | read_options.snapshot = nullptr; | |
137 | ||
138 | // Cleanup | |
139 | delete txn_db; | |
140 | DestroyDB(kDBPath, options); | |
141 | return 0; | |
142 | } | |
143 | ||
144 | #endif // ROCKSDB_LITE |