9#ifndef RAD_MAL_OLDB_ADAPTER_HPP
10#define RAD_MAL_OLDB_ADAPTER_HPP
16#include <ciiOldbFactory.hpp>
17#include <ciiOldbDpValue.hpp>
18#include <ciiOldbExceptions.hpp>
19#include <ciiOldbUtil.hpp>
61 explicit OldbAdapter(
const std::chrono::seconds conn_timeout);
117 std::shared_ptr<elt::oldb::CiiOldbTypedDataBase>
GetDataPoint(
const std::string& key);
126 void Get(
const std::string& key, T& value);
142 std::shared_ptr<elt::oldb::CiiOldbDpValue<T>>
Get(
const std::string& key);
152 bool TryGet(
const std::string& key, T& value)
noexcept;
162 std::shared_ptr<elt::oldb::CiiOldbDpValue<T>>
TryGet(
const std::string& key)
noexcept;
171 void Get(
const std::string& key, std::vector<T>& value);
181 std::shared_ptr<elt::oldb::CiiOldbDpValue<std::vector<T>>>
GetVector(
const std::string& key);
192 bool TryGet(
const std::string& key, std::vector<T>& value)
noexcept;
202 std::shared_ptr<elt::oldb::CiiOldbDpValue<std::vector<T>>>
TryGetVector(
const std::string& key)
noexcept;
212 void Set(
const std::string& key,
214 const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
224 bool TrySet(
const std::string& key,
226 const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now()) noexcept;
235 void Set(const
std::
string& key,
236 const
std::vector<T>& value,
237 const
std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
248 const
std::vector<T>& value,
249 const
std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now()) noexcept;
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());
271 elt::mal::shared_vector<const T>& value,
272 const
std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now()) noexcept;
287 void Set(const
std::
string& key,
289 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp,
290 const
std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
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());
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());
336 void Del(const
std::
string& key);
339 std::shared_ptr<elt::oldb::CiiOldb> m_oldb;
340 std::map<
std::
string,
std::shared_ptr<elt::oldb::CiiOldbTypedDataBase>> m_data_points;
355 std::shared_ptr<elt::oldb::CiiOldbDpValue<T>> GetInternal(const
std::
string& key,
356 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp);
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);
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);
386 auto triplet = OldbAdapter::GetInternal(key, dp);
388 value = triplet->GetValue();
391 if (it == m_data_points.end() && dp) {
392 m_data_points[key] = dp;
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);
407 auto triplet = OldbAdapter::GetInternal(key, dp);
409 if (it == m_data_points.end() && dp) {
410 m_data_points[key] = dp;
424 }
catch (
const std::exception& e) {
425 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of " << e.what());
428 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of unknown exception.");
440 }
catch (
const std::exception& e) {
441 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of " << e.what());
444 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of unknown exception.");
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);
459 auto triplet = OldbAdapter::GetInternal(key, dp);
461 value = triplet->GetValue();
463 if (it == m_data_points.end() && dp) {
464 m_data_points[key] = dp;
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);
478 auto triplet = OldbAdapter::GetInternal(key, dp);
480 if (it == m_data_points.end() && dp) {
481 m_data_points[key] = dp;
495 }
catch (
const std::exception& e) {
496 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of " << e.what());
499 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of unknown exception.");
509 return GetVector<T>(key);
510 }
catch (
const std::exception& e) {
511 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of " << e.what());
514 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of unknown exception.");
521void OldbAdapter::Set(
const std::string& key,
const T& value,
const std::int64_t timestamp) {
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);
529 Set<T>(key, value, dp, timestamp);
530 if (it == m_data_points.end() && dp) {
531 m_data_points[key] = dp;
537 const std::int64_t timestamp)
noexcept {
541 Set<T>(key, value, timestamp);
543 }
catch (
const std::exception& e) {
544 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of " << e.what());
547 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of unknown exception.");
554 const std::vector<T>& value,
555 const std::int64_t timestamp) {
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);
563 Set<T>(key, value, dp, timestamp);
564 if (it == m_data_points.end() && dp) {
565 m_data_points[key] = dp;
571 const std::vector<T>& value,
572 const std::int64_t timestamp)
noexcept {
576 Set<T>(key, value, timestamp);
578 }
catch (
const std::exception& e) {
579 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of " << e.what());
582 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of unknown exception.");
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);
597 elt::mal::shared_vector<const T>& value,
598 const std::int64_t timestamp)
noexcept {
601 std::vector<T> vec(value.begin(), value.end());
602 Set<T>(key, vec, timestamp);
604 }
catch (
const std::exception& e) {
605 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of " << e.what());
608 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of unknown exception.");
614std::shared_ptr<elt::oldb::CiiOldbDpValue<T>> OldbAdapter::GetInternal(
const std::string& key,
615 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp) {
619 if (m_oldb ==
nullptr) {
621 if (m_oldb ==
nullptr) {
628 elt::mal::Uri key_uri(key);
630 if (m_oldb->DataPointExists(key_uri) ==
false) {
631 LOG4CPLUS_DEBUG(
GetLogger(),
"Data point not existing " << key_uri);
635 dp = m_oldb->GetDataPoint<T>(key_uri);
637 LOG4CPLUS_DEBUG(
GetLogger(),
"Data point not existing " << key_uri);
642 return dp->ReadValue();
643 }
catch (
const std::exception& e) {
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) {
654 if (m_oldb ==
nullptr) {
656 if (m_oldb ==
nullptr) {
663 elt::mal::Uri key_uri(key);
665 if (m_oldb->DataPointExists(key_uri) ==
false) {
666 LOG4CPLUS_DEBUG(
GetLogger(),
"Data point not existing " << key_uri);
670 dp = m_oldb->GetDataPoint<std::vector<T>>(key_uri);
672 LOG4CPLUS_DEBUG(
GetLogger(),
"Data point not existing " << key_uri);
677 return dp->ReadValue();
678 }
catch (
const std::exception& e) {
687 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp,
688 const std::int64_t timestamp) {
691 if (m_oldb ==
nullptr) {
693 if (m_oldb ==
nullptr)
return;
699 elt::mal::Uri key_uri(key);
700 if (m_oldb->DataPointExists(key_uri) ==
false) {
702 dp = m_oldb->CreateDataPointByValue<T>(key_uri, value);
704 dp = m_oldb->GetDataPoint<T>(key_uri);
706 dp->WriteValue(value, timestamp);
709 LOG4CPLUS_DEBUG(
GetLogger(),
"Cannot get data point for key: " << key_uri);
714 dp->WriteValue(value, timestamp);
716 }
catch (
const std::exception& e) {
723 const std::vector<T>& values,
724 std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp,
725 const std::int64_t timestamp) {
728 if (m_oldb ==
nullptr) {
730 if (m_oldb ==
nullptr)
return;
736 elt::mal::Uri key_uri(key);
738 if (m_oldb->DataPointExists(key_uri) ==
false) {
739 dp = m_oldb->CreateDataPointByValue<std::vector<T>>(key_uri, values);
742 dp = m_oldb->GetDataPoint<std::vector<T>>(key_uri);
744 dp->WriteValue(values, timestamp);
746 LOG4CPLUS_DEBUG(
GetLogger(),
"Cannot get data point for key: " << key_uri);
751 dp->WriteValue(values, timestamp);
753 }
catch (
const std::exception& e) {
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);
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
#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