ConnectionManager.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <mutex>
5 #include <map>
6 #include <memory>
7 #include <sstream>
8 
9 #include <bsoncxx/json.hpp>
10 #include <mongocxx/client.hpp>
11 #include <mongocxx/pool.hpp>
12 #include <mongocxx/stdx.hpp>
13 #include <mongocxx/uri.hpp>
14 #include <mongocxx/instance.hpp>
15 #include <bsoncxx/builder/stream/helpers.hpp>
16 #include <bsoncxx/builder/stream/document.hpp>
17 #include <bsoncxx/builder/stream/array.hpp>
18 
19 
21 {
22 
23  using PoolClientPtr = mongocxx::pool::entry;
24 
25  /**
26  * @brief A manager of multiple mongodb connection
27  */
29  {
30  public:
31  ConnectionManager() = delete;
32 
34  {
35  std::string host = "";
36  unsigned int port = 0;
37  std::string user = "";
38  std::string password = "";
39  std::string database = "Test";
40  int minPoolSize = 5;
41  int maxPoolSize = 100;
42 
44 
45  bool isSet() const;
46 
47  std::string baseUri() const;
48 
49  std::string key() const;
50 
51  std::string uri() const;
52 
53  std::string toString() const;
54  };
55 
56  static mongocxx::pool& Connect(const MongoDBSettings& settings);
57  static bool ConnectionIsValid(const MongoDBSettings& settings, bool forceNewConnection = false);
58  static bool ConnectionExists(const MongoDBSettings& settings);
59 
60  private:
61  static void initialize_if();
62 
63 
64  private:
65  static std::mutex initializationMutex;
66  static bool initialized;
67  static std::map<std::string, std::unique_ptr<mongocxx::pool>> Connections;
68 
69  };
70 }
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::key
std::string key() const
Definition: ConnectionManager.cpp:78
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::isSet
bool isSet() const
Definition: ConnectionManager.cpp:54
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings
Definition: ConnectionManager.h:33
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::port
unsigned int port
Definition: ConnectionManager.h:36
armarx::armem::server::ltm::mongodb::ConnectionManager::ConnectionExists
static bool ConnectionExists(const MongoDBSettings &settings)
Definition: ConnectionManager.cpp:154
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::database
std::string database
Definition: ConnectionManager.h:39
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::uri
std::string uri() const
Definition: ConnectionManager.cpp:84
armarx::armem::server::ltm::mongodb::ConnectionManager::Connect
static mongocxx::pool & Connect(const MongoDBSettings &settings)
Definition: ConnectionManager.cpp:104
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::user
std::string user
Definition: ConnectionManager.h:37
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::toString
std::string toString() const
Definition: ConnectionManager.cpp:89
armarx::armem::server::ltm::mongodb
Definition: ConnectionManager.cpp:7
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::baseUri
std::string baseUri() const
Definition: ConnectionManager.cpp:60
armarx::armem::server::ltm::mongodb::ConnectionManager::ConnectionManager
ConnectionManager()=delete
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::maxPoolSize
int maxPoolSize
Definition: ConnectionManager.h:41
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::MongoDBSettings
MongoDBSettings()
Definition: ConnectionManager.cpp:13
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::host
std::string host
Definition: ConnectionManager.h:35
armarx::armem::server::ltm::mongodb::PoolClientPtr
mongocxx::pool::entry PoolClientPtr
Definition: ConnectionManager.h:23
armarx::armem::server::ltm::mongodb::ConnectionManager::ConnectionIsValid
static bool ConnectionIsValid(const MongoDBSettings &settings, bool forceNewConnection=false)
Definition: ConnectionManager.cpp:124
armarx::armem::server::ltm::mongodb::ConnectionManager
A manager of multiple mongodb connection.
Definition: ConnectionManager.h:28
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::minPoolSize
int minPoolSize
Definition: ConnectionManager.h:40
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::password
std::string password
Definition: ConnectionManager.h:38