/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 | 165k | 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.14M | const std::string& table_name) : namespace_type_(db_type) { |
50 | 1.14M | set_namespace_name(namespace_name); |
51 | 1.14M | set_table_name(table_name); |
52 | 1.14M | } |
53 | | |
54 | | YBTableName(YQLDatabase db_type, |
55 | | const std::string& namespace_id, |
56 | | const std::string& namespace_name, |
57 | 1.44k | const std::string& table_name) : namespace_type_(db_type) { |
58 | 1.44k | set_namespace_id(namespace_id); |
59 | 1.44k | set_namespace_name(namespace_name); |
60 | 1.44k | set_table_name(table_name); |
61 | 1.44k | } |
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 | 2.53k | : namespace_type_(db_type) { |
71 | 2.53k | set_namespace_id(namespace_id); |
72 | 2.53k | set_namespace_name(namespace_name); |
73 | 2.53k | set_table_id(table_id); |
74 | 2.53k | set_table_name(table_name); |
75 | 2.53k | set_pgschema_name(pgschema_name); |
76 | 2.53k | set_relation_type(relation_type); |
77 | 2.53k | } |
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 | 165 | YBTableName(YQLDatabase db_type, const std::string& table_name) : namespace_type_(db_type) { |
82 | 165 | set_table_name(table_name); |
83 | 165 | } |
84 | | |
85 | 4.66k | bool empty() const { |
86 | 4.66k | return namespace_id_.empty() && namespace_name_.empty() && table_name_.empty(); |
87 | 4.66k | } |
88 | | |
89 | 5.09M | bool has_namespace() const { |
90 | 5.09M | return !namespace_name_.empty(); |
91 | 5.09M | } |
92 | | |
93 | 2.31M | const std::string& namespace_name() const { |
94 | 2.31M | return namespace_name_; // Can be empty. |
95 | 2.31M | } |
96 | | |
97 | 12 | const std::string& namespace_id() const { |
98 | 12 | return namespace_id_; // Can be empty. |
99 | 12 | } |
100 | | |
101 | 239 | YQLDatabase namespace_type() const { |
102 | 239 | return namespace_type_; |
103 | 239 | } |
104 | | |
105 | | const std::string& resolved_namespace_name() const; |
106 | | |
107 | 19.7k | bool has_table() const { |
108 | 19.7k | return !table_name_.empty(); |
109 | 19.7k | } |
110 | | |
111 | 2.33M | const std::string& table_name() const { |
112 | 2.33M | return table_name_; |
113 | 2.33M | } |
114 | | |
115 | 0 | bool has_table_id() const { |
116 | 0 | return !table_id_.empty(); |
117 | 0 | } |
118 | | |
119 | 12.9k | const std::string& table_id() const { |
120 | 12.9k | return table_id_; // Can be empty |
121 | 12.9k | } |
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 | 0 | bool is_cql_namespace() const { |
138 | 0 | return namespace_type_ == YQL_DATABASE_CQL; |
139 | 0 | } |
140 | | |
141 | 323k | bool is_redis_namespace() const { |
142 | 323k | return namespace_type_ == YQL_DATABASE_REDIS; |
143 | 323k | } |
144 | | |
145 | | bool is_redis_table() const; |
146 | | |
147 | 18.6k | std::string ToString() const { |
148 | 18.6k | return has_namespace() ? (namespace_name_ + '.' + table_name_) : table_name_; |
149 | 18.6k | } |
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 | 2.54k | void set_relation_type(boost::optional<master::RelationType> relation_type) { |
158 | 2.54k | relation_type_ = relation_type; |
159 | 2.54k | } |
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 | 524k | inline bool operator ==(const YBTableName& lhs, const YBTableName& rhs) { |
182 | | // Not comparing namespace_id and table_id because they are optional. |
183 | 524k | return (lhs.namespace_name() == rhs.namespace_name() && lhs.table_name() == rhs.table_name()); |
184 | 524k | } |
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_ |