RedundantPersistenceStrategy.h
Go to the documentation of this file.
1#pragma once
2
5
7{
8
9 /**
10 * Basically the option to use multiple ltm sinks as source or target.
11 * Holds a list to different persistence strategies potentially also another
12 * redundant strategy resulting in tree structure.
13 *
14 * Why the name 'redundant'?
15 * -> Comes from the possibility to search in multiple ltm sinks for an item or store item to different
16 * sinks that are potentially at different location/different implementations.
17 * But you won't find any more redundancies features.
18 *
19 * What if an item key occurs multiple times?
20 * -> FIFO: Strategy that was added first is choosen. In case we have a tree, you go from "left to right through the graph".
21 */
23 {
24 public:
26
28 std::vector<std::shared_ptr<MemoryPersistenceStrategy>>& strategies) :
29 strategies_(strategies)
30 {
31 }
32
33 /**
34 * Returns all containers over all strategies.
35 * Duplicates are not treated.
36 */
37 std::vector<std::string>
39 {
40 std::vector<std::string> keys;
41
42 for (auto& strategy : strategies_)
43 {
44 std::vector<std::string> strategyKeys = strategy->getContainerKeys(id);
45 keys.insert(keys.end(), strategyKeys.begin(), strategyKeys.end());
46 }
47
48 return keys;
49 }
50
51 /**
52 * Returns all items over all strategies.
53 * Duplicates are not treated.
54 */
55 std::vector<std::string>
57 {
58 std::vector<std::string> keys;
59
60 for (auto& strategy : strategies_)
61 {
62 std::vector<std::string> strategyKeys = strategy->getItemKeys(id);
63 keys.insert(keys.end(), strategyKeys.begin(), strategyKeys.end());
64 }
65
66 return keys;
67 }
68
69 /**
70 * Checks if any strategy contains the container.
71 */
72 bool
73 containsContainer(const armarx::armem::MemoryID& id, std::string key) override
74 {
75 for (auto& s : strategies_)
76 {
77 if (s->containsContainer(id, key))
78 {
79 return true;
80 }
81 }
82
83 return false;
84 }
85
86 /**
87 * Checks if any strategy contains the item.
88 */
89 bool
90 containsItem(const armarx::armem::MemoryID& id, std::string key) override
91 {
92 for (auto& s : strategies_)
93 {
94 if (s->containsItem(id, key))
95 {
96 return true;
97 }
98 }
99
100 return false;
101 }
102
103 /**
104 * Stores the item to every strategy
105 */
106 void
108 const std::string key,
109 std::vector<unsigned char>& data) override
110 {
111 for (auto& s : strategies_)
112 {
113 s->storeItem(id, key, data);
114 }
115 }
116
117 /**
118 * Retrieves the data of the item following the FIFO principle (if the item exists at any strategy).
119 */
120 std::vector<unsigned char>
121 retrieveItem(const armarx::armem::MemoryID& id, std::string key) override
122 {
123 for (auto& s : strategies_)
124 {
125 if (s->containsItem(id, key))
126 {
127 return s->retrieveItem(id, key);
128 }
129 }
130
131 return std::vector<unsigned char>();
132 }
133
135 retrieveItemWithOrigin(const armarx::armem::MemoryID& id, std::string& key) override
136 {
137 for (auto& s : strategies_)
138 {
139 if (s->containsItem(id, key))
140 {
141 return s->retrieveItemWithOrigin(id, key);
142 }
143 }
144
145 return {"undefined4", std::vector<unsigned char>()};
146 }
147
148 /**
149 * Sets the export name for all strategies
150 */
151 void
152 setExportName(const std::string& exportName) override
153 {
154 exportName_ = exportName;
155
156 for (auto& s : strategies_)
157 {
158 s->setExportName(exportName);
159 }
160 }
161
162 /**
163 * Add another strategy to the end.
164 * Will be last in the item/container FIFO.
165 */
166 void
167 addStrategy(std::shared_ptr<MemoryPersistenceStrategy> strategy)
168 {
169 strategies_.emplace_back(strategy);
170 }
171
172 /**
173 * Removes all strategies
174 */
175 void
177 {
178 strategies_.clear();
179 }
180
181 void
182 createPropertyDefinitions(PropertyDefinitionsPtr& defs, const std::string& prefix) override
183 {
184 for (auto& s : strategies_)
185 {
186 s->createPropertyDefinitions(defs, prefix);
187 }
188 }
189
190 bool
191 isEnabled() override
192 {
193 bool enabled = false;
194
195 for (auto& s : strategies_)
196 {
197 enabled &= s->isEnabled();
198 }
199
200 return enabled;
201 }
202
203 void
204 disable() override
205 {
206 for (auto& s : strategies_)
207 {
208 s->disable();
209 }
210 }
211
212 void
213 enable() override
214 {
215 for (auto& s : strategies_)
216 {
217 s->enable();
218 }
219 }
220
221
222 private:
223 std::vector<std::shared_ptr<MemoryPersistenceStrategy>> strategies_;
224 };
225} // 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).
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.