9 std::mutex ConnectionManager::initializationMutex;
10 bool ConnectionManager::initialized =
false;
11 std::map<std::string, std::unique_ptr<mongocxx::pool>> ConnectionManager::Connections = {};
15 std::string armarx_home = std::string(getenv(
"HOME")) +
"/.armarx";
16 if (getenv(
"ARMARX_DEFAULTS_DIR"))
18 armarx_home = getenv(
"ARMARX_DEFAULTS_DIR");
20 std::ifstream cFile (armarx_home +
"/default.cfg");
24 while(getline(cFile, line))
26 line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());
27 if(line[0] ==
'#' || line.empty())
31 auto delimiterPos = line.find(
"=");
32 const auto name = line.substr(0, delimiterPos);
33 const auto value = line.substr(delimiterPos + 1);
34 if (name ==
"ArmarX.MongoHost")
38 if (name ==
"ArmarX.MongoPort")
42 if (name ==
"ArmarX.MongoUser")
46 if (name ==
"ArmarX.MongoPassword")
57 return !host.empty() and port != 0;
68 if (!password.empty())
70 ss <<
":" << password;
91 return uri() +
"&database=" + database;
94 void ConnectionManager::initialize_if()
96 std::lock_guard l(initializationMutex);
100 mongocxx::instance instance{};
108 const auto uri_str = settings.
uri();
109 auto it = Connections.find(uri_str);
110 if (it == Connections.end())
112 mongocxx::uri uri(uri_str);
113 auto pool = std::make_unique<mongocxx::pool>(uri);
114 auto con = Connections.emplace(settings.
key(), std::move(pool));
115 return *con.first->second;
130 if (!forceNewConnection)
132 auto it = Connections.find(settings.
key());
133 if (it != Connections.end())
135 auto client = it->second->acquire();
136 auto admin = client->database(
"admin");
137 auto result = admin.run_command(bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp(
"isMaster", 1)));
142 mongocxx::uri uri(settings.
uri());
143 auto client = mongocxx::client(uri);
144 auto admin = client[
"admin"];
145 auto result = admin.run_command(bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp(
"isMaster", 1)));
148 catch (
const std::exception& xcp)
158 auto it = Connections.find(settings.
key());
159 return it != Connections.end();