rad  4.0.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 
135  template<typename T>
136  bool TryGet(const std::string& key, T& value) noexcept;
137 
144  template<typename T>
145  void Get(const std::string& key, std::vector<T>& value);
146 
154  template<typename T>
155  bool TryGet(const std::string& key, std::vector<T>& value) noexcept;
156 
163  template<typename T>
164  void Set(const std::string& key,
165  const T& value,
166  const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
167 
175  template<typename T>
176  bool TrySet(const std::string& key,
177  const T& value,
178  const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now()) noexcept;
179 
186  template<typename T>
187  void Set(const std::string& key,
188  const std::vector<T>& value,
189  const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
190 
198  template<typename T>
199  bool TrySet(const std::string& key,
200  const std::vector<T>& value,
201  const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now()) noexcept;
202 
209  template<typename T>
210  void Set(const std::string& key,
211  elt::mal::shared_vector<const T>& value,
212  const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
213 
221  template<typename T>
222  bool TrySet(const std::string& key,
223  elt::mal::shared_vector<const T>& value,
224  const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now()) noexcept;
225 
238  template<typename T>
239  void Get(const std::string& key,
240  T& value,
241  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp);
242 
255  template<typename T>
256  void Get(const std::string& key,
257  std::vector<T>& values,
258  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp);
259 
272  template<typename T>
273  void Set(const std::string& key,
274  const T& value,
275  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp,
276  const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
277 
291  template<typename T>
292  void Set(const std::string& key,
293  const std::vector<T>& values,
294  const bool is_matrix,
295  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp,
296  const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
297 
311  template<typename T>
312  void Set(const std::string& key,
313  elt::mal::shared_vector<const T>& values,
314  const bool is_matrix,
315  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp,
316  const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
317 
326  void Del(const std::string& key);
327 
328 private:
329  std::shared_ptr<elt::oldb::CiiOldb> m_oldb;
330  std::map<std::string, std::shared_ptr<elt::oldb::CiiOldbTypedDataBase>> m_data_points;
331 };
332 
333 
334 template<typename T>
335 void OldbAdapter::Get(const std::string& key, T& value) {
336  RAD_TRACE(GetLogger());
337 
338  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>> dp = nullptr;
339  auto it = m_data_points.find(key);
340  if (it != m_data_points.end()) {
341  dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<T>>(it->second);
342  }
343  Get<T>(key, value, dp);
344  if (it == m_data_points.end() && dp != nullptr) {
345  m_data_points[key] = dp;
346  }
347 }
348 
349 template<typename T>
350 bool OldbAdapter::TryGet(const std::string& key, T& value) noexcept {
351  RAD_TRACE(GetLogger());
352 
353  try {
354  Get<T>(key, value);
355  return true;
356  } catch (const std::exception& e) {
357  LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of " << e.what());
358  return false;
359  } catch (...) {
360  LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of unknown exception.");
361  return false;
362  }
363 }
364 
365 template<typename T>
366 void OldbAdapter::Get(const std::string& key, std::vector<T>& value) {
367  RAD_TRACE(GetLogger());
368 
369  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>> dp = nullptr;
370  auto it = m_data_points.find(key);
371  if (it != m_data_points.end()) {
372  dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<std::vector<T>>>(it->second);
373  }
374  Get<T>(key, value, false, dp);
375  if (it == m_data_points.end() && dp != nullptr) {
376  m_data_points[key] = dp;
377  }
378 }
379 
380 template<typename T>
381 bool OldbAdapter::TryGet(const std::string& key, std::vector<T>& value) noexcept {
382  RAD_TRACE(GetLogger());
383 
384  try {
385  Get<T>(key, value);
386  return true;
387  } catch (const std::exception& e) {
388  LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of " << e.what());
389  return false;
390  } catch (...) {
391  LOG4CPLUS_DEBUG(GetLogger(), "Failed reading key " << key << " because of unknown exception.");
392  return false;
393  }
394 }
395 
396 template<typename T>
397 void OldbAdapter::Set(const std::string& key, const T& value, const std::int64_t timestamp) {
398  RAD_TRACE(GetLogger());
399 
400  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>> dp = nullptr;
401  auto it = m_data_points.find(key);
402  if (it != m_data_points.end()) {
403  dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<T>>(it->second);
404  }
405  Set<T>(key, value, dp, timestamp);
406  if (it == m_data_points.end() && dp != nullptr) {
407  m_data_points[key] = dp;
408  }
409 }
410 
411 template<typename T>
412 bool OldbAdapter::TrySet(const std::string& key, const T& value,
413  const std::int64_t timestamp) noexcept {
414  RAD_TRACE(GetLogger());
415 
416  try {
417  Set<T>(key, value, timestamp);
418  return true;
419  } catch (const std::exception& e) {
420  LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of " << e.what());
421  return false;
422  } catch (...) {
423  LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of unknown exception.");
424  return false;
425  }
426 }
427 
428 template<typename T>
429 void OldbAdapter::Set(const std::string& key,
430  const std::vector<T>& value,
431  const std::int64_t timestamp) {
432  RAD_TRACE(GetLogger());
433 
434  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>> dp = nullptr;
435  auto it = m_data_points.find(key);
436  if (it != m_data_points.end()) {
437  dp = std::dynamic_pointer_cast<elt::oldb::CiiOldbDataPoint<std::vector<T>>>(it->second);
438  }
439  Set<T>(key, value, false, dp, timestamp);
440  if (it == m_data_points.end() && dp != nullptr) {
441  m_data_points[key] = dp;
442  }
443 }
444 
445 template<typename T>
446 bool OldbAdapter::TrySet(const std::string& key,
447  const std::vector<T>& value,
448  const std::int64_t timestamp) noexcept {
449  RAD_TRACE(GetLogger());
450 
451  try {
452  Set<T>(key, value, timestamp);
453  return true;
454  } catch (const std::exception& e) {
455  LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of " << e.what());
456  return false;
457  } catch (...) {
458  LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of unknown exception.");
459  return false;
460  }
461 }
462 
463 template<typename T>
464 void OldbAdapter::Set(const std::string& key,
465  elt::mal::shared_vector<const T>& value,
466  const std::int64_t timestamp) {
467  std::vector<T> vec(value.begin(), value.end());
468  Set<T>(key, vec, timestamp);
469 }
470 
471 template<typename T>
472 bool OldbAdapter::TrySet(const std::string& key,
473  elt::mal::shared_vector<const T>& value,
474  const std::int64_t timestamp) noexcept {
475 
476  try {
477  std::vector<T> vec(value.begin(), value.end());
478  Set<T>(key, vec, timestamp);
479  return true;
480  } catch (const std::exception& e) {
481  LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of " << e.what());
482  return false;
483  } catch (...) {
484  LOG4CPLUS_DEBUG(GetLogger(), "Failed writing key " << key << " because of unknown exception.");
485  return false;
486  }
487 }
488 
489 template<typename T>
490 void OldbAdapter::Get(const std::string& key,
491  T& value,
492  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp) {
493  RAD_TRACE(GetLogger());
494 
495  if (m_oldb == nullptr) {
496  this->Connect();
497  if (m_oldb == nullptr) {
498  return;
499  }
500  }
501 
502  try {
503  if (dp == nullptr) {
504  elt::mal::Uri key_uri(key);
505 
506  if (m_oldb->DataPointExists(key_uri) == false) {
507  LOG4CPLUS_DEBUG(GetLogger(), "Data point not existing " << key_uri);
508  return;
509  }
510 
511  dp = m_oldb->GetDataPoint<T>(key_uri);
512  if (dp == nullptr) {
513  LOG4CPLUS_DEBUG(GetLogger(), "Data point not existing " << key_uri);
514  return;
515  }
516 
517  }
518 
519  if (dp->ReadValue()) {
520  value = dp->ReadValue()->GetValue();
521  }
522  } catch (const std::exception& e) {
524  }
525 }
526 
527 template<typename T>
528 void OldbAdapter::Get(const std::string& key,
529  std::vector<T>& values,
530  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp) {
531  RAD_TRACE(GetLogger());
532 
533  if (m_oldb == nullptr) {
534  this->Connect();
535  if (m_oldb == nullptr) {
536  return;
537  }
538  }
539 
540  try {
541  if (dp == nullptr) {
542  elt::mal::Uri key_uri(key);
543 
544  if (m_oldb->DataPointExists(key_uri) == false) {
545  LOG4CPLUS_DEBUG(GetLogger(), "Data point not existing " << key_uri);
546  return;
547  }
548 
549  dp = m_oldb->GetDataPoint<std::vector<T>>(key_uri);
550  if (dp == nullptr) {
551  LOG4CPLUS_DEBUG(GetLogger(), "Data point not existing " << key_uri);
552  return;
553  }
554  }
555 
556  if (dp->ReadValue()) {
557  values = dp->ReadValue()->GetValue();
558  }
559  } catch (const std::exception& e) {
561  }
562 }
563 
564 template<typename T>
565 void OldbAdapter::Set(const std::string& key,
566  const T& value,
567  std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp,
568  const std::int64_t timestamp) {
569  RAD_TRACE(GetLogger());
570 
571  if (m_oldb == nullptr) {
572  this->Connect();
573  if (m_oldb == nullptr) return;
574  }
575 
576  try {
577  if (dp == nullptr) {
578  // Try to create the attribute.
579  elt::mal::Uri key_uri(key);
580  if (m_oldb->DataPointExists(key_uri) == false) {
581  // TODO how to we pass timestamp?
582  dp = m_oldb->CreateDataPointByValue<T>(key_uri, value);
583  } else {
584  dp = m_oldb->GetDataPoint<T>(key_uri);
585  if (dp != nullptr) {
586  dp->WriteValue(value, timestamp);
587  } else {
588  // do we log a warning or not?
589  LOG4CPLUS_DEBUG(GetLogger(), "Cannot get data point for key: " << key_uri);
590  }
591  }
592  } else {
593  // Try to write
594  dp->WriteValue(value, timestamp);
595  }
596  } catch (const std::exception& e) {
598  }
599 }
600 
601 template<typename T>
602 void OldbAdapter::Set(const std::string& key,
603  const std::vector<T>& values,
604  const bool is_matrix,
605  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp,
606  const std::int64_t timestamp) {
607  RAD_TRACE(GetLogger());
608 
609  if (m_oldb == nullptr) {
610  Connect();
611  if (m_oldb == nullptr) return;
612  }
613 
614  try {
615  if (dp == nullptr) {
616  // Try to create the vector.
617  elt::mal::Uri key_uri(key);
618 
619  if (m_oldb->DataPointExists(key_uri) == false) {
620  dp = m_oldb->CreateDataPointByValue<T>(key_uri, values, is_matrix);
621  } else {
622  // point already exists
623  dp = m_oldb->GetDataPoint<std::vector<T>>(key_uri);
624  if (dp != nullptr) {
625  dp->WriteValue(values, timestamp);
626  } else {
627  LOG4CPLUS_DEBUG(GetLogger(), "Cannot get data point for key: " << key_uri);
628  }
629  }
630  } else {
631  // Try to write
632  dp->WriteValue(values, timestamp);
633  }
634  } catch (const std::exception& e) {
636  }
637 }
638 
639 template<typename T>
640 void OldbAdapter::Set(const std::string& key,
641  elt::mal::shared_vector<const T>& values,
642  const bool is_matrix,
643  std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp,
644  const std::int64_t timestamp) {
645  std::vector<T> vec(values.begin(), values.end());
646  Set(key, vec, is_matrix, dp, timestamp);
647 }
648 
649 } // namespace cii
650 } // namespace rad
651 
652 #endif // RAD_MAL_OLDB_ADAPTER_HPP
exceptions.hpp
Exception classes header file.
rad::cii::OldbAdapter::GetDataPoint
std::shared_ptr< elt::oldb::CiiOldbTypedDataBase > GetDataPoint(const std::string &key)
Definition: oldbAdapter.cpp:73
RAD_TRACE
#define RAD_TRACE(logger)
Definition: logger.hpp:24
rad::cii::OldbAdapter::Connect
virtual void Connect()
Definition: oldbAdapter.cpp:52
rad::Exception
Base class for the exceptions thrown by RAD and its users.
Definition: exceptions.hpp:53
rad::cii::OldbAdapter::SetOldbInstance
void SetOldbInstance(std::shared_ptr< elt::oldb::CiiOldb > oldb)
Definition: oldbAdapter.cpp:47
rad::cii::OldbAdapter::Set
void Set(const std::string &key, const T &value, const std::int64_t timestamp=elt::oldb::CiiOldbUtil::Now())
Definition: oldbAdapter.hpp:397
rad::cii::OldbAdapter::ConfigureConnTimeout
void ConfigureConnTimeout(const std::chrono::seconds conn_timeout)
Definition: oldbAdapter.cpp:34
rad::Exception::what
virtual const char * what() const noexcept override
Return the exception message.
Definition: exceptions.cpp:221
rad::cii::OldbAdapter::Get
void Get(const std::string &key, T &value)
Definition: oldbAdapter.hpp:335
logger.hpp
Logger class.
rad::cii::OldbAdapter::operator=
OldbAdapter & operator=(const OldbAdapter &)=default
rad
Definition: oldbAdapter.hpp:26
rad::GetLogger
log4cplus::Logger & GetLogger()
Definition: logger.cpp:68
assert.hpp
Assert header file.
rad::cii::OldbAdapter::TrySet
bool TrySet(const std::string &key, const T &value, const std::int64_t timestamp=elt::oldb::CiiOldbUtil::Now()) noexcept
Definition: oldbAdapter.hpp:412
rad::cii::OldbAdapter
Definition: oldbAdapter.hpp:44
std
Definition: errors.hpp:58
RAD_RETHROW
#define RAD_RETHROW(exceptionType_t, nested_exception, msg)
Throw exception with information about the throw location.
Definition: exceptions.hpp:345
rad::cii::OldbAdapter::GetOldbInstance
std::shared_ptr< elt::oldb::CiiOldb > GetOldbInstance()
Definition: oldbAdapter.cpp:42
rad::cii::OldbAdapter::OldbAdapter
OldbAdapter()
Definition: oldbAdapter.cpp:18
rad::cii::OldbAdapter::ClearDataPoints
void ClearDataPoints()
Definition: oldbAdapter.cpp:67
rad::cii::OldbAdapter::Del
void Del(const std::string &key)
Definition: oldbAdapter.cpp:86
rad::cii::OldbAdapter::TryGet
bool TryGet(const std::string &key, T &value) noexcept
Definition: oldbAdapter.hpp:350
rad::cii::OldbAdapter::OldbAdapter
OldbAdapter(const OldbAdapter &)=default
rad::cii::OldbAdapter::~OldbAdapter
virtual ~OldbAdapter()
Definition: oldbAdapter.cpp:30