forked from apache/cassandra-cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection.cpp
More file actions
167 lines (137 loc) · 5.99 KB
/
collection.cpp
File metadata and controls
167 lines (137 loc) · 5.99 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
/*
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.
*/
#include "collection.hpp"
#include "constants.hpp"
#include "external.hpp"
#include "macros.hpp"
#include "tuple.hpp"
#include "user_type_value.hpp"
#include <string.h>
using namespace datastax;
using namespace datastax::internal::core;
extern "C" {
CassCollection* cass_collection_new(CassCollectionType type, size_t item_count) {
Collection* collection = new Collection(type, item_count);
collection->inc_ref();
return CassCollection::to(collection);
}
CassCollection* cass_collection_new_from_data_type(const CassDataType* data_type,
size_t item_count) {
if (!data_type->is_collection()) {
return NULL;
}
Collection* collection = new Collection(DataType::ConstPtr(data_type), item_count);
collection->inc_ref();
return CassCollection::to(collection);
}
void cass_collection_free(CassCollection* collection) { collection->dec_ref(); }
const CassDataType* cass_collection_data_type(const CassCollection* collection) {
return CassDataType::to(collection->data_type().get());
}
#define CASS_COLLECTION_APPEND(Name, Params, Value) \
CassError cass_collection_append_##Name(CassCollection* collection Params) { \
return collection->append(Value); \
}
CASS_COLLECTION_APPEND(null, ZERO_PARAMS_(), CassNull())
CASS_COLLECTION_APPEND(int8, ONE_PARAM_(cass_int8_t value), value)
CASS_COLLECTION_APPEND(int16, ONE_PARAM_(cass_int16_t value), value)
CASS_COLLECTION_APPEND(int32, ONE_PARAM_(cass_int32_t value), value)
CASS_COLLECTION_APPEND(uint32, ONE_PARAM_(cass_uint32_t value), value)
CASS_COLLECTION_APPEND(int64, ONE_PARAM_(cass_int64_t value), value)
CASS_COLLECTION_APPEND(float, ONE_PARAM_(cass_float_t value), value)
CASS_COLLECTION_APPEND(double, ONE_PARAM_(cass_double_t value), value)
CASS_COLLECTION_APPEND(bool, ONE_PARAM_(cass_bool_t value), value)
CASS_COLLECTION_APPEND(uuid, ONE_PARAM_(CassUuid value), value)
CASS_COLLECTION_APPEND(inet, ONE_PARAM_(CassInet value), value)
CASS_COLLECTION_APPEND(collection, ONE_PARAM_(const CassCollection* value), value)
CASS_COLLECTION_APPEND(tuple, ONE_PARAM_(const CassTuple* value), value)
CASS_COLLECTION_APPEND(user_type, ONE_PARAM_(const CassUserType* value), value)
CASS_COLLECTION_APPEND(bytes, TWO_PARAMS_(const cass_byte_t* value, size_t value_size),
CassBytes(value, value_size))
CASS_COLLECTION_APPEND(decimal,
THREE_PARAMS_(const cass_byte_t* varint, size_t varint_size, int scale),
CassDecimal(varint, varint_size, scale))
CASS_COLLECTION_APPEND(duration,
THREE_PARAMS_(cass_int32_t months, cass_int32_t days, cass_int64_t nanos),
CassDuration(months, days, nanos))
#undef CASS_COLLECTION_APPEND
CassError cass_collection_append_string(CassCollection* collection, const char* value) {
return collection->append(CassString(value, SAFE_STRLEN(value)));
}
CassError cass_collection_append_string_n(CassCollection* collection, const char* value,
size_t value_length) {
return collection->append(CassString(value, value_length));
}
CassError cass_collection_append_custom(CassCollection* collection, const char* class_name,
const cass_byte_t* value, size_t value_size) {
return collection->append(CassCustom(StringRef(class_name), value, value_size));
}
CassError cass_collection_append_custom_n(CassCollection* collection, const char* class_name,
size_t class_name_length, const cass_byte_t* value,
size_t value_size) {
return collection->append(
CassCustom(StringRef(class_name, class_name_length), value, value_size));
}
} // extern "C"
CassError Collection::append(CassNull value) {
CASS_COLLECTION_CHECK_TYPE(value);
items_.push_back(Buffer());
return CASS_OK;
}
CassError Collection::append(const Collection* value) {
CASS_COLLECTION_CHECK_TYPE(value);
items_.push_back(value->encode());
return CASS_OK;
}
CassError Collection::append(const Tuple* value) {
CASS_COLLECTION_CHECK_TYPE(value);
items_.push_back(value->encode());
return CASS_OK;
}
CassError Collection::append(const UserTypeValue* value) {
CASS_COLLECTION_CHECK_TYPE(value);
items_.push_back(value->encode());
return CASS_OK;
}
size_t Collection::get_items_size() const {
size_t size = 0;
for (BufferVec::const_iterator i = items_.begin(), end = items_.end(); i != end; ++i) {
size += sizeof(int32_t);
size += i->size();
}
return size;
}
void Collection::encode_items(char* buf) const {
for (BufferVec::const_iterator i = items_.begin(), end = items_.end(); i != end; ++i) {
encode_int32(buf, i->size());
buf += sizeof(int32_t);
memcpy(buf, i->data(), i->size());
buf += i->size();
}
}
size_t Collection::get_size() const { return sizeof(int32_t) + get_items_size(); }
size_t Collection::get_size_with_length() const { return sizeof(int32_t) + get_size(); }
Buffer Collection::encode() const {
Buffer buf(get_size());
size_t pos = buf.encode_int32(0, get_count());
encode_items(buf.data() + pos);
return buf;
}
Buffer Collection::encode_with_length() const {
size_t internal_size = get_size();
Buffer buf(sizeof(int32_t) + internal_size);
size_t pos = buf.encode_int32(0, internal_size);
pos = buf.encode_int32(pos, get_count());
encode_items(buf.data() + pos);
return buf;
}