/Users/deen/code/yugabyte-db/src/yb/util/opid.cc
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 | | |
16 | | #include "yb/util/opid.h" |
17 | | |
18 | | #include <algorithm> |
19 | | |
20 | | #include <boost/functional/hash.hpp> |
21 | | |
22 | | #include "yb/util/stol_utils.h" |
23 | | |
24 | | namespace yb { |
25 | | |
26 | | constexpr int64_t OpId::kUnknownTerm; |
27 | | |
28 | 46.3M | void OpId::MakeAtLeast(const OpId& rhs) { |
29 | 46.3M | if (rhs.empty()) { |
30 | 51.2k | return; |
31 | 51.2k | } |
32 | 46.2M | if (empty()) { |
33 | 51.4k | *this = rhs; |
34 | 51.4k | return; |
35 | 51.4k | } |
36 | 46.2M | if (rhs.term > term || (46.1M rhs.term == term46.1M && rhs.index > index46.1M )) { |
37 | 15.4M | *this = rhs; |
38 | 15.4M | } |
39 | 46.2M | } |
40 | | |
41 | 11.5M | void OpId::MakeAtMost(const OpId& rhs) { |
42 | 11.5M | if (rhs.empty()) { |
43 | 18 | return; |
44 | 18 | } |
45 | 11.5M | if (empty()) { |
46 | 0 | *this = rhs; |
47 | 0 | return; |
48 | 0 | } |
49 | 11.5M | if (rhs.term < term || (11.5M rhs.term == term11.5M && rhs.index < index9.35M )) { |
50 | 1.93k | *this = rhs; |
51 | 1.93k | } |
52 | 11.5M | } |
53 | | |
54 | 11.8M | std::string OpId::ToString() const { |
55 | 11.8M | return Format("$0.$1", term, index); |
56 | 11.8M | } |
57 | | |
58 | 834 | Result<OpId> OpId::FromString(Slice input) { |
59 | 834 | auto pos = std::find(input.cdata(), input.cend(), '.'); |
60 | 834 | if (pos == input.cend()) { |
61 | 0 | return STATUS(InvalidArgument, "OpId should contain '.'", input); |
62 | 0 | } |
63 | 834 | auto term = VERIFY_RESULT(CheckedStoll(Slice(input.cdata(), pos))); |
64 | 834 | auto index = VERIFY_RESULT(CheckedStoll(Slice(pos + 1, input.cend()))); |
65 | 0 | return OpId(term, index); |
66 | 834 | } |
67 | | |
68 | 602k | std::ostream& operator<<(std::ostream& out, const OpId& op_id) { |
69 | 602k | return out << op_id.term << "." << op_id.index; |
70 | 602k | } |
71 | | |
72 | 0 | size_t hash_value(const OpId& op_id) noexcept { |
73 | 0 | size_t result = 0; |
74 | |
|
75 | 0 | boost::hash_combine(result, op_id.term); |
76 | 0 | boost::hash_combine(result, op_id.index); |
77 | |
|
78 | 0 | return result; |
79 | 0 | } |
80 | | |
81 | | } // namespace yb |