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