-
-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathtbson.cpp
354 lines (284 loc) · 8.78 KB
/
tbson.cpp
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
/* Copyright (c) 2012-2019, AOYAMA Kazuharu
* All rights reserved.
*
* This software may be used and distributed according to the terms of
* the New BSD License, which is incorporated herein by reference.
*/
#include "tsystemglobal.h"
#include <QCoreApplication>
#include <QDateTime>
#include <QRegularExpression>
#include <QStringList>
#include <QtEndian>
#include <TBson>
#include <atomic>
extern "C" {
#include <bson/bson.h>
}
/*!
\class TBson
\brief The TBson class represents a Binary JSON for MongoDB.
*/
TBson::TBson() :
_bsonData(bson_new())
{
}
TBson::~TBson()
{
bson_destroy((bson_t *)_bsonData);
}
TBson::TBson(const TBsonObject *bson)
{
_bsonData = (bson) ? bson_copy((const bson_t *)bson) : bson_new();
}
QVariant TBson::value(const QString &key, const QVariant &defaultValue) const
{
return fromBson(*this).value(key, defaultValue);
}
QVariantMap TBson::fromBson(const TBson &bson)
{
return TBson::fromBson(bson.constData());
}
QVariantMap TBson::fromBson(const TBsonObject *obj)
{
QVariantMap ret;
bson_iter_t it;
const bson_t *bson = (const bson_t *)obj;
bson_iter_init(&it, bson);
while (bson_iter_next(&it)) {
bson_type_t t = bson_iter_type(&it);
QString key(bson_iter_key(&it));
switch (t) {
case BSON_TYPE_EOD:
return ret;
break;
case BSON_TYPE_DOUBLE:
ret[key] = bson_iter_double(&it);
break;
case BSON_TYPE_UTF8:
ret[key] = QString::fromUtf8(bson_iter_utf8(&it, nullptr));
break;
case BSON_TYPE_ARRAY: {
const uint8_t *docbuf = nullptr;
uint32_t doclen = 0;
bson_t sub[1];
bson_iter_array(&it, &doclen, &docbuf);
if (bson_init_static(sub, docbuf, doclen)) {
ret[key] = fromBson(sub).values();
}
break;
}
case BSON_TYPE_DOCUMENT: {
const uint8_t *docbuf = nullptr;
uint32_t doclen = 0;
bson_t sub[1];
bson_iter_document(&it, &doclen, &docbuf);
if (bson_init_static(sub, docbuf, doclen)) {
ret[key] = fromBson(sub);
}
break;
}
case BSON_TYPE_BINARY: {
const uint8_t *binary = nullptr;
bson_subtype_t subtype = BSON_SUBTYPE_BINARY;
uint32_t len = 0;
bson_iter_binary(&it, &subtype, &len, &binary);
if (binary) {
ret[key] = QByteArray((char *)binary, len);
}
break;
}
case BSON_TYPE_UNDEFINED:
ret[key] = QVariant();
break;
case BSON_TYPE_OID: {
char oidhex[25];
bson_oid_to_string(bson_iter_oid(&it), oidhex);
ret[key] = QString(oidhex);
break;
}
case BSON_TYPE_BOOL:
ret[key] = (bool)bson_iter_bool(&it);
break;
case BSON_TYPE_DATE_TIME: {
QDateTime date;
date.setMSecsSinceEpoch(bson_iter_date_time(&it));
ret[key] = date;
break;
}
case BSON_TYPE_NULL:
ret[key] = QVariant();
break;
case BSON_TYPE_REGEX:
ret[key] = QRegularExpression(QLatin1String(bson_iter_regex(&it, nullptr)));
break;
case BSON_TYPE_CODE:
ret[key] = QString(bson_iter_code(&it, nullptr));
break;
case BSON_TYPE_SYMBOL:
ret[key] = QString(bson_iter_symbol(&it, nullptr));
break;
case BSON_TYPE_INT32:
ret[key] = bson_iter_int32(&it);
break;
case BSON_TYPE_INT64:
ret[key] = (qint64)bson_iter_int64(&it);
break;
case BSON_TYPE_CODEWSCOPE: // FALLTHRU
case BSON_TYPE_TIMESTAMP: // FALLTHRU (internal use)
// do nothing
break;
default:
Tf::error("fromBson() unknown type: {}", (int)t);
break;
}
//tSystemDebug("fromBson : t:{} key:{} = {}", t, qUtf8Printable(key), qUtf8Printable(ret[key].toString()));
}
return ret;
}
static bool appendBsonValue(bson_t *bson, const QString &key, const QVariant &value)
{
static const QLatin1String oidkey("_id");
bool ok = true;
auto type = value.typeId();
// _id
if (key == oidkey) {
QByteArray oidVal = value.toByteArray();
if (oidVal.length() == 24) {
// ObjectId
bson_oid_t oid;
bson_oid_init_from_string(&oid, oidVal.data());
BSON_APPEND_OID(bson, oidkey.latin1(), &oid);
} else {
int id = value.toInt(&ok);
if (ok) {
BSON_APPEND_INT32(bson, oidkey.latin1(), id);
} else {
BSON_APPEND_UTF8(bson, oidkey.latin1(), value.toString().toUtf8().data());
}
}
return true;
}
switch (type) {
case QMetaType::Int:
BSON_APPEND_INT32(bson, qUtf8Printable(key), value.toInt(&ok));
break;
case QMetaType::QString:
BSON_APPEND_UTF8(bson, qUtf8Printable(key), value.toString().toUtf8().data());
break;
case QMetaType::LongLong:
BSON_APPEND_INT64(bson, qUtf8Printable(key), value.toLongLong(&ok));
break;
case QMetaType::QVariantMap:
BSON_APPEND_DOCUMENT(bson, qUtf8Printable(key), (const bson_t *)TBson::toBson(value.toMap()).constData());
break;
case QMetaType::Double:
BSON_APPEND_DOUBLE(bson, qUtf8Printable(key), value.toDouble(&ok));
break;
case QMetaType::Bool:
BSON_APPEND_BOOL(bson, qUtf8Printable(key), value.toBool());
break;
case QMetaType::QDateTime: {
BSON_APPEND_DATE_TIME(bson, qUtf8Printable(key), value.toDateTime().toMSecsSinceEpoch());
break;
}
case QMetaType::QByteArray: {
QByteArray ba = value.toByteArray();
BSON_APPEND_BINARY(bson, qUtf8Printable(key), BSON_SUBTYPE_BINARY, (uint8_t *)ba.constData(), ba.length());
break;
}
case QMetaType::QVariantList: // FALLTHRU
case QMetaType::QStringList: {
bson_t child;
BSON_APPEND_ARRAY_BEGIN(bson, qUtf8Printable(key), &child);
int i = 0;
for (auto &var : (const QList<QVariant> &)value.toList()) {
appendBsonValue(&child, QString::number(i++), var);
}
bson_append_array_end(bson, &child);
break;
}
case QMetaType::UnknownType:
BSON_APPEND_UNDEFINED(bson, qUtf8Printable(key));
break;
default:
Tf::error("toBson() failed to convert name:{} type:{}", qUtf8Printable(key), type);
ok = false;
break;
}
return ok;
}
static void appendBson(TBsonObject *bson, const QVariantMap &map)
{
for (auto it = map.begin(); it != map.end(); ++it) {
bool res = appendBsonValue((bson_t *)bson, qUtf8Printable(it.key()), it.value());
if (!res) {
break;
}
}
}
bool TBson::insert(const QString &key, const QVariant &value)
{
return appendBsonValue((bson_t *)_bsonData, key, value);
}
TBson TBson::toBson(const QVariantMap &map)
{
TBson ret;
if (!map.isEmpty()) {
appendBson(ret.data(), map);
}
return ret;
}
TBson TBson::toBson(const QString &op, const QVariantMap &map)
{
TBson ret;
if (!op.isEmpty()) {
BSON_APPEND_DOCUMENT((bson_t *)ret.data(), qUtf8Printable(op), (const bson_t *)TBson::toBson(map).constData());
}
return ret;
}
TBson TBson::toBson(const QVariantMap &query, const QVariantMap &orderBy)
{
TBson ret;
bson_t child;
// query clause
BSON_APPEND_DOCUMENT_BEGIN((bson_t *)ret.data(), "$query", &child);
appendBson(&child, query);
bson_append_document_end((bson_t *)ret.data(), &child);
// orderBy clause
if (!orderBy.isEmpty()) {
BSON_APPEND_DOCUMENT_BEGIN((bson_t *)ret.data(), "$orderby", &child);
appendBson(&child, orderBy);
bson_append_document_end((bson_t *)ret.data(), &child);
}
return ret;
}
TBson TBson::toBson(const QStringList &lst)
{
TBson ret;
for (auto &str : lst) {
bool res = appendBsonValue((bson_t *)ret.data(), qUtf8Printable(str), 1);
if (!res)
break;
}
return ret;
}
QString TBson::generateObjectId()
{
bson_oid_t oid;
bson_oid_init(&oid, nullptr);
QByteArray oidhex = QByteArray((char *)&oid, sizeof(oid)).toHex();
return QLatin1String(oidhex.data());
}
/*
ObjectId is a 12-byte BSON type, constructed using:
a 4-byte value representing the seconds since the Unix epoch,
a 3-byte machine identifier,
a 2-byte process id, and
a 3-byte counter, starting with a random value.
*/
/* BSON dump
auto str = bson_as_json(bson, nullptr);
printf("%s\n", str);
bson_free(str);
*/