/Users/deen/code/yugabyte-db/src/yb/util/version_tracker.h
Line | Count | Source |
1 | | // |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | // |
15 | | |
16 | | #ifndef YB_UTIL_VERSION_TRACKER_H |
17 | | #define YB_UTIL_VERSION_TRACKER_H |
18 | | |
19 | | #include <stddef.h> |
20 | | |
21 | | #include <atomic> |
22 | | |
23 | | namespace yb { |
24 | | |
25 | | template <class Value> |
26 | | class VersionTrackerCheckOut; |
27 | | |
28 | | // Utility class to track version of stored data. |
29 | | // VersionTracker<T> provides read access to data. |
30 | | // If data should be modified, then it should be checked out: |
31 | | // auto checkout = versioned_data.CheckOut(); |
32 | | // And checkout would provide write access to data. |
33 | | // After checkout is destroyed, version is incremented. |
34 | | template <class Value> |
35 | | class VersionTracker { |
36 | | public: |
37 | | VersionTracker(const VersionTracker&) = delete; |
38 | | void operator=(const VersionTracker&) = delete; |
39 | | |
40 | 10.9k | VersionTracker() = default; _ZN2yb14VersionTrackerINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master9TableInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEEC2Ev Line | Count | Source | 40 | 5.45k | VersionTracker() = default; |
_ZN2yb14VersionTrackerINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master10TabletInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEEC2Ev Line | Count | Source | 40 | 5.45k | VersionTracker() = default; |
|
41 | | |
42 | | // Data is modified in place, so external synchronization is required to prevent |
43 | | // undesired access to partially modified data. |
44 | | VersionTrackerCheckOut<Value> CheckOut(); |
45 | | |
46 | 290k | void Commit() { |
47 | 290k | version_.fetch_add(1, std::memory_order_acq_rel); |
48 | 290k | } _ZN2yb14VersionTrackerINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master10TabletInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEE6CommitEv Line | Count | Source | 46 | 41.0k | void Commit() { | 47 | 41.0k | version_.fetch_add(1, std::memory_order_acq_rel); | 48 | 41.0k | } |
_ZN2yb14VersionTrackerINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master9TableInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEE6CommitEv Line | Count | Source | 46 | 249k | void Commit() { | 47 | 249k | version_.fetch_add(1, std::memory_order_acq_rel); | 48 | 249k | } |
|
49 | | |
50 | 2.93M | const Value& operator*() const { |
51 | 2.93M | return value_; |
52 | 2.93M | } _ZNK2yb14VersionTrackerINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master10TabletInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEEdeEv Line | Count | Source | 50 | 1.02M | const Value& operator*() const { | 51 | 1.02M | return value_; | 52 | 1.02M | } |
_ZNK2yb14VersionTrackerINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master9TableInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEEdeEv Line | Count | Source | 50 | 1.91M | const Value& operator*() const { | 51 | 1.91M | return value_; | 52 | 1.91M | } |
|
53 | | |
54 | 2.30M | const Value* operator->() const { |
55 | 2.30M | return &value_; |
56 | 2.30M | } _ZNK2yb14VersionTrackerINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master10TabletInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEEptEv Line | Count | Source | 54 | 4.35k | const Value* operator->() const { | 55 | 4.35k | return &value_; | 56 | 4.35k | } |
_ZNK2yb14VersionTrackerINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master9TableInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEEptEv Line | Count | Source | 54 | 2.30M | const Value* operator->() const { | 55 | 2.30M | return &value_; | 56 | 2.30M | } |
|
57 | | |
58 | 8.03k | size_t Version() const { |
59 | 8.03k | return version_.load(std::memory_order_acquire); |
60 | 8.03k | } _ZNK2yb14VersionTrackerINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master10TabletInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEE7VersionEv Line | Count | Source | 58 | 4.01k | size_t Version() const { | 59 | 4.01k | return version_.load(std::memory_order_acquire); | 60 | 4.01k | } |
_ZNK2yb14VersionTrackerINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master9TableInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEE7VersionEv Line | Count | Source | 58 | 4.01k | size_t Version() const { | 59 | 4.01k | return version_.load(std::memory_order_acquire); | 60 | 4.01k | } |
|
61 | | |
62 | | private: |
63 | | friend class VersionTrackerCheckOut<Value>; |
64 | | |
65 | | Value value_; |
66 | | std::atomic<size_t> version_{0}; |
67 | | }; |
68 | | |
69 | | template <class Value> |
70 | | class VersionTrackerCheckOut { |
71 | | public: |
72 | | VersionTrackerCheckOut(const VersionTrackerCheckOut&) = delete; |
73 | | void operator=(const VersionTrackerCheckOut&) = delete; |
74 | | |
75 | | VersionTrackerCheckOut(VersionTrackerCheckOut&& rhs) : tracker_(rhs.tracker_) { |
76 | | rhs.tracker_ = nullptr; |
77 | | } |
78 | | |
79 | 289k | explicit VersionTrackerCheckOut(VersionTracker<Value>* tracker) : tracker_(tracker) {} _ZN2yb22VersionTrackerCheckOutINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master9TableInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEEC2EPNS_14VersionTrackerISJ_EE Line | Count | Source | 79 | 248k | explicit VersionTrackerCheckOut(VersionTracker<Value>* tracker) : tracker_(tracker) {} |
_ZN2yb22VersionTrackerCheckOutINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master10TabletInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEEC2EPNS_14VersionTrackerISJ_EE Line | Count | Source | 79 | 41.0k | explicit VersionTrackerCheckOut(VersionTracker<Value>* tracker) : tracker_(tracker) {} |
|
80 | | |
81 | 289k | ~VersionTrackerCheckOut() { |
82 | 289k | if (tracker_) { |
83 | 289k | tracker_->Commit(); |
84 | 289k | } |
85 | 289k | } _ZN2yb22VersionTrackerCheckOutINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master9TableInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEED2Ev Line | Count | Source | 81 | 248k | ~VersionTrackerCheckOut() { | 82 | 248k | if (tracker_) { | 83 | 248k | tracker_->Commit(); | 84 | 248k | } | 85 | 248k | } |
_ZN2yb22VersionTrackerCheckOutINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master10TabletInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEED2Ev Line | Count | Source | 81 | 41.0k | ~VersionTrackerCheckOut() { | 82 | 41.0k | if (tracker_) { | 83 | 41.0k | tracker_->Commit(); | 84 | 41.0k | } | 85 | 41.0k | } |
|
86 | | |
87 | 243k | Value& operator*() { |
88 | 243k | return tracker_->value_; |
89 | 243k | } _ZN2yb22VersionTrackerCheckOutINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master10TabletInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEEdeEv Line | Count | Source | 87 | 1.63k | Value& operator*() { | 88 | 1.63k | return tracker_->value_; | 89 | 1.63k | } |
_ZN2yb22VersionTrackerCheckOutINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master9TableInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEEdeEv Line | Count | Source | 87 | 242k | Value& operator*() { | 88 | 242k | return tracker_->value_; | 89 | 242k | } |
|
90 | | |
91 | 13.9k | Value* operator->() { |
92 | 13.9k | return get_ptr(); |
93 | 13.9k | } _ZN2yb22VersionTrackerCheckOutINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master9TableInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEEptEv Line | Count | Source | 91 | 3.96k | Value* operator->() { | 92 | 3.96k | return get_ptr(); | 93 | 3.96k | } |
_ZN2yb22VersionTrackerCheckOutINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master10TabletInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEEptEv Line | Count | Source | 91 | 10.0k | Value* operator->() { | 92 | 10.0k | return get_ptr(); | 93 | 10.0k | } |
|
94 | | |
95 | 69.6k | Value* get_ptr() { |
96 | 69.6k | return &tracker_->value_; |
97 | 69.6k | } _ZN2yb22VersionTrackerCheckOutINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master9TableInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEE7get_ptrEv Line | Count | Source | 95 | 5.96k | Value* get_ptr() { | 96 | 5.96k | return &tracker_->value_; | 97 | 5.96k | } |
_ZN2yb22VersionTrackerCheckOutINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master10TabletInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEE7get_ptrEv Line | Count | Source | 95 | 63.7k | Value* get_ptr() { | 96 | 63.7k | return &tracker_->value_; | 97 | 63.7k | } |
|
98 | | |
99 | | private: |
100 | | VersionTracker<Value>* tracker_; |
101 | | }; |
102 | | |
103 | | template <class Value> |
104 | 289k | VersionTrackerCheckOut<Value> VersionTracker<Value>::CheckOut() { |
105 | 289k | return VersionTrackerCheckOut<Value>(this); |
106 | 289k | } _ZN2yb14VersionTrackerINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master9TableInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEE8CheckOutEv Line | Count | Source | 104 | 248k | VersionTrackerCheckOut<Value> VersionTracker<Value>::CheckOut() { | 105 | 248k | return VersionTrackerCheckOut<Value>(this); | 106 | 248k | } |
_ZN2yb14VersionTrackerINSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE13scoped_refptrINS_6master10TabletInfoEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEEE8CheckOutEv Line | Count | Source | 104 | 41.0k | VersionTrackerCheckOut<Value> VersionTracker<Value>::CheckOut() { | 105 | 41.0k | return VersionTrackerCheckOut<Value>(this); | 106 | 41.0k | } |
|
107 | | |
108 | | } // namespace yb |
109 | | |
110 | | #endif // YB_UTIL_VERSION_TRACKER_H |