YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/client/yb_table_name.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_CLIENT_YB_TABLE_NAME_H_
15
#define YB_CLIENT_YB_TABLE_NAME_H_
16
17
#include <string>
18
#include <boost/optional.hpp>
19
20
#include <gflags/gflags_declare.h>
21
22
#include "yb/common/common_types.pb.h"
23
24
#include "yb/master/master_types.pb.h"
25
26
namespace yb {
27
28
namespace master {
29
class NamespaceIdentifierPB;
30
class TableIdentifierPB;
31
}
32
33
namespace client {
34
35
// Is system keyspace read-only?
36
DECLARE_bool(yb_system_namespace_readonly);
37
38
// The class is used to store a table name, which can include namespace name as a suffix.
39
class YBTableName {
40
 public:
41
  // Empty (undefined) name.
42
364k
  YBTableName() : namespace_type_(YQL_DATABASE_UNKNOWN) {}
43
44
  // Complex table name: 'namespace_name.table_name'.
45
  // The namespace must not be empty.
46
  // For the case of undefined namespace the next constructor must be used.
47
  YBTableName(YQLDatabase db_type,
48
              const std::string& namespace_name,
49
1.37M
              const std::string& table_name) : namespace_type_(db_type) {
50
1.37M
    set_namespace_name(namespace_name);
51
1.37M
    set_table_name(table_name);
52
1.37M
  }
53
54
  YBTableName(YQLDatabase db_type,
55
              const std::string& namespace_id,
56
              const std::string& namespace_name,
57
5.15k
              const std::string& table_name) : namespace_type_(db_type) {
58
5.15k
    set_namespace_id(namespace_id);
59
5.15k
    set_namespace_name(namespace_name);
60
5.15k
    set_table_name(table_name);
61
5.15k
  }
62
63
  YBTableName(YQLDatabase db_type,
64
              const std::string& namespace_id,
65
              const std::string& namespace_name,
66
              const std::string& table_id,
67
              const std::string& table_name,
68
              const std::string& pgschema_name = "",
69
              boost::optional<master::RelationType> relation_type = boost::none)
70
27.7k
            : namespace_type_(db_type) {
71
27.7k
    set_namespace_id(namespace_id);
72
27.7k
    set_namespace_name(namespace_name);
73
27.7k
    set_table_id(table_id);
74
27.7k
    set_table_name(table_name);
75
27.7k
    set_pgschema_name(pgschema_name);
76
27.7k
    set_relation_type(relation_type);
77
27.7k
  }
78
79
  // Simple table name (no namespace provided at the moment of construction).
80
  // In this case the namespace has not been set yet and it MUST be set later.
81
173
  YBTableName(YQLDatabase db_type, const std::string& table_name) : namespace_type_(db_type) {
82
173
    set_table_name(table_name);
83
173
  }
84
85
5.67k
  bool empty() const {
86
5.67k
    return namespace_id_.empty() && 
namespace_name_.empty()688
&&
table_name_.empty()21
;
87
5.67k
  }
88
89
9.97M
  bool has_namespace() const {
90
9.97M
    return !namespace_name_.empty();
91
9.97M
  }
92
93
2.98M
  const std::string& namespace_name() const {
94
2.98M
    return namespace_name_; // Can be empty.
95
2.98M
  }
96
97
60.3k
  const std::string& namespace_id() const {
98
60.3k
    return namespace_id_; // Can be empty.
99
60.3k
  }
100
101
311
  YQLDatabase namespace_type() const {
102
311
    return namespace_type_;
103
311
  }
104
105
  const std::string& resolved_namespace_name() const;
106
107
37.3k
  bool has_table() const {
108
37.3k
    return !table_name_.empty();
109
37.3k
  }
110
111
3.03M
  const std::string& table_name() const {
112
3.03M
    return table_name_;
113
3.03M
  }
114
115
0
  bool has_table_id() const {
116
0
    return !table_id_.empty();
117
0
  }
118
119
30.6k
  const std::string& table_id() const {
120
30.6k
    return table_id_; // Can be empty
121
30.6k
  }
122
123
0
  bool has_pgschema_name() const {
124
0
    return !pgschema_name_.empty();
125
0
  }
126
127
0
  const std::string& pgschema_name() const {
128
0
    return pgschema_name_; // Can be empty
129
0
  }
130
131
0
  boost::optional<master::RelationType> relation_type() const {
132
0
    return relation_type_;
133
0
  }
134
135
  bool is_system() const;
136
137
4
  bool is_cql_namespace() const {
138
4
    return namespace_type_ == YQL_DATABASE_CQL;
139
4
  }
140
141
328k
  bool is_redis_namespace() const {
142
328k
    return namespace_type_ == YQL_DATABASE_REDIS;
143
328k
  }
144
145
  bool is_redis_table() const;
146
147
26.8k
  std::string ToString() const {
148
26.8k
    return has_namespace() ? 
(namespace_name_ + '.' + table_name_)26.8k
:
table_name_3
;
149
26.8k
  }
150
151
  void set_namespace_id(const std::string& namespace_id);
152
  void set_namespace_name(const std::string& namespace_name);
153
  void set_table_name(const std::string& table_name);
154
  void set_table_id(const std::string& table_id);
155
  void set_pgschema_name(const std::string& pgschema_name);
156
157
27.7k
  void set_relation_type(boost::optional<master::RelationType> relation_type) {
158
27.7k
    relation_type_ = relation_type;
159
27.7k
  }
160
161
  // ProtoBuf helpers.
162
  void SetIntoTableIdentifierPB(master::TableIdentifierPB* id) const;
163
  void GetFromTableIdentifierPB(const master::TableIdentifierPB& id);
164
165
  void SetIntoNamespaceIdentifierPB(master::NamespaceIdentifierPB* id) const;
166
  void GetFromNamespaceIdentifierPB(const master::NamespaceIdentifierPB& id);
167
168
 private:
169
  void check_db_type();
170
171
  std::string namespace_id_; // Optional. Can be set when the client knows the namespace id.
172
  std::string namespace_name_; // Can be empty, that means the namespace has not been set yet.
173
  YQLDatabase namespace_type_; // Can be empty, that means the namespace id will be used.
174
  std::string table_id_; // Optional. Can be set when client knows the table id also.
175
  std::string table_name_;
176
  std::string pgschema_name_; // Can be empty
177
  // Optional. Can be set when the client knows the table type.
178
  boost::optional<master::RelationType> relation_type_;
179
};
180
181
736k
inline bool operator ==(const YBTableName& lhs, const YBTableName& rhs) {
182
  // Not comparing namespace_id and table_id because they are optional.
183
736k
  return (lhs.namespace_name() == rhs.namespace_name() && 
lhs.table_name() == rhs.table_name()716k
);
184
736k
}
185
186
0
inline bool operator !=(const YBTableName& lhs, const YBTableName& rhs) {
187
0
  return !(lhs == rhs);
188
0
}
189
190
// In order to be able to use YBTableName with boost::hash
191
size_t hash_value(const YBTableName& table_name);
192
193
}  // namespace client
194
}  // namespace yb
195
196
#endif  // YB_CLIENT_YB_TABLE_NAME_H_