YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
521M
  OpId() noexcept : term(0), index(0) {}
32
960M
  OpId(int64_t term_, int64_t index_) noexcept : term(term_), index(index_) {}
33
34
91.3M
  static OpId Invalid() {
35
91.3M
    return OpId(kUnknownTerm, -1);
36
91.3M
  }
37
38
365M
  static OpId Max() {
39
365M
    return OpId(std::numeric_limits<int64_t>::max(), std::numeric_limits<int64_t>::max());
40
365M
  }
41
42
36.8M
  static OpId Min() {
43
36.8M
    return OpId();
44
36.8M
  }
45
46
98.9M
  bool valid() const {
47
98.9M
    return term >= 0 && 
index >= 034.9M
;
48
98.9M
  }
49
50
311M
  bool empty() const {
51
311M
    return term == 0 && 
index == 047.4M
;
52
311M
  }
53
54
15.0M
  bool operator!() const {
55
15.0M
    return empty();
56
15.0M
  }
57
58
  void MakeAtMost(const OpId& rhs);
59
  void MakeAtLeast(const OpId& rhs);
60
61
  template <class PB>
62
244M
  void ToPB(PB* out) const {
63
244M
    out->set_term(term);
64
244M
    out->set_index(index);
65
244M
  }
void yb::OpId::ToPB<yb::OpIdPB>(yb::OpIdPB*) const
Line
Count
Source
62
244M
  void ToPB(PB* out) const {
63
244M
    out->set_term(term);
64
244M
    out->set_index(index);
65
244M
  }
void yb::OpId::ToPB<yb::cdc::CDCSDKCheckpointPB>(yb::cdc::CDCSDKCheckpointPB*) const
Line
Count
Source
62
605
  void ToPB(PB* out) const {
63
605
    out->set_term(term);
64
605
    out->set_index(index);
65
605
  }
66
67
  template <class PB>
68
150k
  PB ToPB() const {
69
150k
    PB out;
70
150k
    out.set_term(term);
71
150k
    out.set_index(index);
72
150k
    return out;
73
150k
  }
74
75
  template <class PB>
76
453M
  static OpId FromPB(const PB& pb) {
77
453M
    return OpId(pb.term(), pb.index());
78
453M
  }
yb::OpId yb::OpId::FromPB<yb::OpIdPB>(yb::OpIdPB const&)
Line
Count
Source
76
453M
  static OpId FromPB(const PB& pb) {
77
453M
    return OpId(pb.term(), pb.index());
78
453M
  }
yb::OpId yb::OpId::FromPB<yb::cdc::CDCSDKCheckpointPB>(yb::cdc::CDCSDKCheckpointPB const&)
Line
Count
Source
76
641
  static OpId FromPB(const PB& pb) {
77
641
    return OpId(pb.term(), pb.index());
78
641
  }
79
80
  std::string ToString() const;
81
82
  // Parse OpId from TERM.INDEX string.
83
  static Result<OpId> FromString(Slice input);
84
};
85
86
289M
inline bool operator==(const OpId& lhs, const OpId& rhs) {
87
289M
  return lhs.term == rhs.term && 
lhs.index == rhs.index115M
;
88
289M
}
89
90
165M
inline bool operator!=(const OpId& lhs, const OpId& rhs) {
91
165M
  return !(lhs == rhs);
92
165M
}
93
94
280M
inline bool operator<(const OpId& lhs, const OpId& rhs) {
95
280M
  return (lhs.term < rhs.term) || 
(199M
lhs.term == rhs.term199M
&&
lhs.index < rhs.index190M
);
96
280M
}
97
98
3.14M
inline bool operator<=(const OpId& lhs, const OpId& rhs) {
99
3.14M
  return !(rhs < lhs);
100
3.14M
}
101
102
8.77M
inline bool operator>(const OpId& lhs, const OpId& rhs) {
103
8.77M
  return rhs < lhs;
104
8.77M
}
105
106
18.3M
inline bool operator>=(const OpId& lhs, const OpId& rhs) {
107
18.3M
  return !(lhs < rhs);
108
18.3M
}
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