/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/transactions/optimistic_transaction_db_impl.cc
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | |
21 | | #ifndef ROCKSDB_LITE |
22 | | |
23 | | #include "yb/rocksdb/utilities/transactions/optimistic_transaction_db_impl.h" |
24 | | |
25 | | #include <string> |
26 | | #include <vector> |
27 | | |
28 | | #include "yb/rocksdb/db.h" |
29 | | #include "yb/rocksdb/options.h" |
30 | | #include "yb/rocksdb/utilities/optimistic_transaction_db.h" |
31 | | #include "yb/rocksdb/utilities/transactions/optimistic_transaction_impl.h" |
32 | | |
33 | | namespace rocksdb { |
34 | | |
35 | | Transaction* OptimisticTransactionDBImpl::BeginTransaction( |
36 | | const WriteOptions& write_options, |
37 | 60 | const OptimisticTransactionOptions& txn_options, Transaction* old_txn) { |
38 | 60 | if (old_txn != nullptr) { |
39 | 6 | ReinitializeTransaction(old_txn, write_options, txn_options); |
40 | 6 | return old_txn; |
41 | 54 | } else { |
42 | 54 | return new OptimisticTransactionImpl(this, write_options, txn_options); |
43 | 54 | } |
44 | 60 | } |
45 | | |
46 | | Status OptimisticTransactionDB::Open(const Options& options, |
47 | | const std::string& dbname, |
48 | 18 | OptimisticTransactionDB** dbptr) { |
49 | 18 | DBOptions db_options(options); |
50 | 18 | ColumnFamilyOptions cf_options(options); |
51 | 18 | std::vector<ColumnFamilyDescriptor> column_families; |
52 | 18 | column_families.push_back( |
53 | 18 | ColumnFamilyDescriptor(kDefaultColumnFamilyName, cf_options)); |
54 | 18 | std::vector<ColumnFamilyHandle*> handles; |
55 | 18 | Status s = Open(db_options, dbname, column_families, &handles, dbptr); |
56 | 18 | if (s.ok()) { |
57 | 18 | assert(handles.size() == 1); |
58 | | // i can delete the handle since DBImpl is always holding a reference to |
59 | | // default column family |
60 | 18 | delete handles[0]; |
61 | 18 | } |
62 | | |
63 | 18 | return s; |
64 | 18 | } |
65 | | |
66 | | Status OptimisticTransactionDB::Open( |
67 | | const DBOptions& db_options, const std::string& dbname, |
68 | | const std::vector<ColumnFamilyDescriptor>& column_families, |
69 | | std::vector<ColumnFamilyHandle*>* handles, |
70 | 19 | OptimisticTransactionDB** dbptr) { |
71 | 19 | Status s; |
72 | 19 | DB* db; |
73 | | |
74 | 19 | std::vector<ColumnFamilyDescriptor> column_families_copy = column_families; |
75 | | |
76 | | // Enable MemTable History if not already enabled |
77 | 21 | for (auto& column_family : column_families_copy) { |
78 | 21 | ColumnFamilyOptions* options = &column_family.options; |
79 | | |
80 | 21 | if (options->max_write_buffer_number_to_maintain == 0) { |
81 | | // Setting to -1 will set the History size to max_write_buffer_number. |
82 | 21 | options->max_write_buffer_number_to_maintain = -1; |
83 | 21 | } |
84 | 21 | } |
85 | | |
86 | 19 | s = DB::Open(db_options, dbname, column_families_copy, handles, &db); |
87 | | |
88 | 19 | if (s.ok()) { |
89 | 19 | *dbptr = new OptimisticTransactionDBImpl(db); |
90 | 19 | } |
91 | | |
92 | 19 | return s; |
93 | 19 | } |
94 | | |
95 | | void OptimisticTransactionDBImpl::ReinitializeTransaction( |
96 | | Transaction* txn, const WriteOptions& write_options, |
97 | 6 | const OptimisticTransactionOptions& txn_options) { |
98 | 6 | assert(dynamic_cast<OptimisticTransactionImpl*>(txn) != nullptr); |
99 | 6 | auto txn_impl = reinterpret_cast<OptimisticTransactionImpl*>(txn); |
100 | | |
101 | 6 | txn_impl->Reinitialize(this, write_options, txn_options); |
102 | 6 | } |
103 | | |
104 | | } // namespace rocksdb |
105 | | #endif // ROCKSDB_LITE |