RedundantPersistenceStrategy.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4
7
9{
10
11 /**
12 * Basically the option to use multiple ltm sinks as source or target.
13 * Holds a list to different persistence strategies potentially also another
14 * redundant strategy resulting in tree structure.
15 *
16 * Why the name 'redundant'?
17 * -> Comes from the possibility to search in multiple ltm sinks for an item or store item to different
18 * sinks that are potentially at different location/different implementations.
19 * But you won't find any more redundancies features.
20 *
21 * What if an item key occurs multiple times?
22 * -> FIFO: Strategy that was added first is choosen. In case we have a tree, you go from "left to right through the graph".
23 */
25 {
26 public:
28
30 std::vector<std::shared_ptr<MemoryPersistenceStrategy>>& strategies) :
31 strategies_(strategies)
32 {
33 }
34
35 /**
36 * Returns all containers over all strategies.
37 * Duplicates are not treated.
38 */
39 std::vector<std::string>
41 {
42 std::vector<std::string> keys;
43
44 for (auto& strategy : strategies_)
45 {
46 std::vector<std::string> strategyKeys = strategy->getContainerKeys(id);
47 keys.insert(keys.end(), strategyKeys.begin(), strategyKeys.end());
48 }
49
50 return keys;
51 }
52
53 /**
54 * Returns all items over all strategies.
55 * Duplicates are not treated.
56 */
57 std::vector<std::string>
59 {
60 std::vector<std::string> keys;
61
62 for (auto& strategy : strategies_)
63 {
64 std::vector<std::string> strategyKeys = strategy->getItemKeys(id);
65 keys.insert(keys.end(), strategyKeys.begin(), strategyKeys.end());
66 }
67
68 return keys;
69 }
70
71 /**
72 * Checks if any strategy contains the container.
73 */
74 bool
75 containsContainer(const armarx::armem::MemoryID& id, std::string key) override
76 {
77 for (auto& s : strategies_)
78 {
79 if (s->containsContainer(id, key))
80 {
81 return true;
82 }
83 }
84
85 return false;
86 }
87
88 /**
89 * Checks if any strategy contains the item.
90 */
91 bool
92 containsItem(const armarx::armem::MemoryID& id, std::string key) override
93 {
94 for (auto& s : strategies_)
95 {
96 if (s->containsItem(id, key))
97 {
98 return true;
99 }
100 }
101
102 return false;
103 }
104
105 /**
106 * Stores the item to every strategy
107 */
108 void
110 const std::string key,
111 std::vector<unsigned char>& data) override
112 {
113 for (auto& s : strategies_)
114 {
115 s->storeItem(id, key, data);
116 }
117 }
118
119 /**
120 * Retrieves the data of the item following the FIFO principle (if the item exists at any strategy).
121 */
122 std::vector<unsigned char>
123 retrieveItem(const armarx::armem::MemoryID& id, std::string key) override
124 {
125 for (auto& s : strategies_)
126 {
127 if (s->containsItem(id, key))
128 {
129 return s->retrieveItem(id, key);
130 }
131 }
132
133 return std::vector<unsigned char>();
134 }
135
137 retrieveItemWithOrigin(const armarx::armem::MemoryID& id, std::string& key) override
138 {
139 for (auto& s : strategies_)
140 {
141 if (s->containsItem(id, key))
142 {
143 return s->retrieveItemWithOrigin(id, key);
144 }
145 }
146
147 return {"undefined4", std::vector<unsigned char>()};
148 }
149
150 /**
151 * Sets the export name for all strategies
152 */
153 void
154 setExportName(const std::string& exportName) override
155 {
156 exportName_ = exportName;
157
158 for (auto& s : strategies_)
159 {
160 s->setExportName(exportName);
161 }
162 }
163
164 /**
165 * Add another strategy to the end.
166 * Will be last in the item/container FIFO.
167 */
168 void
169 addStrategy(std::shared_ptr<MemoryPersistenceStrategy> strategy)
170 {
171 strategies_.emplace_back(strategy);
172 }
173
174 /**
175 * Removes all strategies
176 */
177 void
179 {
180 strategies_.clear();
181 }
182
183 void
184 createPropertyDefinitions(PropertyDefinitionsPtr& defs, const std::string& prefix) override
185 {
186 for (auto& s : strategies_)
187 {
188 s->createPropertyDefinitions(defs, prefix);
189 }
190 }
191
192 /**
193 * @brief Whether anything would actually be stored, i.e. whether at least one sink is
194 * enabled.
195 *
196 * storeItem() fans out to every strategy and each skips the write when disabled, so the
197 * composite is still doing work as long as one of them is live. A composite holding no
198 * strategies stores nothing and is reported disabled, which falls out of this on its own.
199 *
200 * This was seeded to false and combined with `&=`, so it could only ever return false --
201 * two mistakes in three lines, unnoticed because nothing calls it yet.
202 */
203 bool
204 isEnabled() override
205 {
206 return std::any_of(strategies_.begin(),
207 strategies_.end(),
208 [](const auto& s) { return s->isEnabled(); });
209 }
210
211 void
212 disable() override
213 {
214 for (auto& s : strategies_)
215 {
216 s->disable();
217 }
218 }
219
220 void
221 enable() override
222 {
223 for (auto& s : strategies_)
224 {
225 s->enable();
226 }
227 }
228
229
230 private:
231 std::vector<std::shared_ptr<MemoryPersistenceStrategy>> strategies_;
232 };
233} // namespace armarx::armem::server::ltm::persistence
std::string exportName_
Name of the specific memory export where our items should be stored.
void addStrategy(std::shared_ptr< MemoryPersistenceStrategy > strategy)
Add another strategy to the end.
RedundantPersistenceStrategy(std::vector< std::shared_ptr< MemoryPersistenceStrategy > > &strategies)
bool containsContainer(const armarx::armem::MemoryID &id, std::string key) override
Checks if any strategy contains the container.
std::vector< std::string > getItemKeys(const armarx::armem::MemoryID &id) override
Returns all items over all strategies.
ItemResult retrieveItemWithOrigin(const armarx::armem::MemoryID &id, std::string &key) override
Retrieves the actual data but also appends the origin which is in this case the strategy identifier.
bool containsItem(const armarx::armem::MemoryID &id, std::string key) override
Checks if any strategy contains the item.
std::vector< std::string > getContainerKeys(const armarx::armem::MemoryID &id) override
Returns all containers over all strategies.
void setExportName(const std::string &exportName) override
Sets the export name for all strategies.
void storeItem(const armarx::armem::MemoryID &id, const std::string key, std::vector< unsigned char > &data) override
Stores the item to every strategy.
std::vector< unsigned char > retrieveItem(const armarx::armem::MemoryID &id, std::string key) override
Retrieves the data of the item following the FIFO principle (if the item exists at any strategy).
bool isEnabled() override
Whether anything would actually be stored, i.e.
void createPropertyDefinitions(PropertyDefinitionsPtr &defs, const std::string &prefix) override
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Retrieved items' data with an origin (resp.