rad  5.1.0
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 
26 namespace rad {
27 namespace cii {
28 
29 
44 class OldbAdapter {
45 public:
52  OldbAdapter();
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  [[deprecated("Should be used only internally, but then use new method GetInternal instead.")]]
288  void Get(const std::string& key,
289  T& value,
290  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp);
291 
304  template<typename T>
305  [[deprecated("Should be used only internally, but then use new method GetInternal instead.")]]
306  void Get(const std::string& key,
307  std::vector<T>& values,
308  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp);
309 
322  template<typename T>
323  void Set(const std::string& key,
324  const T& value,
325  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp,
326  const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
327 
341  template<typename T>
342  void Set(const std::string& key,
343  const std::vector<T>& values,
344  const bool is_matrix,
345  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp,
346  const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
347 
361  template<typename T>
362  void Set(const std::string& key,
363  elt::mal::shared_vector<const T>& values,
364  const bool is_matrix,
365  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp,
366  const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
367 
376  void Del(const std::string& key);
377 
378 private:
379  std::shared_ptr<elt::oldb::CiiOldb> m_oldb;
380  std::map<std::string, std::shared_ptr<elt::oldb::CiiOldbTypedDataBase>> m_data_points;
381 
382  template<typename T>
383  std::shared_ptr<elt::oldb::CiiOldbDpValue<T>> GetInternal(const std::string& key,
384  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp);
385 
386  template<typename T>
387  std::shared_ptr<elt::oldb::CiiOldbDpValue<std::vector<T>>> GetInternal(const std::string& key,
388  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp);
389 };
390 
391 
392 template<typename T>
393 void OldbAdapter::Get(const std::string& key, T& value) {
394  RAD_TRACE(GetLogger());
395 
396  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>> dp = nullptr;
397  auto it = m_data_points.find(key);
398  if (it != m_data_points.end()) {
399  dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<T>>(it->second);
400  }
401 
402  auto triplet = OldbAdapter::GetInternal(key, dp);
403  if (triplet) {
404  value = triplet->GetValue();
405  }
406 
407  if (it == m_data_points.end() && dp) {
408  m_data_points[key] = dp;
409  }
410 }
411 
412 
413 template<typename T>
414 std::shared_ptr<elt::oldb::CiiOldbDpValue<T>> OldbAdapter::Get(const std::string& key) {
415  RAD_TRACE(GetLogger());
416 
417  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>> dp = nullptr;
418  auto it = m_data_points.find(key);
419  if (it != m_data_points.end()) {
420  dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<T>>(it->second);
421  }
422 
423  auto triplet = OldbAdapter::GetInternal(key, dp);
424 
425  if (it == m_data_points.end() && dp) {
426  m_data_points[key] = dp;
427  }
428 
429  return triplet;
430 }
431 
432 
433 template<typename T>
434 bool OldbAdapter::TryGet(const std::string& key, T& value) noexcept {
435  RAD_TRACE(GetLogger());
436 
437  try {
438  Get<T>(key, value);
439  return true;
440  } catch (const std::exception& e) {
441  LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of " << e.what());
442  return false;
443  } catch (...) {
444  LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of unknown exception.");
445  return false;
446  }
447 }
448 
449 
450 template<typename T>
451 std::shared_ptr<elt::oldb::CiiOldbDpValue<T>> OldbAdapter::TryGet(const std::string& key) noexcept {
452  RAD_TRACE(GetLogger());
453 
454  try {
455  return Get<T>(key);
456  } catch (const std::exception& e) {
457  LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of " << e.what());
458  return nullptr;
459  } catch (...) {
460  LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of unknown exception.");
461  return nullptr;
462  }
463 }
464 
465 
466 template<typename T>
467 void OldbAdapter::Get(const std::string& key, std::vector<T>& value) {
468  RAD_TRACE(GetLogger());
469 
470  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>> dp = nullptr;
471  auto it = m_data_points.find(key);
472  if (it != m_data_points.end()) {
473  dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<std::vector<T>>>(it->second);
474  }
475  auto triplet = OldbAdapter::GetInternal(key, dp);
476  if (triplet) {
477  value = triplet->GetValue();
478  }
479  if (it == m_data_points.end() && dp) {
480  m_data_points[key] = dp;
481  }
482 }
483 
484 template<typename T>
485 std::shared_ptr<elt::oldb::CiiOldbDpValue<std::vector<T>>> OldbAdapter::GetVector(const std::string& key) {
486  RAD_TRACE(GetLogger());
487 
488  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>> dp = nullptr;
489  auto it = m_data_points.find(key);
490  if (it != m_data_points.end()) {
491  dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<std::vector<T>>>(it->second);
492  }
493 
494  auto triplet = OldbAdapter::GetInternal(key, dp);
495 
496  if (it == m_data_points.end() && dp) {
497  m_data_points[key] = dp;
498  }
499 
500  return triplet;
501 }
502 
503 
504 template<typename T>
505 bool OldbAdapter::TryGet(const std::string& key, std::vector<T>& value) noexcept {
506  RAD_TRACE(GetLogger());
507 
508  try {
509  Get<T>(key, value);
510  return true;
511  } catch (const std::exception& e) {
512  LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of " << e.what());
513  return false;
514  } catch (...) {
515  LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of unknown exception.");
516  return false;
517  }
518 }
519 
520 template<typename T>
521 std::shared_ptr<elt::oldb::CiiOldbDpValue<std::vector<T>>> OldbAdapter::TryGetVector(const std::string& key) noexcept {
522  RAD_TRACE(GetLogger());
523 
524  try {
525  return GetVector<T>(key);
526  } catch (const std::exception& e) {
527  LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of " << e.what());
528  return false;
529  } catch (...) {
530  LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of unknown exception.");
531  return false;
532  }
533 }
534 
535 
536 template<typename T>
537 void OldbAdapter::Set(const std::string& key, const T& value, const std::int64_t timestamp) {
538  RAD_TRACE(GetLogger());
539 
540  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>> dp = nullptr;
541  auto it = m_data_points.find(key);
542  if (it != m_data_points.end()) {
543  dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<T>>(it->second);
544  }
545  Set<T>(key, value, dp, timestamp);
546  if (it == m_data_points.end() && dp) {
547  m_data_points[key] = dp;
548  }
549 }
550 
551 template<typename T>
552 bool OldbAdapter::TrySet(const std::string& key, const T& value,
553  const std::int64_t timestamp) noexcept {
554  RAD_TRACE(GetLogger());
555 
556  try {
557  Set<T>(key, value, timestamp);
558  return true;
559  } catch (const std::exception& e) {
560  LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of " << e.what());
561  return false;
562  } catch (...) {
563  LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of unknown exception.");
564  return false;
565  }
566 }
567 
568 template<typename T>
569 void OldbAdapter::Set(const std::string& key,
570  const std::vector<T>& value,
571  const std::int64_t timestamp) {
572  RAD_TRACE(GetLogger());
573 
574  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>> dp = nullptr;
575  auto it = m_data_points.find(key);
576  if (it != m_data_points.end()) {
577  dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<std::vector<T>>>(it->second);
578  }
579  Set<T>(key, value, false, dp, timestamp);
580  if (it == m_data_points.end() && dp) {
581  m_data_points[key] = dp;
582  }
583 }
584 
585 template<typename T>
586 bool OldbAdapter::TrySet(const std::string& key,
587  const std::vector<T>& value,
588  const std::int64_t timestamp) noexcept {
589  RAD_TRACE(GetLogger());
590 
591  try {
592  Set<T>(key, value, timestamp);
593  return true;
594  } catch (const std::exception& e) {
595  LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of " << e.what());
596  return false;
597  } catch (...) {
598  LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of unknown exception.");
599  return false;
600  }
601 }
602 
603 template<typename T>
604 void OldbAdapter::Set(const std::string& key,
605  elt::mal::shared_vector<const T>& value,
606  const std::int64_t timestamp) {
607  std::vector<T> vec(value.begin(), value.end());
608  Set<T>(key, vec, timestamp);
609 }
610 
611 template<typename T>
612 bool OldbAdapter::TrySet(const std::string& key,
613  elt::mal::shared_vector<const T>& value,
614  const std::int64_t timestamp) noexcept {
615 
616  try {
617  std::vector<T> vec(value.begin(), value.end());
618  Set<T>(key, vec, timestamp);
619  return true;
620  } catch (const std::exception& e) {
621  LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of " << e.what());
622  return false;
623  } catch (...) {
624  LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of unknown exception.");
625  return false;
626  }
627 }
628 
629 template<typename T>
630 void OldbAdapter::Get(const std::string& key,
631  T& value,
632  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp) {
633 
634  auto triplet = OldbAdapter::GetInternal(key, dp);
635  if (triplet) {
636  value = triplet->GetValue();
637  }
638 }
639 
640 
641 template<typename T>
642 std::shared_ptr<elt::oldb::CiiOldbDpValue<T>> OldbAdapter::GetInternal(const std::string& key,
643  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp) {
644 
645  RAD_TRACE(GetLogger());
646 
647  if (m_oldb == nullptr) {
648  this->Connect();
649  if (m_oldb == nullptr) {
650  return nullptr;
651  }
652  }
653 
654  try {
655  if (dp == nullptr) {
656  elt::mal::Uri key_uri(key);
657 
658  if (m_oldb->DataPointExists(key_uri) == false) {
659  LOG4CPLUS_DEBUG(GetLogger(), "Data point not existing " << key_uri);
660  return nullptr;
661  }
662 
663  dp = m_oldb->GetDataPoint<T>(key_uri);
664  if (dp == nullptr) {
665  LOG4CPLUS_DEBUG(GetLogger(), "Data point not existing " << key_uri);
666  return nullptr;
667  }
668  }
669 
670  return dp->ReadValue();
671  } catch (const std::exception& e) {
673  }
674 }
675 
676 template<typename T>
677 void OldbAdapter::Get(const std::string& key,
678  std::vector<T>& values,
679  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp) {
680  auto triplet = OldbAdapter::GetInternal(key, dp);
681  if (triplet) {
682  values = triplet->GetValue();
683  }
684 }
685 
686 template<typename T>
687 std::shared_ptr<elt::oldb::CiiOldbDpValue<std::vector<T>>> OldbAdapter::GetInternal(const std::string& key,
688  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp) {
689 
690  RAD_TRACE(GetLogger());
691 
692  if (m_oldb == nullptr) {
693  this->Connect();
694  if (m_oldb == nullptr) {
695  return nullptr;
696  }
697  }
698 
699  try {
700  if (dp == nullptr) {
701  elt::mal::Uri key_uri(key);
702 
703  if (m_oldb->DataPointExists(key_uri) == false) {
704  LOG4CPLUS_DEBUG(GetLogger(), "Data point not existing " << key_uri);
705  return nullptr;
706  }
707 
708  dp = m_oldb->GetDataPoint<std::vector<T>>(key_uri);
709  if (dp == nullptr) {
710  LOG4CPLUS_DEBUG(GetLogger(), "Data point not existing " << key_uri);
711  return nullptr;
712  }
713  }
714 
715  return dp->ReadValue();
716  } catch (const std::exception& e) {
718  }
719 }
720 
721 
722 template<typename T>
723 void OldbAdapter::Set(const std::string& key,
724  const T& value,
725  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp,
726  const std::int64_t timestamp) {
727  RAD_TRACE(GetLogger());
728 
729  if (m_oldb == nullptr) {
730  this->Connect();
731  if (m_oldb == nullptr) return;
732  }
733 
734  try {
735  if (dp == nullptr) {
736  // Try to create the attribute.
737  elt::mal::Uri key_uri(key);
738  if (m_oldb->DataPointExists(key_uri) == false) {
739  // TODO how to we pass timestamp?
740  dp = m_oldb->CreateDataPointByValue<T>(key_uri, value);
741  } else {
742  dp = m_oldb->GetDataPoint<T>(key_uri);
743  if (dp) {
744  dp->WriteValue(value, timestamp);
745  } else {
746  // do we log a warning or not?
747  LOG4CPLUS_DEBUG(GetLogger(), "Cannot get data point for key: " << key_uri);
748  }
749  }
750  } else {
751  // Try to write
752  dp->WriteValue(value, timestamp);
753  }
754  } catch (const std::exception& e) {
756  }
757 }
758 
759 template<typename T>
760 void OldbAdapter::Set(const std::string& key,
761  const std::vector<T>& values,
762  const bool is_matrix,
763  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp,
764  const std::int64_t timestamp) {
765  RAD_TRACE(GetLogger());
766 
767  if (m_oldb == nullptr) {
768  Connect();
769  if (m_oldb == nullptr) return;
770  }
771 
772  try {
773  if (dp == nullptr) {
774  // Try to create the vector.
775  elt::mal::Uri key_uri(key);
776 
777  if (m_oldb->DataPointExists(key_uri) == false) {
778  dp = m_oldb->CreateDataPointByValue<T>(key_uri, values, is_matrix);
779  } else {
780  // point already exists
781  dp = m_oldb->GetDataPoint<std::vector<T>>(key_uri);
782  if (dp) {
783  dp->WriteValue(values, timestamp);
784  } else {
785  LOG4CPLUS_DEBUG(GetLogger(), "Cannot get data point for key: " << key_uri);
786  }
787  }
788  } else {
789  // Try to write
790  dp->WriteValue(values, timestamp);
791  }
792  } catch (const std::exception& e) {
794  }
795 }
796 
797 template<typename T>
798 void OldbAdapter::Set(const std::string& key,
799  elt::mal::shared_vector<const T>& values,
800  const bool is_matrix,
801  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp,
802  const std::int64_t timestamp) {
803  std::vector<T> vec(values.begin(), values.end());
804  Set(key, vec, is_matrix, dp, timestamp);
805 }
806 
807 } // namespace cii
808 } // namespace rad
809 
810 #endif // RAD_MAL_OLDB_ADAPTER_HPP
Assert header file.
Base class for the exceptions thrown by RAD and its users.
Definition: exceptions.hpp:53
virtual const char * what() const noexcept override
Return the exception message.
Definition: exceptions.cpp:221
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:552
OldbAdapter & operator=(const OldbAdapter &)=default
std::shared_ptr< elt::oldb::CiiOldb > GetOldbInstance()
Definition: oldbAdapter.cpp:42
bool TryGet(const std::string &key, T &value) noexcept
Definition: oldbAdapter.hpp:434
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:537
std::shared_ptr< elt::oldb::CiiOldbDpValue< std::vector< T > > > GetVector(const std::string &key)
Definition: oldbAdapter.hpp:485
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
void ConfigureConnTimeout(const std::chrono::seconds conn_timeout)
Definition: oldbAdapter.cpp:34
void Get(const std::string &key, T &value)
Definition: oldbAdapter.hpp:393
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:521
Logger class.
#define RAD_TRACE(logger)
Definition: logger.hpp:24
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:20
log4cplus::Logger & GetLogger()
Definition: logger.cpp:70
Definition: errors.hpp:58
use
Definition: wscript:14