-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mongo.hpp
468 lines (425 loc) · 10.8 KB
/
Mongo.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
465
466
467
#ifndef ___EASY__MONGO_HPP__
#define ___EASY__MONGO_HPP__
#include <mongoc/mongoc.h>
#include <bson/bson.h>
#include "Type.h"
#include <string>
#include <memory>
#include "Bson.hpp"
#include <list>
#include "NoCopy.h"
#include "Log.hpp"
#include "CopyablePtr.hpp"
namespace nicehero
{
class MongoCursor
:public NoCopy
{
friend class MongoClient;
friend class MongoConnectionPool;
protected:
MongoCursor(int err, mongoc_cursor_t* cursor = nullptr);
public:
virtual ~MongoCursor();
BsonPtr fetch();
int m_err = 0;
std::list<_bson_t*> m_cursorImpl;
};
using MongoCursorPtr = CopyablePtr<MongoCursor>;
class MongoConnectionPool;
class MongoUpdater
{
public:
MongoUpdater();
MongoUpdater(const Bson& doc);
void set(BsonPtr&& doc);
void unset(BsonPtr&& doc);
bool available() const;
operator BsonPtr() const;
std::vector<BsonPtr> m_sets;
std::vector<BsonPtr> m_unsets;
};
class MongoClient
:public NoCopy
{
friend class MongoConnectionPool;
protected:
MongoClient(MongoConnectionPool& pool, mongoc_client_t* c);
public:
~MongoClient();
bson_error_t insert(const std::string& collection
, const Bson& doc
, mongoc_insert_flags_t flags = MONGOC_INSERT_NONE
, bool specialWriteConcern = false
, int writeConcern = 1
);
bson_error_t update(const std::string& collection
, const Bson& query
, const MongoUpdater& doc
, mongoc_update_flags_t flags = MONGOC_UPDATE_NONE
, bool specialWriteConcern = false
, int writeConcern = 1
);
MongoCursorPtr find(const std::string& collection
, const Bson& query
, const Bson& opt
, mongoc_read_mode_t readMode = MONGOC_READ_PRIMARY
);
std::string m_dbname;
protected:
private:
MongoConnectionPool& m_pool;
mongoc_client_t* m_client;
};
using MongoClientPtr = std::shared_ptr<MongoClient>;
class MongoConnectionPool
:public NoCopy
{
public:
virtual ~MongoConnectionPool();
bool init(const std::string& mongoUrl
, const std::string& dbname
, int writeConcern = 1
, const std::string& appName = std::string()
);
mongoc_uri_t* m_url = nullptr;
mongoc_client_pool_t * m_poolImpl = nullptr;
bson_error_t insert(const std::string& collection
, const Bson& doc
, mongoc_insert_flags_t flags = MONGOC_INSERT_NONE
, bool specialWriteConcern = false
, int writeConcern = 1
);
bson_error_t update(const std::string& collection
, const Bson& query
, const MongoUpdater& doc
, mongoc_update_flags_t flags = MONGOC_UPDATE_NONE
, bool specialWriteConcern = false
, int writeConcern = 1
);
MongoCursorPtr find(const std::string& collection
, const Bson& query
, const Bson& opt
, mongoc_read_mode_t readMode = MONGOC_READ_PRIMARY
);
MongoClientPtr popClient();
const std::string& getDBName()const;
private:
std::string m_dbname;//NotSet because thread safe
};
inline MongoConnectionPool::~MongoConnectionPool()
{
if (m_poolImpl)
{
mongoc_client_pool_destroy(m_poolImpl);
}
if (m_url)
{
mongoc_uri_destroy(m_url);
}
}
inline bool MongoConnectionPool::init(const std::string& mongoUrl
, const std::string& dbname
, int writeConcern
, const std::string& appName
)
{
static bool firstInit = true;
if (firstInit)
{
firstInit = false;
mongoc_init();
}
m_url = mongoc_uri_new(mongoUrl.c_str());
if (!m_url)
{
nlogerr("create mongo url err:%s", mongoUrl.c_str());
return false;
}
if (appName != "")
{
if (!mongoc_uri_set_appname(m_url, appName.c_str()))
{
nlogerr("mongoc_uri_set_appname err:%s", appName.c_str());
return false;
}
}
if (!mongoc_uri_set_option_as_int32(m_url, MONGOC_URI_MAXPOOLSIZE, 100))
{
nlogerr("mongoc_uri_set_option_as_int32 err");
return false;
}
auto write_concern = mongoc_write_concern_new();
if (!write_concern)
{
nlogerr("mongoc_write_concern_new err");
return false;
}
mongoc_write_concern_set_w(write_concern, writeConcern);
mongoc_uri_set_write_concern(m_url, write_concern);
mongoc_write_concern_destroy(write_concern);
m_poolImpl = mongoc_client_pool_new(m_url);
if (!m_poolImpl)
{
nlogerr("mongoc_client_pool_new err");
return false;
}
auto client = mongoc_client_pool_pop(m_poolImpl);
if (!client)
{
nlogerr("mongoc_client_pool_new2 err");
return false;
}
bson_t ping = BSON_INITIALIZER;
bson_error_t error;
bool r;
BSON_APPEND_INT32(&ping, "ping", 1);
r = mongoc_client_command_simple(
client, "admin", &ping, NULL, NULL, &error);
if (!r)
{
nlogerr("mongoc_client_pool_new3 %s", error.message);
mongoc_client_pool_push(m_poolImpl,client);
return false;
}
m_dbname = dbname;
return true;
}
inline MongoCursor::MongoCursor(int err, mongoc_cursor_t* cursor /*= nullptr*/)
{
m_err = err;
if (!cursor)
{
return;
}
const bson_t *doc = nullptr;
while (mongoc_cursor_next(cursor, &doc))
{
if (doc)
{
bson_t * doc_ = bson_copy(doc);
m_cursorImpl.push_back(doc_);
}
}
mongoc_cursor_destroy(cursor);
}
inline MongoCursor::~MongoCursor()
{
for (auto v:m_cursorImpl)
{
if (v)
{
bson_destroy(v);
}
}
m_cursorImpl.clear();
}
inline BsonPtr MongoCursor::fetch()
{
if (m_cursorImpl.size() < 1)
{
return BsonPtr();
}
auto ret = BsonPtr(new Bson(m_cursorImpl.front()));
m_cursorImpl.pop_front();
return ret;
}
inline bson_error_t MongoConnectionPool::insert(const std::string& collection, const Bson& doc, mongoc_insert_flags_t flags /*= MONGOC_INSERT_NONE */, bool specialWriteConcern /*= false */, int writeConcern /*= 1 */)
{
bson_error_t error;
bson_set_error(&error, 0, 3, "pool empty");
auto c = popClient();
if (!c)
{
return error;
}
return c->insert(collection, doc, flags, specialWriteConcern, writeConcern);
}
inline bson_error_t MongoConnectionPool::update(const std::string& collection, const Bson& query, const MongoUpdater& doc, mongoc_update_flags_t flags /*= MONGOC_UPDATE_NONE */, bool specialWriteConcern /*= false */, int writeConcern /*= 1 */)
{
bson_error_t error;
bson_set_error(&error, 0, 3, "pool empty");
auto c = popClient();
if (!c)
{
return error;
}
return c->update(collection,query, doc, flags, specialWriteConcern, writeConcern);
}
inline const std::string& nicehero::MongoConnectionPool::getDBName()const
{
return m_dbname;
}
inline MongoCursorPtr MongoConnectionPool::find(const std::string& collection, const Bson& query, const Bson& opt, mongoc_read_mode_t readMode /*= MONGOC_READ_PRIMARY */)
{
auto c = popClient();
if (!c)
{
return MongoCursorPtr(new MongoCursor(2));
}
return c->find(collection, query, opt, readMode);
}
inline nicehero::MongoClient::MongoClient(MongoConnectionPool& pool,mongoc_client_t* c)
:m_pool(pool),m_client(c)
{
m_dbname = m_pool.getDBName();
}
inline nicehero::MongoClient::~MongoClient()
{
mongoc_client_pool_push(m_pool.m_poolImpl, m_client);
}
inline bson_error_t nicehero::MongoClient::insert(const std::string& collection, const Bson& doc, mongoc_insert_flags_t flags /*= MONGOC_INSERT_NONE */, bool specialWriteConcern /*= false */, int writeConcern /*= 1 */)
{
bson_error_t error;
bson_set_error(&error, 0, 2, "db error");
auto collection_ = mongoc_client_get_collection(m_client, m_dbname.c_str(), collection.c_str());
if (!collection_)
{
return error;
}
mongoc_write_concern_t* write_concern = nullptr;
if (specialWriteConcern)
{
write_concern = mongoc_write_concern_new();
if (!write_concern)
{
return error;
}
mongoc_write_concern_set_w(write_concern, writeConcern);
}
if (!mongoc_collection_insert(collection_, MONGOC_INSERT_NONE, doc.m_bson, write_concern, &error))
{
if (write_concern)
{
mongoc_write_concern_destroy(write_concern);
}
return error;
}
if (write_concern)
{
mongoc_write_concern_destroy(write_concern);
}
return error;
}
inline bson_error_t nicehero::MongoClient::update(const std::string& collection, const Bson& query, const MongoUpdater& doc, mongoc_update_flags_t flags /*= MONGOC_UPDATE_NONE */, bool specialWriteConcern /*= false */, int writeConcern /*= 1 */)
{
bson_error_t error;
bson_set_error(&error, 0, 2, "db error");
if (!doc.available())
{
bson_set_error(&error, 0, 2, "!doc.available()");
return error;
}
auto collection_ = mongoc_client_get_collection(m_client, m_dbname.c_str(), collection.c_str());
if (!collection_)
{
return error;
}
mongoc_write_concern_t* write_concern = nullptr;
if (specialWriteConcern)
{
write_concern = mongoc_write_concern_new();
if (!write_concern)
{
nlogerr("mongoc_write_concern_new err");
return error;
}
mongoc_write_concern_set_w(write_concern, writeConcern);
}
if (!mongoc_collection_update(collection_, flags, query.m_bson, ((BsonPtr)doc)->m_bson, write_concern, &error))
{
if (write_concern)
{
mongoc_write_concern_destroy(write_concern);
}
return error;
}
if (write_concern)
{
mongoc_write_concern_destroy(write_concern);
}
return error;
}
inline MongoCursorPtr nicehero::MongoClient::find(const std::string& collection, const Bson& query, const Bson& opt, mongoc_read_mode_t readMode /*= MONGOC_READ_PRIMARY */)
{
auto c = m_client;
if (!c)
{
return MongoCursorPtr(new MongoCursor(2));
}
auto collection_ = mongoc_client_get_collection(c, m_dbname.c_str(), collection.c_str());
if (!collection_)
{
return MongoCursorPtr(new MongoCursor(2));
}
auto read_prefs = mongoc_read_prefs_new(readMode);
auto cursor = mongoc_collection_find_with_opts(collection_, query.m_bson, opt.m_bson, read_prefs);
mongoc_read_prefs_destroy(read_prefs);
if (!cursor)
{
return MongoCursorPtr(new MongoCursor(2));
}
auto ret = MongoCursorPtr(new MongoCursor(0, cursor));
return ret;
}
inline MongoClientPtr nicehero::MongoConnectionPool::popClient()
{
auto c = mongoc_client_pool_pop(m_poolImpl);
if (!c)
{
return MongoClientPtr();
}
return MongoClientPtr(new MongoClient(*this,c));
}
inline void MongoUpdater::set(BsonPtr&& doc)
{
m_sets.push_back(std::move(doc));
}
inline void MongoUpdater::unset(BsonPtr&& doc)
{
m_unsets.push_back(std::move(doc));
}
inline MongoUpdater::operator BsonPtr() const
{
if (m_sets.empty() && m_unsets.empty())
{
return nullptr;
}
auto r = Bson::createBsonPtr();
if (m_sets.size() > 0)
{
auto sets = Bson::createBsonPtr();
for (auto& i : m_sets)
{
sets->merge(*i);
}
r->appendBson("$set", *sets);
}
if (m_unsets.size() > 0)
{
auto unsets = Bson::createBsonPtr();
for (auto& i : m_unsets)
{
unsets->merge(*i);
}
r->appendBson("$unset", *unsets);
}
return r;
}
inline bool MongoUpdater::available() const
{
if (m_sets.empty() && m_unsets.empty())
{
return false;
}
return true;
}
inline MongoUpdater::MongoUpdater()
{
}
inline MongoUpdater::MongoUpdater(const Bson& doc)
{
set(Bson::createBsonPtr(doc));
}
}
#endif