YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
21.5M
void OpId::MakeAtLeast(const OpId& rhs) {
29
21.5M
  if (rhs.empty()) {
30
28.9k
    return;
31
28.9k
  }
32
21.4M
  if (empty()) {
33
30.5k
    *this = rhs;
34
30.5k
    return;
35
30.5k
  }
36
21.4M
  if (rhs.term > term || (rhs.term == term && rhs.index > index)) {
37
8.37M
    *this = rhs;
38
8.37M
  }
39
21.4M
}
40
41
6.34M
void OpId::MakeAtMost(const OpId& rhs) {
42
6.34M
  if (rhs.empty()) {
43
0
    return;
44
0
  }
45
6.34M
  if (empty()) {
46
0
    *this = rhs;
47
0
    return;
48
0
  }
49
6.34M
  if (rhs.term < term || (rhs.term == term && rhs.index < index)) {
50
1.60k
    *this = rhs;
51
1.60k
  }
52
6.34M
}
53
54
5.48M
std::string OpId::ToString() const {
55
5.48M
  return Format("$0.$1", term, index);
56
5.48M
}
57
58
465
Result<OpId> OpId::FromString(Slice input) {
59
465
  auto pos = std::find(input.cdata(), input.cend(), '.');
60
465
  if (pos == input.cend()) {
61
0
    return STATUS(InvalidArgument, "OpId should contain '.'", input);
62
0
  }
63
465
  auto term = VERIFY_RESULT(CheckedStoll(Slice(input.cdata(), pos)));
64
465
  auto index = VERIFY_RESULT(CheckedStoll(Slice(pos + 1, input.cend())));
65
465
  return OpId(term, index);
66
465
}
67
68
347k
std::ostream& operator<<(std::ostream& out, const OpId& op_id) {
69
347k
  return out << op_id.term << "." << op_id.index;
70
347k
}
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