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 << "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 
170  if (enoughDiskSpaceLeft())
171  {
172  writeDataToFile(id, key, data);
173  }
174  else
175  {
176  ARMARX_DEBUG << "Not enough DiskSpace available for DiskPersistance-Strategy";
177  }
178  }
179 
180  std::vector<unsigned char>
182  {
183  if (!enabled_)
184  {
185  ARMARX_DEBUG << "Disk strategy " << identifier_ << " disabled. Aborting call.";
186  return std::vector<unsigned char>();
187  }
188 
189  if (fileExists(id, key))
190  {
191  return readDataFromFile(id, key);
192  }
193 
194  return std::vector<unsigned char>();
195  }
196 
197  std::filesystem::path
198  DiskPersistence::getMemoryParentPath()
199  {
200  std::string p = memoryParentPath_.string();
201 
203 
204  return p;
205  }
206 
207  std::filesystem::path
208  DiskPersistence::getFullPath(const armarx::armem::MemoryID& id)
209  {
210  auto p = getMemoryParentPath() / getExportName();
211 
212  auto cleanID =
213  id.cleanID(); //somehow, the iDs are jumbled when loading the LTM from disk, this solves it for now
214 
215  auto fullPath = util::fs::toPath(p, cleanID);
216 
217  //inform user about change:
218  ARMARX_DEBUG << "Full path is " << fullPath;
219 
220  return fullPath;
221  }
222 
223  bool
224  DiskPersistence::fullPathExists(const armarx::armem::MemoryID& id)
225  {
226  auto p = getFullPath(id);
227  return util::fs::directoryExists(p);
228  }
229 
230  bool
231  DiskPersistence::fileExists(const armarx::armem::MemoryID& id, const std::string& filename)
232  {
233  auto p = getFullPath(id) / filename;
234  return util::fs::fileExists(p);
235  }
236 
237  void
238  DiskPersistence::ensureFullPathExists(const armarx::armem::MemoryID& id,
239  bool createIfNotExistent)
240  {
241  auto p = getFullPath(id);
242  util::fs::ensureDirectoryExists(p, createIfNotExistent);
243  }
244 
245  void
246  DiskPersistence::ensureFileExists(const armarx::armem::MemoryID& id,
247  const std::string& filename,
248  bool createIfNotExistent)
249  {
250  auto p = getFullPath(id) / filename;
251  util::fs::ensureFileExists(p, createIfNotExistent);
252  }
253 
254  void
255  DiskPersistence::writeDataToFile(const armarx::armem::MemoryID& id,
256  const std::string& filename,
257  const std::vector<unsigned char>& data)
258  {
259  auto p = getFullPath(id) / filename;
261  }
262 
263  std::vector<unsigned char>
264  DiskPersistence::readDataFromFile(const armarx::armem::MemoryID& id,
265  const std::string& filename)
266  {
267  auto p = getFullPath(id) / filename;
268  return util::fs::readDataFromFile(p);
269  }
270 
271  std::vector<std::filesystem::path>
272  DiskPersistence::getAllFiles(const armarx::armem::MemoryID& id)
273  {
274  if (fullPathExists(id))
275  {
276  auto p = getFullPath(id);
277  return util::fs::getAllFiles(p);
278  }
279 
280  return std::vector<std::filesystem::path>();
281  }
282 
283  std::vector<std::filesystem::path>
284  DiskPersistence::getAllDirectories(const armarx::armem::MemoryID& id)
285  {
286  if (fullPathExists(id))
287  {
288  auto p = getFullPath(id);
289  return util::fs::getAllDirectories(p);
290  }
291 
292  return std::vector<std::filesystem::path>();
293  }
294 
295  bool
296  DiskPersistence::enoughDiskSpaceLeft()
297  {
298  std::string path_to_disk = this->getMemoryParentPath();
299  ARMARX_DEBUG << "Checking availability of disk space at " << path_to_disk;
300 
301  if (std::filesystem::exists(path_to_disk))
302  {
303  try
304  {
305  auto space_info = std::filesystem::space(path_to_disk);
306  int const conversion_factor = 1024;
307 
308  auto available_space = space_info.available /
309  (conversion_factor * conversion_factor * conversion_factor);
310  ARMARX_DEBUG << "Capacity: "
311  << space_info.capacity /
312  (conversion_factor * conversion_factor * conversion_factor)
313  << " GB\n";
314  ARMARX_DEBUG << "Free space: "
315  << space_info.free /
316  (conversion_factor * conversion_factor * conversion_factor)
317  << " GB\n";
318  ARMARX_DEBUG << "Available space: "
319  << space_info.available /
320  (conversion_factor * conversion_factor * conversion_factor)
321  << " GB\n";
322 
323  ARMARX_DEBUG << "Min disk space: " << this->minDiskSpace << " GB\n";
324 
325  return static_cast<bool>(available_space >= this->minDiskSpace);
326  }
327  catch (const std::filesystem::filesystem_error& e)
328  {
329  ARMARX_WARNING << "Error: " << e.what() << '\n';
330  return false;
331  }
332  catch (...)
333  {
334  ARMARX_DEBUG << "Error while trying to get info on available disk space";
335  return false;
336  }
337  }
338  else
339  {
340  ARMARX_DEBUG << "Cannot find path to disk and thus cannot check if enough space is "
341  "still available";
342  return true;
343  }
344  }
345 } // 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:181
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:213
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:150
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:201
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