ProviderSegment.cpp
Go to the documentation of this file.
1 // Header
2 #include "ProviderSegment.h"
3 
4 // ArmarX
7 
9 
11 {
14  const std::string& exportName,
15  const armem::MemoryID& id /* UNESCAPED */,
16  const std::shared_ptr<Processors>& filters) :
17  ProviderSegmentBase(exportName, id, filters),
18  DiskMemoryItemMixin(p, exportName, id),
19  MongoDBStorageMixin(s, exportName, id)
20  {
21  //start();
22  }
23 
24  bool
25  ProviderSegment::forEachEntity(std::function<void(Entity&)> func) const
26  {
27  std::lock_guard l(ltm_mutex);
28 
29  /*if (connected() && collectionExists())
30  {
31  for (const auto& doc : getAllDocuments())
32  {
33  std::string id_str = doc[FOREIGN_KEY];
34  armem::MemoryID segment_id(id_str);
35  Entity c(
36  getMemoryBasePath(), getSettings(), getExportName(), segment_id, processors);
37  func(c);
38  }
39  } else */
40 
41  if (fullPathExists())
42  {
43 
44  for (const auto& subdirName : getAllDirectories())
45  {
46 
47  std::string segmentName = util::fs::detail::unescapeName(subdirName);
49  getSettings(),
50  getExportName(),
51  id().withEntityName(segmentName),
52  processors);
53  func(c);
54  }
55  }
56  return true;
57  }
58 
59  bool
60  ProviderSegment::hasEntity(const std::string& name) const
61  {
62  std::lock_guard l(ltm_mutex);
63 
64  /*if (connected() && collectionExists())
65  {
66  auto c = Entity(getMemoryBasePath(),
67  getSettings(),
68  getExportName(),
69  id().withEntityName(name),
70  processors);
71  return (bool)c.collectionExists();
72  }*/
73 
74  if (fullPathExists())
75  {
76  ARMARX_INFO << VAROUT(id().getEntityID());
77  auto c = Entity(getMemoryBasePath(),
78  getSettings(),
79  getExportName(),
80  id().withEntityName(name),
81  processors);
82  return c.fullPathExists();
83  }
84 
85  return false;
86  }
87 
88  std::shared_ptr<Entity>
89  ProviderSegment::findEntity(const std::string& entityName) const
90  {
91  std::lock_guard l(ltm_mutex);
92 
93  if (!hasEntity(entityName))
94  {
95  return nullptr;
96  }
97  return std::make_shared<Entity>(getMemoryBasePath(),
98  getSettings(),
99  getExportName(),
100  id().withEntityName(entityName),
101  processors);
102  }
103 
104  void
106  {
107  std::lock_guard l(ltm_mutex);
108 
109  e.id() = id().getProviderSegmentID().cleanID();
110 
111  auto& conv = processors->defaultTypeConverter;
113  {
114  // load and set type
115  auto& conv = processors->defaultTypeConverter;
116 
117  auto filecontent = readDataFromFile(DiskMemoryItemMixin::TYPE_FILENAME + conv.suffix);
118  auto aron = conv.convert(filecontent, "");
119  e.aronType() = aron;
120  }
121 
122  ARMARX_INFO << VAROUT(id());
123 
124  /*
125 
126  auto& conv = processors->defaultTypeConverter;
127  auto setType = [&conv, &e](const std::vector<unsigned char>& aronJson)
128  {
129  auto typeAron = conv.convert(aronJson, "");
130  e.aronType() = aron::type::Object::DynamicCastAndCheck(typeAron);
131  };
132 
133  */
134 
135  /*if (connected() && collectionExists())
136  {
137  // TODO:
138  } else */
139 
140  /*
141 
142  if (std::string filename = TYPE_FILENAME + conv.suffix;
143  fullPathExists() && fileExists(filename))
144  {
145  auto typeFileContent = readDataFromFile(filename);
146  setType(typeFileContent);
147  }
148 
149  */
150 
152  [&e](auto& x)
153  {
155  x.loadAllReferences(s);
156  e.addEntity(s);
157  });
158  }
159 
160  void
162  {
163  std::lock_guard l(ltm_mutex);
164 
165  if (/*(connected() && collectionExists()) ||*/ fullPathExists())
166  {
167  p.forEachEntity(
168  [&](auto& e)
169  {
171  getSettings(),
172  getExportName(),
173  id().withEntityName(e.id().entityName),
174  processors);
175  c.resolve(e);
176  });
177  }
178  }
179 
180  void
182  {
183  std::lock_guard l(ltm_mutex);
184 
185  if (id().providerSegmentName.empty())
186  {
188  << "During storage of segment '" << p.id().str()
189  << "' I noticed that the corresponding LTM has no id set. "
190  << "I set the id of the LTM to the same name, however this should not happen!";
192  }
193 
194  /*if (!connected())
195  {
196  ARMARX_WARNING << "LTM PROVIDER SEGMENT NOT CONNECTED ALTHOUGH ENABLED " << id().str();
197  return;
198  }*/
199 
200  // add foreign key to memory collection
201  if (p.hasAronType())
202  {
203  auto& conv = processors->defaultTypeConverter;
204 
205  auto [vec, modeSuffix] = conv.convert(p.aronType());
206  ARMARX_CHECK_EMPTY(modeSuffix);
207 
208  //std::string dataStr{vec.begin(), vec.end()};
209  //auto dataJson = nlohmann::json::parse(dataStr);
210 
211  //writeForeignKeyToPreviousDocument(dataJson);
212 
213  ensureFullPathExists(true);
214  std::string filename = (TYPE_FILENAME + conv.suffix);
216  }
217  else
218  {
219  ARMARX_INFO << "ProviderSegment does not seem to have an aron type, so aron type information connot be exported";
220  //writeForeignKeyToPreviousDocument();
221  }
222 
223  p.forEachEntity(
224  [&](const auto& e)
225  {
227  getSettings(),
228  getExportName(),
229  id().withEntityName(e.id().entityName),
230  processors);
231 
232  c.store(e);
233  statistics.recordedEntities++;
234  });
235  }
236 
237 } // namespace armarx::armem::server::ltm
armarx::armem::server::ltm::detail::MemoryItem::processors
std::shared_ptr< Processors > processors
Definition: MemoryItem.h:54
armarx::armem::wm::ProviderSegment
Client-side working memory provider segment.
Definition: memory_definitions.h:105
armarx::armem::MemoryID::providerSegmentName
std::string providerSegmentName
Definition: MemoryID.h:52
armarx::armem::server::ltm::ProviderSegment::_loadAllReferences
void _loadAllReferences(armem::wm::ProviderSegment &) override
Definition: ProviderSegment.cpp:105
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getMemoryBasePath
Path getMemoryBasePath() const
Definition: DiskStorageMixin.cpp:77
ARMARX_CHECK_EMPTY
#define ARMARX_CHECK_EMPTY(c)
Definition: ExpressionException.h:218
armarx::armem::server::ltm::ProviderSegment::_resolve
void _resolve(armem::wm::ProviderSegment &) override
Definition: ProviderSegment.cpp:161
armarx::armem::server::ltm::ProviderSegment::findEntity
std::shared_ptr< Entity > findEntity(const std::string &) const override
find entity segment
Definition: ProviderSegment.cpp:89
armarx::armem::MemoryID::str
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition: MemoryID.cpp:102
armarx::armem::server::ltm::Entity
A memory storing data in mongodb (needs 'armarx memory start' to start the mongod instance)
Definition: Entity.h:14
armarx::armem::base::detail::AronTyped::aronType
aron::type::ObjectPtr & aronType()
Definition: AronTyped.cpp:18
armarx::armem::server::ltm::detail::ProviderSegmentBase< Entity >::ltm_mutex
std::recursive_mutex ltm_mutex
Definition: ProviderSegmentBase.h:91
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::armem::server::ltm::util::fs::detail::unescapeName
std::string unescapeName(const std::string &escapedName)
Definition: filesystem.cpp:31
armarx::armem::server::ltm::ProviderSegment::ProviderSegment
ProviderSegment(const detail::mixin::Path &, const detail::mixin::MongoDBSettings &, const std::string &, const MemoryID &id, const std::shared_ptr< Processors > &)
Definition: ProviderSegment.cpp:12
memory_definitions.h
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getAllDirectories
std::vector< Path > getAllDirectories() const
Definition: DiskStorageMixin.cpp:156
armarx::armem::server::ltm::ProviderSegment::forEachEntity
bool forEachEntity(std::function< void(Entity &)> func) const override
iterate over all core segments of this ltm
Definition: ProviderSegment.cpp:25
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::fileExists
bool fileExists(const std::string &filename) const
Definition: DiskStorageMixin.cpp:113
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::readDataFromFile
std::vector< unsigned char > readDataFromFile(const std::string &filename) const
Definition: DiskStorageMixin.cpp:149
armarx::armem::server::ltm::ProviderSegment::hasEntity
bool hasEntity(const std::string &) const override
check if entity segment exists
Definition: ProviderSegment.cpp:60
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::TYPE_FILENAME
static const constexpr char * TYPE_FILENAME
Definition: DiskStorageMixin.h:66
filename
std::string filename
Definition: VisualizationRobot.cpp:83
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::getSettings
MongoDBSettings getSettings() const
Definition: MongoDBStorageMixin.cpp:436
ProviderSegment.h
armarx::armem::server::ltm::ProviderSegment::_store
void _store(const armem::wm::ProviderSegment &) override
Definition: ProviderSegment.cpp:181
armarx::armem::server::ltm::detail::ProviderSegmentBase< Entity >::statistics
Statistics statistics
Definition: ProviderSegmentBase.h:93
armarx::armem::server::ltm::detail::MemoryItem::getExportName
virtual std::string getExportName() const
Definition: MemoryItem.h:26
armarx::armem::MemoryID::getProviderSegmentID
MemoryID getProviderSegmentID() const
Definition: MemoryID.cpp:297
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
armarx::armem::server::ltm
Definition: forward_declarations.h:20
armarx::armem::server::ltm::detail::MemoryItem::id
MemoryID id() const
Definition: MemoryItem.cpp:37
armarx::armem::MemoryID::cleanID
MemoryID cleanID() const
Definition: MemoryID.cpp:132
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::armem::base::ProviderSegmentBase::forEachEntity
bool forEachEntity(EntityFunctionT &&func)
Definition: ProviderSegmentBase.h:189
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::fullPathExists
bool fullPathExists() const
Definition: DiskStorageMixin.cpp:106
armarx::armem::base::detail::AronTyped::hasAronType
bool hasAronType() const
Definition: AronTyped.cpp:13
armarx::armem::server::ltm::detail::mixin::MongoDBSettings
Definition: MongoDBStorageMixin.h:15
armarx::armem::server::ltm::detail::mixin::Path
std::filesystem::path Path
Definition: DiskStorageMixin.h:17
armarx::armem::wm::Entity
Client-side working memory entity.
Definition: memory_definitions.h:93
Logging.h
armarx::armem::base::ProviderSegmentBase::addEntity
EntityT & addEntity(const std::string &name)
Add an empty entity with the given name.
Definition: ProviderSegmentBase.h:298
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::writeDataToFile
void writeDataToFile(const std::string &filename, const std::vector< unsigned char > &data) const
Definition: DiskStorageMixin.cpp:141
armarx::armem::server::ltm::disk::constantes::TYPE_FILENAME
const std::string TYPE_FILENAME
Definition: util.h:12
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::ensureFullPathExists
void ensureFullPathExists(bool createIfNotExistent=false) const
Definition: DiskStorageMixin.cpp:126
armarx::armem::server::ltm::detail::MemoryItem::name
std::string name() const
Definition: MemoryItem.cpp:43