From 4671f4a46bf9aca39f0a09d0bec115a7f8913614 Mon Sep 17 00:00:00 2001 From: neodarz Date: Thu, 8 Aug 2019 13:48:39 +0200 Subject: Implement mongocxx pool connection --- CMakeLists.txt | 2 +- api/umosapi.cpp | 9 +++++++-- db/mongo_access.cpp | 16 ++++++++++++++++ db/mongo_access.h | 43 +++++++++++++++++++++++++++++++++++++++++++ shared.h | 2 ++ 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 db/mongo_access.cpp create mode 100644 db/mongo_access.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 964ead7..56af362 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,7 @@ or endif(NOT JSONC_FOUND) -add_executable(umosapi main.cpp config.cpp api/umosapi.cpp api/umosapi.h config.h shared.h) +add_executable(umosapi main.cpp config.cpp api/umosapi.cpp db/mongo_access.cpp api/umosapi.h config.h db/mongo_access.h shared.h) set_property(TARGET umosapi PROPERTY CXX_STANDARD 17) diff --git a/api/umosapi.cpp b/api/umosapi.cpp index 65f28d2..de2a2c3 100644 --- a/api/umosapi.cpp +++ b/api/umosapi.cpp @@ -14,6 +14,9 @@ #include "umosapi.h" #include "../shared.h" +#include "../db/mongo_access.h" + +mongo_access mongo; using bsoncxx::builder::stream::close_array; @@ -40,6 +43,8 @@ UmosapiService::UmosapiService(Address addr) { } void UmosapiService::init(size_t thr = 2) { + auto uri = mongocxx::uri{config["mongoURI"]}; + mongo.configure(std::move(uri)); auto opts = Http::Endpoint::options() .threads(thr); httpEndpoint->init(opts); @@ -101,9 +106,9 @@ void UmosapiService::createDescription() { } void UmosapiService::retrieveAll(const Rest::Request& request, Http::ResponseWriter response) { - mongocxx::client conn{mongocxx::uri{config["mongoURI"]}}; + auto conn = mongo.get_connection(); - auto collection = conn[config["mongo_db"]][request.param(":mcollection").as()]; + auto collection = (*conn)[config["mongo_db"]][request.param(":mcollection").as()]; auto cursor = collection.find({}); diff --git a/db/mongo_access.cpp b/db/mongo_access.cpp new file mode 100644 index 0000000..56936f3 --- /dev/null +++ b/db/mongo_access.cpp @@ -0,0 +1,16 @@ +#include "mongo_access.h" + +mongo_access::mongo_access(void) {}; +mongo_access::~mongo_access(void) {}; + +void mongo_access::configure(mongocxx::uri uri) { + class noop_logger : public mongocxx::logger { + public: + virtual void operator()(mongocxx::log_level, + bsoncxx::stdx::string_view, + bsoncxx::stdx::string_view) noexcept {} + }; + + _pool = bsoncxx::stdx::make_unique(std::move(uri)); + _instance = bsoncxx::stdx::make_unique(bsoncxx::stdx::make_unique()); +} diff --git a/db/mongo_access.h b/db/mongo_access.h new file mode 100644 index 0000000..f03cece --- /dev/null +++ b/db/mongo_access.h @@ -0,0 +1,43 @@ +#ifndef MongoAccess_H_ +#define MongoAccess_H_ + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include + + +class mongo_access { + public: + + mongo_access (); + ~mongo_access (); + + void configure(mongocxx::uri uri); + + using connection = mongocxx::pool::entry; + + connection get_connection() { + return _pool->acquire(); + } + + bsoncxx::stdx::optional try_get_connection() { + return _pool->try_acquire(); + } + + private: + std::unique_ptr _instance = nullptr; + std::unique_ptr _pool = nullptr; + +}; + +#endif diff --git a/shared.h b/shared.h index 0814b6c..7ba7006 100644 --- a/shared.h +++ b/shared.h @@ -2,7 +2,9 @@ #define SHARED_H_ #include +#include "db/mongo_access.h" extern std::map config; +extern mongo_access mongo; #endif -- cgit v1.2.1