forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.hh
349 lines (316 loc) · 12.2 KB
/
schema.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright (C) 2015 Cloudius Systems, Ltd.
*/
#pragma once
#include <unordered_map>
#include <boost/range/iterator_range.hpp>
#include <boost/range/join.hpp>
#include <boost/range/algorithm/transform.hpp>
#include "cql3/column_specification.hh"
#include "core/shared_ptr.hh"
#include "types.hh"
#include "compound.hh"
#include "gc_clock.hh"
#include "unimplemented.hh"
#include "utils/UUID.hh"
#include "compress.hh"
using column_id = uint32_t;
// make sure these match the order we like columns back from schema
enum class column_kind { partition_key, clustering_key, static_column, regular_column, };
// CMH this is also manually defined in thrift gen file.
enum class index_type {
keys,
custom,
composites,
none, // cwi: added none to avoid "optional" bs.
};
typedef std::unordered_map<sstring, sstring> index_options_map;
class schema;
struct index_info {
index_info(::index_type = ::index_type::none
, std::experimental::optional<sstring> index_name = std::experimental::optional<sstring>()
, std::experimental::optional<index_options_map> = std::experimental::optional<index_options_map>());
enum index_type index_type = ::index_type::none;
std::experimental::optional<sstring> index_name;
std::experimental::optional<index_options_map> index_options;
};
class column_definition final {
public:
template<typename ColumnRange>
static std::vector<const column_definition*> vectorize(ColumnRange&& columns) {
std::vector<const column_definition*> r;
boost::transform(std::forward<ColumnRange>(columns), std::back_inserter(r), [] (auto& def) { return &def; });
return r;
}
private:
bytes _name;
struct thrift_bits {
thrift_bits()
: is_on_all_components(0)
{}
uint8_t is_on_all_components : 1;
// more...?
};
thrift_bits _thrift_bits;
friend class schema;
public:
column_definition(bytes name, data_type type, column_kind kind, index_info = index_info());
data_type type;
// Unique within (kind, schema instance).
// schema::position() and component_index() depend on the fact that for PK columns this is
// equivalent to component index.
// Note: set by schema::build()
column_id id = 0;
column_kind kind;
::shared_ptr<cql3::column_specification> column_specification;
index_info idx_info;
bool is_static() const { return kind == column_kind::static_column; }
bool is_regular() const { return kind == column_kind::regular_column; }
bool is_partition_key() const { return kind == column_kind::partition_key; }
bool is_clustering_key() const { return kind == column_kind::clustering_key; }
bool is_primary_key() const { return kind == column_kind::partition_key || kind == column_kind::clustering_key; }
bool is_atomic() const { return !type->is_multi_cell(); }
bool is_compact_value() const;
const sstring& name_as_text() const;
const bytes& name() const;
friend std::ostream& operator<<(std::ostream& os, const column_definition& cd) {
return os << cd.name_as_text();
}
bool has_component_index() const {
return is_primary_key();
}
uint32_t component_index() const {
assert(has_component_index());
return id;
}
uint32_t position() const {
if (has_component_index()) {
return component_index();
}
return 0;
}
bool is_on_all_components() const;
friend bool operator==(const column_definition&, const column_definition&);
};
/*
* Sub-schema for thrift aspects, i.e. not currently supported stuff.
* But might be, and should be kept isolated (and starved)
*/
class thrift_schema {
public:
bool is_dense() const;
bool has_compound_comparator() const;
};
bool operator==(const column_definition&, const column_definition&);
class schema_builder;
/*
* Effectively immutable.
* Not safe to access across cores because of shared_ptr's.
*/
class schema final {
private:
// More complex fields are derived from these inside rebuild().
// Contains only fields which can be safely default-copied.
struct raw_schema {
raw_schema(utils::UUID id);
utils::UUID _id;
sstring _ks_name;
sstring _cf_name;
// regular columns are sorted by name
// static columns are sorted by name, but present only when there's any clustering column
std::vector<column_definition> _columns;
sstring _comment;
gc_clock::duration _default_time_to_live = gc_clock::duration::zero();
data_type _regular_column_name_type;
double _bloom_filter_fp_chance = 0.01;
compression_parameters _compressor_params;
};
raw_schema _raw;
thrift_schema _thrift;
const std::array<size_t, 3> _offsets;
inline size_t column_offset(column_kind k) const {
return k == column_kind::partition_key ? 0 : _offsets[size_t(k) - 1];
}
std::unordered_map<bytes, const column_definition*> _columns_by_name;
std::map<bytes, const column_definition*, serialized_compare> _regular_columns_by_name;
lw_shared_ptr<compound_type<allow_prefixes::no>> _partition_key_type;
lw_shared_ptr<compound_type<allow_prefixes::no>> _clustering_key_type;
lw_shared_ptr<compound_type<allow_prefixes::yes>> _clustering_key_prefix_type;
friend class schema_builder;
public:
typedef std::vector<column_definition> columns_type;
typedef typename columns_type::iterator iterator;
typedef typename columns_type::const_iterator const_iterator;
typedef boost::iterator_range<iterator> iterator_range_type;
typedef boost::iterator_range<const_iterator> const_iterator_range_type;
static constexpr int32_t NAME_LENGTH = 48;
static const std::experimental::optional<sstring> DEFAULT_COMPRESSOR;
struct column {
bytes name;
data_type type;
index_info idx_info;
};
private:
::shared_ptr<cql3::column_specification> make_column_specification(const column_definition& def);
void rebuild();
schema(const raw_schema&);
public:
schema(std::experimental::optional<utils::UUID> id,
sstring ks_name,
sstring cf_name,
std::vector<column> partition_key,
std::vector<column> clustering_key,
std::vector<column> regular_columns,
std::vector<column> static_columns,
data_type regular_column_name_type,
sstring comment = {});
schema(const schema&);
double bloom_filter_fp_chance() const {
return _raw._bloom_filter_fp_chance;
}
schema& set_bloom_filter_fp_chance(double fp) {
_raw._bloom_filter_fp_chance = fp;
return *this;
}
void set_compressor_params(compression_parameters c) {
_raw._compressor_params = c;
}
const compression_parameters& get_compressor_params() const {
return _raw._compressor_params;
}
thrift_schema& thrift() {
return _thrift;
}
const thrift_schema& thrift() const {
return _thrift;
}
const utils::UUID& id() const {
return _raw._id;
}
const sstring& comment() const {
return _raw._comment;
}
void set_comment(const sstring& comment) {
_raw._comment = comment;
}
void set_id(utils::UUID new_id) {
_raw._id = new_id;
}
bool is_counter() const {
return false;
}
const column_definition* get_column_definition(const bytes& name) const;
const_iterator regular_begin() const {
return regular_columns().begin();
}
const_iterator regular_end() const {
return regular_columns().end();
}
const_iterator regular_lower_bound(const bytes& name) const {
// TODO: use regular_columns and a version of std::lower_bound() with heterogeneous comparator
auto i = _regular_columns_by_name.lower_bound(name);
if (i == _regular_columns_by_name.end()) {
return regular_end();
} else {
return regular_begin() + i->second->id;
}
}
const_iterator regular_upper_bound(const bytes& name) const {
// TODO: use regular_columns and a version of std::upper_bound() with heterogeneous comparator
auto i = _regular_columns_by_name.upper_bound(name);
if (i == _regular_columns_by_name.end()) {
return regular_end();
} else {
return regular_begin() + i->second->id;
}
}
data_type column_name_type(const column_definition& def) const {
return def.kind == column_kind::regular_column ? _raw._regular_column_name_type : utf8_type;
}
const column_definition& regular_column_at(column_id id) const {
if (id > regular_columns_count()) {
throw std::out_of_range("column_id");
}
return _raw._columns.at(column_offset(column_kind::regular_column) + id);
}
const column_definition& static_column_at(column_id id) const {
if (id > static_columns_count()) {
throw std::out_of_range("column_id");
}
return _raw._columns.at(column_offset(column_kind::static_column) + id);
}
bool is_last_partition_key(const column_definition& def) const {
return &_raw._columns.at(partition_key_size() - 1) == &def;
}
bool has_collections() const ;
bool has_static_columns() const {
return !static_columns().empty();
}
size_t partition_key_size() const {
return column_offset(column_kind::clustering_key);
}
size_t clustering_key_size() const {
return column_offset(column_kind::static_column) - column_offset(column_kind::clustering_key);
}
size_t static_columns_count() const {
return column_offset(column_kind::regular_column) - column_offset(column_kind::static_column);
}
size_t regular_columns_count() const {
return _raw._columns.size() - column_offset(column_kind::static_column);
}
// Returns a range of column definitions
const_iterator_range_type partition_key_columns() const {
return boost::make_iterator_range(_raw._columns.begin() + column_offset(column_kind::partition_key)
, _raw._columns.begin() + column_offset(column_kind::clustering_key));
}
// Returns a range of column definitions
const_iterator_range_type clustering_key_columns() const {
return boost::make_iterator_range(_raw._columns.begin() + column_offset(column_kind::clustering_key)
, _raw._columns.begin() + column_offset(column_kind::static_column));
}
// Returns a range of column definitions
const_iterator_range_type static_columns() const {
return boost::make_iterator_range(_raw._columns.begin() + column_offset(column_kind::static_column)
, _raw._columns.begin() + column_offset(column_kind::regular_column));
}
// Returns a range of column definitions
const_iterator_range_type regular_columns() const {
return boost::make_iterator_range(_raw._columns.begin() + column_offset(column_kind::regular_column)
, _raw._columns.end());
}
// Returns a range of column definitions
const columns_type& all_columns_in_select_order() const {
return _raw._columns;
}
uint32_t position(const column_definition& column) const {
if (column.is_primary_key()) {
return column.id;
}
return clustering_key_size();
}
gc_clock::duration default_time_to_live() const {
return _raw._default_time_to_live;
}
const sstring& ks_name() const {
return _raw._ks_name;
}
const sstring& cf_name() const {
return _raw._cf_name;
}
const lw_shared_ptr<compound_type<allow_prefixes::no>>& partition_key_type() const {
return _partition_key_type;
}
const lw_shared_ptr<compound_type<allow_prefixes::no>>& clustering_key_type() const {
return _clustering_key_type;
}
const lw_shared_ptr<compound_type<allow_prefixes::yes>>& clustering_key_prefix_type() const {
return _clustering_key_prefix_type;
}
const data_type& regular_column_name_type() const {
return _raw._regular_column_name_type;
}
friend bool operator==(const schema&, const schema&);
};
bool operator==(const schema&, const schema&);
using schema_ptr = lw_shared_ptr<const schema>;
utils::UUID generate_legacy_id(const sstring& ks_name, const sstring& cf_name);