rad 6.2.0
Loading...
Searching...
No Matches
oldbAdapter.hpp
Go to the documentation of this file.
1
9#ifndef RAD_MAL_OLDB_ADAPTER_HPP
10#define RAD_MAL_OLDB_ADAPTER_HPP
11
12#include <rad/logger.hpp>
13#include <rad/exceptions.hpp>
14#include <rad/assert.hpp>
15
16#include <ciiOldbFactory.hpp>
17#include <ciiOldbDpValue.hpp>
18#include <ciiOldbExceptions.hpp>
19#include <ciiOldbUtil.hpp>
20
21#include <string>
22#include <map>
23#include <typeinfo>
24#include <chrono>
25
26namespace rad {
27namespace cii {
28
29
45public:
53
61 explicit OldbAdapter(const std::chrono::seconds conn_timeout);
62
66 OldbAdapter(const OldbAdapter&) = default;
67
71 virtual ~OldbAdapter();
72
76 OldbAdapter& operator=(const OldbAdapter&) = default;
77
81 std::shared_ptr<elt::oldb::CiiOldb> GetOldbInstance();
82
87 void SetOldbInstance(std::shared_ptr<elt::oldb::CiiOldb> oldb);
88
94 void ConfigureConnTimeout(const std::chrono::seconds conn_timeout);
95
106 virtual void Connect();
107
111 void ClearDataPoints();
112
117 std::shared_ptr<elt::oldb::CiiOldbTypedDataBase> GetDataPoint(const std::string& key);
118
125 template<typename T>
126 void Get(const std::string& key, T& value);
127
141 template<typename T>
142 std::shared_ptr<elt::oldb::CiiOldbDpValue<T>> Get(const std::string& key);
143
151 template<typename T>
152 bool TryGet(const std::string& key, T& value) noexcept;
153
161 template<typename T>
162 std::shared_ptr<elt::oldb::CiiOldbDpValue<T>> TryGet(const std::string& key) noexcept;
163
170 template<typename T>
171 void Get(const std::string& key, std::vector<T>& value);
172
180 template<typename T>
181 std::shared_ptr<elt::oldb::CiiOldbDpValue<std::vector<T>>> GetVector(const std::string& key);
182
183
191 template<typename T>
192 bool TryGet(const std::string& key, std::vector<T>& value) noexcept;
193
201 template<typename T>
202 std::shared_ptr<elt::oldb::CiiOldbDpValue<std::vector<T>>> TryGetVector(const std::string& key) noexcept;
203
204
211 template<typename T>
212 void Set(const std::string& key,
213 const T& value,
214 const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
215
223 template<typename T>
224 bool TrySet(const std::string& key,
225 const T& value,
226 const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now()) noexcept;
227
234 template<typename T>
235 void Set(const std::string& key,
236 const std::vector<T>& value,
237 const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
238
246 template<typename T>
247 bool TrySet(const std::string& key,
248 const std::vector<T>& value,
249 const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now()) noexcept;
250
257 template<typename T>
258 void Set(const std::string& key,
259 elt::mal::shared_vector<const T>& value,
260 const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
261
269 template<typename T>
270 bool TrySet(const std::string& key,
271 elt::mal::shared_vector<const T>& value,
272 const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now()) noexcept;
273
286 template<typename T>
287 void Set(const std::string& key,
288 const T& value,
289 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp,
290 const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
291
304 template<typename T>
305 void Set(const std::string& key,
306 const std::vector<T>& values,
307 std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp,
308 const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
309
322 template<typename T>
323 void Set(const std::string& key,
324 elt::mal::shared_vector<const T>& values,
325 std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp,
326 const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
327
336 void Del(const std::string& key);
337
338private:
339 std::shared_ptr<elt::oldb::CiiOldb> m_oldb;
340 std::map<std::string, std::shared_ptr<elt::oldb::CiiOldbTypedDataBase>> m_data_points;
341
354 template<typename T>
355 std::shared_ptr<elt::oldb::CiiOldbDpValue<T>> GetInternal(const std::string& key,
356 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp);
357
370 template<typename T>
371 std::shared_ptr<elt::oldb::CiiOldbDpValue<std::vector<T>>> GetInternal(const std::string& key,
372 std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp);
373};
374
375
376template<typename T>
377void OldbAdapter::Get(const std::string& key, T& value) {
379
380 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>> dp = nullptr;
381 auto it = m_data_points.find(key);
382 if (it != m_data_points.end()) {
383 dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<T>>(it->second);
384 }
385
386 auto triplet = OldbAdapter::GetInternal(key, dp);
387 if (triplet) {
388 value = triplet->GetValue();
389 }
390
391 if (it == m_data_points.end() && dp) {
392 m_data_points[key] = dp;
393 }
394}
395
396
397template<typename T>
398std::shared_ptr<elt::oldb::CiiOldbDpValue<T>> OldbAdapter::Get(const std::string& key) {
400
401 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>> dp = nullptr;
402 auto it = m_data_points.find(key);
403 if (it != m_data_points.end()) {
404 dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<T>>(it->second);
405 }
406
407 auto triplet = OldbAdapter::GetInternal(key, dp);
408
409 if (it == m_data_points.end() && dp) {
410 m_data_points[key] = dp;
411 }
412
413 return triplet;
414}
415
416
417template<typename T>
418bool OldbAdapter::TryGet(const std::string& key, T& value) noexcept {
420
421 try {
422 Get<T>(key, value);
423 return true;
424 } catch (const std::exception& e) {
425 LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of " << e.what());
426 return false;
427 } catch (...) {
428 LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of unknown exception.");
429 return false;
430 }
431}
432
433
434template<typename T>
435std::shared_ptr<elt::oldb::CiiOldbDpValue<T>> OldbAdapter::TryGet(const std::string& key) noexcept {
437
438 try {
439 return Get<T>(key);
440 } catch (const std::exception& e) {
441 LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of " << e.what());
442 return nullptr;
443 } catch (...) {
444 LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of unknown exception.");
445 return nullptr;
446 }
447}
448
449
450template<typename T>
451void OldbAdapter::Get(const std::string& key, std::vector<T>& value) {
453
454 std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>> dp = nullptr;
455 auto it = m_data_points.find(key);
456 if (it != m_data_points.end()) {
457 dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<std::vector<T>>>(it->second);
458 }
459 auto triplet = OldbAdapter::GetInternal(key, dp);
460 if (triplet) {
461 value = triplet->GetValue();
462 }
463 if (it == m_data_points.end() && dp) {
464 m_data_points[key] = dp;
465 }
466}
467
468template<typename T>
469std::shared_ptr<elt::oldb::CiiOldbDpValue<std::vector<T>>> OldbAdapter::GetVector(const std::string& key) {
471
472 std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>> dp = nullptr;
473 auto it = m_data_points.find(key);
474 if (it != m_data_points.end()) {
475 dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<std::vector<T>>>(it->second);
476 }
477
478 auto triplet = OldbAdapter::GetInternal(key, dp);
479
480 if (it == m_data_points.end() && dp) {
481 m_data_points[key] = dp;
482 }
483
484 return triplet;
485}
486
487
488template<typename T>
489bool OldbAdapter::TryGet(const std::string& key, std::vector<T>& value) noexcept {
491
492 try {
493 Get<T>(key, value);
494 return true;
495 } catch (const std::exception& e) {
496 LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of " << e.what());
497 return false;
498 } catch (...) {
499 LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of unknown exception.");
500 return false;
501 }
502}
503
504template<typename T>
505std::shared_ptr<elt::oldb::CiiOldbDpValue<std::vector<T>>> OldbAdapter::TryGetVector(const std::string& key) noexcept {
507
508 try {
509 return GetVector<T>(key);
510 } catch (const std::exception& e) {
511 LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of " << e.what());
512 return false;
513 } catch (...) {
514 LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of unknown exception.");
515 return false;
516 }
517}
518
519
520template<typename T>
521void OldbAdapter::Set(const std::string& key, const T& value, const std::int64_t timestamp) {
523
524 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>> dp = nullptr;
525 auto it = m_data_points.find(key);
526 if (it != m_data_points.end()) {
527 dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<T>>(it->second);
528 }
529 Set<T>(key, value, dp, timestamp);
530 if (it == m_data_points.end() && dp) {
531 m_data_points[key] = dp;
532 }
533}
534
535template<typename T>
536bool OldbAdapter::TrySet(const std::string& key, const T& value,
537 const std::int64_t timestamp) noexcept {
539
540 try {
541 Set<T>(key, value, timestamp);
542 return true;
543 } catch (const std::exception& e) {
544 LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of " << e.what());
545 return false;
546 } catch (...) {
547 LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of unknown exception.");
548 return false;
549 }
550}
551
552template<typename T>
553void OldbAdapter::Set(const std::string& key,
554 const std::vector<T>& value,
555 const std::int64_t timestamp) {
557
558 std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>> dp = nullptr;
559 auto it = m_data_points.find(key);
560 if (it != m_data_points.end()) {
561 dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<std::vector<T>>>(it->second);
562 }
563 Set<T>(key, value, dp, timestamp);
564 if (it == m_data_points.end() && dp) {
565 m_data_points[key] = dp;
566 }
567}
568
569template<typename T>
570bool OldbAdapter::TrySet(const std::string& key,
571 const std::vector<T>& value,
572 const std::int64_t timestamp) noexcept {
574
575 try {
576 Set<T>(key, value, timestamp);
577 return true;
578 } catch (const std::exception& e) {
579 LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of " << e.what());
580 return false;
581 } catch (...) {
582 LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of unknown exception.");
583 return false;
584 }
585}
586
587template<typename T>
588void OldbAdapter::Set(const std::string& key,
589 elt::mal::shared_vector<const T>& value,
590 const std::int64_t timestamp) {
591 std::vector<T> vec(value.begin(), value.end());
592 Set<T>(key, vec, timestamp);
593}
594
595template<typename T>
596bool OldbAdapter::TrySet(const std::string& key,
597 elt::mal::shared_vector<const T>& value,
598 const std::int64_t timestamp) noexcept {
599
600 try {
601 std::vector<T> vec(value.begin(), value.end());
602 Set<T>(key, vec, timestamp);
603 return true;
604 } catch (const std::exception& e) {
605 LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of " << e.what());
606 return false;
607 } catch (...) {
608 LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of unknown exception.");
609 return false;
610 }
611}
612
613template<typename T>
614std::shared_ptr<elt::oldb::CiiOldbDpValue<T>> OldbAdapter::GetInternal(const std::string& key,
615 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp) {
616
618
619 if (m_oldb == nullptr) {
620 this->Connect();
621 if (m_oldb == nullptr) {
622 return nullptr;
623 }
624 }
625
626 try {
627 if (dp == nullptr) {
628 elt::mal::Uri key_uri(key);
629
630 if (m_oldb->DataPointExists(key_uri) == false) {
631 LOG4CPLUS_DEBUG(GetLogger(), "Data point not existing " << key_uri);
632 return nullptr;
633 }
634
635 dp = m_oldb->GetDataPoint<T>(key_uri);
636 if (dp == nullptr) {
637 LOG4CPLUS_DEBUG(GetLogger(), "Data point not existing " << key_uri);
638 return nullptr;
639 }
640 }
641
642 return dp->ReadValue();
643 } catch (const std::exception& e) {
644 RAD_RETHROW(rad::Exception, e, e.what());
645 }
646}
647
648template<typename T>
649std::shared_ptr<elt::oldb::CiiOldbDpValue<std::vector<T>>> OldbAdapter::GetInternal(const std::string& key,
650 std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp) {
651
653
654 if (m_oldb == nullptr) {
655 this->Connect();
656 if (m_oldb == nullptr) {
657 return nullptr;
658 }
659 }
660
661 try {
662 if (dp == nullptr) {
663 elt::mal::Uri key_uri(key);
664
665 if (m_oldb->DataPointExists(key_uri) == false) {
666 LOG4CPLUS_DEBUG(GetLogger(), "Data point not existing " << key_uri);
667 return nullptr;
668 }
669
670 dp = m_oldb->GetDataPoint<std::vector<T>>(key_uri);
671 if (dp == nullptr) {
672 LOG4CPLUS_DEBUG(GetLogger(), "Data point not existing " << key_uri);
673 return nullptr;
674 }
675 }
676
677 return dp->ReadValue();
678 } catch (const std::exception& e) {
679 RAD_RETHROW(rad::Exception, e, e.what());
680 }
681}
682
683
684template<typename T>
685void OldbAdapter::Set(const std::string& key,
686 const T& value,
687 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp,
688 const std::int64_t timestamp) {
690
691 if (m_oldb == nullptr) {
692 this->Connect();
693 if (m_oldb == nullptr) return;
694 }
695
696 try {
697 if (dp == nullptr) {
698 // Try to create the attribute.
699 elt::mal::Uri key_uri(key);
700 if (m_oldb->DataPointExists(key_uri) == false) {
701 // TODO how to we pass timestamp?
702 dp = m_oldb->CreateDataPointByValue<T>(key_uri, value);
703 } else {
704 dp = m_oldb->GetDataPoint<T>(key_uri);
705 if (dp) {
706 dp->WriteValue(value, timestamp);
707 } else {
708 // do we log a warning or not?
709 LOG4CPLUS_DEBUG(GetLogger(), "Cannot get data point for key: " << key_uri);
710 }
711 }
712 } else {
713 // Try to write
714 dp->WriteValue(value, timestamp);
715 }
716 } catch (const std::exception& e) {
717 RAD_RETHROW(rad::Exception, e, e.what());
718 }
719}
720
721template<typename T>
722void OldbAdapter::Set(const std::string& key,
723 const std::vector<T>& values,
724 std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp,
725 const std::int64_t timestamp) {
727
728 if (m_oldb == nullptr) {
729 Connect();
730 if (m_oldb == nullptr) return;
731 }
732
733 try {
734 if (dp == nullptr) {
735 // Try to create the vector.
736 elt::mal::Uri key_uri(key);
737
738 if (m_oldb->DataPointExists(key_uri) == false) {
739 dp = m_oldb->CreateDataPointByValue<std::vector<T>>(key_uri, values);
740 } else {
741 // point already exists
742 dp = m_oldb->GetDataPoint<std::vector<T>>(key_uri);
743 if (dp) {
744 dp->WriteValue(values, timestamp);
745 } else {
746 LOG4CPLUS_DEBUG(GetLogger(), "Cannot get data point for key: " << key_uri);
747 }
748 }
749 } else {
750 // Try to write
751 dp->WriteValue(values, timestamp);
752 }
753 } catch (const std::exception& e) {
754 RAD_RETHROW(rad::Exception, e, e.what());
755 }
756}
757
758template<typename T>
759void OldbAdapter::Set(const std::string& key,
760 elt::mal::shared_vector<const T>& values,
761 std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp,
762 const std::int64_t timestamp) {
763 std::vector<T> vec(values.begin(), values.end());
764 Set(key, vec, dp, timestamp);
765}
766
767} // namespace cii
768} // namespace rad
769
770#endif // RAD_MAL_OLDB_ADAPTER_HPP
Assert header file.
Base class for the exceptions thrown by RAD and its users.
Definition exceptions.hpp:53
Definition oldbAdapter.hpp:44
OldbAdapter()
Definition oldbAdapter.cpp:18
bool TrySet(const std::string &key, const T &value, const std::int64_t timestamp=elt::oldb::CiiOldbUtil::Now()) noexcept
Definition oldbAdapter.hpp:536
std::shared_ptr< elt::oldb::CiiOldb > GetOldbInstance()
Definition oldbAdapter.cpp:42
bool TryGet(const std::string &key, T &value) noexcept
Definition oldbAdapter.hpp:418
OldbAdapter(const OldbAdapter &)=default
void ClearDataPoints()
Definition oldbAdapter.cpp:67
void Set(const std::string &key, const T &value, const std::int64_t timestamp=elt::oldb::CiiOldbUtil::Now())
Definition oldbAdapter.hpp:521
std::shared_ptr< elt::oldb::CiiOldbDpValue< std::vector< T > > > GetVector(const std::string &key)
Definition oldbAdapter.hpp:469
void SetOldbInstance(std::shared_ptr< elt::oldb::CiiOldb > oldb)
Definition oldbAdapter.cpp:47
std::shared_ptr< elt::oldb::CiiOldbTypedDataBase > GetDataPoint(const std::string &key)
Definition oldbAdapter.cpp:73
virtual ~OldbAdapter()
Definition oldbAdapter.cpp:30
OldbAdapter & operator=(const OldbAdapter &)=default
void ConfigureConnTimeout(const std::chrono::seconds conn_timeout)
Definition oldbAdapter.cpp:34
void Get(const std::string &key, T &value)
Definition oldbAdapter.hpp:377
virtual void Connect()
Definition oldbAdapter.cpp:52
void Del(const std::string &key)
Definition oldbAdapter.cpp:86
std::shared_ptr< elt::oldb::CiiOldbDpValue< std::vector< T > > > TryGetVector(const std::string &key) noexcept
Definition oldbAdapter.hpp:505
Logger class.
#define RAD_TRACE(logger)
Definition logger.hpp:21
Exception classes header file.
#define RAD_RETHROW(exceptionType_t, nested_exception, msg)
Throw exception with information about the throw location.
Definition exceptions.hpp:345
Definition actionsApp.cpp:23
log4cplus::Logger & GetLogger()
Definition logger.cpp:72
Definition errors.hpp:58