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")
52 port = (
unsigned int)std::stoi(value);
54 if (
user.empty() && name ==
"armarx.mongouser")
58 if (
password.empty() && name ==
"armarx.mongopassword")
96 return "mongodb://" +
host +
":" + std::to_string(
port);
114 const std::string& en,
116 settings(settings), exportName(en), _id(id)
141 settings.password = n;
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)
251 ARMARX_CHECK(!_id.memoryName.empty() and !_id.coreSegmentName.empty());
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;
358 nlohmann::json update;
368 nlohmann::json
query;
371 nlohmann::json update;
381 nlohmann::json
query;
384 nlohmann::json update;
408 nlohmann::json::parse(std::string(
"{\"") +
ID +
"\": " +
getDocumentName() +
"}");
424 std::vector<nlohmann::json>
#define ARMARX_CHECK_NOT_EMPTY(c)
std::string toString() const
void initializeFromArmarXConfig()
Fills missing fields from armarx config file.
std::string baseUri() const
void setPassword(const std::string &)
void writeForeignKeyToPreviousDocument()
std::vector< nlohmann::json > getAllDocuments() const
nlohmann::json readDataFromDocument() const
MongoDBSettings getSettings() const
std::string getDatabaseName() const
MongoDBStorageMixin()=default
static const constexpr char * ID
void configureMixin(const nlohmann::json &json)
configuration
void setMixinMemoryID(const armem::MemoryID &)
setter
std::optional< mongocxx::database > databaseExists() const
mongocxx::collection ensureCollectionExists(bool createIfNotExistent=false)
nlohmann::json ensureDocumentExists(bool createIfNotExistent=false)
static const constexpr char * FOREIGN_KEY
std::string getPreviousCollectionName() const
mongocxx::database ensureDatabaseExists(bool createIfNotExistent=false)
void writeDataToDocument(const nlohmann::json &data)
static const constexpr char * DATA
std::optional< nlohmann::json > documentExists() const
std::optional< mongocxx::collection > collectionExists() const
void setUser(const std::string &)
static const constexpr char * TYPE
mongocxx::collection ensurePreviousCollectionExists(bool createIfNotExistent=false)
void setPort(const unsigned int)
std::string getCollectionName() const
std::optional< mongocxx::collection > previousCollectionExists() const
std::string getDocumentName() const
void setMixinExportName(const std::string &n)
void setHost(const std::string &)
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
This file is part of ArmarX.
bool directoryExists(const std::filesystem::path &p)
std::optional< mongocxx::database > databaseExists(mongocxx::client &client, const std::string &databaseName)
mongocxx::collection ensureCollectionExists(mongocxx::database &db, const std::string &collectionName, bool createIfNotExistent)
std::optional< nlohmann::json > documentExists(mongocxx::collection &collection, const nlohmann::json &json)
std::vector< nlohmann::json > getAllDocuments(mongocxx::collection &collection)
void writeDataToDocument(mongocxx::collection &collection, const nlohmann::json &query, const nlohmann::json &update)
nlohmann::json ensureDocumentExists(mongocxx::collection &collection, const nlohmann::json &json, bool createIfNotExistent)
mongocxx::database ensureDatabaseExists(mongocxx::client &client, const std::string &databaseName, bool createIfNotExistent)
std::optional< mongocxx::collection > collectionExists(mongocxx::database &db, const std::string &collectionName)
nlohmann::json readDataFromDocument(mongocxx::collection &collection, const nlohmann::json &json)
std::string toCollectionName(const armem::MemoryID &id)
std::string toDocumentID(const armem::MemoryID &id)