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());
226 nlohmann::json json = nlohmann::json::parse(bsoncxx::to_json(*res));
229 if (instances.size() != e.
size())
232 "The size of the mongodb entity entry at id " +
234 " has wrong size. Expected: " + std::to_string(e.
size()) +
235 " but got: " + std::to_string(instances.size()));
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>();
333 auto doc_value = bsoncxx::from_json(j.dump(2));
334 array_builder.append(doc_value);
337 auto after_array = in_array << array_builder;
338 bsoncxx::document::value doc = after_array
339 << bsoncxx::builder::stream::finalize;
340 coll.insert_one(doc.view());
MemoryID getEntitySnapshotID() const
std::string coreSegmentName
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
MemoryID withInstanceIndex(int index) const
std::string providerSegmentName
MemoryID getEntityID() const
EntitySnapshotT & addSnapshot(const Time ×tamp)
Add a snapshot at the given time.
bool forEachSnapshot(SnapshotFunctionT &&func)
const DataT & data() const
bool forEachCoreSegment(CoreSegmentFunctionT &&func)
bool hasCoreSegment(const std::string &name) const
CoreSegmentT & addCoreSegment(const std::string &name, aron::type::ObjectPtr coreSegmentType=nullptr, const std::vector< PredictionEngine > &predictionEngines={})
Add an empty core segment with the given name, type and prediction engines.
bool hasEntity(const std::string &name) const
bool forEachEntity(EntityFunctionT &&func)
EntityT & addEntity(const std::string &name)
Add an empty entity with the given name.
Base class for all exceptions thrown by the armem library.
Indicates that a memory ID is invalid, e.g.
Client-side working memory core segment.
Client-side working memory entity snapshot.
Client-side working memory entity.
Client-side working memory.
Client-side working memory provider segment.
static data::DictPtr ConvertFromNlohmannJSONObject(const nlohmann::json &, const armarx::aron::Path &p={})
static nlohmann::json ConvertToNlohmannJSON(const data::VariantPtr &)
static DateTime microSeconds(long microSeconds)
std::int64_t toMicroSeconds() const
const std::string INSTANCES
const std::string TIMESTAMP
const std::string FOREIGN_KEY
void store(const mongocxx::database &db, const armem::wm::Memory &m)
void load(const mongocxx::database &db, armem::wm::Memory &m)
void convert(const mongocxx::database &db, armem::wm::Memory &m)
armem::wm::EntityInstance EntityInstance
void from_aron(const aron::data::DictPtr &metadata, const aron::data::DictPtr &data, wm::EntityInstance &)
convert from metadata and data aron instance
void to_aron(aron::data::DictPtr &metadata, aron::data::DictPtr &data, const wm::EntityInstance &)
convert to metadata and aron instance
bool forEachInstance(InstanceFunctionT &&func)
auto & getInstance(const MemoryID &instanceID)
Retrieve an entity instance.