DiskPersistence.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <filesystem>
4 #include <string>
5 #include <vector>
6 
9 
11 {
13  {
14  public:
15  FileIdentifier(std::string& filename, std::string& fileType) :
16  filename_(filename),
17  fileType_(fileType){
18 
19  };
20 
21  virtual ~FileIdentifier()
22  {
23  }
24 
25  std::string
26  getKey() override
27  {
28  return filename_ + fileType_;
29  }
30 
31  private:
32  std::string filename_;
33  std::string fileType_;
34  };
35 
36  /**
37  * @brief Persistence strategy that writes items (e.g. json files) to a specific container (a directory)
38  * Use it to write the data of a WM to disk.
39  *
40  * Where are the items written (=:location)?
41  * /memoryParentPath/exportName/path(id)/
42  *
43  * How is the file of an item named?
44  * -> Just they key (e.g. filename = key = "data.aron.json")
45  * If you might want a more sophisticated solution fell free to implement your custom ItemIdentifier today!
46  */
48  {
49  public:
50  DiskPersistence() : DiskPersistence(std::filesystem::path("."))
51  {
52  }
53 
54  DiskPersistence(const std::filesystem::path& memoryParentPath) :
55  DiskPersistence("Disk", "DefaultExport", memoryParentPath)
56  {
57  }
58 
59  /**
60  * @param identifier basically a unique name for the strategy (important if you use different strategies @see RedundantPersistenceStrategy)
61  * @param exportName identifier for the exported memory. A new directory with name is created beneath the memoryParentPath. Everything is stored inside it.
62  * @param memoryParentPath path where the memory should be exported to
63  */
64  DiskPersistence(const std::string& identifier,
65  const std::string& exportName,
66  const std::filesystem::path& memoryParentPath) :
67  MemoryPersistenceStrategy(identifier, exportName), memoryParentPath_(memoryParentPath)
68  {
69  }
70 
71  /**
72  * Returns all containers for the current id.
73  * @return containers <=> directories at current location (=/memoryParentPath/exportName/path(id)/)
74  */
75  std::vector<std::string> getContainerKeys(const armarx::armem::MemoryID& id) override;
76 
77  /**
78  * Returns all items for the current id.
79  * @return items <=> files at the current location (=/memoryParentPath/exportName/path(id)/)
80  */
81  std::vector<std::string> getItemKeys(const armarx::armem::MemoryID& id) override;
82 
83  /**
84  * Checks if the container is available for the current memory id.
85  * @return true if the current location contains the directory with name 'key'
86  */
87  bool containsContainer(const armarx::armem::MemoryID& id, std::string key) override;
88 
89  /**
90  * Checks if current container contains the item defined by its key.
91  * @return true if the current location contains a file with the name 'key'
92  */
93  bool containsItem(const armarx::armem::MemoryID& id, std::string key) override;
94 
95  /**
96  * Create a new file with name 'key' and stores the data inside it.
97  */
98  void storeItem(const armarx::armem::MemoryID& id,
99  std::string key,
100  std::vector<unsigned char>& data) override;
101 
102  /**
103  * Reads the data of the file with name 'key' at the current location.
104  * @return data if a file was found, an empty vector if the file is empty or was not found
105  */
106  std::vector<unsigned char> retrieveItem(const armarx::armem::MemoryID& id,
107  std::string key) override;
108 
109  void
110  createPropertyDefinitions(PropertyDefinitionsPtr& defs, const std::string& prefix) override
111  {
112  // Nothing to do
113  }
114 
115  void
117  {
118  this->minDiskSpace = minDiskSpace;
119  if (!enoughDiskSpaceLeft())
120  {
121  ARMARX_WARNING << "Not enough available disk space for DiskPersistance Strategy. "
122  "You need at least "
123  << this->minDiskSpace
124  << " GB available disk space to record into LTM using this strategy";
125  }
126  }
127 
128  int minDiskSpace = 50; // in GB
129 
130 
131  private:
132  std::filesystem::path memoryParentPath_;
133 
134  /* Internal disk logic */
135 
136  bool fullPathExists(const armarx::armem::MemoryID& id);
137 
138  std::vector<std::filesystem::path> getAllFiles(const armarx::armem::MemoryID& id);
139 
140  std::vector<std::filesystem::path> getAllDirectories(const armarx::armem::MemoryID& id);
141 
142  bool fileExists(const armarx::armem::MemoryID& id, const std::string& filename);
143 
144  std::filesystem::path getFullPath(const armarx::armem::MemoryID& id);
145 
146  void ensureFullPathExists(const armarx::armem::MemoryID& id,
147  bool createIfNotExistent = false);
148 
149  void ensureFileExists(const armarx::armem::MemoryID& id,
150  const std::string& filename,
151  bool createIfNotExistent = false);
152 
153  void writeDataToFile(const armarx::armem::MemoryID& id,
154  const std::string& filename,
155  const std::vector<unsigned char>& data);
156 
157  std::vector<unsigned char> readDataFromFile(const armarx::armem::MemoryID& id,
158  const std::string& filename);
159 
160  std::filesystem::path getMemoryParentPath();
161 
162  bool enoughDiskSpaceLeft();
163  };
164 } // namespace armarx::armem::server::ltm::persistence
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::persistence::FileIdentifier::FileIdentifier
FileIdentifier(std::string &filename, std::string &fileType)
Definition: DiskPersistence.h:15
armarx::armem::server::ltm::persistence::DiskPersistence::DiskPersistence
DiskPersistence(const std::string &identifier, const std::string &exportName, const std::filesystem::path &memoryParentPath)
Definition: DiskPersistence.h:64
armarx::armem::server::ltm::persistence::DiskPersistence::createPropertyDefinitions
void createPropertyDefinitions(PropertyDefinitionsPtr &defs, const std::string &prefix) override
Definition: DiskPersistence.h:110
armarx::armem::server::ltm::persistence::MemoryPersistenceStrategy
Abstract memory persistence strategy (resp.
Definition: MemoryPersistenceStrategy.h:62
MemoryPersistenceStrategy.h
armarx::armem::server::ltm::persistence::ItemIdentifier
For usage if you might want to create the key using some logic defined with your strategy rather than...
Definition: MemoryPersistenceStrategy.h:19
MemoryID.h
armarx::armem::server::ltm::persistence::DiskPersistence::DiskPersistence
DiskPersistence(const std::filesystem::path &memoryParentPath)
Definition: DiskPersistence.h:54
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::persistence::DiskPersistence
Persistence strategy that writes items (e.g.
Definition: DiskPersistence.h:47
armarx::armem::server::ltm::persistence::DiskPersistence::DiskPersistence
DiskPersistence()
Definition: DiskPersistence.h:50
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
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
filename
std::string filename
Definition: VisualizationRobot.cpp:86
armarx::armem::server::ltm::persistence::DiskPersistence::setMinAvailableDiskSpace
void setMinAvailableDiskSpace(const int minDiskSpace)
Definition: DiskPersistence.h:116
armarx::armem::server::ltm::persistence::FileIdentifier::getKey
std::string getKey() override
Definition: DiskPersistence.h:26
std
Definition: Application.h:66
IceUtil::Handle< class PropertyDefinitionContainer >
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::armem::server::ltm::persistence::FileIdentifier::~FileIdentifier
virtual ~FileIdentifier()
Definition: DiskPersistence.h:21
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::armem::server::ltm::persistence::FileIdentifier
Definition: DiskPersistence.h:12
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::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