/Users/deen/code/yugabyte-db/src/yb/client/table_creator.h
Line | Count | Source |
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_TABLE_CREATOR_H |
15 | | #define YB_CLIENT_TABLE_CREATOR_H |
16 | | |
17 | | #include <boost/optional/optional.hpp> |
18 | | |
19 | | #include "yb/client/client_fwd.h" |
20 | | #include "yb/client/yb_table_name.h" |
21 | | |
22 | | #include "yb/common/constants.h" |
23 | | #include "yb/common/common_fwd.h" |
24 | | |
25 | | #include "yb/gutil/macros.h" |
26 | | |
27 | | #include "yb/master/master_fwd.h" |
28 | | |
29 | | #include "yb/util/monotime.h" |
30 | | |
31 | | namespace yb { |
32 | | struct TransactionMetadata; |
33 | | |
34 | | namespace client { |
35 | | |
36 | | // Creates a new table with the desired options. |
37 | | class YBTableCreator { |
38 | | public: |
39 | | ~YBTableCreator(); |
40 | | |
41 | | // Sets the name to give the table. It is copied. Required. |
42 | | YBTableCreator& table_name(const YBTableName& name); |
43 | | |
44 | | // Sets the type of the table. |
45 | | YBTableCreator& table_type(YBTableType table_type); |
46 | | |
47 | | // Sets the name of the role creating this table. |
48 | | YBTableCreator& creator_role_name(const RoleName& creator_role_name); |
49 | | |
50 | | // For Postgres: sets table id to assign, and whether the table is a sys catalog / shared table. |
51 | | YBTableCreator& table_id(const std::string& table_id); |
52 | | YBTableCreator& is_pg_catalog_table(); |
53 | | YBTableCreator& is_pg_shared_table(); |
54 | | |
55 | | // Sets the partition hash schema. |
56 | | YBTableCreator& hash_schema(YBHashSchema hash_schema); |
57 | | |
58 | | // Number of tablets that should be used for this table. If tablet_count is not given, YBClient |
59 | | // will calculate this value (num_shards_per_tserver * num_of_tservers). |
60 | | YBTableCreator& num_tablets(int32_t count); |
61 | | |
62 | | // Whether this table should be colocated. Will be ignored by catalog manager if the database is |
63 | | // not colocated. |
64 | | YBTableCreator& colocated(const bool colocated); |
65 | | |
66 | | // Tablegroup ID - will be ignored by catalog manager if the table is not in a tablegroup. |
67 | | YBTableCreator& tablegroup_id(const std::string& tablegroup_id); |
68 | | |
69 | | YBTableCreator& colocation_id(ColocationId colocation_id); |
70 | | |
71 | | YBTableCreator& tablespace_id(const std::string& tablespace_id); |
72 | | |
73 | | YBTableCreator& matview_pg_table_id(const std::string& matview_pg_table_id); |
74 | | |
75 | | // Sets the schema with which to create the table. Must remain valid for |
76 | | // the lifetime of the builder. Required. |
77 | | YBTableCreator& schema(const YBSchema* schema); |
78 | | |
79 | | // The creation of this table is dependent upon the success of this higher-level transaction. |
80 | | YBTableCreator& part_of_transaction(const TransactionMetadata* txn); |
81 | | |
82 | | // Adds a partitions to the table. |
83 | | YBTableCreator& add_partition(const Partition& partition); |
84 | | |
85 | | // Adds a set of hash partitions to the table. |
86 | | // |
87 | | // For each set of hash partitions added to the table, the total number of |
88 | | // table partitions is multiplied by the number of buckets. For example, if a |
89 | | // table is created with 3 split rows, and two hash partitions with 4 and 5 |
90 | | // buckets respectively, the total number of table partitions will be 80 |
91 | | // (4 range partitions * 4 hash buckets * 5 hash buckets). |
92 | | YBTableCreator& add_hash_partitions(const std::vector<std::string>& columns, |
93 | | int32_t num_buckets); |
94 | | |
95 | | // Adds a set of hash partitions to the table. |
96 | | // |
97 | | // This constructor takes a seed value, which can be used to randomize the |
98 | | // mapping of rows to hash buckets. Setting the seed may provide some |
99 | | // amount of protection against denial of service attacks when the hashed |
100 | | // columns contain user provided values. |
101 | | YBTableCreator& add_hash_partitions(const std::vector<std::string>& columns, |
102 | | int32_t num_buckets, int32_t seed); |
103 | | |
104 | | // Sets the columns on which the table will be range-partitioned. |
105 | | // |
106 | | // Every column must be a part of the table's primary key. If not set, the |
107 | | // table will be created with the primary-key columns as the range-partition |
108 | | // columns. If called with an empty vector, the table will be created without |
109 | | // range partitioning. |
110 | | // |
111 | | // Optional. |
112 | | YBTableCreator& set_range_partition_columns( |
113 | | const std::vector<std::string>& columns, |
114 | | const std::vector<std::string>& split_rows = {}); |
115 | | |
116 | | // For index table: sets the indexed table id of this index. |
117 | | YBTableCreator& indexed_table_id(const std::string& id); |
118 | | |
119 | | // For index table: uses the old style request without index_info. |
120 | | YBTableCreator& TEST_use_old_style_create_request(); |
121 | | |
122 | | // For index table: sets whether this is a local index. |
123 | | YBTableCreator& is_local_index(bool is_local_index); |
124 | | |
125 | | // For index table: sets whether this is a unique index. |
126 | | YBTableCreator& is_unique_index(bool is_unique_index); |
127 | | |
128 | | // For index table: should backfill be deferred for batching. |
129 | | YBTableCreator& is_backfill_deferred(bool is_backfill_deferred); |
130 | | |
131 | | // For index table: sets whether to do online schema migration when creating index. |
132 | | YBTableCreator& skip_index_backfill(const bool skip_index_backfill); |
133 | | |
134 | | // For index table: indicates whether this index has mangled column name. |
135 | | // - Older index supports only ColumnRef, and its name is identical with colum name. |
136 | | // - Newer index supports expressions including ColumnRef, and its name is a mangled name of |
137 | | // the expression. For example, ColumnRef name = "$C_" + escape_column_name. |
138 | | YBTableCreator& use_mangled_column_name(bool value); |
139 | | |
140 | | // Return index_info for caller to fill index information. |
141 | 445 | IndexInfoPB* mutable_index_info() { |
142 | 445 | return index_info_.get(); |
143 | 445 | } |
144 | | |
145 | | // Set the timeout for the operation. This includes any waiting |
146 | | // after the create has been submitted (i.e if the create is slow |
147 | | // to be performed for a large table, it may time out and then |
148 | | // later be successful). |
149 | | YBTableCreator& timeout(const MonoDelta& timeout); |
150 | | |
151 | | // Wait for the table to be fully created before returning. |
152 | | // Optional. |
153 | | // |
154 | | // If not provided, defaults to true. |
155 | | YBTableCreator& wait(bool wait); |
156 | | |
157 | | YBTableCreator& replication_info(const master::ReplicationInfoPB& ri); |
158 | | |
159 | | // Creates the table. |
160 | | // |
161 | | // The return value may indicate an error in the create table operation, |
162 | | // or a misuse of the builder; in the latter case, only the last error is |
163 | | // returned. |
164 | | CHECKED_STATUS Create(); |
165 | | |
166 | | Result<int> NumTabletsForUserTable(); |
167 | | |
168 | | private: |
169 | | friend class YBClient; |
170 | | |
171 | | explicit YBTableCreator(YBClient* client); |
172 | | |
173 | | YBClient* const client_; |
174 | | |
175 | | YBTableName table_name_; // Required. |
176 | | |
177 | | TableType table_type_ = TableType::DEFAULT_TABLE_TYPE; |
178 | | |
179 | | RoleName creator_role_name_; |
180 | | |
181 | | // For Postgres: table id to assign, and whether the table is a sys catalog / shared table. |
182 | | // For all tables, table_id_ will contain the table id assigned after creation. |
183 | | std::string table_id_; |
184 | | boost::optional<bool> is_pg_catalog_table_; |
185 | | boost::optional<bool> is_pg_shared_table_; |
186 | | |
187 | | int32_t num_tablets_ = 0; |
188 | | |
189 | | const YBSchema* schema_ = nullptr; |
190 | | |
191 | | std::unique_ptr<PartitionSchemaPB> partition_schema_; |
192 | | |
193 | | std::vector<Partition> partitions_; |
194 | | |
195 | | int num_replicas_ = 0; |
196 | | |
197 | | std::unique_ptr<master::ReplicationInfoPB> replication_info_; |
198 | | |
199 | | // When creating index, proxy server construct index_info_, and master server will write it to |
200 | | // the data-table being indexed. |
201 | | std::unique_ptr<IndexInfoPB> index_info_; |
202 | | |
203 | | bool skip_index_backfill_ = false; |
204 | | |
205 | | bool TEST_use_old_style_create_request_ = false; |
206 | | |
207 | | MonoDelta timeout_; |
208 | | bool wait_ = true; |
209 | | |
210 | | bool colocated_ = true; |
211 | | |
212 | | // The tablegroup id to assign (if a table is in a tablegroup). |
213 | | std::string tablegroup_id_; |
214 | | |
215 | | // Colocation ID to distinguish a table within a colocation group. |
216 | | ColocationId colocation_id_ = kColocationIdNotSet; |
217 | | |
218 | | // The id of the tablespace to which this table is to be associated with. |
219 | | std::string tablespace_id_; |
220 | | |
221 | | std::string matview_pg_table_id_; |
222 | | |
223 | | const TransactionMetadata* txn_ = nullptr; |
224 | | |
225 | | DISALLOW_COPY_AND_ASSIGN(YBTableCreator); |
226 | | }; |
227 | | |
228 | | } // namespace client |
229 | | } // namespace yb |
230 | | |
231 | | #endif // YB_CLIENT_TABLE_CREATOR_H |