4 #include <SimoxUtility/json.h>
6 #include "../operations.h"
12 namespace bsoncxxbuilder = bsoncxx::builder::stream;
13 namespace bsoncxxdoc = bsoncxx::document;
17 void mongodbInsertForeignKey(mongocxx::collection& coll,
const std::string& key)
20 coll.insert_one(
q.view());
23 bool mongodbContainsForeignKey(mongocxx::collection& coll,
const std::string& key)
27 auto res = coll.find_one(
q.view());
31 bool mongodbContainsTimestamp(mongocxx::collection& coll,
const long ts)
34 auto q = bsoncxxbuilder::document{} << std::string(
constantes::TIMESTAMP) << ts << bsoncxxbuilder::finalize;
35 auto res = coll.find_one(
q.view());
42 if (!db.has_collection(m.
id().
str()))
46 mongocxx::collection coll = db.collection(m.
id().
str());
47 mongocxx::cursor cursor = coll.find({});
48 for (
const auto& doc : cursor)
51 auto foreignKey = el.get_utf8().value;
53 MemoryID i((std::string) foreignKey);
62 throw error::ArMemError(
"Somehow the container already contains the key k = " + k +
". Do you have double entries in mongodb?");
74 if (!db.has_collection(
c.id().str()))
78 mongocxx::collection coll = db.collection(
c.id().str());
79 mongocxx::cursor cursor = coll.find({});
80 for (
const auto& doc : cursor)
83 auto foreignKey = el.get_utf8().value;
85 MemoryID i((std::string) foreignKey);
93 if (
c.hasProviderSegment(k))
95 throw error::ArMemError(
"Somehow the container already contains the key k = " + k +
". Do you have double entries in mongodb?");
99 auto& pSeg =
c.addProviderSegment(k);
107 if (!db.has_collection(p.
id().
str()))
111 mongocxx::collection coll = db.collection(p.
id().
str());
112 mongocxx::cursor cursor = coll.find({});
113 for (
const auto& doc : cursor)
116 auto foreignKey = el.get_utf8().value;
118 MemoryID i((std::string) foreignKey);
129 throw error::ArMemError(
"Somehow the container already contains the key k = " + k +
". Do you have double entries in mongodb?");
141 if (!db.has_collection(e.
id().
str()))
145 mongocxx::collection coll = db.collection(e.
id().
str());
146 mongocxx::cursor cursor = coll.find({});
147 for (
const auto& doc : cursor)
153 unsigned long length =
std::distance(i.get_array().value.begin(), i.get_array().value.end());
155 for (
unsigned long i = 0; i < length; ++i)
158 newSnapshot.addInstance({});
194 if (!ltm::util::entityHasData(e))
199 auto res = eColl.find_one(
q.view());
212 if (instances.size() != e.
size())
217 for (
unsigned int i = 0; i < e.
size(); ++i)
222 nlohmann::json doc = instances[i];
230 ins.data() = tmp.
data();
239 auto coll = db.collection(m.
id().
str());
242 if (!mongodbContainsForeignKey(coll, e.
id().
str()))
244 mongodbInsertForeignKey(coll, e.id().str());
253 auto coll = db.collection(
c.id().str());
256 if (!mongodbContainsForeignKey(coll, e.
id().
str()))
258 mongodbInsertForeignKey(coll, e.id().str());
267 auto coll = db.collection(p.
id().
str());
270 if (!mongodbContainsForeignKey(coll, e.
id().
str()))
272 mongodbInsertForeignKey(coll, e.id().str());
281 auto coll = db.collection(e.
id().
str());
284 if (!mongodbContainsTimestamp(coll, e.
id().
timestamp.toMilliSeconds()))
287 bsoncxxbuilder::document builder{};
288 auto in_array = builder
292 auto array_builder = bsoncxx::builder::basic::array{};
296 auto aron = std::make_shared<aron::data::Dict>();
298 nlohmann::json j = aron::converter::AronNlohmannJSONConverter::ConvertToNlohmannJSON(aron);
300 auto doc_value = bsoncxx::from_json(j.dump(2));
301 array_builder.append(doc_value);
304 auto after_array = in_array << array_builder;
306 coll.insert_one(doc.view());