3 #include <SimoxUtility/algorithm/string.h>
13 std::unique_ptr<mongocxx::instance> MongoDBStorageMixin::MongoCXXInstance =
nullptr;
14 std::atomic_bool MongoDBStorageMixin::MongoCXXInitialized =
false;
15 std::recursive_mutex MongoDBStorageMixin::MongoCXXConnectionPoolMutex;
16 std::map<std::string, std::unique_ptr<mongocxx::pool>>
17 MongoDBStorageMixin::MongoCXXConnectionPool = {};
22 std::filesystem::path armarx_config_home = std::string(getenv(
"ARMARX_USER_CONFIG_DIR"));
25 ARMARX_WARNING << (
"Could not find an ArmarX Config folder. Tried: '" +
26 armarx_config_home.string() +
27 "' (from ARMARX_USER_CONFIG_DIR). If not set via scenario params "
28 "this means that LTM is disabled.");
32 std::ifstream cFile(armarx_config_home /
"default.cfg");
36 while (getline(cFile, line))
38 line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());
39 if (line.empty() || line[0] ==
'#')
43 auto delimiterPos = line.find(
"=");
44 const auto name = simox::alg::to_lower(line.substr(0, delimiterPos));
45 const auto value = line.substr(delimiterPos + 1);
46 if (
host.empty() && name ==
"armarx.mongohost")
50 if (
port == 0 && name ==
"armarx.mongoport")
54 if (
user.empty() && name ==
"armarx.mongouser")
58 if (
password.empty() && name ==
"armarx.mongopassword")
114 const std::string& en,
116 settings(settings), exportName(en), _id(id)
147 std::lock_guard l(MongoCXXConnectionPoolMutex);
148 if (!MongoCXXInitialized.exchange(
true))
152 std::make_unique<mongocxx::instance>();
155 const auto key = settings.
key();
156 const auto uri = settings.
uri();
157 auto it = MongoCXXConnectionPool.find(key);
158 if (it == MongoCXXConnectionPool.end())
160 ARMARX_INFO <<
"Establishing new connection to: " << uri <<
" from id: " << _id.
str();
161 mongocxx::uri u(uri);
162 auto pool = std::make_unique<mongocxx::pool>(u);
163 MongoCXXConnectionPool.emplace(settings.
key(), std::move(pool));
168 MongoDBStorageMixin::getPool(
bool tryToConnect)
const
170 std::lock_guard l(MongoCXXConnectionPoolMutex);
171 const auto key = settings.
key();
172 auto it = MongoCXXConnectionPool.find(key);
173 if (it == MongoCXXConnectionPool.end())
179 return getPool(
false);
185 return it->second.get();
190 MongoDBStorageMixin::ensurePool()
const
192 std::lock_guard l(MongoCXXConnectionPoolMutex);
193 auto pool = getPool();
200 throw armarx::LocalException(
"Pool could not be ensured...");
219 std::lock_guard l(MongoCXXConnectionPoolMutex);
223 auto client = ensurePool()->acquire();
224 auto admin = client->database(
"admin");
225 auto result = admin.run_command(bsoncxx::builder::basic::make_document(
226 bsoncxx::builder::basic::kvp(
"isMaster", 1)));
229 catch (
const std::exception& xcp)
261 std::optional<mongocxx::database>
264 auto client = this->ensurePool()->acquire();
268 std::optional<mongocxx::collection>
280 std::optional<mongocxx::collection>
292 std::optional<nlohmann::json>
298 std::optional<nlohmann::json>
307 nlohmann::json query;
315 auto client = this->ensurePool()->acquire();
345 nlohmann::json query;
355 nlohmann::json query;
368 nlohmann::json query;
369 query[
ID] = _id.
str();
381 nlohmann::json query;
382 query[
ID] = _id.
str();
408 nlohmann::json::parse(std::string(
"{\"") +
ID +
"\": " +
getDocumentName() +
"}");
424 std::vector<nlohmann::json>