MemoryPersistenceStrategy.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4
6
10
12{
13 /**
14 * For usage if you might want to create the key using some logic
15 * defined with your strategy rather than the caller.
16 *
17 * NOTE: Not used currently
18 */
20 {
21 public:
22 virtual ~ItemIdentifier();
23
24 virtual std::string getKey();
25 };
26
27 /**
28 * Retrieved items' data with an origin (resp. the strategy identifier).
29 * Why an origin? Used to display which LTM sink provided the entity instance.
30 */
32 {
33 std::string origin;
34 std::vector<unsigned char> data;
35 };
36
37 /**
38 * @brief Abstract memory persistence strategy (resp. LTM sink)
39 *
40 * Given a memory id, we can store items for it. A memory id is in terms of the persistence strategy an container that
41 * again might contain other containers or items with the actual data.
42 * We can also retrieve the current containers, which are a hierarchy below of the current memory id.
43 * The export name specifies our specific LTM export. You might want to write different exports with the same strategy.
44 * Nonetheless you could also create another strategy (@see RedundantPersistenceStrategy)
45 *
46 * Why do we basically have to supply the MemoryID everywhere?
47 * The first approach had an internal variable to the memory id, as it is kind of bound the memory hierarchy level and thus the id.
48 * But even though the MemoryID was stored by pass-by-value
49 * there was still somewhere and somehow and sometimes a reference to it, getting you into Teufels-Küche.
50 * Because the MemoryID is extended with finer hierarchy details, having the sideeffect, that this memory id would be changed.
51 * Also you needed a clone function, now you can just pass the strategy pointer.
52 *
53 *
54 *
55 * How are the containers created?
56 * -> Implicitly, whenever a item underneath is stored
57 *
58 * Want to create a new fancy ltm sink option? Maybe MongoDB?
59 * -> Implement this interface and add it similar to the existing strategy (e.g. DiskPersistence) at all locations.
60 * Don´t forget any ice proxies! (a powerful full text search is your friend)
61 */
63 {
64 public:
65 MemoryPersistenceStrategy() : MemoryPersistenceStrategy("Unspecified", "DefaultExport")
66 {
67 }
68
69 /**
70 * @param identifier basically a unique name for the strategy (important if you use different strategies @see RedundantPersistenceStrategy)
71 * @param exportName identifier for the exported memory where any items are written (e.g. a directory)
72 */
73 MemoryPersistenceStrategy(const std::string& identifier,
74 const std::string& exportName,
75 bool enabled = true) :
76 identifier_(identifier), exportName_(exportName), enabled_(enabled)
77 {
78 }
79
80 virtual ~MemoryPersistenceStrategy() = default;
81
82 /**
83 * Returns keys that allow use to move a step further in the hierarchy (e.g. if our memory id ends in a core segment it gives us the provider segment keys).
84 */
85 virtual std::vector<std::string> getContainerKeys(const armarx::armem::MemoryID& id) = 0;
86
87 /**
88 * Keys of the actual items containing data stored for the memory id.
89 */
90 virtual std::vector<std::string> getItemKeys(const armarx::armem::MemoryID& id) = 0;
91
92 virtual bool containsContainer(const armarx::armem::MemoryID& id, std::string key) = 0;
93
94 virtual bool containsItem(const armarx::armem::MemoryID& id, std::string key) = 0;
95
96 /**
97 * Stores an item containing actual data for the current memory id.
98 * How it is stored depends on the implementation of a concrete strategy (e.g. on disk in a specific directory).
99 *
100 * @param key unique identifier of our item for the specific id (e.g. filename).
101 * Note the keys can be equal for different id levels. So you might find the same key on the core segment level as well as on the entity level.
102 * @param data actual data written to the persistent item
103 */
104 virtual void storeItem(const armarx::armem::MemoryID& id,
105 std::string key,
106 std::vector<unsigned char>& data) = 0;
107
108 // If you need a more powerful identification than a string as key (nonetheless the end will probably also again be a string)
109 void
111 ItemIdentifier itemIdentifer,
112 std::vector<unsigned char>& data)
113 {
114 storeItem(memoryId, itemIdentifer.getKey(), data);
115 }
116
117 /**
118 * Retrieve the actual data of an item stored for the memory id.
119 *
120 * @return stored data
121 */
122 virtual std::vector<unsigned char> retrieveItem(const armarx::armem::MemoryID& id,
123 std::string key) = 0;
124
125 /**
126 * Retrieves the actual data but also appends the origin which is in this case the strategy identifier
127 */
128 virtual ItemResult
130 {
131 ItemResult res;
132 res.origin = identifier_;
133 res.data = retrieveItem(id, key);
134 return res;
135 }
136
137 std::vector<unsigned char>
138 retrieveItem(const armarx::armem::MemoryID& memoryId, ItemIdentifier itemIdentifier)
139 {
140 return retrieveItem(memoryId, itemIdentifier.getKey());
141 }
142
143 virtual void
144 setExportName(const std::string& exportName)
145 {
146 exportName_ = exportName;
147 }
148
149 std::string
151 {
152 return exportName_;
153 }
154
155 std::string
157 {
158 return identifier_;
159 }
160
161 void
162 setIdentifier(const std::string& identifier)
163 {
164 identifier_ = identifier;
165 }
166
167 virtual bool
169 {
170 return enabled_;
171 }
172
173 virtual void
175 {
176 enabled_ = false;
177 }
178
179 virtual void
181 {
182 enabled_ = true;
183 }
184
186 const std::string& prefix) = 0;
187
188
189 static const int DEPTH_TO_DATA_FILES = 7;
190
191 static const constexpr char* TYPE_FILENAME = "type.aron";
192 static const constexpr char* DATA_FILENAME = "data.aron";
193 static const constexpr char* METADATA_FILENAME = "metadata.aron";
194
195 static const constexpr char* MEMORY_EXPORT_SUFFIX = "_";
196
197 protected:
198 /**
199 * Name of the strategy. Should be unique to avoid ambiguities.
200 */
201 std::string identifier_;
202
203 /**
204 * Name of the specific memory export where our items should be stored.
205 * If you change the export name you will probably write the data to another target (e.g. different directory).
206 */
207 std::string exportName_;
208
209 /**
210 * If false, the strategy is not writing or reading anything.
211 * Only default values are returned if necessary.
212 */
213 bool enabled_ = true;
214 };
215} // namespace armarx::armem::server::ltm::persistence
For usage if you might want to create the key using some logic defined with your strategy rather than...
virtual void createPropertyDefinitions(PropertyDefinitionsPtr &defs, const std::string &prefix)=0
void storeItem(const armarx::armem::MemoryID &memoryId, ItemIdentifier itemIdentifer, std::vector< unsigned char > &data)
virtual void storeItem(const armarx::armem::MemoryID &id, std::string key, std::vector< unsigned char > &data)=0
Stores an item containing actual data for the current memory id.
virtual std::vector< unsigned char > retrieveItem(const armarx::armem::MemoryID &id, std::string key)=0
Retrieve the actual data of an item stored for the memory id.
virtual bool containsItem(const armarx::armem::MemoryID &id, std::string key)=0
virtual bool containsContainer(const armarx::armem::MemoryID &id, std::string key)=0
std::vector< unsigned char > retrieveItem(const armarx::armem::MemoryID &memoryId, ItemIdentifier itemIdentifier)
virtual ItemResult retrieveItemWithOrigin(const armarx::armem::MemoryID &id, std::string &key)
Retrieves the actual data but also appends the origin which is in this case the strategy identifier.
MemoryPersistenceStrategy(const std::string &identifier, const std::string &exportName, bool enabled=true)
bool enabled_
If false, the strategy is not writing or reading anything.
std::string exportName_
Name of the specific memory export where our items should be stored.
virtual std::vector< std::string > getItemKeys(const armarx::armem::MemoryID &id)=0
Keys of the actual items containing data stored for the memory id.
virtual std::vector< std::string > getContainerKeys(const armarx::armem::MemoryID &id)=0
Returns keys that allow use to move a step further in the hierarchy (e.g.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Retrieved items' data with an origin (resp.