lintel 0.2.0
Loading...
Searching...
No Matches
SharedMemoryRepository.h
Go to the documentation of this file.
1#ifndef LINTEL_SHAREDMEMORYREPOSITORY_H
2#define LINTEL_SHAREDMEMORYREPOSITORY_H
3
5
6#include <memory>
7
8#include <boost/container/map.hpp>
9#include <boost/container/vector.hpp>
10
16
17namespace lintel {
20
25{
26private:
27 std::shared_ptr<SharedMemorySegment> m_sharedMemorySegment;
28 std::size_t m_sizeOfData;
29 std::string m_sharedMemoryRepository;
30 int32_t m_codeVersion;
31 std::string m_uuid;
32
33protected:
34 template<typename TYPE> static constexpr bool isSerializable()
35 {
36 return std::is_base_of_v<JsonSerializable, TYPE> || std::is_same_v<shm::String, TYPE> || std::is_arithmetic_v<TYPE>;
37 }
38
39public:
48 SharedMemoryRepository(std::shared_ptr<SharedMemorySegment> sharedMemorySegment,
49 std::size_t sizeOfData,
50 std::string_view sharedMemoryRepository,
52 std::string uuid);
53
55 [[nodiscard]] const std::shared_ptr<SharedMemorySegment> &getSharedMemorySegment() const;
56
58 [[nodiscard]] std::size_t getSizeOfData() const;
59
61 [[nodiscard]] const std::string &getSharedMemoryRepository() const;
62
65
67 [[nodiscard]] const std::string &getUuid() const;
68
70 [[nodiscard]] virtual SharedMemoryType getType() const = 0;
71
72 [[nodiscard]] std::string_view getTypeName() const
73 {
74 if (getType() == SharedMemoryType::Map) { return "Map"; }
75 if (getType() == SharedMemoryType::Vector) { return "Vector"; }
76 if (getType() == SharedMemoryType::Array) { return "Array"; }
77 return "Object";
78 }
79
80protected:
86 {
87 public:
88 explicit ReadLockGuard(const SharedMemoryRepository &owner) : m_owner(owner) { m_owner.onAcquireReadLock(); }
89
91
92 ReadLockGuard(const ReadLockGuard &) = delete;
94
95 private:
96 const SharedMemoryRepository &m_owner;
97 };
98
101 virtual void onAcquireReadLock() const {}
102
104 virtual void onReleaseReadLock() const {}
105
106public:
107 ~SharedMemoryRepository() override = default;
108
110};
111
112template<typename DATA, typename DAO, std::size_t MaxSize>
120{
121private:
122 mutable std::array<DATA, MaxSize> *m_array;
123 mutable std::size_t m_generation;
124
125protected:
126 std::shared_ptr<SharedMemoryService> m_sharedMemoryService;
127
129 std::array<DATA, MaxSize> &getArray() const
130 {
131 const auto generation = m_sharedMemoryService->getGeneration();
132 if (generation != m_generation) {
133 m_array = &shm::constructArray<DATA, MaxSize>(
135 m_generation = generation;
136 }
137 return *m_array;
138 }
139
140public:
141 SharedMemoryArrayRepository(const std::shared_ptr<SharedMemoryService> &sharedMemoryService,
142 const std::shared_ptr<SharedMemorySegment> &segment,
144 std::string uuid)
146 m_array(&shm::constructArray<DATA, MaxSize>(*sharedMemoryService, segment, getSharedMemoryRepository())),
147 m_generation(sharedMemoryService->getGeneration()), m_sharedMemoryService(sharedMemoryService)
148 {}
149
151
152 void serialize(rapidjson::Writer<rapidjson::StringBuffer> *writer) const override
153 {
154 ReadLockGuard guard(*this);
155 writer->StartObject();
156 writer->String("repository");
157 writer->String("SharedMemoryArrayRepository");
158 writer->String("array");
159 writer->StartArray();
160 for (const auto &iter : getArray()) {
161 LOG_TRACE("serialize vector entry");
162 if constexpr (std::is_base_of<JsonSerializable, DAO>()) {
163 DAO dao(iter);
164 dao.serialize(writer);
165 } else if constexpr (std::is_same<shm::String, DATA>()) {
166 writer->String(iter.c_str());
167 }
168 }
169 writer->EndArray();
170 writer->EndObject();
171 }
172
173 bool deserialize(const rapidjson::Value &obj) override { throw std::runtime_error("Unsupported operation"); }
174};
175
176template<typename DATA, typename DAO>
183{
184private:
185 using Vector = boost::container::vector<DATA,
186 boost::interprocess::allocator<DATA, boost::interprocess::managed_mapped_file::segment_manager>>;
187 mutable Vector *m_vector;
188 mutable std::size_t m_generation;
189
190protected:
191 std::shared_ptr<SharedMemoryService> m_sharedMemoryService;
192
193 [[nodiscard]] Vector &getVector() const
194 {
195 const auto generation = m_sharedMemoryService->getGeneration();
196 if (generation != m_generation) {
197 m_vector =
199 m_generation = generation;
200 }
201 return *m_vector;
202 }
203
204public:
205 SharedMemoryVectorRepository(const std::shared_ptr<SharedMemoryService> &sharedMemoryService,
206 const std::shared_ptr<SharedMemorySegment> &segment,
208 std::string uuid)
210 m_vector(&shm::constructVector<DATA>(*sharedMemoryService, segment, getSharedMemoryRepository())),
211 m_generation(sharedMemoryService->getGeneration()), m_sharedMemoryService(sharedMemoryService)
212 {}
213
215
216 void serialize(rapidjson::Writer<rapidjson::StringBuffer> *writer) const override
217 {
218 ReadLockGuard guard(*this);
219 writer->StartObject();
220 writer->String("repository");
221 writer->String("SharedMemoryVectorRepository");
222 writer->String("vector");
223 writer->StartArray();
224 for (const auto &iter : getVector()) {
225 LOG_TRACE("serialize vector entry");
226 if constexpr (std::is_base_of<JsonSerializable, DAO>()) {
227 DAO dao(iter);
228 dao.serialize(writer);
229 } else if constexpr (std::is_same<shm::String, DATA>()) {
230 writer->String(iter.c_str());
231 }
232 }
233 writer->EndArray();
234 writer->EndObject();
235 }
236
237 bool deserialize(const rapidjson::Value &obj) override { throw std::runtime_error("Unsupported operation"); }
238};
239
240template<typename KEY, typename VALUE, typename KeyDao, typename ValueDao>
249{
250private:
251 using Map = boost::container::map<KEY,
252 VALUE,
253 std::less<KEY>,
254 boost::interprocess::allocator<std::pair<const KEY, VALUE>,
255 boost::interprocess::managed_mapped_file::segment_manager>>;
256 mutable Map *m_map;
257 mutable std::size_t m_generation;
258
259protected:
260 std::shared_ptr<SharedMemoryService> m_sharedMemoryService;
261
262 [[nodiscard]] Map &getMap() const
263 {
264 const auto generation = m_sharedMemoryService->getGeneration();
265 if (generation != m_generation) {
266 m_map =
267 &shm::constructMap<KEY, VALUE>(*m_sharedMemoryService, getSharedMemorySegment(), getSharedMemoryRepository());
268 m_generation = generation;
269 }
270 return *m_map;
271 }
272
273public:
274 SharedMemoryMapRepository(const std::shared_ptr<SharedMemoryService> &sharedMemoryService,
275 const std::shared_ptr<SharedMemorySegment> &segment,
277 std::string uuid,
278 int32_t sizeOfData = sizeof(VALUE))
280 m_map(&shm::constructMap<KEY, VALUE>(*sharedMemoryService, segment, getSharedMemoryRepository())),
281 m_generation(sharedMemoryService->getGeneration()), m_sharedMemoryService(sharedMemoryService)
282 {
283 static_assert(isSerializable<KeyDao>());
284 static_assert(isSerializable<ValueDao>());
285 LOG_TRACE("map loaded with size of {}", getMap().size());
286 }
287
289
290 void serialize(rapidjson::Writer<rapidjson::StringBuffer> *writer) const override
291 {
292 ReadLockGuard guard(*this);
293 writer->StartObject();
294 writer->String("repository");
295 writer->String("SharedMemoryMapRepository");
296 writer->String("map");
297 writer->StartArray();
298 for (auto &iterator : getMap()) {
299 const KEY &key = iterator.first;
300 const VALUE &value = iterator.second;
301 LOG_TRACE("serialize map entry");
302 writer->StartObject();
303 writer->String("key");
304 if constexpr (std::is_base_of<JsonSerializable, KeyDao>()) {
305 KeyDao dao(key);
306 dao.serialize(writer);
307 } else if constexpr (std::is_same<shm::String, KEY>()) {
308 writer->String(key.c_str());
309 }
310 writer->String("value");
311 if constexpr (std::is_base_of<JsonSerializable, ValueDao>()) {
312 ValueDao dao(value);
313 dao.serialize(writer);
314 } else if constexpr (std::is_same<shm::String, VALUE>()) {
315 writer->String(value.c_str());
316 }
317 writer->EndObject();
318 }
319 writer->EndArray();
320 writer->EndObject();
321 }
322
323 bool deserialize(const rapidjson::Value &obj) override { throw std::runtime_error("Unsupported operation"); }
324};
325
326template<typename DATA, typename DAO>
333{
334private:
335 mutable DATA *m_data;
336 mutable std::size_t m_generation;
337
338protected:
339 std::shared_ptr<SharedMemoryService> m_sharedMemoryService;
340
341 DATA &getData() const
342 {
343 const auto generation = m_sharedMemoryService->getGeneration();
344 if (generation != m_generation) {
345 m_data =
347 m_generation = generation;
348 }
349 return *m_data;
350 }
351
352public:
353 SharedMemoryObjectRepository(const std::shared_ptr<SharedMemoryService> &sharedMemoryService,
354 const std::shared_ptr<SharedMemorySegment> &segment,
356 std::string uuid)
358 m_data(&shm::constructObject<DATA>(*sharedMemoryService, segment, getSharedMemoryRepository())),
359 m_generation(sharedMemoryService->getGeneration()), m_sharedMemoryService(sharedMemoryService)
360 {}
361
363
364 void serialize(rapidjson::Writer<rapidjson::StringBuffer> *writer) const override
365 {
366 ReadLockGuard guard(*this);
367 writer->StartObject();
368 writer->String("repository");
369 writer->String("SharedMemoryObjectRepository");
370 writer->String("object");
371 if constexpr (std::is_base_of<JsonSerializable, DAO>()) {
372 DAO dao(getData());
373 dao.serialize(writer);
374 } else if constexpr (std::is_same<shm::String, DATA>()) {
375 writer->String(getData().c_str());
376 }
377 writer->EndObject();
378 }
379
380 bool deserialize(const rapidjson::Value &obj) override { throw std::runtime_error("Unsupported operation"); }
381};
382
383}// namespace lintel
384#endif// LINTEL_SHAREDMEMORYREPOSITORY_H
#define LOG_TRACE(message,...)
Definition LoggerService.h:128
Definition JsonSerializable.h:17
Definition SharedMemoryRepository.h:120
std::shared_ptr< SharedMemoryService > m_sharedMemoryService
Definition SharedMemoryRepository.h:126
bool deserialize(const rapidjson::Value &obj) override
Definition SharedMemoryRepository.h:173
SharedMemoryType getType() const override
Definition SharedMemoryRepository.h:150
std::array< DATA, MaxSize > & getArray() const
Definition SharedMemoryRepository.h:129
SharedMemoryArrayRepository(const std::shared_ptr< SharedMemoryService > &sharedMemoryService, const std::shared_ptr< SharedMemorySegment > &segment, int32_t codeVersion, std::string uuid)
Definition SharedMemoryRepository.h:141
void serialize(rapidjson::Writer< rapidjson::StringBuffer > *writer) const override
Definition SharedMemoryRepository.h:152
Definition SharedMemoryRepository.h:249
SharedMemoryType getType() const override
Definition SharedMemoryRepository.h:288
std::shared_ptr< SharedMemoryService > m_sharedMemoryService
Definition SharedMemoryRepository.h:260
SharedMemoryMapRepository(const std::shared_ptr< SharedMemoryService > &sharedMemoryService, const std::shared_ptr< SharedMemorySegment > &segment, int32_t codeVersion, std::string uuid, int32_t sizeOfData=sizeof(VALUE))
Definition SharedMemoryRepository.h:274
Map & getMap() const
Definition SharedMemoryRepository.h:262
bool deserialize(const rapidjson::Value &obj) override
Definition SharedMemoryRepository.h:323
void serialize(rapidjson::Writer< rapidjson::StringBuffer > *writer) const override
Definition SharedMemoryRepository.h:290
Definition SharedMemoryRepository.h:333
void serialize(rapidjson::Writer< rapidjson::StringBuffer > *writer) const override
Definition SharedMemoryRepository.h:364
std::shared_ptr< SharedMemoryService > m_sharedMemoryService
Definition SharedMemoryRepository.h:339
bool deserialize(const rapidjson::Value &obj) override
Definition SharedMemoryRepository.h:380
DATA & getData() const
Definition SharedMemoryRepository.h:341
SharedMemoryObjectRepository(const std::shared_ptr< SharedMemoryService > &sharedMemoryService, const std::shared_ptr< SharedMemorySegment > &segment, int32_t codeVersion, std::string uuid)
Definition SharedMemoryRepository.h:353
SharedMemoryType getType() const override
Definition SharedMemoryRepository.h:362
Definition SharedMemoryRepository.h:86
ReadLockGuard(const ReadLockGuard &)=delete
ReadLockGuard & operator=(const ReadLockGuard &)=delete
~ReadLockGuard()
Definition SharedMemoryRepository.h:90
ReadLockGuard(const SharedMemoryRepository &owner)
Definition SharedMemoryRepository.h:88
Definition SharedMemoryRepository.h:25
const std::string & getSharedMemoryRepository() const
std::size_t getSizeOfData() const
virtual void onMigrate(int32_t currentActiveVersion)=0
const std::shared_ptr< SharedMemorySegment > & getSharedMemorySegment() const
std::string_view getTypeName() const
Definition SharedMemoryRepository.h:72
virtual void onAcquireReadLock() const
Definition SharedMemoryRepository.h:101
virtual void onReleaseReadLock() const
Definition SharedMemoryRepository.h:104
static constexpr bool isSerializable()
Definition SharedMemoryRepository.h:34
const std::string & getUuid() const
SharedMemoryRepository(std::shared_ptr< SharedMemorySegment > sharedMemorySegment, std::size_t sizeOfData, std::string_view sharedMemoryRepository, int32_t codeVersion, std::string uuid)
virtual SharedMemoryType getType() const =0
~SharedMemoryRepository() override=default
Definition SharedMemoryRepository.h:183
SharedMemoryType getType() const override
Definition SharedMemoryRepository.h:214
void serialize(rapidjson::Writer< rapidjson::StringBuffer > *writer) const override
Definition SharedMemoryRepository.h:216
Vector & getVector() const
Definition SharedMemoryRepository.h:193
SharedMemoryVectorRepository(const std::shared_ptr< SharedMemoryService > &sharedMemoryService, const std::shared_ptr< SharedMemorySegment > &segment, int32_t codeVersion, std::string uuid)
Definition SharedMemoryRepository.h:205
bool deserialize(const rapidjson::Value &obj) override
Definition SharedMemoryRepository.h:237
std::shared_ptr< SharedMemoryService > m_sharedMemoryService
Definition SharedMemoryRepository.h:191
Definition BaseFeature.h:6
SharedMemoryType
Definition SharedMemoryRepository.h:19
@ Vector
Definition SharedMemoryRepository.h:19
@ Array
Definition SharedMemoryRepository.h:19
@ Map
Definition SharedMemoryRepository.h:19
@ Object
Definition SharedMemoryRepository.h:19
constexpr std::string_view type_name()
Definition TypeName.h:9