-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBson.hpp
270 lines (255 loc) · 5.69 KB
/
Bson.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
#ifndef ___EASY__BSON_HPP__
#define ___EASY__BSON_HPP__
#include <bson/bson.h>
#include "Type.h"
#include <string>
#include <memory>
#include "Log.h"
#include "CopyablePtr.hpp"
namespace nicehero
{
class Bson;
class BsonView;
typedef CopyablePtr<Bson> BsonPtr;
template <bool isView = false>
class BsonT
{
public:
BsonT() {
}
BsonT(bson_t* t)
:m_bson(t)
{
}
~BsonT()
{
if (isView && m_bson)
{
bson_destroy(m_bson);
}
}
operator const bson_t*() const
{
return m_bson;
}
operator bson_t*() const
{
return m_bson;
}
bool isObject(const char* dotkey) const
{
bson_iter_t iter, iter2;
if (!m_bson
|| !bson_iter_init(&iter, m_bson)
|| !bson_iter_find_descendant(&iter, dotkey, &iter2))
{
return false;
}
auto t = bson_iter_type(&iter2);
return t == BSON_TYPE_DOCUMENT || t == BSON_TYPE_NULL || t == BSON_TYPE_UNDEFINED;
}
BsonView asObject(const char* dotkey);
bool isString(const char* dotkey) const
{
bson_iter_t iter, iter2;
if (!m_bson
|| !bson_iter_init(&iter, m_bson)
|| !bson_iter_find_descendant(&iter, dotkey, &iter2))
{
return false;
}
return bson_iter_type(&iter2) == BSON_TYPE_UTF8;
}
const char* asString(const char* dotkey) const
{
bson_iter_t iter, iter2;
if (bson_iter_init(&iter, m_bson)
&& bson_iter_find_descendant(&iter, dotkey, &iter2)
&& BSON_ITER_HOLDS_UTF8(&iter2))
{
return bson_iter_utf8(&iter2, nullptr);
}
return "";
}
bool isInt64(const char* dotkey) const
{
bson_iter_t iter, iter2;
if (!m_bson
|| !bson_iter_init(&iter, m_bson)
|| !bson_iter_find_descendant(&iter, dotkey, &iter2))
{
return false;
}
return bson_iter_type(&iter2) == BSON_TYPE_INT64;
}
i64 asInt64(const char* dotkey) const
{
bson_iter_t iter,iter2;
if (bson_iter_init(&iter, m_bson)
&& bson_iter_find_descendant(&iter, dotkey, &iter2)
&& BSON_ITER_HOLDS_INT64(&iter2))
{
return bson_iter_as_int64(&iter2);
}
return 0;
}
bool isNumber(const char* dotkey) const
{
bson_iter_t iter,iter2;
if (!m_bson
|| !bson_iter_init(&iter, m_bson)
|| !bson_iter_find_descendant(&iter, dotkey, &iter2))
{
return false;
}
if (iter2.value.value_type == BSON_TYPE_INT64
|| iter2.value.value_type == BSON_TYPE_INT32
|| iter2.value.value_type == BSON_TYPE_DECIMAL128
|| iter2.value.value_type == BSON_TYPE_DOUBLE
)
{
return true;
}
return false;
}
double asNumber(const char* dotkey) const
{
bson_iter_t iter, iter2;
if (bson_iter_init(&iter, m_bson)
&& bson_iter_find_descendant(&iter, dotkey, &iter2)
&& BSON_ITER_HOLDS_NUMBER(&iter2))
{
return bson_iter_as_double(&iter2);
}
return 0.0;
}
std::string toJson() const
{
if (!m_bson) {
return "";
}
auto r = bson_as_json(m_bson, nullptr);
auto rr = std::string(r);
bson_free(r);
return rr;
}
bson_t* m_bson = nullptr;
operator bool() const
{
if (!m_bson) {
return false;
}
bson_iter_t iter;
bson_iter_init(&iter, m_bson);
auto t = bson_iter_type(&iter);
if (t == BSON_TYPE_NULL || t == BSON_TYPE_UNDEFINED)
{
return false;
}
if (t == BSON_TYPE_DOCUMENT || t == BSON_TYPE_ARRAY)
{
return bson_count_keys(m_bson) > 0;
}
return true;
}
template <bool b>
void appendBson(const std::string& k, const BsonT<b>& o)
{
bson_append_document(m_bson, k.c_str(), (int)k.length(), o.m_bson);
}
void append(const std::string& k, i32 numInt)
{
bson_append_int32(m_bson, k.c_str(), (int)k.length(), numInt);
}
void append(const std::string& k, i64 numLong)
{
bson_append_int64(m_bson, k.c_str(), (int)k.length(), numLong);
}
void append(const std::string& k, const std::string& str)
{
bson_append_utf8(m_bson, k.c_str(), (int)k.length(), str.c_str(), (int)str.length());
}
void append(const std::string& k, double number)
{
bson_append_double(m_bson, k.c_str(), (int)k.length(), number);
}
template<bool b>
void merge(const BsonT<b>& o)
{
if (!o.m_bson)
{
return;
}
bson_iter_t iter;
if (!bson_iter_init(&iter, o.m_bson))
{
return;
}
while (bson_iter_next(&iter))
{
bson_append_iter(m_bson, bson_iter_key(&iter), bson_iter_key_len(&iter), &iter);
}
}
};
class BsonView : public BsonT<true> {
public:
BsonView() {
}
BsonView(const uint8_t *buf, uint32_t len) {
bson_init_static(&m_static_buff, buf, size_t(len));
m_bson = &m_static_buff;
}
bson_t m_static_buff;
};
class Bson : public BsonT<false> {
public:
Bson() {
m_bson = bson_new();
}
Bson(bson_t* t) : BsonT<false>(t) {
if (!m_bson)
{
m_bson = bson_new();
}
}
Bson(const BsonT<true>& view) {
if (view) {
m_bson = bson_copy(view.m_bson);
}
else {
m_bson = bson_new();
}
}
static BsonPtr createBsonPtr()
{
return BsonPtr(new Bson(bson_new()));
}
static BsonPtr createBsonPtr(const Bson& doc)
{
return BsonPtr(new Bson(bson_copy(doc.m_bson)));
}
};
template <bool isView /*= false*/>
BsonView nicehero::BsonT<isView>::asObject(const char* dotkey)
{
bson_iter_t iter, iter2;
if (bson_iter_init(&iter, m_bson)
&& bson_iter_find_descendant(&iter, dotkey, &iter2))
{
auto t = bson_iter_type(&iter2);
if (t == BSON_TYPE_DOCUMENT)
{
const uint8_t *buf;
uint32_t len;
bson_iter_document(&iter, &len, &buf);
return BsonView(buf, len);
}
}
return BsonView();
}
}
#ifndef NBSON
#define NBSON(...) nicehero::BsonPtr(new ::nicehero::Bson(bcon_new (NULL, __VA_ARGS__, (void *) NULL))) //BCON_NEW
#define NBSON_T(...) ::nicehero::Bson(bcon_new (NULL, __VA_ARGS__, (void *) NULL)) //BCON_NEW
#endif
#endif // !___EASY__BSON_HPP__