/Users/deen/code/yugabyte-db/src/yb/util/opid.h
Line | Count | Source (jump to first uncovered line) |
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 | | #ifndef YB_UTIL_OPID_H |
16 | | #define YB_UTIL_OPID_H |
17 | | |
18 | | #include <iosfwd> |
19 | | #include <vector> |
20 | | |
21 | | #include "yb/util/slice.h" |
22 | | |
23 | | namespace yb { |
24 | | |
25 | | struct OpId { |
26 | | static constexpr int64_t kUnknownTerm = -1; |
27 | | |
28 | | int64_t term; |
29 | | int64_t index; |
30 | | |
31 | 230M | OpId() noexcept : term(0), index(0) {} |
32 | 411M | OpId(int64_t term_, int64_t index_) noexcept : term(term_), index(index_) {} |
33 | | |
34 | 19.2M | static OpId Invalid() { |
35 | 19.2M | return OpId(kUnknownTerm, -1); |
36 | 19.2M | } |
37 | | |
38 | 143M | static OpId Max() { |
39 | 143M | return OpId(std::numeric_limits<int64_t>::max(), std::numeric_limits<int64_t>::max()); |
40 | 143M | } |
41 | | |
42 | 16.3M | static OpId Min() { |
43 | 16.3M | return OpId(); |
44 | 16.3M | } |
45 | | |
46 | 17.9M | bool valid() const { |
47 | 17.9M | return term >= 0 && index >= 0; |
48 | 17.9M | } |
49 | | |
50 | 151M | bool empty() const { |
51 | 151M | return term == 0 && index == 0; |
52 | 151M | } |
53 | | |
54 | 8.17M | bool operator!() const { |
55 | 8.17M | return empty(); |
56 | 8.17M | } |
57 | | |
58 | | void MakeAtMost(const OpId& rhs); |
59 | | void MakeAtLeast(const OpId& rhs); |
60 | | |
61 | | template <class PB> |
62 | 116M | void ToPB(PB* out) const { |
63 | 116M | out->set_term(term); |
64 | 116M | out->set_index(index); |
65 | 116M | } _ZNK2yb4OpId4ToPBINS_6OpIdPBEEEvPT_ Line | Count | Source | 62 | 116M | void ToPB(PB* out) const { | 63 | 116M | out->set_term(term); | 64 | 116M | out->set_index(index); | 65 | 116M | } |
_ZNK2yb4OpId4ToPBINS_3cdc18CDCSDKCheckpointPBEEEvPT_ Line | Count | Source | 62 | 301 | void ToPB(PB* out) const { | 63 | 301 | out->set_term(term); | 64 | 301 | out->set_index(index); | 65 | 301 | } |
|
66 | | |
67 | | template <class PB> |
68 | 88.7k | PB ToPB() const { |
69 | 88.7k | PB out; |
70 | 88.7k | out.set_term(term); |
71 | 88.7k | out.set_index(index); |
72 | 88.7k | return out; |
73 | 88.7k | } |
74 | | |
75 | | template <class PB> |
76 | 227M | static OpId FromPB(const PB& pb) { |
77 | 227M | return OpId(pb.term(), pb.index()); |
78 | 227M | } _ZN2yb4OpId6FromPBINS_6OpIdPBEEES0_RKT_ Line | Count | Source | 76 | 227M | static OpId FromPB(const PB& pb) { | 77 | 227M | return OpId(pb.term(), pb.index()); | 78 | 227M | } |
_ZN2yb4OpId6FromPBINS_3cdc18CDCSDKCheckpointPBEEES0_RKT_ Line | Count | Source | 76 | 319 | static OpId FromPB(const PB& pb) { | 77 | 319 | return OpId(pb.term(), pb.index()); | 78 | 319 | } |
|
79 | | |
80 | | std::string ToString() const; |
81 | | |
82 | | // Parse OpId from TERM.INDEX string. |
83 | | static Result<OpId> FromString(Slice input); |
84 | | }; |
85 | | |
86 | 128M | inline bool operator==(const OpId& lhs, const OpId& rhs) { |
87 | 128M | return lhs.term == rhs.term && lhs.index == rhs.index; |
88 | 128M | } |
89 | | |
90 | 70.9M | inline bool operator!=(const OpId& lhs, const OpId& rhs) { |
91 | 70.9M | return !(lhs == rhs); |
92 | 70.9M | } |
93 | | |
94 | 143M | inline bool operator<(const OpId& lhs, const OpId& rhs) { |
95 | 143M | return (lhs.term < rhs.term) || (lhs.term == rhs.term && lhs.index < rhs.index); |
96 | 143M | } |
97 | | |
98 | 2.34M | inline bool operator<=(const OpId& lhs, const OpId& rhs) { |
99 | 2.34M | return !(rhs < lhs); |
100 | 2.34M | } |
101 | | |
102 | 5.00M | inline bool operator>(const OpId& lhs, const OpId& rhs) { |
103 | 5.00M | return rhs < lhs; |
104 | 5.00M | } |
105 | | |
106 | 10.8M | inline bool operator>=(const OpId& lhs, const OpId& rhs) { |
107 | 10.8M | return !(lhs < rhs); |
108 | 10.8M | } |
109 | | |
110 | | std::ostream& operator<<(std::ostream& out, const OpId& op_id); |
111 | | |
112 | | size_t hash_value(const OpId& op_id) noexcept; |
113 | | |
114 | | struct OpIdHash { |
115 | 0 | size_t operator()(const OpId& v) const { |
116 | 0 | return hash_value(v); |
117 | 0 | } |
118 | | }; |
119 | | |
120 | | typedef std::vector<OpId> OpIds; |
121 | | |
122 | | } // namespace yb |
123 | | |
124 | | #endif // YB_UTIL_OPID_H |