4#include <SimoxUtility/algorithm/string/string_tools.h>
5#include <SimoxUtility/json.h>
13 namespace bbuilder = bsoncxx::builder;
14 namespace bdoc = bsoncxx::document;
21 std::string ret = segmentName;
25 ret = simox::alg::replace_all(ret, s, r);
33 std::string ret = escapedName;
38 ret = simox::alg::replace_all(ret, r, s);
44 insert(mongocxx::collection& coll,
const nlohmann::json& value)
46 std::string v = value.dump();
47 auto q = bsoncxx::from_json(v);
48 auto res = coll.insert_one(
q.view());
53 std::optional<nlohmann::json>
54 contains(mongocxx::collection& coll,
const nlohmann::json& value)
57 std::string v = value.dump();
58 auto q = bsoncxx::from_json(v);
59 auto res = coll.find_one(
q.view());
62 return nlohmann::json::parse(bsoncxx::to_json(*res));
69 const nlohmann::json&
query,
70 const nlohmann::json&
update)
73 auto q = bsoncxx::from_json(
query.dump());
74 auto udoc = bsoncxx::from_json(
update.dump());
75 auto u = bbuilder::basic::make_document(bbuilder::basic::kvp(
"$set", udoc));
77 auto res = coll.update_one(
q.view(), u.view());
83 std::optional<mongocxx::database>
96 const std::string& databaseName,
97 bool createIfNotExistent)
102 if (!createIfNotExistent)
104 throw armarx::LocalException(
"Database existence can not be ensured: " +
108 return client[databaseName];
111 std::optional<mongocxx::collection>
114 if (db.has_collection(collectionName))
116 return db[collectionName];
123 const std::string& collectionName,
124 bool createIfNotExistent)
129 if (!createIfNotExistent)
131 throw armarx::LocalException(
"Collection existence can not be ensured: " +
135 return db[collectionName];
141 if (
id.hasTimestamp())
143 return id.timestampStr();
147 throw armarx::LocalException(
"Called toDocumentName() on non-snapshot id: " +
id.
str());
154 std::stringstream ss;
155 if (
id.hasMemoryName())
159 if (
id.hasCoreSegmentName())
163 if (
id.hasProviderSegmentName())
167 if (
id.hasEntityName())
175 std::optional<nlohmann::json>
183 const nlohmann::json& json,
184 bool createIfNotExistent)
189 if (!createIfNotExistent)
191 throw armarx::LocalException(
"Document existence can not be ensured: " +
204 throw armarx::LocalException(
205 "Even after creating the docuemnt it wasnt found.. error: " + json.dump());
215 ARMARX_CHECK((
bool)doc) <<
" Could not find document matching: " << json.dump()
216 <<
" in collection " << collection.name();
223 const nlohmann::json&
query,
224 const nlohmann::json& update)
239 std::vector<nlohmann::json>
242 std::vector<nlohmann::json> ret;
243 mongocxx::cursor cursor = collection.find({});
244 for (
const auto& doc : cursor)
247 nlohmann::json::parse(bsoncxx::to_json(doc, bsoncxx::ExtendedJsonMode::k_relaxed)));
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
This file is part of ArmarX.
std::string unescapeName(const std::string &escapedName)
std::string escapeName(const std::string &segmentName)
std::optional< nlohmann::json > contains(mongocxx::collection &coll, const nlohmann::json &value)
bool insert(mongocxx::collection &coll, const nlohmann::json &value)
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
const std::map< std::string, std::string > EscapeTable
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)