-
Notifications
You must be signed in to change notification settings - Fork 283
/
data_type.hpp
464 lines (366 loc) · 12.3 KB
/
data_type.hpp
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
/*
Copyright (c) 2014-2015 DataStax
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 __CASS_DATA_TYPE_HPP_INCLUDED__
#define __CASS_DATA_TYPE_HPP_INCLUDED__
#include "cassandra.h"
#include "hash_table.hpp"
#include "macros.hpp"
#include "ref_counted.hpp"
#include "types.hpp"
#include <map>
#include <string>
#include <vector>
namespace cass {
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;
}
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 std::string& s1,
const std::string& s2) {
return s1.empty() || s2.empty() || s1 == s2;
}
class DataType : public RefCounted<DataType> {
public:
static const SharedRefPtr<const DataType> NIL;
DataType(CassValueType value_type)
: value_type_(value_type) { }
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;
}
virtual bool equals(const SharedRefPtr<const DataType>& 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* copy() const {
return new DataType(value_type_);
}
private:
int protocol_version_;
CassValueType value_type_;
private:
DISALLOW_COPY_AND_ASSIGN(DataType);
};
typedef std::vector<SharedRefPtr<const DataType> > DataTypeVec;
class CustomType : public DataType {
public:
CustomType()
: DataType(CASS_VALUE_TYPE_CUSTOM) { }
CustomType(const std::string& class_name)
: DataType(CASS_VALUE_TYPE_CUSTOM)
, class_name_(class_name) { }
const std::string& class_name() const { return class_name_; }
void set_class_name(const std::string& class_name) {
class_name_ = class_name;
}
virtual bool equals(const SharedRefPtr<const DataType>& data_type) const {
assert(value_type() == CASS_VALUE_TYPE_CUSTOM);
if (data_type->value_type() != CASS_VALUE_TYPE_CUSTOM) {
return false;
}
const SharedRefPtr<const CustomType>& custom_type(data_type);
return equals_both_not_empty(class_name_, custom_type->class_name_);
}
virtual DataType* copy() const {
return new CustomType(class_name_);
}
private:
std::string class_name_;
};
class SubTypesDataType : public DataType {
public:
SubTypesDataType(CassValueType type)
: DataType(type) { }
SubTypesDataType(CassValueType type, const DataTypeVec& types)
: DataType(type)
, types_(types) { }
DataTypeVec& types() { return types_; }
const DataTypeVec& types() const { return types_; }
protected:
DataTypeVec types_;
};
class CollectionType : public SubTypesDataType {
public:
CollectionType(CassValueType collection_type)
: SubTypesDataType(collection_type) { }
CollectionType(CassValueType collection_type,
size_t types_count)
: SubTypesDataType(collection_type) {
types_.reserve(types_count);
}
CollectionType(CassValueType collection_type, const DataTypeVec& types)
: SubTypesDataType(collection_type, types) { }
virtual bool equals(const SharedRefPtr<const DataType>& data_type) const {
assert(value_type() == CASS_VALUE_TYPE_LIST ||
value_type() == CASS_VALUE_TYPE_SET ||
value_type() == CASS_VALUE_TYPE_MAP);
if (value_type() != data_type->value_type()) {
return false;
}
const SharedRefPtr<const CollectionType>& 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* copy() const {
return new CollectionType(value_type(), types_);
}
public:
static SharedRefPtr<DataType> list(SharedRefPtr<DataType> element_type) {
DataTypeVec types;
types.push_back(element_type);
return SharedRefPtr<DataType>(new CollectionType(CASS_VALUE_TYPE_LIST, types));
}
static SharedRefPtr<DataType> set(SharedRefPtr<DataType> element_type) {
DataTypeVec types;
types.push_back(element_type);
return SharedRefPtr<DataType>(new CollectionType(CASS_VALUE_TYPE_SET, types));
}
static SharedRefPtr<DataType> map(SharedRefPtr<DataType> key_type, SharedRefPtr<DataType> value_type) {
DataTypeVec types;
types.push_back(key_type);
types.push_back(value_type);
return SharedRefPtr<DataType>(new CollectionType(CASS_VALUE_TYPE_MAP, types));
}
};
class TupleType : public SubTypesDataType {
public:
TupleType()
: SubTypesDataType(CASS_VALUE_TYPE_TUPLE) { }
TupleType(const DataTypeVec& types)
: SubTypesDataType(CASS_VALUE_TYPE_TUPLE, types) { }
virtual bool equals(const SharedRefPtr<const DataType>& data_type) const {
assert(value_type() == CASS_VALUE_TYPE_TUPLE);
if (value_type() != data_type->value_type()) {
return false;
}
const SharedRefPtr<const TupleType>& 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* copy() const {
return new TupleType(types_);
}
};
class UserType : public DataType {
public:
struct Field : public HashTableEntry<Field> {
Field(const std::string& field_name,
const SharedRefPtr<const DataType>& type)
: name(field_name)
, type(type) { }
std::string name;
SharedRefPtr<const DataType> type;
};
typedef CaseInsensitiveHashTable<Field>::EntryVec FieldVec;
UserType()
: DataType(CASS_VALUE_TYPE_UDT)
, fields_(0) { }
UserType(size_t field_count)
: DataType(CASS_VALUE_TYPE_UDT)
, fields_(field_count) { }
UserType(const std::string& keyspace,
const std::string& type_name,
const FieldVec& fields)
: DataType(CASS_VALUE_TYPE_UDT)
, keyspace_(keyspace)
, type_name_(type_name)
, fields_(fields) { }
const std::string& keyspace() const { return keyspace_; }
void set_keyspace(const std::string& keyspace) {
keyspace_ = keyspace;
}
const std::string& type_name() const { return type_name_; }
void set_type_name(const std::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 std::string name, const SharedRefPtr<const DataType>& data_type) {
fields_.add(Field(name, data_type));
}
virtual bool equals(const SharedRefPtr<const DataType>& data_type) const {
assert(value_type() == CASS_VALUE_TYPE_UDT);
if (data_type->value_type() != CASS_VALUE_TYPE_UDT) {
return false;
}
const SharedRefPtr<const UserType>& 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;
}
if (fields_.size() != user_type->fields_.size()) {
return false;
}
for (size_t i = 0; i < 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* copy() const {
return new UserType(keyspace_, type_name_, fields_.entries());
}
private:
std::string keyspace_;
std::string type_name_;
CaseInsensitiveHashTable<Field> fields_;
};
template<class T>
struct IsValidDataType;
template<>
struct IsValidDataType<CassNull> {
bool operator()(CassNull, const SharedRefPtr<const DataType>& data_type) const {
return true;
}
};
template<>
struct IsValidDataType<cass_int32_t> {
bool operator()(cass_int32_t, const SharedRefPtr<const DataType>& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_INT;
}
};
template<>
struct IsValidDataType<cass_int64_t> {
bool operator()(cass_int64_t, const SharedRefPtr<const DataType>& data_type) const {
return is_int64_type(data_type->value_type());
}
};
template<>
struct IsValidDataType<cass_float_t> {
bool operator()(cass_float_t, const SharedRefPtr<const DataType>& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_FLOAT;
}
};
template<>
struct IsValidDataType<cass_double_t> {
bool operator()(cass_double_t, const SharedRefPtr<const DataType>& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_DOUBLE;
}
};
template<>
struct IsValidDataType<cass_bool_t> {
bool operator()(cass_bool_t, const SharedRefPtr<const DataType>& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_BOOLEAN;
}
};
template<>
struct IsValidDataType<CassString> {
bool operator()(CassString, const SharedRefPtr<const DataType>& data_type) const {
return is_string_type(data_type->value_type());
}
};
template<>
struct IsValidDataType<CassBytes> {
bool operator()(CassBytes, const SharedRefPtr<const DataType>& data_type) const {
return is_bytes_type(data_type->value_type());
}
};
template<>
struct IsValidDataType<CassUuid> {
bool operator()(CassUuid, const SharedRefPtr<const DataType>& data_type) const {
return is_uuid_type(data_type->value_type());
}
};
template<>
struct IsValidDataType<CassInet> {
bool operator()(CassInet, const SharedRefPtr<const DataType>& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_INET;
}
};
template<>
struct IsValidDataType<CassDecimal> {
bool operator()(CassDecimal, const SharedRefPtr<const DataType>& data_type) const {
return data_type->value_type() == CASS_VALUE_TYPE_DECIMAL;
}
};
template<>
struct IsValidDataType<const Collection*> {
bool operator()(const Collection* value, const SharedRefPtr<const DataType>& data_type) const;
};
template<>
struct IsValidDataType<const Tuple*> {
bool operator()(const Tuple* value, const SharedRefPtr<const DataType>& data_type) const;
};
template<>
struct IsValidDataType<const UserTypeValue*> {
bool operator()(const UserTypeValue* value, const SharedRefPtr<const DataType>& data_type) const;
};
} // namespace cass
#endif