4 #include <SimoxUtility/json.h>
9 #include "../operations.h"
13 namespace bsoncxxbuilder = bsoncxx::builder::stream;
14 namespace bsoncxxdoc = bsoncxx::document;
19 mongodbInsertForeignKey(mongocxx::collection& coll,
const std::string& key)
22 << bsoncxxbuilder::finalize;
23 coll.insert_one(
q.view());
27 mongodbContainsForeignKey(mongocxx::collection& coll,
const std::string& key)
31 << bsoncxxbuilder::finalize;
32 auto res = coll.find_one(
q.view());
37 mongodbContainsTimestamp(mongocxx::collection& coll,
const long ts)
41 << bsoncxxbuilder::finalize;
42 auto res = coll.find_one(
q.view());
50 if (!db.has_collection(m.
id().
str()))
54 mongocxx::collection coll = db.collection(m.
id().
str());
55 mongocxx::cursor cursor = coll.find({});
56 for (
const auto& doc : cursor)
59 auto foreignKey = el.get_utf8().value;
66 "A MemoryID in mongodb was invalid. Found the wrong memory name: " + i.
str());
72 throw error::ArMemError(
"Somehow the container already contains the key k = " + k +
73 ". Do you have double entries in mongodb?");
86 if (!db.has_collection(
c.id().str()))
90 mongocxx::collection coll = db.collection(
c.id().str());
91 mongocxx::cursor cursor = coll.find({});
92 for (
const auto& doc : cursor)
95 auto foreignKey = el.get_utf8().value;
102 "A MemoryID in mongodb was invalid. Found the wrong memory name: " + i.
str());
106 if (
c.hasProviderSegment(k))
108 throw error::ArMemError(
"Somehow the container already contains the key k = " + k +
109 ". Do you have double entries in mongodb?");
113 auto& pSeg =
c.addProviderSegment(k);
122 if (!db.has_collection(p.
id().
str()))
126 mongocxx::collection coll = db.collection(p.
id().
str());
127 mongocxx::cursor cursor = coll.find({});
128 for (
const auto& doc : cursor)
131 auto foreignKey = el.get_utf8().value;
133 MemoryID i((std::string)foreignKey);
139 "A MemoryID in mongodb was invalid. Found the wrong memory name: " + i.
str());
145 throw error::ArMemError(
"Somehow the container already contains the key k = " + k +
146 ". Do you have double entries in mongodb?");
159 if (!db.has_collection(e.
id().
str()))
163 mongocxx::collection coll = db.collection(e.
id().
str());
164 mongocxx::cursor cursor = coll.find({});
165 for (
const auto& doc : cursor)
171 unsigned long length =
172 std::distance(i.get_array().value.begin(), i.get_array().value.end());
174 for (
unsigned long i = 0; i < length; ++i)
177 newSnapshot.addInstance({});
209 if (!ltm::util::entityHasData(e))
215 << bsoncxxbuilder::finalize;
216 auto res = eColl.find_one(
q.view());
229 if (instances.size() != e.
size())
232 "The size of the mongodb entity entry at id " +
238 for (
unsigned int i = 0; i < e.
size(); ++i)
243 nlohmann::json doc = instances[i];
252 ins.data() = tmp.
data();
262 auto coll = db.collection(m.
id().
str());
266 if (!mongodbContainsForeignKey(coll, e.
id().
str()))
268 mongodbInsertForeignKey(coll, e.id().str());
278 auto coll = db.collection(
c.id().str());
279 c.forEachProviderSegment(
282 if (!mongodbContainsForeignKey(coll, e.
id().
str()))
284 mongodbInsertForeignKey(coll, e.id().str());
294 auto coll = db.collection(p.
id().
str());
298 if (!mongodbContainsForeignKey(coll, e.
id().
str()))
300 mongodbInsertForeignKey(coll, e.id().str());
310 auto coll = db.collection(e.
id().
str());
314 if (!mongodbContainsTimestamp(coll, e.
id().
timestamp.toMilliSeconds()))
317 bsoncxxbuilder::document builder{};
322 auto array_builder = bsoncxx::builder::basic::array{};
327 auto aron = std::make_shared<aron::data::Dict>();
330 aron::converter::AronNlohmannJSONConverter::ConvertToNlohmannJSON(
333 auto doc_value = bsoncxx::from_json(j.dump(2));
334 array_builder.append(doc_value);
337 auto after_array = in_array << array_builder;
339 << bsoncxx::builder::stream::finalize;
340 coll.insert_one(doc.view());