Skip to content
Snippets Groups Projects

Draft: Feature/mongo db

Open Joana Plewnia requested to merge feature/MongoDB into master
1 unresolved thread
Compare and
11 files
+ 264
151
Compare changes
  • Side-by-side
  • Inline
Files
11
@@ -39,6 +39,34 @@ namespace armarx::armem::server::ltm::util::mongodb
return ret;
}
//Only for MongoCXX Version < 3.5.0 required.
std::vector <std::string>
list_database_names(mongocxx::client &client) {
std::vector <std::string> database_names;
auto dbs = client.list_databases();
for (auto &&view: dbs) {
std::string name = std::string(view["name"].get_utf8().value);
database_names.push_back(name);
}
return database_names;
}
//Only for MongoCXX Version < 3.4.0 required.
std::vector <std::string>
list_collection_names(mongocxx::database &db) {
std::vector <std::string> collection_names;
auto dbs = db.list_collections();;
for (auto &&view: dbs) {
std::string name = std::string(view["name"].get_utf8().value);
collection_names.push_back(name);
}
return collection_names;
}
bool
insert(mongocxx::collection& coll, const nlohmann::json& value)
{
@@ -79,16 +107,21 @@ namespace armarx::armem::server::ltm::util::mongodb
}
} // namespace detail
std::optional<mongocxx::database>
databaseExists(mongocxx::client& client, const std::string& databaseName)
{
//auto names = client.list_databases();
//if (auto it = std::find(names.begin(), names.end(), databaseName); it != names.end())
//{
// return client[databaseName];
//}
return std::nullopt;
std::optional<mongocxx::database>
databaseExists(mongocxx::client& client, const std::string& databaseName) {
// MongoCXX Version > 3.5.0 required
//auto names = client.list_database_names();
auto names = detail::list_database_names(client);
if (auto it = std::find(names.begin(), names.end(), databaseName); it != names.end()) {
//Workaround to create an empty Database client[databaseName] does not create a database if nothing is added
//db = client[databaseName];
//db.create_collection("empty");
return client[databaseName];
}
return std::nullopt;
}
mongocxx::database
ensureDatabaseExists(mongocxx::client& client,
@@ -104,6 +137,9 @@ namespace armarx::armem::server::ltm::util::mongodb
databaseName);
}
}
//Workaround to create an empty Database client[databaseName] does not create a database if nothing is added
//db = client[databaseName];
//db.create_collection("empty");
return client[databaseName];
}
@@ -112,7 +148,7 @@ namespace armarx::armem::server::ltm::util::mongodb
{
if (db.has_collection(collectionName))
{
return db[collectionName];
return db.create_collection(collectionName);
}
return std::nullopt;
}
@@ -131,7 +167,8 @@ namespace armarx::armem::server::ltm::util::mongodb
collectionName);
}
}
return db[collectionName];
//Workaround to create an empty Collection db[collectionName] does not create a Collection if no document is added
return db.create_collection(collectionName);
}
std::string
@@ -171,6 +208,35 @@ namespace armarx::armem::server::ltm::util::mongodb
return ss.str();
}
std::vector<std::string>
getDirectSuccessors(const std::vector<std::string>& collection_names, const std::string& prefix) {
std::vector<std::string> direct_successors;
for (const auto& coll_name : collection_names) {
auto segments = split(coll_name, '.');
if (segments.size() > collectionNameDepth) {
std::string current_prefix;
for (int i = 0; i < depth; ++i) {
if (i > 0) {
current_prefix += ".";
}
current_prefix += segments[i];
}
if (current_prefix == prefix) {
direct_successors.push_back(segments[depth]);
}
}
}
// Removing duplicates
std::set<std::string> unique_successors(direct_successors.begin(), direct_successors.end());
direct_successors.assign(unique_successors.begin(), unique_successors.end());
return direct_successors;
}
std::vector<std::string>
getSubCollections(mongocxx::database& db, const armem::MemoryID& id) {
return getDirectSuccessors(detail::list_collection_names(db), toCollectionName(id))
}
std::optional<nlohmann::json>
documentExists(mongocxx::collection& collection, const nlohmann::json& json)
{
Loading