DiskStorageMixin.cpp
Go to the documentation of this file.
1 // Header
2 #include "DiskStorageMixin.h"
3 
4 // STD/STL
5 #include <algorithm>
6 #include <fstream>
7 #include <iostream>
8 
9 // Simox
10 #include <SimoxUtility/algorithm/string.h>
11 
12 // ArmarX
16 
18 {
20  const std::string& exportName,
21  const armem::MemoryID& id) :
22  memoryBasePath(memoryParentPath), exportName(exportName), _id(id)
23  {
24  }
25 
26  Path
28  {
29  //set path to include date of creation as prefix:
30  auto current_date = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
31  std::tm* localTime = std::localtime(&current_date);
32 
33  // convert current time into string:
34  std::stringstream ss;
35  ss << std::put_time(localTime, "%Y_%m_%d");
36  std::string dateString = ss.str();
37 
38  // change memory base path to include current date (date of memory creation)
39  std::filesystem::path current_base_path = n;
40  current_base_path.append(dateString);
41 
42  //inform user about change:
43  ARMARX_DEBUG << "Changed memory base path to include current date of "
44  << dateString
45  << " to "
46  << this->getMemoryBasePath()
47  ;
48 
49  return current_base_path;
50  }
51 
52  void
54  {
55  ARMARX_INFO << "Currently setting export name to: " << n;
56  exportName = n;
57  }
58 
59  void
61  {
62  ARMARX_CHECK_NOT_EMPTY(_id.memoryName) << " The full id was: " << _id.str();
63 
64  _id = n;
65  }
66 
67  void
68  DiskMemoryItemMixin::configureMixin(const nlohmann::json& json)
69  {
70  if (json.find("DiskMemory.memoryParentPath") != json.end())
71  {
72  //memoryBasePath = Path(json.at("DiskMemory.memoryParentPath"));
73  }
74  }
75 
76  Path
78  {
79  if(memoryBasePathString.empty()){
80  return memoryBasePath;
81  } else {
82  std::filesystem::path newPath;
83  newPath.assign(memoryBasePathString);
84  return this->addDateToMemoryBasePath(newPath);
85  }
86  }
87 
88  Path
90  {
91  auto p = getMemoryBasePath() / exportName;
92  //ARMARX_INFO << VAROUT(_id);
93  //ARMARX_INFO << VAROUT(_id.cleanID());
94  auto cleanID = _id.cleanID(); //somehow, the iDs are jumbled when loading the LTM from disk, this solves it for now
95 
96  return util::fs::toPath(p, cleanID);
97  }
98 
99  bool
101  {
103  }
104 
105  bool
107  {
108  auto p = getFullPath();
109  return util::fs::directoryExists(p);
110  }
111 
112  bool
113  DiskMemoryItemMixin::fileExists(const std::string& filename) const
114  {
115  auto p = getFullPath() / filename;
116  return util::fs::fileExists(p);
117  }
118 
119  void
120  DiskMemoryItemMixin::ensureMemoryBasePathExists(bool createIfNotExistent) const
121  {
122  util::fs::ensureDirectoryExists(getMemoryBasePath(), createIfNotExistent);
123  }
124 
125  void
126  DiskMemoryItemMixin::ensureFullPathExists(bool createIfNotExistent) const
127  {
128  auto p = getFullPath();
129  util::fs::ensureDirectoryExists(p, createIfNotExistent);
130  }
131 
132  void
134  bool createIfNotExistent) const
135  {
136  auto p = getFullPath() / filename;
137  util::fs::ensureFileExists(p, createIfNotExistent);
138  }
139 
140  void
142  const std::vector<unsigned char>& data) const
143  {
144  auto p = getFullPath() / filename;
146  }
147 
148  std::vector<unsigned char>
150  {
151  auto p = getFullPath() / filename;
152  return util::fs::readDataFromFile(p);
153  }
154 
155  std::vector<Path>
157  {
158  auto p = getFullPath();
159  return util::fs::getAllDirectories(p);
160  }
161 
162  std::vector<Path>
164  {
165  auto p = getFullPath();
166  return util::fs::getAllFiles(p);
167  }
168 
170  {
171  defs->optional(memoryBasePathString, prefix + "exportPath");
172  }
173 } // namespace armarx::armem::server::ltm::detail::mixin
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::addDateToMemoryBasePath
Path addDateToMemoryBasePath(const std::filesystem::path &n) const
Definition: DiskStorageMixin.cpp:27
LocalException.h
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::ensureMemoryBasePathExists
void ensureMemoryBasePathExists(bool createIfNotExistent=false) const
Definition: DiskStorageMixin.cpp:120
armarx::armem::server::ltm::util::fs::ensureFileExists
void ensureFileExists(const std::filesystem::path &p, bool createIfNotExistent=false)
Definition: filesystem.cpp:147
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getMemoryBasePath
Path getMemoryBasePath() const
Definition: DiskStorageMixin.cpp:77
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::ensureFileExists
void ensureFileExists(const std::string &filename, bool createIfNotExistent=false) const
Definition: DiskStorageMixin.cpp:133
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::createPropertyDefinitions
void createPropertyDefinitions(PropertyDefinitionsPtr &defs, const std::string &prefix)
Definition: DiskStorageMixin.cpp:169
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::util::fs::writeDataToFile
void writeDataToFile(const std::filesystem::path &p, const std::vector< unsigned char > &data)
Definition: filesystem.cpp:166
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getAllFiles
std::vector< Path > getAllFiles() const
Definition: DiskStorageMixin.cpp:163
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::setMixinMemoryID
void setMixinMemoryID(const MemoryID &n)
Definition: DiskStorageMixin.cpp:60
ARMARX_CHECK_NOT_EMPTY
#define ARMARX_CHECK_NOT_EMPTY(c)
Definition: ExpressionException.h:224
armarx::armem::server::ltm::util::fs::readDataFromFile
std::vector< unsigned char > readDataFromFile(const std::filesystem::path &p)
Definition: filesystem.cpp:180
armarx::armem::server::ltm::util::fs::ensureDirectoryExists
void ensureDirectoryExists(const std::filesystem::path &p, bool createIfNotExistent=false)
Definition: filesystem.cpp:130
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getAllDirectories
std::vector< Path > getAllDirectories() const
Definition: DiskStorageMixin.cpp:156
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
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
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::util::fs::getAllDirectories
std::vector< std::filesystem::path > getAllDirectories(const std::filesystem::path &p)
Definition: filesystem.cpp:190
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::setMixinExportName
void setMixinExportName(const std::string &n)
Definition: DiskStorageMixin.cpp:53
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::DiskMemoryItemMixin
DiskMemoryItemMixin()=default
filename
std::string filename
Definition: VisualizationRobot.cpp:84
armarx::armem::server::ltm::util::fs::toPath
std::filesystem::path toPath(const std::filesystem::path &base, const armem::MemoryID &id)
Definition: filesystem.cpp:82
armarx::armem::MemoryID::memoryName
std::string memoryName
Definition: MemoryID.h:50
armarx::armem::server::ltm::util::fs::directoryExists
bool directoryExists(const std::filesystem::path &p)
Definition: filesystem.cpp:118
armarx::armem::MemoryID::cleanID
MemoryID cleanID() const
Definition: MemoryID.cpp:132
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::armem::server::ltm::detail::mixin
Definition: BufferedMemoryMixin.cpp:3
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::fullPathExists
bool fullPathExists() const
Definition: DiskStorageMixin.cpp:106
armarx::armem::server::ltm::detail::mixin::Path
std::filesystem::path Path
Definition: DiskStorageMixin.h:17
Logging.h
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::configureMixin
void configureMixin(const nlohmann::json &json)
Definition: DiskStorageMixin.cpp:68
armarx::armem::server::ltm::util::fs::fileExists
bool fileExists(const std::filesystem::path &p)
Definition: filesystem.cpp:124
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getFullPath
Path getFullPath() const
Definition: DiskStorageMixin.cpp:89
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
DiskStorageMixin.h
armarx::armem::server::ltm::util::fs::getAllFiles
std::vector< std::filesystem::path > getAllFiles(const std::filesystem::path &p)
Definition: filesystem.cpp:209
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::ensureFullPathExists
void ensureFullPathExists(bool createIfNotExistent=false) const
Definition: DiskStorageMixin.cpp:126
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::memoryBasePathExists
bool memoryBasePathExists() const
Definition: DiskStorageMixin.cpp:100