15#ifndef CCGL_DB_MONGOC_H
16#define CCGL_DB_MONGOC_H
64 mongoc_client_t*
GetConn() {
return conn_; }
67 mongoc_database_t*
GetDatabase(
string const& dbname);
70 mongoc_collection_t*
GetCollection(
string const& dbname,
string const& collectionname);
73 mongoc_gridfs_t*
GetGridFs(
string const& dbname,
string const& gfsname);
85 void GetGridFsFileNames(
string const& dbname,
string const& gfsname, vector<string>& gfs_exists);
90 mongoc_client_t* conn_;
112 mongoc_database_t* db_;
129 mongoc_cursor_t*
ExecuteQuery(
const bson_t* b,
const bson_t* opts =
nullptr);
134 mongoc_collection_t* collection_;
153 mongoc_gridfs_file_t*
GetFile(
string const& gfilename, mongoc_gridfs_t* gfs = NULL,
157 bool RemoveFile(
string const& gfilename, mongoc_gridfs_t* gfs = NULL,
161 void GetFileNames(vector<string>& files_existed, mongoc_gridfs_t* gfs = NULL,
165 bson_t*
GetFileMetadata(
string const& gfilename, mongoc_gridfs_t* gfs = NULL,
169 bool GetStreamData(
string const& gfilename,
char*& databuf, vint& datalength,
170 mongoc_gridfs_t* gfs = NULL,
175 const bson_t* p, mongoc_gridfs_t* gfs = NULL);
178 mongoc_gridfs_t* gfs_;
183 const string& prefix =
string());
193 const bson_value_t* vv = bson_iter_value(iter);
194 if (vv->value_type == BSON_TYPE_INT32) {
195 numericvalue =
static_cast<T
>(vv->value.v_int32);
196 }
else if (vv->value_type == BSON_TYPE_INT64) {
197 numericvalue =
static_cast<T
>(vv->value.v_int64);
198 }
else if (vv->value_type == BSON_TYPE_DOUBLE) {
199 numericvalue =
static_cast<T
>(vv->value.v_double);
200 }
else if (vv->value_type == BSON_TYPE_UTF8) {
201 string tmp = vv->value.v_utf8.str;
202 if (tmp.find_first_of(
"0123456789") == string::npos) {
203 cout <<
"bson iterator isn't or not contains a numeric value." << endl;
207 numericvalue =
static_cast<T
>(strtod(tmp.c_str(), &end));
209 cout <<
"bson iterator isn't or not contains a numeric value." << endl;
221 const bson_value_t* vv = bson_iter_value(iter);
223 if (vv->value_type == BSON_TYPE_ARRAY) {
224 bson_iter_t sub_iter;
225 bson_iter_recurse(iter, &sub_iter);
226 while (bson_iter_next(&sub_iter)) {
227 const bson_value_t* sub_vv = bson_iter_value(&sub_iter);
228 if (sub_vv->value_type == BSON_TYPE_INT32) {
229 out.emplace_back(
CVT_INT(sub_vv->value.v_int32));
230 }
else if (sub_vv->value_type == BSON_TYPE_INT64) {
231 out.emplace_back(
CVT_INT(sub_vv->value.v_int64));
232 }
else if (sub_vv->value_type == BSON_TYPE_DOUBLE) {
233 out.emplace_back(sub_vv->value.v_double);
235 StatusMessage(
"Failed in get vector value in the inner loop of GetVectorFromBsonIter.");
240 out.emplace_back(value);
243 StatusMessage(
"Failed in get vector value in the outer loop of GetVectorFromBsonIter.");
257 const bson_value_t* vv = bson_iter_value(iter);
259 if (vv->value_type == BSON_TYPE_ARRAY) {
260 bson_iter_t sub_iter;
261 bson_iter_recurse(iter, &sub_iter);
262 while (bson_iter_next(&sub_iter)) {
265 out.emplace_back(tmp);
270 tmp.emplace_back(value);
271 out.emplace_back(tmp);
289 if (bson_iter_init(&iter, bmeta) && bson_iter_find(&iter, key)) {
292 StatusMessage((
"WARNING: GetNumericFromBson, Failed in get value of: " +
string(key) +
"\n").c_str());
#define CVT_INT(param)
A reference to the postfix of executable file for RELWITHDEBINFO mode.
Definition: basic.h:325
Base class for classes that cannot be copied.
Definition: basic.h:385
A simple wrapper of the class of MongoDB Client mongoc_client_t.
Definition: db_mongoc.h:46
void GetDatabaseNames(vector< string > &dbnames)
Get existing database names.
Definition: db_mongoc.cpp:116
mongoc_database_t * GetDatabase(string const &dbname)
Get existing or newly created mongoc_database_t instance.
Definition: db_mongoc.cpp:146
mongoc_collection_t * GetCollection(string const &dbname, string const &collectionname)
Get mongoc_collection_t instance.
Definition: db_mongoc.cpp:153
mongoc_client_t * GetConn()
Get mongoc_client_t instance.
Definition: db_mongoc.h:64
~MongoClient()
Destructor.
Definition: db_mongoc.cpp:99
MongoClient(const char *host, vuint16_t port)
Constructor using IP address and port number.
Definition: db_mongoc.cpp:47
void Destroy()
Destroy explicitly.
Definition: db_mongoc.cpp:103
mongoc_gridfs_t * GetGridFs(string const &dbname, string const &gfsname)
Get mongoc_gridfs_t instance.
Definition: db_mongoc.cpp:167
void GetGridFsFileNames(string const &dbname, string const &gfsname, vector< string > &gfs_exists)
Get GridFs file names in MongoDB database.
Definition: db_mongoc.cpp:192
MongoGridFs * GridFs(string const &dbname, string const &gfsname)
Get MongoGridFs instance.
Definition: db_mongoc.cpp:183
void GetCollectionNames(string const &dbname, vector< string > &collnames)
Get collection names in MongoDB database.
Definition: db_mongoc.cpp:142
static MongoClient * Init(const char *host, vuint16_t port)
Initialization of MongoClient with the validation check of database.
Definition: db_mongoc.cpp:78
A simple wrapper of the class of MongoDB Collection mongoc_collection_t.
Definition: db_mongoc.h:120
mongoc_cursor_t * ExecuteQuery(const bson_t *b, const bson_t *opts=nullptr)
Execute query.
Definition: db_mongoc.cpp:275
MongoCollection(mongoc_collection_t *coll)
Constructor by a mongoc_collection_t pointer.
Definition: db_mongoc.cpp:267
~MongoCollection()
Destructor.
Definition: db_mongoc.cpp:271
vint QueryRecordsCount()
Query the records number.
Definition: db_mongoc.cpp:281
A simple wrapper of the class of MongoDB database mongoc_database_t.
Definition: db_mongoc.h:97
MongoDatabase(mongoc_database_t *db)
Constructor by a mongoc_database_t pointer.
Definition: db_mongoc.cpp:200
void GetCollectionNames(vector< string > &collnames)
Get collection names in current database.
Definition: db_mongoc.cpp:215
~MongoDatabase()
Destructor.
Definition: db_mongoc.cpp:208
A simple wrapper of the class of MongoDB database mongoc_gridfs_t.
Definition: db_mongoc.h:141
bool RemoveFile(string const &gfilename, mongoc_gridfs_t *gfs=NULL, STRING_MAP opts=STRING_MAP())
Remove GridFS all matching files and their data chunks.
Definition: db_mongoc.cpp:338
~MongoGridFs()
Destructor.
Definition: db_mongoc.cpp:304
mongoc_gridfs_file_t * GetFile(string const &gfilename, mongoc_gridfs_t *gfs=NULL, const STRING_MAP &opts=STRING_MAP())
Get GridFS file by name.
Definition: db_mongoc.cpp:308
MongoGridFs(mongoc_gridfs_t *gfs=NULL)
Constructor by a mongoc_gridfs_t pointer or NULL.
Definition: db_mongoc.cpp:300
bool GetStreamData(string const &gfilename, char *&databuf, vint &datalength, mongoc_gridfs_t *gfs=NULL, const STRING_MAP *opts=nullptr)
Get stream data of a given GridFS file name.
Definition: db_mongoc.cpp:416
bool WriteStreamData(const string &gfilename, char *&buf, vint length, const bson_t *p, mongoc_gridfs_t *gfs=NULL)
Write stream data to a GridFS file.
Definition: db_mongoc.cpp:447
bson_t * GetFileMetadata(string const &gfilename, mongoc_gridfs_t *gfs=NULL, STRING_MAP opts=STRING_MAP())
Get metadata of a given GridFS file name, remember to destory bson_t after use.
Definition: db_mongoc.cpp:396
void GetFileNames(vector< string > &files_existed, mongoc_gridfs_t *gfs=NULL, STRING_MAP opts=STRING_MAP())
Get GridFS file names.
Definition: db_mongoc.cpp:374
mongoc_gridfs_t * GetGridFs()
Get the current instance of mongoc_gridfs_t
Definition: db_mongoc.h:150
void GetVectorVectorFromBsonIter(bson_iter_t *iter, vector< vector< T > > &out)
parse the following nested vector from bson_t: [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
Definition: db_mongoc.h:256
bool GetNumericFromBson(bson_t *bmeta, const char *key, T &numericvalue)
Get numeric value from bson_t according to a given key.
Definition: db_mongoc.h:287
void AppendStringOptionsToBson(bson_t *bson_opts, const STRING_MAP &opts, const string &prefix)
Append options to bson_t
Definition: db_mongoc.cpp:486
string GetStringFromBsonIterator(bson_iter_t *iter)
Get String from the iterator (bson_iter_t) of bson_t
Definition: db_mongoc.cpp:508
bool GetBoolFromBsonIterator(bson_iter_t *iter)
Get Bool from the iterator (bson_iter_t) of bson_t
Definition: db_mongoc.cpp:535
time_t GetDatetimeFromBsonIterator(bson_iter_t *iter)
Get Datetime from the iterator (bson_iter_t) of bson_t
Definition: db_mongoc.cpp:563
string GetStringFromBson(bson_t *bmeta, const char *key)
Get String from bson_t
Definition: db_mongoc.cpp:526
bool GetNumericFromBsonIterator(bson_iter_t *iter, T &numericvalue)
Get numeric value from the iterator (bson_iter_t) of bson_taccording to a given key.
Definition: db_mongoc.h:192
time_t GetDatetimeFromBson(bson_t *bmeta, const char *key)
Get Datetime from bson_t
Definition: db_mongoc.cpp:582
bool GetBoolFromBson(bson_t *bmeta, const char *key)
Get String from bson_t
Definition: db_mongoc.cpp:554
void GetVectorFromBsonIter(bson_iter_t *iter, vector< T > &out)
parse the following nested vector from bson_t: [1, 2, 3]
Definition: db_mongoc.h:220
Common Cross-platform Geographic Library (CCGL)
Definition: basic.cpp:17
std::map< string, string > STRING_MAP
Map of string key and string value.
Definition: basic.h:349
void StatusMessage(const char *msg)
Print status messages for Debug.
Definition: basic.cpp:136