-
Notifications
You must be signed in to change notification settings - Fork 127
/
bson_decoder.h
279 lines (245 loc) · 7.71 KB
/
bson_decoder.h
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
/*
* Copyright (C) 2021 Duowan Inc. All rights reserved.
*
* 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 __X_PACK_BSON_DECODER_H
#define __X_PACK_BSON_DECODER_H
#include <string.h>
#include <libbson-1.0/bson.h>
#include "util.h"
#include "xdecoder.h"
#include "bson_type.h"
namespace xpack {
class BsonNode {
typedef XDecoder<BsonNode> decoder;
friend class BsonDecoder;
public:
typedef size_t Iterator;
BsonNode(const bson_iter_t* iter = NULL):it(iter),inited(false) {
if (NULL != it) {
type = bson_iter_type(it);
}
}
inline static const char * Name() {
return "bson";
}
operator bool () const {
return it != NULL;
}
bool IsNull() const {
return NULL!=it && BSON_TYPE_NULL==bson_iter_type(it);
}
BsonNode Find(decoder&de, const char*key, const Extend *ext) {
(void)de;
(void)ext;
if (!inited) {
this->init();
}
if (BSON_TYPE_DOCUMENT != type) {
de.decode_exception("not document", NULL);
}
node_index::iterator iter = _childs_index.find(key);
if (iter != _childs_index.end()) {
return BsonNode(&_childs[iter->second]);
} else {
return BsonNode();
}
}
size_t Size(decoder&de) {
(void)de;
if (!inited) {
this->init();
}
if (BSON_TYPE_ARRAY == type) {
return _childs.size();
} else {
de.decode_exception("not array", NULL);
return 0;
}
}
BsonNode At(size_t index) const { // no exception
return BsonNode(&(_childs[index]));
}
BsonNode Next(decoder&de, BsonNode&p, Iterator&iter, std::string&key) {
(void)de;
if (!p.inited) {
p.init();
}
if (BSON_TYPE_DOCUMENT != p.type) {
de.decode_exception("not document", NULL);
}
if (it != p.it) {
++iter;
} else {
iter = 0;
}
if (iter != p._childs.size()) {
bson_iter_t *t = &p._childs[iter];
key = bson_iter_key(t);
return BsonNode(t);
}
return BsonNode();
}
bool Get(decoder&de, std::string&val, const Extend*ext) {
(void)ext;
if (BSON_TYPE_UTF8 == type) {
uint32_t length;
const char* data = bson_iter_utf8(it, &length);
if (NULL != data) {
val = std::string(data, length);
}
} else if (BSON_TYPE_NULL != type) {
de.decode_exception("not string", NULL);
}
return true;
}
bool Get(decoder&de, bool &val, const Extend*ext) {
(void)de;
(void)ext;
val = bson_iter_as_bool(it);
return true;
}
template <class T>
typename x_enable_if<numeric<T>::is_integer, bool>::type Get(decoder&de, T &val, const Extend*ext){
(void)de;
(void)ext;
val = (T)bson_iter_as_int64(it);
return true;
}
template <class T>
typename x_enable_if<numeric<T>::is_float, bool>::type Get(decoder&de, T &val, const Extend*ext){
(void)de;
(void)ext;
val = (T)bson_iter_double(it);
return true;
}
/*std::string json() const {
if (NULL == _data) {
return "";
}
size_t length=BSON_UINT32_TO_LE(*(int32_t*)_data);
bson_t b; // local is ok
bson_init_static(&b, _data, length);
size_t s;
char *jstr = bson_as_json(&b, &s);
if (jstr) {
std::string j(jstr);
bson_free(jstr);
return j;
} else {
return "";
}
}*/
// bson type
bool decode_type_spec(bson_oid_t &val, const Extend *ext) {
(void)ext;
const bson_oid_t *t = bson_iter_oid(it);
if (t != NULL) {
bson_oid_init_from_data(&val, t->bytes);
return true;
} else {
return false;
}
}
bool decode_type_spec(bson_date_time_t &val, const Extend *ext) {
(void)ext;
val.ts = bson_iter_date_time(it);
return true;
}
bool decode_type_spec(bson_timestamp_t &val, const Extend *ext) {
(void)ext;
bson_iter_timestamp(it, &val.timestamp, &val.increment);
return true;
}
bool decode_type_spec(bson_decimal128_t &val, const Extend *ext) {
(void)ext;
return bson_iter_decimal128(it, &val);
}
bool decode_type_spec(bson_regex_t &val, const Extend *ext) {
(void)ext;
const char *options = NULL;
const char *regex = bson_iter_regex(it, &options);
if (NULL != regex) {
val.pattern = regex;
}
if (NULL != options) {
val.options = options;
}
return true;
}
bool decode_type_spec(bson_binary_t &val, const Extend *ext) {
(void)ext;
uint32_t len = 0;
const uint8_t *data = NULL;
bson_iter_binary(it, &val.subType, &len, &data);
if (data != NULL && len > 0) {
val.data = std::string((const char*)data, size_t(len));
}
return true;
}
private:
typedef std::map<const char*, size_t, cmp_str> node_index; // index of _childs
// get object or array info
void init(bool top = false) {
inited = true;
bson_iter_t sub;
if (NULL != it) {
if (top) {
type = BSON_TYPE_DOCUMENT;
memcpy((void*)&sub, (void*)it, sizeof(sub));
} else if (!bson_iter_recurse(it, &sub)) {
return;
}
size_t i = 0;
while (bson_iter_next(&sub)) {
_childs.push_back(sub);
if (type == BSON_TYPE_DOCUMENT) {
_childs_index[bson_iter_key(&sub)] = i++;
}
}
}
}
const bson_iter_t* it;
bson_type_t type;
bool inited;
std::vector<bson_iter_t> _childs; // childs
node_index _childs_index;
};
class BsonDecoder {
public:
// if length==0, length will get from data
template <class T>
bool decode(const uint8_t*data, size_t length, T&val) {
length = (length>0)?length:BSON_UINT32_TO_LE(*(int32_t*)data);
bson_t b;
bson_iter_t it;
bson_init_static(&b, data, length);
bson_iter_init(&it, &b);
BsonNode node(&it);
node.init(true);
return XDecoder<BsonNode>(NULL, (const char*)NULL, node).decode(val, NULL);
}
template <class T>
bool decode(const std::string&data, T&val) {
return decode((const uint8_t*)data.data(), data.length(), val);
}
};
template<>struct is_xpack_type_spec<BsonNode, bson_oid_t> {static bool const value = true;};
template<>struct is_xpack_type_spec<BsonNode, bson_date_time_t> {static bool const value = true;};
template<>struct is_xpack_type_spec<BsonNode, bson_timestamp_t> {static bool const value = true;};
template<>struct is_xpack_type_spec<BsonNode, bson_decimal128_t> {static bool const value = true;};
template<>struct is_xpack_type_spec<BsonNode, bson_binary_t> {static bool const value = true;};
template<>struct is_xpack_type_spec<BsonNode, bson_regex_t> {static bool const value = true;};
}
#endif