YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/common/column_id.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#ifndef YB_COMMON_COLUMN_ID_H
15
#define YB_COMMON_COLUMN_ID_H
16
17
#include <stdint.h>
18
19
#include <iosfwd>
20
#include <limits>
21
#include <string>
22
23
#include "yb/util/status_fwd.h"
24
25
namespace yb {
26
27
template<char digit1, char... digits>
28
struct ColumnIdHelper {
29
  typedef ColumnIdHelper<digit1> Current;
30
  typedef ColumnIdHelper<digits...> Next;
31
  static constexpr int mul = Next::mul * 10;
32
  static constexpr int value = Current::value * mul + Next::value;
33
  static_assert(value <= std::numeric_limits<int32_t>::max(), "Too big constant");
34
};
35
36
template<char digit>
37
struct ColumnIdHelper<digit> {
38
  static_assert(digit >= '0' && digit <= '9', "Only digits is allowed");
39
  static constexpr int value = digit - '0';
40
  static constexpr int mul = 1;
41
};
42
43
typedef int32_t ColumnIdRep;
44
45
// The ID of a column. Each column in a table has a unique ID.
46
class ColumnId {
47
 public:
48
  explicit ColumnId(ColumnIdRep t_);
49
50
  template<char... digits>
51
  constexpr explicit ColumnId(ColumnIdHelper<digits...>) : t_(ColumnIdHelper<digits...>::value) {}
52
53
536M
  ColumnId() : t_() {}
54
5.84G
  constexpr ColumnId(const ColumnId& t) : t_(t.t_) {}
55
791M
  ColumnId& operator=(const ColumnId& rhs) { t_ = rhs.t_; return *this; }
56
  ColumnId& operator=(const ColumnIdRep& rhs);
57
3.05G
  operator ColumnIdRep() const { return t_; }
58
1.65G
  ColumnIdRep rep() const { return t_; }
59
60
1.54M
  bool operator==(const ColumnId& rhs) const { return t_ == rhs.t_; }
61
0
  bool operator!=(const ColumnId& rhs) const { return t_ != rhs.t_; }
62
14.1G
  bool operator<(const ColumnId& rhs) const { return t_ < rhs.t_; }
63
83.1M
  bool operator>(const ColumnId& rhs) const { return t_ > rhs.t_; }
64
65
868k
  std::string ToString() const {
66
868k
    return std::to_string(t_);
67
868k
  }
68
69
  uint64_t ToUint64() const;
70
71
  static CHECKED_STATUS FromInt64(int64_t value, ColumnId *column_id);
72
73
1.22M
  size_t hash() const {
74
1.22M
    return t_;
75
1.22M
  }
76
77
 private:
78
  ColumnIdRep t_;
79
};
80
81
1.22M
inline size_t hash_value(ColumnId col) {
82
1.22M
  return col.hash();
83
1.22M
}
84
85
std::ostream& operator<<(std::ostream& os, ColumnId column_id);
86
87
88
static const ColumnId kInvalidColumnId = ColumnId(std::numeric_limits<ColumnIdRep>::max());
89
90
// In a new schema, we typically would start assigning column IDs at 0. However, this
91
// makes it likely that in many test cases, the column IDs and the column indexes are
92
// equal to each other, and it's easy to accidentally pass an index where we meant to pass
93
// an ID, without having any issues. So, in DEBUG builds, we start assigning columns at ID
94
// 10, ensuring that if we accidentally mix up IDs and indexes, we're likely to fire an
95
// assertion or bad memory access.
96
#ifdef NDEBUG
97
constexpr ColumnIdRep kFirstColumnIdRep = 0;
98
#else
99
constexpr ColumnIdRep kFirstColumnIdRep = 10;
100
#endif
101
const ColumnId kFirstColumnId(kFirstColumnIdRep);
102
103
template<char... digits>
104
ColumnId operator"" _ColId() {
105
  return ColumnId(ColumnIdHelper<digits...>());
106
}
107
108
}  // namespace yb
109
110
#endif  // YB_COMMON_COLUMN_ID_H