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  /*
112 
113  auto& conv = processors->defaultTypeConverter;
114  auto setType = [&conv, &e](const std::vector<unsigned char>& aronJson)
115  {
116  auto typeAron = conv.convert(aronJson, "");
117  e.aronType() = aron::type::Object::DynamicCastAndCheck(typeAron);
118  };
119 
120  */
121 
122  /*if (connected() && collectionExists())
123  {
124  // TODO:
125  } else */
126 
127  /*
128 
129  if (std::string filename = TYPE_FILENAME + conv.suffix;
130  fullPathExists() && fileExists(filename))
131  {
132  auto typeFileContent = readDataFromFile(filename);
133  setType(typeFileContent);
134  }
135 
136  */
137 
139  [&e](auto& x)
140  {
142  x.loadAllReferences(s);
143  e.addEntity(s);
144  });
145  }
146 
147  void
149  {
150  std::lock_guard l(ltm_mutex);
151 
152  if (/*(connected() && collectionExists()) ||*/ fullPathExists())
153  {
154  p.forEachEntity(
155  [&](auto& e)
156  {
158  getSettings(),
159  getExportName(),
160  id().withEntityName(e.id().entityName),
161  processors);
162  c.resolve(e);
163  });
164  }
165  }
166 
167  void
169  {
170  std::lock_guard l(ltm_mutex);
171 
172  if (id().providerSegmentName.empty())
173  {
175  << "During storage of segment '" << p.id().str()
176  << "' I noticed that the corresponding LTM has no id set. "
177  << "I set the id of the LTM to the same name, however this should not happen!";
179  }
180 
181  /*if (!connected())
182  {
183  ARMARX_WARNING << "LTM PROVIDER SEGMENT NOT CONNECTED ALTHOUGH ENABLED " << id().str();
184  return;
185  }*/
186 
187  // add foreign key to memory collection
188  if (p.hasAronType())
189  {
190  auto& conv = processors->defaultTypeConverter;
191 
192  auto [vec, modeSuffix] = conv.convert(p.aronType());
193  ARMARX_CHECK_EMPTY(modeSuffix);
194 
195  //std::string dataStr{vec.begin(), vec.end()};
196  //auto dataJson = nlohmann::json::parse(dataStr);
197 
198  //writeForeignKeyToPreviousDocument(dataJson);
199 
200  ensureFullPathExists(true);
201  std::string filename = (TYPE_FILENAME + conv.suffix);
203  }
204  else
205  {
206  ARMARX_INFO << "ProviderSegment does not seem to have an aron type, so aron type information connot be exported";
207  //writeForeignKeyToPreviousDocument();
208  }
209 
210  p.forEachEntity(
211  [&](const auto& e)
212  {
214  getSettings(),
215  getExportName(),
216  id().withEntityName(e.id().entityName),
217  processors);
218 
219  c.store(e);
220  statistics.recordedEntities++;
221  });
222  }
223 
224 } // 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:148
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::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:84
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:168
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::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