ProviderSegment.cpp
Go to the documentation of this file.
1// Header
2#include "ProviderSegment.h"
3
4#include <memory>
5
6// ArmarX
9
12
14{
16 const std::string& exportName,
17 const armem::MemoryID& memoryId /* UNESCAPED */,
18 const std::shared_ptr<Processors>& filters,
19 const std::shared_ptr<persistence::MemoryPersistenceStrategy>& persistenceStrategy) :
20 ProviderSegmentBase(exportName, memoryId, filters),
21 persistenceStrategy_(persistenceStrategy)
22 {
23 //start();
24 }
25
26 bool
27 ProviderSegment::_implForEachEntity(std::function<void(Entity&)> func) const
28 {
30
31 ARMARX_DEBUG << "For each entity (provider segment id=" << id().cleanID().str();
32
33 for (std::string& entityName : persistenceStrategy_->getContainerKeys(id()))
34 {
35 ARMARX_DEBUG << "Found entity=" << entityName;
36
37 std::shared_ptr<persistence::MemoryPersistenceStrategy> entityPersistenceStrategy(
38 persistenceStrategy_);
39
40 Entity ltmEntity(getExportName(),
41 id().withEntityName(entityName),
43 entityPersistenceStrategy);
44
45 func(ltmEntity);
46 }
47
48 return true;
49 }
50
51 bool
52 ProviderSegment::_implHasEntity(const std::string& entityName) const
53 {
55
56 return persistenceStrategy_->containsContainer(id(), entityName);
57 }
58
59 std::shared_ptr<Entity>
60 ProviderSegment::_implFindEntity(const std::string& entityName) const
61 {
63
64 if (!_implHasEntity(entityName)) // Call _impl version
65 {
66 return nullptr;
67 }
68
69 return std::make_shared<Entity>(
70 getExportName(), id().withEntityName(entityName), processors, persistenceStrategy_);
71 }
72
73 void
75 {
77
78 wmProviderSegment.id() = id().getProviderSegmentID().cleanID();
79
80 auto& conv = processors->defaultTypeConverter;
81 if (persistenceStrategy_->containsItem(
83 {
84 // load and set type
85 auto& conv = processors->defaultTypeConverter;
86 try
87 {
88 auto filecontent = persistenceStrategy_->retrieveItem(
90 auto aron = conv.convert(filecontent, "");
91 wmProviderSegment.aronType() = aron;
92 }
93 catch (const nlohmann::json::type_error& e)
94 {
95 ARMARX_WARNING << "You are trying to access aron data in a ProviderSegment which "
96 "is not available, skipping...";
97 wmProviderSegment.aronType() = nullptr;
98 }
99 }
100
101 _implForEachEntity( // Call _impl version
102 [&wmProviderSegment](auto& ltmEntity)
103 {
104 armem::wm::Entity wmEntity;
105 ltmEntity.loadAllReferences(wmEntity);
106
107 // Because there might be multiple LTMs with the same entity
108 if (wmProviderSegment.hasEntity(wmEntity.name()))
109 {
110 armem::wm::Entity existingWmEntity =
111 wmProviderSegment.getEntity(wmEntity.name());
112 existingWmEntity.append(wmEntity);
113 }
114 else
115 {
116 wmProviderSegment.addEntity(wmEntity);
117 }
118 });
119 }
120
121 void
123 {
125
126 wmProviderSegment.id() = id().getProviderSegmentID().cleanID();
127
128 auto& conv = processors->defaultTypeConverter;
129
130 if (persistenceStrategy_->containsItem(
132 {
133 // load and set type
134 auto& conv = processors->defaultTypeConverter;
135
136 try
137 {
138 auto filecontent = persistenceStrategy_->retrieveItem(
140 auto aron = conv.convert(filecontent, "");
141 wmProviderSegment.aronType() = aron;
142 }
143 catch (const nlohmann::json::type_error& e)
144 {
146 << "You are trying to access aron data which is not available, skipping...";
147 wmProviderSegment.aronType() = nullptr;
148 }
149 }
150
151 _implForEachEntity( // Call _impl version
152 [&wmProviderSegment, &n](auto& ltmEntity)
153 {
154 armem::wm::Entity wmEntity;
155 ltmEntity.loadLatestNReferences(n, wmEntity);
156
157 // Because there might be multiple LTMs with the same entity
158 if (wmProviderSegment.hasEntity(wmEntity.name()))
159 {
160 armem::wm::Entity existingWmEntity =
161 wmProviderSegment.getEntity(wmEntity.name());
162 existingWmEntity.append(wmEntity);
163 }
164 else
165 {
166 wmProviderSegment.addEntity(wmEntity);
167 }
168 });
169 }
170
171 void
173 {
175
176 ARMARX_DEBUG << "Resolve provider segment id=" << id().cleanID().str();
177
178 wmProviderSegment.forEachEntity(
179 [&](auto& wmEntity)
180 {
181 std::shared_ptr<persistence::MemoryPersistenceStrategy> entityPersistenceStrategy(
182 persistenceStrategy_);
183
184 Entity ltmEntity(getExportName(),
185 id().withEntityName(wmEntity.id().entityName),
187 entityPersistenceStrategy);
188 ltmEntity.resolve(wmEntity);
189 });
190 }
191
192 void
194 bool simulatedVersion)
195 {
197
198 if (id().providerSegmentName.empty())
199 {
201 << "During storage of segment '" << wmProviderSegment.id().str()
202 << "' I noticed that the corresponding LTM has no id set. "
203 << "I set the id of the LTM to the same name, however this should not happen!";
204 id().providerSegmentName = wmProviderSegment.id().providerSegmentName;
205 }
206
207 ARMARX_DEBUG << "Store provider segment id=" << id().cleanID().str();
208
209 if (wmProviderSegment.hasAronType())
210 {
211 auto& conv = processors->defaultTypeConverter;
212
213 auto type = wmProviderSegment.aronType();
214
215 auto [vec, modeSuffix] = conv.convert(type);
216 ARMARX_CHECK_EMPTY(modeSuffix);
217
218 std::string key = (persistence::MemoryPersistenceStrategy::TYPE_FILENAME + conv.suffix);
219
220 // Only write type file if it doesn't already exist
221 if (!persistenceStrategy_->containsItem(id(), key))
222 {
223 persistenceStrategy_->storeItem(id(), key, vec);
224
225 ARMARX_DEBUG << "Stored type information at " << key;
226 }
227 }
228 else
229 {
230 ARMARX_DEBUG << "ProviderSegment " << wmProviderSegment.id().providerSegmentName
231 << " does not seem to have an aron type, so aron type "
232 "information connot be exported";
233 //writeForeignKeyToPreviousDocument();
234 }
235
236 wmProviderSegment.forEachEntity(
237 [&](const auto& wmEntity)
238 {
239 std::shared_ptr<persistence::MemoryPersistenceStrategy> entityPersistenceStrategy(
240 persistenceStrategy_);
241
242 Entity ltmEntity(getExportName(),
243 id().withEntityName(wmEntity.id().entityName),
245 entityPersistenceStrategy);
246
247 ltmEntity.store(wmEntity, simulatedVersion);
248 statistics.recordedEntities++;
249 });
250 }
251
252} // namespace armarx::armem::server::ltm
#define ARMARX_CHECK_EMPTY(c)
MemoryID getProviderSegmentID() const
Definition MemoryID.cpp:302
MemoryID cleanID() const
Definition MemoryID.cpp:133
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition MemoryID.cpp:102
std::string providerSegmentName
Definition MemoryID.h:52
void append(const OtherDerivedT &other)
Definition EntityBase.h:652
bool hasEntity(const std::string &name) const
EntityT & getEntity(const std::string &name)
EntityT & addEntity(const std::string &name)
Add an empty entity with the given name.
aron::type::ObjectPtr & aronType()
Definition AronTyped.cpp:19
A memory storing data in mongodb (needs 'armarx memory start' to start the mongod instance)
Definition Entity.h:16
ProviderSegment(const std::string &exportName, const MemoryID &memoryId, const std::shared_ptr< Processors > &filters, const std::shared_ptr< persistence::MemoryPersistenceStrategy > &persistenceStrategy)
void _store(const armem::wm::ProviderSegment &wmProviderSegment, bool simulatedVersion) override
std::shared_ptr< Entity > _implFindEntity(const std::string &entityName) const override
void _loadAllReferences(armem::wm::ProviderSegment &wmProviderSegment) override
void _loadLatestNReferences(int n, armem::wm::ProviderSegment &wmProviderSegment) override
bool _implForEachEntity(std::function< void(Entity &)> func) const override
bool _implHasEntity(const std::string &entityName) const override
void _resolve(armem::wm::ProviderSegment &wmProviderSegment) override
void resolve(armem::wm::Entity &e)
convert the references of the input into a wm::Memory
Definition EntityBase.h:46
void store(const armem::wm::Entity &e, bool simulatedVersion)
encode the content of a wm::Memory and store
Definition EntityBase.h:53
virtual std::string getExportName() const
Definition MemoryItem.h:27
std::shared_ptr< Processors > processors
Definition MemoryItem.h:54
Client-side working memory entity.
Client-side working memory provider segment.
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_TRACE
Definition trace.h:77