forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.cc
441 lines (375 loc) · 15.3 KB
/
schema.cc
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* Copyright (C) 2014 Cloudius Systems, Ltd.
*/
#include "utils/UUID_gen.hh"
#include "cql3/column_identifier.hh"
#include "schema.hh"
#include "schema_builder.hh"
#include <boost/algorithm/cxx11/any_of.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include "version.hh"
constexpr int32_t schema::NAME_LENGTH;
const std::experimental::optional<sstring> schema::DEFAULT_COMPRESSOR = sstring("LZ4Compressor");
template<typename Sequence>
std::vector<data_type>
get_column_types(const Sequence& column_definitions) {
std::vector<data_type> result;
for (auto&& col : column_definitions) {
result.push_back(col.type);
}
return result;
}
::shared_ptr<cql3::column_specification>
schema::make_column_specification(const column_definition& def) {
auto id = ::make_shared<cql3::column_identifier>(def.name(), column_name_type(def));
return ::make_shared<cql3::column_specification>(_raw._ks_name, _raw._cf_name, std::move(id), def.type);
}
void schema::rebuild() {
_partition_key_type = make_lw_shared<compound_type<>>(get_column_types(partition_key_columns()));
_clustering_key_type = make_lw_shared<compound_type<>>(get_column_types(clustering_key_columns()));
_clustering_key_prefix_type = make_lw_shared(_clustering_key_type->as_prefix());
_columns_by_name.clear();
_regular_columns_by_name.clear();
for (const column_definition& def : all_columns_in_select_order()) {
_columns_by_name[def.name()] = &def;
}
for (const column_definition& def : regular_columns()) {
_regular_columns_by_name[def.name()] = &def;
}
}
schema::raw_schema::raw_schema(utils::UUID id)
: _id(id)
{ }
schema::schema(const raw_schema& raw)
: _raw(raw)
, _offsets([this] {
auto& cols = _raw._columns;
std::array<size_t, 5> count = { 0, 0, 0, 0, 0 };
auto i = cols.begin();
auto e = cols.end();
for (auto k : { column_kind::partition_key, column_kind::clustering_key, column_kind::static_column, column_kind::regular_column, column_kind::compact_column }) {
auto j = std::stable_partition(i, e, [k](const auto& c) {
return c.kind == k;
});
count[size_t(k)] = std::distance(i, j);
i = j;
}
return std::array<size_t, 4> { count[0], count[0] + count[1], count[0] + count[1] + count[2], count[0] + count[1] + count[2] + count[3] };
}())
, _regular_columns_by_name(serialized_compare(_raw._regular_column_name_type))
{
struct name_compare {
data_type type;
name_compare(data_type type) : type(type) {}
bool operator()(const column_definition& cd1, const column_definition& cd2) const {
return type->less(cd1.name(), cd2.name());
}
};
thrift()._compound = is_compound();
std::sort(
_raw._columns.begin() + column_offset(column_kind::static_column),
_raw._columns.begin()
+ column_offset(column_kind::regular_column),
name_compare(utf8_type));
std::sort(
_raw._columns.begin()
+ column_offset(column_kind::regular_column),
_raw._columns.end(), name_compare(regular_column_name_type()));
std::sort(_raw._columns.begin(),
_raw._columns.begin() + column_offset(column_kind::clustering_key),
[] (auto x, auto y) { return x.id < y.id; });
std::sort(_raw._columns.begin() + column_offset(column_kind::clustering_key),
_raw._columns.begin() + column_offset(column_kind::static_column),
[] (auto x, auto y) { return x.id < y.id; });
column_id id = 0;
for (auto& def : _raw._columns) {
def.column_specification = make_column_specification(def);
assert(!def.id || def.id == id - column_offset(def.kind));
def.id = id - column_offset(def.kind);
def._thrift_bits = column_definition::thrift_bits();
{
// is_on_all_components
// TODO : In origin, this predicate is "componentIndex == null", which is true in
// a number of cases, some of which I've most likely missed...
switch (def.kind) {
case column_kind::partition_key:
// In origin, ci == null is true for a PK column where CFMetaData "keyValidator" is non-composite.
// Which is true of #pk == 1
def._thrift_bits.is_on_all_components = partition_key_size() == 1;
break;
case column_kind::compact_column:
// compact values are alone, so they have no index
def._thrift_bits.is_on_all_components = true;
break;
default:
// Or any other column where "comparator" is not compound
def._thrift_bits.is_on_all_components = !thrift().has_compound_comparator();
break;
}
}
++id;
}
rebuild();
}
schema::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([&] {
raw_schema raw(id ? *id : utils::UUID_gen::get_time_UUID());
raw._comment = std::move(comment);
raw._ks_name = std::move(ks_name);
raw._cf_name = std::move(cf_name);
raw._regular_column_name_type = regular_column_name_type;
auto build_columns = [&raw](std::vector<column>& columns, column_kind kind) {
for (auto& sc : columns) {
raw._columns.emplace_back(std::move(sc.name), std::move(sc.type), kind);
}
};
build_columns(partition_key, column_kind::partition_key);
build_columns(clustering_key, column_kind::clustering_key);
build_columns(static_columns, column_kind::static_column);
build_columns(regular_columns, column_kind::regular_column);
return raw;
}())
{}
schema::schema(const schema& o)
: _raw(o._raw)
, _offsets(o._offsets)
, _regular_columns_by_name(serialized_compare(_raw._regular_column_name_type))
{
rebuild();
}
sstring schema::thrift_key_validator() const {
if (partition_key_size() == 1) {
return partition_key_columns().begin()->type->name();
} else {
sstring type_params = ::join(", ", partition_key_columns()
| boost::adaptors::transformed(std::mem_fn(&column_definition::type))
| boost::adaptors::transformed(std::mem_fn(&abstract_type::name)));
return "org.apache.cassandra.db.marshal.CompositeType(" + type_params + ")";
}
}
bool
schema::has_collections() const {
return boost::algorithm::any_of(all_columns_in_select_order(), [] (const column_definition& cdef) {
return cdef.type->is_collection();
});
}
bool operator==(const schema& x, const schema& y)
{
return x._raw._id == y._raw._id
&& x._raw._ks_name == y._raw._ks_name
&& x._raw._cf_name == y._raw._cf_name
&& x._raw._columns == y._raw._columns
&& x._raw._comment == y._raw._comment
&& x._raw._default_time_to_live == y._raw._default_time_to_live
&& x._raw._regular_column_name_type->equals(y._raw._regular_column_name_type)
&& x._raw._bloom_filter_fp_chance == y._raw._bloom_filter_fp_chance;
}
index_info::index_info(::index_type idx_type,
std::experimental::optional<sstring> idx_name,
std::experimental::optional<index_options_map> idx_options)
: index_type(idx_type), index_name(idx_name), index_options(idx_options)
{}
column_definition::column_definition(bytes name, data_type type, column_kind kind, column_id component_index, index_info idx)
: _name(std::move(name)), type(std::move(type)), id(component_index), kind(kind), idx_info(std::move(idx))
{}
const column_definition*
schema::get_column_definition(const bytes& name) const {
auto i = _columns_by_name.find(name);
if (i == _columns_by_name.end()) {
return nullptr;
}
return i->second;
}
const sstring&
column_definition::name_as_text() const {
return column_specification->name->text();
}
const bytes&
column_definition::name() const {
return _name;
}
bool column_definition::is_on_all_components() const {
return _thrift_bits.is_on_all_components;
}
bool operator==(const column_definition& x, const column_definition& y)
{
return x._name == y._name
&& x.type->equals(y.type)
&& x.id == y.id
&& x.kind == y.kind;
}
// Based on org.apache.cassandra.config.CFMetaData#generateLegacyCfId
utils::UUID
generate_legacy_id(const sstring& ks_name, const sstring& cf_name) {
return utils::UUID_gen::get_name_UUID(ks_name + cf_name);
}
bool thrift_schema::has_compound_comparator() const {
return _compound;
}
schema_builder::schema_builder(const sstring& ks_name, const sstring& cf_name,
std::experimental::optional<utils::UUID> id, data_type rct)
: _raw(id ? *id : utils::UUID_gen::get_time_UUID())
{
_raw._ks_name = ks_name;
_raw._cf_name = cf_name;
_raw._regular_column_name_type = rct;
}
schema_builder::schema_builder(const schema_ptr s)
: schema_builder(s->_raw)
{}
schema_builder::schema_builder(const schema::raw_schema& raw)
: _raw(raw)
{}
column_definition& schema_builder::find_column(const cql3::column_identifier& c) {
auto i = std::find_if(_raw._columns.begin(), _raw._columns.end(), [c](auto& p) {
return p.name() == c.name();
});
if (i != _raw._columns.end()) {
return *i;
}
throw std::invalid_argument(sprint("No such column %s", c.name()));
}
void schema_builder::add_default_index_names(database& db) {
auto s = db.find_schema(ks_name(), cf_name());
if (s) {
for (auto& sc : _raw._columns) {
if (sc.idx_info.index_type == index_type::none) {
continue;
}
auto* c = s->get_column_definition(sc.name());
if (c == nullptr || !c->idx_info.index_name) {
continue;
}
if (sc.idx_info.index_name
&& sc.idx_info.index_name != c->idx_info.index_name) {
throw exceptions::configuration_exception(
sprint(
"Can't modify index name: was '%s' changed to '%s'",
*c->idx_info.index_name,
*sc.idx_info.index_name));
}
sc.idx_info.index_name = c->idx_info.index_name;
}
}
auto existing_names = db.existing_index_names();
for (auto& sc : _raw._columns) {
if (sc.idx_info.index_type != index_type::none && sc.idx_info.index_name) {
sstring base_name = cf_name() + "_" + *sc.idx_info.index_name + "_idx";
auto i = std::remove_if(base_name.begin(), base_name.end(), [](char c) {
return ::isspace(c);
});
base_name.erase(i, base_name.end());
auto index_name = base_name;
int n = 0;
while (existing_names.count(index_name)) {
index_name = base_name + "_" + to_sstring(++n);
}
sc.idx_info.index_name = index_name;
}
}
}
schema_builder& schema_builder::with_column(const column_definition& c) {
return with_column(bytes(c.name()), data_type(c.type), index_info(c.idx_info), column_kind(c.kind), c.position());
}
schema_builder& schema_builder::with_column(bytes name, data_type type, column_kind kind) {
return with_column(name, type, index_info(), kind);
}
schema_builder& schema_builder::with_column(bytes name, data_type type, index_info info, column_kind kind) {
// component_index will be determined by schema cosntructor
return with_column(name, type, info, kind, 0);
}
schema_builder& schema_builder::with_column(bytes name, data_type type, index_info info, column_kind kind, column_id component_index) {
_raw._columns.emplace_back(name, type, kind, component_index, info);
return *this;
}
schema_ptr schema_builder::build() {
return make_lw_shared<schema>(schema(_raw));
}
schema_ptr schema_builder::build(compact_storage cp) {
schema s(_raw);
// Dense means that no part of the comparator stores a CQL column name. This means
// COMPACT STORAGE with at least one columnAliases (otherwise it's a thrift "static" CF).
s._raw._is_dense = (cp == compact_storage::yes) && (s.clustering_key_size() > 0);
if (s.clustering_key_size() == 0) {
if (cp == compact_storage::yes) {
s._raw._is_compound = false;
} else {
s._raw._is_compound = true;
}
} else {
if ((cp == compact_storage::yes) && s.clustering_key_size() == 1) {
s._raw._is_compound = false;
} else {
s._raw._is_compound = true;
}
}
if (s._raw._is_dense) {
// In Origin, dense CFs always have at least one regular column
if (s.regular_columns_count() == 0) {
s._raw._columns.emplace_back(bytes(""), s.regular_column_name_type(), column_kind::regular_column, 0, index_info());
}
if (s.regular_columns_count() != 1) {
throw exceptions::configuration_exception(sprint("Expecting exactly one regular column. Found %d", s.regular_columns_count()));
}
s._raw._columns.at(s.column_offset(column_kind::regular_column)).kind = column_kind::compact_column;
// We need to rebuild the schema in case we added some column. This is way simpler than trying to factor out the relevant code
// from the constructor
}
return make_lw_shared<schema>(schema(s._raw));
}
// Useful functions to manipulate the schema's comparator field
namespace cell_comparator {
static constexpr auto _composite_str = "org.apache.cassandra.db.marshal.CompositeType";
static constexpr auto _collection_str = "org.apache.cassandra.db.marshal.ColumnToCollectionType";
static bool always_include_default() {
static thread_local bool def = version::version::current() < version::version(2, 2);
return def;
};
static sstring collection_name(const collection_type& t) {
sstring collection_str(_collection_str);
collection_str += "(00000000:" + t->name() + ")";
return collection_str;
}
static sstring compound_name(const schema& s) {
sstring compound(_composite_str);
compound += "(";
if (s.clustering_key_size()) {
for (auto &t : s.clustering_key_columns()) {
compound += t.type->name() + ",";
}
}
if (always_include_default() || (s.clustering_key_size() == 0)) {
compound += s.regular_column_name_type()->name() + ",";
}
if (s.has_collections()) {
for (auto& t: s.regular_columns()) {
if (t.type->is_collection()) {
auto ct = static_pointer_cast<const collection_type_impl>(t.type);
compound += collection_name(ct) + ",";
}
}
}
// last one will be a ',', just replace it.
compound.back() = ')';
return compound;
}
sstring to_sstring(const schema& s) {
if (!s.is_compound()) {
return s.regular_column_name_type()->name();
} else {
return compound_name(s);
}
}
bool check_compound(sstring comparator) {
static sstring compound(_composite_str);
return comparator.compare(0, compound.size(), compound) == 0;
}
}