-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathdata_type.hpp
More file actions
574 lines (452 loc) · 16.7 KB
/
data_type.hpp
File metadata and controls
574 lines (452 loc) · 16.7 KB
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
Copyright (c) DataStax, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef DATASTAX_INTERNAL_DATA_TYPE_HPP
#define DATASTAX_INTERNAL_DATA_TYPE_HPP
#include "cassandra.h"
#include "external.hpp"
#include "hash_table.hpp"
#include "macros.hpp"
#include "map.hpp"
#include "ref_counted.hpp"
#include "small_dense_hash_map.hpp"
#include "string.hpp"
#include "types.hpp"
#include "vector.hpp"
#include <algorithm>
namespace datastax { namespace internal { namespace core {
class Collection;
class Tuple;
class UserTypeValue;
inline bool is_int64_type(CassValueType value_type) {
return value_type == CASS_VALUE_TYPE_BIGINT || value_type == CASS_VALUE_TYPE_COUNTER ||
value_type == CASS_VALUE_TYPE_TIMESTAMP || value_type == CASS_VALUE_TYPE_TIME;
}
inline bool is_string_type(CassValueType value_type) {
return value_type == CASS_VALUE_TYPE_ASCII || value_type == CASS_VALUE_TYPE_TEXT ||
value_type == CASS_VALUE_TYPE_VARCHAR;
}
inline bool is_bytes_type(CassValueType value_type) {
return value_type == CASS_VALUE_TYPE_BLOB || value_type == CASS_VALUE_TYPE_VARINT ||
value_type == CASS_VALUE_TYPE_CUSTOM;
}
inline bool is_uuid_type(CassValueType value_type) {
return value_type == CASS_VALUE_TYPE_TIMEUUID || value_type == CASS_VALUE_TYPE_UUID;
}
// Only compare when both arguments are not empty
inline bool equals_both_not_empty(const String& s1, const String& s2) {
return s1.empty() || s2.empty() || s1 == s2;
}
class DataType : public RefCounted<DataType> {
public:
typedef SharedRefPtr<DataType> Ptr;
typedef SharedRefPtr<const DataType> ConstPtr;
typedef Vector<ConstPtr> Vec;
static const DataType::ConstPtr NIL;
static ConstPtr create_by_class(StringRef name);
static ConstPtr create_by_cql(StringRef name);
DataType(CassValueType value_type = CASS_VALUE_TYPE_UNKNOWN, bool is_frozen = false)
: value_type_(value_type)
, is_frozen_(is_frozen) {}
virtual ~DataType() {}
CassValueType value_type() const { return value_type_; }
bool is_collection() const {
return value_type_ == CASS_VALUE_TYPE_LIST || value_type_ == CASS_VALUE_TYPE_MAP ||
value_type_ == CASS_VALUE_TYPE_SET;
}
bool is_map() const { return value_type_ == CASS_VALUE_TYPE_MAP; }
bool is_tuple() const { return value_type_ == CASS_VALUE_TYPE_TUPLE; }
bool is_user_type() const { return value_type_ == CASS_VALUE_TYPE_UDT; }
bool is_custom() const { return value_type_ == CASS_VALUE_TYPE_CUSTOM; }
bool is_frozen() const { return is_frozen_; }
virtual bool equals(const DataType::ConstPtr& data_type) const {
switch (value_type_) {
// "text" is an alias for "varchar"
case CASS_VALUE_TYPE_TEXT:
case CASS_VALUE_TYPE_VARCHAR:
return data_type->value_type_ == CASS_VALUE_TYPE_TEXT ||
data_type->value_type_ == CASS_VALUE_TYPE_VARCHAR;
default:
return value_type_ == data_type->value_type_;
}
}
virtual DataType::Ptr copy() const { return Ptr(new DataType(value_type_)); }
virtual String to_string() const {
switch (value_type_) {
#define XX_VALUE_TYPE(name, type, cql, klass) \
case name: \
return cql;
CASS_VALUE_TYPE_MAPPING(XX_VALUE_TYPE)
#undef XX_VALUE_TYPE
default:
return "";
}
}
private:
CassValueType value_type_;
bool is_frozen_;
private:
DISALLOW_COPY_AND_ASSIGN(DataType);
};
class CustomType : public DataType {
public:
typedef SharedRefPtr<const CustomType> ConstPtr;
CustomType()
: DataType(CASS_VALUE_TYPE_CUSTOM) {}
CustomType(const String& class_name)
: DataType(CASS_VALUE_TYPE_CUSTOM)
, class_name_(class_name) {}
const String& class_name() const { return class_name_; }
void set_class_name(const String& class_name) { class_name_ = class_name; }
virtual bool equals(const DataType::ConstPtr& data_type) const {
assert(value_type() == CASS_VALUE_TYPE_CUSTOM);
if (data_type->value_type() != CASS_VALUE_TYPE_CUSTOM) {
return false;
}
const ConstPtr& custom_type(data_type);
return equals_both_not_empty(class_name_, custom_type->class_name_);
}
virtual DataType::Ptr copy() const { return DataType::Ptr(new CustomType(class_name_)); }
virtual String to_string() const { return class_name_; }
private:
String class_name_;
};
class CompositeType : public DataType {
public:
CompositeType(CassValueType type, bool is_frozen)
: DataType(type, is_frozen) {}
CompositeType(CassValueType type, const DataType::Vec& types, bool is_frozen)
: DataType(type, is_frozen)
, types_(types) {}
DataType::Vec& types() { return types_; }
const DataType::Vec& types() const { return types_; }
virtual String to_string() const {
String str;
if (is_frozen()) str.append("frozen<");
str.append(DataType::to_string());
str.push_back('<');
for (DataType::Vec::const_iterator i = types_.begin(), end = types_.end(); i != end; ++i) {
if (i != types_.begin()) str.append(", ");
str.append((*i)->to_string());
}
if (is_frozen()) {
str.append(">>");
} else {
str.push_back('>');
}
return str;
}
protected:
DataType::Vec types_;
};
class CollectionType : public CompositeType {
public:
typedef SharedRefPtr<const CollectionType> ConstPtr;
explicit CollectionType(CassValueType collection_type, bool is_frozen)
: CompositeType(collection_type, is_frozen) {}
explicit CollectionType(CassValueType collection_type, size_t types_count, bool is_frozen)
: CompositeType(collection_type, is_frozen) {
types_.reserve(types_count);
}
explicit CollectionType(CassValueType collection_type, const DataType::Vec& types, bool is_frozen)
: CompositeType(collection_type, types, is_frozen) {}
virtual bool equals(const DataType::ConstPtr& data_type) const {
assert(value_type() == CASS_VALUE_TYPE_LIST || value_type() == CASS_VALUE_TYPE_SET ||
value_type() == CASS_VALUE_TYPE_MAP || value_type() == CASS_VALUE_TYPE_TUPLE);
if (value_type() != data_type->value_type()) {
return false;
}
const CollectionType::ConstPtr& collection_type(data_type);
// Only compare sub-types if both have sub-types
if (!types_.empty() && !collection_type->types_.empty()) {
if (types_.size() != collection_type->types_.size()) {
return false;
}
for (size_t i = 0; i < types_.size(); ++i) {
if (!types_[i]->equals(collection_type->types_[i])) {
return false;
}
}
}
return true;
}
virtual DataType::Ptr copy() const {
return DataType::Ptr(new CollectionType(value_type(), types_, is_frozen()));
}
public:
static DataType::ConstPtr list(DataType::ConstPtr element_type, bool is_frozen) {
DataType::Vec types;
types.push_back(element_type);
return DataType::ConstPtr(new CollectionType(CASS_VALUE_TYPE_LIST, types, is_frozen));
}
static DataType::ConstPtr set(DataType::ConstPtr element_type, bool is_frozen) {
DataType::Vec types;
types.push_back(element_type);
return DataType::ConstPtr(new CollectionType(CASS_VALUE_TYPE_SET, types, is_frozen));
}
static DataType::ConstPtr map(DataType::ConstPtr key_type, DataType::ConstPtr value_type,
bool is_frozen) {
DataType::Vec types;
types.push_back(key_type);
types.push_back(value_type);
return DataType::ConstPtr(new CollectionType(CASS_VALUE_TYPE_MAP, types, is_frozen));
}
};
class TupleType : public CompositeType {
public:
typedef SharedRefPtr<const TupleType> ConstPtr;
TupleType(bool is_frozen)
: CompositeType(CASS_VALUE_TYPE_TUPLE, is_frozen) {}
TupleType(const DataType::Vec& types, bool is_frozen)
: CompositeType(CASS_VALUE_TYPE_TUPLE, types, is_frozen) {}
virtual bool equals(const DataType::ConstPtr& data_type) const {
assert(value_type() == CASS_VALUE_TYPE_TUPLE);
if (value_type() != data_type->value_type()) {
return false;
}
const ConstPtr& tuple_type(data_type);
// Only compare sub-types if both have sub-types
if (!types_.empty() && !tuple_type->types_.empty()) {
if (types_.size() != tuple_type->types_.size()) {
return false;
}
for (size_t i = 0; i < types_.size(); ++i) {
if (!types_[i]->equals(tuple_type->types_[i])) {
return false;
}
}
}
return true;
}
virtual DataType::Ptr copy() const { return DataType::Ptr(new TupleType(types_, is_frozen())); }
};
class UserType : public DataType {
friend class Memory;
public:
typedef SharedRefPtr<UserType> Ptr;
typedef SharedRefPtr<const UserType> ConstPtr;
typedef internal::Map<String, UserType::Ptr> Map;
struct Field : public HashTableEntry<Field> {
Field(const String& field_name, const DataType::ConstPtr& type)
: name(field_name)
, type(type) {}
String name;
DataType::ConstPtr type;
};
typedef CaseInsensitiveHashTable<Field>::EntryVec FieldVec;
explicit UserType(bool is_frozen)
: DataType(CASS_VALUE_TYPE_UDT, is_frozen) {}
explicit UserType(size_t field_count, bool is_frozen)
: DataType(CASS_VALUE_TYPE_UDT, is_frozen)
, fields_(field_count) {}
explicit UserType(const String& keyspace, const String& type_name, bool is_frozen)
: DataType(CASS_VALUE_TYPE_UDT, is_frozen)
, keyspace_(keyspace)
, type_name_(type_name) {}
explicit UserType(const String& keyspace, const String& type_name, const FieldVec& fields,
bool is_frozen)
: DataType(CASS_VALUE_TYPE_UDT, is_frozen)
, keyspace_(keyspace)
, type_name_(type_name)
, fields_(fields) {}
const String& keyspace() const { return keyspace_; }
void set_keyspace(const String& keyspace) { keyspace_ = keyspace; }
const String& type_name() const { return type_name_; }
void set_type_name(const String& type_name) { type_name_ = type_name; }
const FieldVec& fields() const { return fields_.entries(); }
size_t get_indices(StringRef name, IndexVec* result) const {
return fields_.get_indices(name, result);
}
void add_field(const String name, const DataType::ConstPtr& data_type) {
fields_.add(Field(name, data_type));
}
void set_fields(const FieldVec& fields) { fields_.set_entries(fields); }
virtual bool equals(const DataType::ConstPtr& data_type) const {
assert(value_type() == CASS_VALUE_TYPE_UDT);
if (data_type->value_type() != CASS_VALUE_TYPE_UDT) {
return false;
}
const UserType::ConstPtr& user_type(data_type);
if (!equals_both_not_empty(keyspace_, user_type->keyspace_)) {
return false;
}
if (!equals_both_not_empty(type_name_, user_type->type_name_)) {
return false;
}
// UDT's can be considered equal as long as the mutual first fields shared
// between them are equal. UDT's are append only as far as fields go, so a
// newer 'version' of the UDT data type after a schema change event should be
// treated as equivalent in this scenario, by simply looking at the first N
// mutual fields they should share.
size_t min_fields_size = std::min(fields_.size(), user_type->fields_.size());
for (size_t i = 0; i < min_fields_size; ++i) {
if (fields_[i].name != user_type->fields_[i].name ||
!fields_[i].type->equals(user_type->fields_[i].type)) {
return false;
}
}
return true;
}
virtual DataType::Ptr copy() const {
return DataType::Ptr(new UserType(keyspace_, type_name_, fields_.entries(), is_frozen()));
}
virtual String to_string() const {
String str;
if (is_frozen()) str.append("frozen<");
str.append(type_name_);
if (is_frozen()) str.push_back('>');
return str;
}
private:
String keyspace_;
String type_name_;
CaseInsensitiveHashTable<Field> fields_;
};
class ValueTypes {
public:
ValueTypes();
static CassValueType by_class(StringRef name);
static CassValueType by_cql(StringRef name);
private:
typedef SmallDenseHashMap<StringRef, CassValueType,
CASS_VALUE_TYPE_LAST_ENTRY, // Max size
StringRefIHash, StringRefIEquals>
HashMap;
static HashMap value_types_by_class_;
static HashMap value_types_by_cql_;
};
class SimpleDataTypeCache {
public:
const DataType::ConstPtr& by_class(StringRef name) {
return by_value_type(ValueTypes::by_class(name));
}
const DataType::ConstPtr& by_cql(StringRef name) {
return by_value_type(ValueTypes::by_cql(name));
}
const DataType::ConstPtr& by_value_type(uint16_t value_type);
private:
DataType::ConstPtr cache_[CASS_VALUE_TYPE_LAST_ENTRY];
};
template <class T>
struct IsValidDataType;
template <>
struct IsValidDataType<CassNull> {
bool operator()(CassNull, const DataType::ConstPtr& data_type) const { return true; }
};
template <>
struct IsValidDataType<cass_int8_t> {
bool operator()(cass_int8_t, const DataType::ConstPtr& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_TINY_INT;
}
};
template <>
struct IsValidDataType<cass_int16_t> {
bool operator()(cass_int16_t, const DataType::ConstPtr& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_SMALL_INT;
}
};
template <>
struct IsValidDataType<cass_int32_t> {
bool operator()(cass_int32_t, const DataType::ConstPtr& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_INT;
}
};
template <>
struct IsValidDataType<cass_uint32_t> {
bool operator()(cass_uint32_t, const DataType::ConstPtr& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_DATE;
}
};
template <>
struct IsValidDataType<cass_int64_t> {
bool operator()(cass_int64_t, const DataType::ConstPtr& data_type) const {
return is_int64_type(data_type->value_type());
}
};
template <>
struct IsValidDataType<cass_float_t> {
bool operator()(cass_float_t, const DataType::ConstPtr& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_FLOAT;
}
};
template <>
struct IsValidDataType<cass_double_t> {
bool operator()(cass_double_t, const DataType::ConstPtr& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_DOUBLE;
}
};
template <>
struct IsValidDataType<cass_bool_t> {
bool operator()(cass_bool_t, const DataType::ConstPtr& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_BOOLEAN;
}
};
template <>
struct IsValidDataType<CassString> {
bool operator()(CassString, const DataType::ConstPtr& data_type) const {
// Also allow "bytes" types to be bound as "string" types
return is_string_type(data_type->value_type()) || is_bytes_type(data_type->value_type());
}
};
template <>
struct IsValidDataType<CassBytes> {
bool operator()(CassBytes, const DataType::ConstPtr& data_type) const {
return is_bytes_type(data_type->value_type());
}
};
template <>
struct IsValidDataType<CassCustom> {
bool operator()(const CassCustom& custom, const DataType::ConstPtr& data_type) const {
if (!data_type->is_custom()) return false;
CustomType::ConstPtr custom_type(data_type);
return custom.class_name == custom_type->class_name();
}
};
template <>
struct IsValidDataType<CassUuid> {
bool operator()(CassUuid, const DataType::ConstPtr& data_type) const {
return is_uuid_type(data_type->value_type());
}
};
template <>
struct IsValidDataType<CassInet> {
bool operator()(CassInet, const DataType::ConstPtr& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_INET;
}
};
template <>
struct IsValidDataType<CassDecimal> {
bool operator()(CassDecimal, const DataType::ConstPtr& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_DECIMAL;
}
};
template <>
struct IsValidDataType<CassDuration> {
bool operator()(CassDuration, const DataType::ConstPtr& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_DURATION;
}
};
template <>
struct IsValidDataType<const Collection*> {
bool operator()(const Collection* value, const DataType::ConstPtr& data_type) const;
};
template <>
struct IsValidDataType<const Tuple*> {
bool operator()(const Tuple* value, const DataType::ConstPtr& data_type) const;
};
template <>
struct IsValidDataType<const UserTypeValue*> {
bool operator()(const UserTypeValue* value, const DataType::ConstPtr& data_type) const;
};
}}} // namespace datastax::internal::core
EXTERNAL_TYPE(datastax::internal::core::DataType, CassDataType)
#endif