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 (fullPathExists())
30  {
31 
32  for (const auto& subdirName : getAllDirectories())
33  {
34 
35  std::string segmentName = util::fs::detail::unescapeName(subdirName);
36  segmentName = util::fs::detail::extractLastDirectoryFromPath(segmentName);
38  getSettings(),
39  getExportName(),
40  id().withEntityName(segmentName),
41  processors);
42  func(c);
43  }
44  }
45  return true;
46  }
47 
48  bool
49  ProviderSegment::hasEntity(const std::string& name) const
50  {
51  std::lock_guard l(ltm_mutex);
52 
53  if (fullPathExists())
54  {
55  ARMARX_INFO << VAROUT(id().getEntityID());
56  auto c = Entity(getMemoryBasePath(),
57  getSettings(),
58  getExportName(),
59  id().withEntityName(name),
60  processors);
61  return c.fullPathExists();
62  }
63 
64  return false;
65  }
66 
67  std::shared_ptr<Entity>
68  ProviderSegment::findEntity(const std::string& entityName) const
69  {
70  std::lock_guard l(ltm_mutex);
71 
72  if (!hasEntity(entityName))
73  {
74  return nullptr;
75  }
76  return std::make_shared<Entity>(getMemoryBasePath(),
77  getSettings(),
78  getExportName(),
79  id().withEntityName(entityName),
80  processors);
81  }
82 
83  void
85  {
86  std::lock_guard l(ltm_mutex);
87 
88  e.id() = id().getProviderSegmentID().cleanID();
89 
90  auto& conv = processors->defaultTypeConverter;
92  {
93  // load and set type
94  auto& conv = processors->defaultTypeConverter;
95 
96  auto filecontent = readDataFromFile(DiskMemoryItemMixin::TYPE_FILENAME + conv.suffix);
97  auto aron = conv.convert(filecontent, "");
98  e.aronType() = aron;
99  }
100 
102  [&e](auto& x)
103  {
105  x.loadAllReferences(s);
106  e.addEntity(s);
107  });
108  }
109 
110  void
112  {
113  std::lock_guard l(ltm_mutex);
114 
115  e.id() = id().getProviderSegmentID().cleanID();
116 
117  auto& conv = processors->defaultTypeConverter;
119  {
120  // load and set type
121  auto& conv = processors->defaultTypeConverter;
122 
123  auto filecontent = readDataFromFile(DiskMemoryItemMixin::TYPE_FILENAME + conv.suffix);
124  auto aron = conv.convert(filecontent, "");
125  e.aronType() = aron;
126  }
127 
129  [&e, &n](auto& x)
130  {
132  x.loadLatestNReferences(n, s);
133  e.addEntity(s);
134  });
135  }
136 
137  void
139  {
140  std::lock_guard l(ltm_mutex);
141 
142  if (/*(connected() && collectionExists()) ||*/ fullPathExists())
143  {
144  p.forEachEntity(
145  [&](auto& e)
146  {
148  getSettings(),
149  getExportName(),
150  id().withEntityName(e.id().entityName),
151  processors);
152  c.resolve(e);
153  });
154  }
155  }
156 
157  void
159  {
160  std::lock_guard l(ltm_mutex);
161 
162  if (id().providerSegmentName.empty())
163  {
165  << "During storage of segment '" << p.id().str()
166  << "' I noticed that the corresponding LTM has no id set. "
167  << "I set the id of the LTM to the same name, however this should not happen!";
169  }
170 
171  /*if (!connected())
172  {
173  ARMARX_WARNING << "LTM PROVIDER SEGMENT NOT CONNECTED ALTHOUGH ENABLED " << id().str();
174  return;
175  }*/
176 
177  // add foreign key to memory collection
178  if (p.hasAronType())
179  {
180  auto& conv = processors->defaultTypeConverter;
181 
182  auto [vec, modeSuffix] = conv.convert(p.aronType());
183  ARMARX_CHECK_EMPTY(modeSuffix);
184 
185  //std::string dataStr{vec.begin(), vec.end()};
186  //auto dataJson = nlohmann::json::parse(dataStr);
187 
188  //writeForeignKeyToPreviousDocument(dataJson);
189 
190  ensureFullPathExists(true);
191  std::string filename = (TYPE_FILENAME + conv.suffix);
193  }
194  else
195  {
196  ARMARX_INFO << "ProviderSegment does not seem to have an aron type, so aron type information connot be exported";
197  //writeForeignKeyToPreviousDocument();
198  }
199 
200  p.forEachEntity(
201  [&](const auto& e)
202  {
204  getSettings(),
205  getExportName(),
206  id().withEntityName(e.id().entityName),
207  processors);
208 
209  c.store(e);
210  statistics.recordedEntities++;
211  });
212  }
213 
214 } // 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:84
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getMemoryBasePath
Path getMemoryBasePath() const
Definition: DiskStorageMixin.cpp:71
armarx::armem::server::ltm::ProviderSegment::_loadLatestNReferences
void _loadLatestNReferences(int n, armem::wm::ProviderSegment &p) override
Definition: ProviderSegment.cpp:111
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:138
armarx::armem::server::ltm::ProviderSegment::findEntity
std::shared_ptr< Entity > findEntity(const std::string &) const override
find entity segment
Definition: ProviderSegment.cpp:68
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:97
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:160
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:117
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::readDataFromFile
std::vector< unsigned char > readDataFromFile(const std::string &filename) const
Definition: DiskStorageMixin.cpp:153
armarx::armem::server::ltm::ProviderSegment::hasEntity
bool hasEntity(const std::string &) const override
check if entity segment exists
Definition: ProviderSegment.cpp:49
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::TYPE_FILENAME
static const constexpr char * TYPE_FILENAME
Definition: DiskStorageMixin.h:66
armarx::armem::server::ltm::util::fs::detail::extractLastDirectoryFromPath
std::string extractLastDirectoryFromPath(const std::string &path)
Definition: filesystem.cpp:44
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:158
armarx::armem::server::ltm::detail::ProviderSegmentBase< Entity >::statistics
Statistics statistics
Definition: ProviderSegmentBase.h:99
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:110
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:145
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:130
armarx::armem::server::ltm::detail::MemoryItem::name
std::string name() const
Definition: MemoryItem.cpp:43