DiskPersistence.cpp
Go to the documentation of this file.
2 
4 
6 {
7 
8  std::vector<std::string>
10  {
11  ARMARX_DEBUG << "DiskPersistence: GetContainerKey, memID=" << id.str();
12 
13  std::vector<std::string> containers;
14 
15  if (!enabled_)
16  {
17  ARMARX_DEBUG << "Disk strategy " << identifier_ << " disabled. Aborting call.";
18  return containers;
19  }
20 
21  // If it has a nice form
22  if (!id.hasEntityName() || id.hasTimestamp())
23  {
24  std::vector<std::filesystem::path> dirs = getAllDirectories(id);
25 
26 
27  for (auto& path : dirs)
28  {
29  std::string container = path.filename().string();
30 
31  if (!container.empty())
32  {
33  containers.emplace_back(container);
34  }
35  }
36  }
37  // If it has not a nice form
38  else
39  {
40  std::vector<std::filesystem::path> dayDirs = getAllDirectories(id);
41 
42  for (std::filesystem::path& dayDir : dayDirs)
43  {
44  if (!util::fs::detail::isDateString(dayDir.filename()))
45  {
46  ARMARX_WARNING << "Found a non-date folder inside an entity '" << id.str()
47  << "' with name '" << dayDir.filename() << "'. "
48  << "Ignoring this folder, however this is a bad situation.";
49  continue;
50  }
51 
52  std::vector<std::filesystem::path> secondDirs = util::fs::getAllDirectories(dayDir);
53 
54  for (std::filesystem::path& secondDir : secondDirs)
55  {
56  if (!util::fs::detail::isNumberString(secondDir.filename()))
57  {
58  ARMARX_WARNING << "Found a non-timestamp folder inside an entity '"
59  << id.str() << "' hours folder with name '"
60  << secondDir.filename() << "'. "
61  << "Ignoring this folder, however this is a bad situation.";
62  continue;
63  }
64 
65  std::vector<std::filesystem::path> timestampDirs =
66  util::fs::getAllDirectories(secondDir);
67 
68  for (std::filesystem::path& timestampDir : timestampDirs)
69  {
70  if (!util::fs::detail::isNumberString(timestampDir.filename()))
71  {
73  << "Found a non-timestamp folder inside an entity '" << id.str()
74  << "' seconds folder with name '" << timestampDir.filename()
75  << "'. "
76  << "Ignoring this folder, however this is a bad situation.";
77  continue;
78  }
79 
80  std::string container = timestampDir.filename().string();
81 
82  if (!container.empty())
83  {
84  containers.emplace_back(container);
85  }
86  }
87  }
88  }
89  }
90 
91  return containers;
92  }
93 
94  std::vector<std::string>
96  {
97  if (!enabled_)
98  {
99  ARMARX_DEBUG << "Disk strategy " << identifier_ << " disabled. Aborting call.";
100  return std::vector<std::string>();
101  }
102 
103  std::vector<std::filesystem::path> files = getAllFiles(id);
104  std::vector<std::string> filesStr;
105 
106  for (auto& path : files)
107  {
108  std::string item = path.filename().string();
109 
110  if (!item.empty())
111  {
112  filesStr.emplace_back(item);
113  }
114  }
115 
116  return filesStr;
117  }
118 
119  bool
121  {
122  ARMARX_DEBUG << "Contains container key=" << key << "? mem id=" << id.str();
123 
124  if (!enabled_)
125  {
126  ARMARX_DEBUG << "Disk strategy " << identifier_ << " disabled. Aborting call.";
127  return false;
128  }
129 
130  for (auto& storedKey : getContainerKeys(id))
131  {
132  ARMARX_DEBUG << "Compare stored key=" << storedKey << ", key=" << key;
133  if (storedKey == key)
134  {
135  return true;
136  }
137  }
138 
139  ARMARX_DEBUG << "Nope, does not contain the container!";
140 
141  return false;
142  }
143 
144  bool
146  {
147  if (!enabled_)
148  {
149  ARMARX_DEBUG << "Disk strategy " << identifier_ << " disabled. Aborting call.";
150  return false;
151  }
152 
153  return fileExists(id, key);
154  }
155 
156  void
158  std::string key,
159  std::vector<unsigned char>& data)
160  {
161  if (!enabled_)
162  {
163  ARMARX_DEBUG << "Disk strategy " << identifier_ << " disabled. Aborting call.";
164  return;
165  }
166 
167  ensureFullPathExists(id, true);
168  ensureFileExists(id, key, true);
169  if (enoughDiskSpaceLeft())
170  {
171  writeDataToFile(id, key, data);
172  }
173  else
174  {
175  ARMARX_DEBUG << "Not enough DiskSpace available for DiskPersistance-Strategy";
176  }
177  }
178 
179  std::vector<unsigned char>
181  {
182  if (!enabled_)
183  {
184  ARMARX_DEBUG << "Disk strategy " << identifier_ << " disabled. Aborting call.";
185  return std::vector<unsigned char>();
186  }
187 
188  if (fileExists(id, key))
189  {
190  return readDataFromFile(id, key);
191  }
192 
193  return std::vector<unsigned char>();
194  }
195 
196  std::filesystem::path
197  DiskPersistence::getMemoryParentPath()
198  {
199  std::string p = memoryParentPath_.string();
200 
202 
203  return p;
204  }
205 
206  std::filesystem::path
207  DiskPersistence::getFullPath(const armarx::armem::MemoryID& id)
208  {
209  auto p = getMemoryParentPath() / getExportName();
210 
211  auto cleanID =
212  id.cleanID(); //somehow, the iDs are jumbled when loading the LTM from disk, this solves it for now
213 
214  auto fullPath = util::fs::toPath(p, cleanID);
215 
216  //inform user about change:
217  ARMARX_DEBUG << "Full path is " << fullPath;
218 
219  return fullPath;
220  }
221 
222  bool
223  DiskPersistence::fullPathExists(const armarx::armem::MemoryID& id)
224  {
225  auto p = getFullPath(id);
226  return util::fs::directoryExists(p);
227  }
228 
229  bool
230  DiskPersistence::fileExists(const armarx::armem::MemoryID& id, const std::string& filename)
231  {
232  auto p = getFullPath(id) / filename;
233  return util::fs::fileExists(p);
234  }
235 
236  void
237  DiskPersistence::ensureFullPathExists(const armarx::armem::MemoryID& id,
238  bool createIfNotExistent)
239  {
240  auto p = getFullPath(id);
241  util::fs::ensureDirectoryExists(p, createIfNotExistent);
242  }
243 
244  void
245  DiskPersistence::ensureFileExists(const armarx::armem::MemoryID& id,
246  const std::string& filename,
247  bool createIfNotExistent)
248  {
249  auto p = getFullPath(id) / filename;
250  util::fs::ensureFileExists(p, createIfNotExistent);
251  }
252 
253  void
254  DiskPersistence::writeDataToFile(const armarx::armem::MemoryID& id,
255  const std::string& filename,
256  const std::vector<unsigned char>& data)
257  {
258  auto p = getFullPath(id) / filename;
260  }
261 
262  std::vector<unsigned char>
263  DiskPersistence::readDataFromFile(const armarx::armem::MemoryID& id,
264  const std::string& filename)
265  {
266  auto p = getFullPath(id) / filename;
267  return util::fs::readDataFromFile(p);
268  }
269 
270  std::vector<std::filesystem::path>
271  DiskPersistence::getAllFiles(const armarx::armem::MemoryID& id)
272  {
273  if (fullPathExists(id))
274  {
275  auto p = getFullPath(id);
276  return util::fs::getAllFiles(p);
277  }
278 
279  return std::vector<std::filesystem::path>();
280  }
281 
282  std::vector<std::filesystem::path>
283  DiskPersistence::getAllDirectories(const armarx::armem::MemoryID& id)
284  {
285  if (fullPathExists(id))
286  {
287  auto p = getFullPath(id);
288  return util::fs::getAllDirectories(p);
289  }
290 
291  return std::vector<std::filesystem::path>();
292  }
293 
294  bool
295  DiskPersistence::enoughDiskSpaceLeft()
296  {
297  std::string path_to_disk = this->getMemoryParentPath();
298  ARMARX_DEBUG << "Checking availability of disk space at " << path_to_disk;
299 
300  if (std::filesystem::exists(path_to_disk))
301  {
302  try
303  {
304  auto space_info = std::filesystem::space(path_to_disk);
305  int const conversion_factor = 1024;
306 
307  auto available_space = space_info.available /
308  (conversion_factor * conversion_factor * conversion_factor);
309  ARMARX_DEBUG << "Capacity: "
310  << space_info.capacity /
311  (conversion_factor * conversion_factor * conversion_factor)
312  << " GB\n";
313  ARMARX_DEBUG << "Free space: "
314  << space_info.free /
315  (conversion_factor * conversion_factor * conversion_factor)
316  << " GB\n";
317  ARMARX_DEBUG << "Available space: "
318  << space_info.available /
319  (conversion_factor * conversion_factor * conversion_factor)
320  << " GB\n";
321 
322  ARMARX_DEBUG << "Min disk space: " << this->minDiskSpace << " GB\n";
323 
324  return static_cast<bool>(available_space >= this->minDiskSpace);
325  }
326  catch (const std::filesystem::filesystem_error& e)
327  {
328  ARMARX_WARNING << "Error: " << e.what() << '\n';
329  return false;
330  }
331  catch (...)
332  {
333  ARMARX_DEBUG << "Error while trying to get info on available disk space";
334  return false;
335  }
336  }
337  else
338  {
339  ARMARX_DEBUG << "Cannot find path to disk and thus cannot check if enough space is "
340  "still available";
341  return true;
342  }
343  }
344 } // namespace armarx::armem::server::ltm::persistence
files
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE The MIT Marcin Kalicinski Permission is hereby free of to any person obtaining a copy of this software and associated documentation files(the "Software")
armarx::armem::server::ltm::persistence::DiskPersistence::retrieveItem
std::vector< unsigned char > retrieveItem(const armarx::armem::MemoryID &id, std::string key) override
Reads the data of the file with name 'key' at the current location.
Definition: DiskPersistence.cpp:180
armarx::armem::server::ltm::util::fs::ensureFileExists
void ensureFileExists(const std::filesystem::path &p, bool createIfNotExistent=false)
Definition: filesystem.cpp:162
armarx::armem::server::ltm::util::fs::writeDataToFile
void writeDataToFile(const std::filesystem::path &p, const std::vector< unsigned char > &data)
Definition: filesystem.cpp:181
armarx::armem::server::ltm::persistence::MemoryPersistenceStrategy::enabled_
bool enabled_
If false, the strategy is not writing or reading anything.
Definition: MemoryPersistenceStrategy.h:187
armarx::armem::server::ltm::persistence
Definition: DiskPersistence.cpp:5
armarx::armem::server::ltm::persistence::DiskPersistence::getContainerKeys
std::vector< std::string > getContainerKeys(const armarx::armem::MemoryID &id) override
Returns all containers for the current id.
Definition: DiskPersistence.cpp:9
armarx::armem::server::ltm::util::fs::readDataFromFile
std::vector< unsigned char > readDataFromFile(const std::filesystem::path &p)
Definition: filesystem.cpp:195
armarx::armem::server::ltm::util::fs::ensureDirectoryExists
void ensureDirectoryExists(const std::filesystem::path &p, bool createIfNotExistent=false)
Definition: filesystem.cpp:145
armarx::armem::server::ltm::persistence::DiskPersistence::storeItem
void storeItem(const armarx::armem::MemoryID &id, std::string key, std::vector< unsigned char > &data) override
Create a new file with name 'key' and stores the data inside it.
Definition: DiskPersistence.cpp:157
space
std::string space
Definition: OrientedTactileSensorUnit.cpp:371
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::armem::server::ltm::util::fs::getAllDirectories
std::vector< std::filesystem::path > getAllDirectories(const std::filesystem::path &p)
Definition: filesystem.cpp:205
filename
std::string filename
Definition: VisualizationRobot.cpp:86
armarx::armem::server::ltm::persistence::MemoryPersistenceStrategy::getExportName
std::string getExportName()
Definition: MemoryPersistenceStrategy.h:131
armarx::armem::server::ltm::util::fs::detail::isNumberString
bool isNumberString(const std::string &s)
Definition: filesystem.cpp:71
armarx::armem::server::ltm::util::fs::toPath
std::filesystem::path toPath(const std::filesystem::path &base, const armem::MemoryID &id)
Definition: filesystem.cpp:97
armarx::armem::server::ltm::util::fs::directoryExists
bool directoryExists(const std::filesystem::path &p)
Definition: filesystem.cpp:133
armarx::armem::server::ltm::persistence::MemoryPersistenceStrategy::identifier_
std::string identifier_
Name of the strategy.
Definition: MemoryPersistenceStrategy.h:175
armarx::armem::server::ltm::util::fs::detail::isDateString
bool isDateString(const std::string &s)
Definition: filesystem.cpp:84
armarx::armem::server::ltm::persistence::DiskPersistence::containsItem
bool containsItem(const armarx::armem::MemoryID &id, std::string key) override
Checks if current container contains the item defined by its key.
Definition: DiskPersistence.cpp:145
armarx::ArmarXDataPath::ReplaceEnvVars
static bool ReplaceEnvVars(std::string &string)
ReplaceEnvVars replaces environment variables in a string with their values, if the env.
Definition: ArmarXDataPath.cpp:480
filesystem.h
DiskPersistence.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::armem::server::ltm::util::fs::fileExists
bool fileExists(const std::filesystem::path &p)
Definition: filesystem.cpp:139
armarx::armem::server::ltm::persistence::DiskPersistence::getItemKeys
std::vector< std::string > getItemKeys(const armarx::armem::MemoryID &id) override
Returns all items for the current id.
Definition: DiskPersistence.cpp:95
armarx::armem::server::ltm::persistence::DiskPersistence::minDiskSpace
int minDiskSpace
Definition: DiskPersistence.h:128
armarx::armem::server::ltm::util::fs::getAllFiles
std::vector< std::filesystem::path > getAllFiles(const std::filesystem::path &p)
Definition: filesystem.cpp:224
armarx::armem::server::ltm::persistence::DiskPersistence::containsContainer
bool containsContainer(const armarx::armem::MemoryID &id, std::string key) override
Checks if the container is available for the current memory id.
Definition: DiskPersistence.cpp:120