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 [[deprecated("Should be used only internally, but then
use new method GetInternal instead.")]]
288 void Get(const
std::
string& key,
290 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp);
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);
323 void Set(const
std::
string& key,
325 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp,
326 const
std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
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());
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());
376 void Del(const
std::
string& key);
379 std::shared_ptr<elt::oldb::CiiOldb> m_oldb;
380 std::map<
std::
string,
std::shared_ptr<elt::oldb::CiiOldbTypedDataBase>> m_data_points;
383 std::shared_ptr<elt::oldb::CiiOldbDpValue<T>> GetInternal(const
std::
string& key,
384 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp);
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);
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);
402 auto triplet = OldbAdapter::GetInternal(key, dp);
404 value = triplet->GetValue();
407 if (it == m_data_points.end() && dp) {
408 m_data_points[key] = dp;
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);
423 auto triplet = OldbAdapter::GetInternal(key, dp);
425 if (it == m_data_points.end() && dp) {
426 m_data_points[key] = dp;
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.");
456 }
catch (
const std::exception& e) {
457 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of " << e.what());
460 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of unknown exception.");
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);
475 auto triplet = OldbAdapter::GetInternal(key, dp);
477 value = triplet->GetValue();
479 if (it == m_data_points.end() && dp) {
480 m_data_points[key] = dp;
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);
494 auto triplet = OldbAdapter::GetInternal(key, dp);
496 if (it == m_data_points.end() && dp) {
497 m_data_points[key] = dp;
511 }
catch (
const std::exception& e) {
512 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of " << e.what());
515 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of unknown exception.");
525 return GetVector<T>(key);
526 }
catch (
const std::exception& e) {
527 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of " << e.what());
530 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of unknown exception.");
537 void OldbAdapter::Set(
const std::string& key,
const T& value,
const std::int64_t timestamp) {
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);
545 Set<T>(key, value, dp, timestamp);
546 if (it == m_data_points.end() && dp) {
547 m_data_points[key] = dp;
553 const std::int64_t timestamp) noexcept {
557 Set<T>(key, value, timestamp);
559 }
catch (
const std::exception& e) {
560 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of " << e.what());
563 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of unknown exception.");
570 const std::vector<T>& value,
571 const std::int64_t timestamp) {
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);
579 Set<T>(key, value,
false, dp, timestamp);
580 if (it == m_data_points.end() && dp) {
581 m_data_points[key] = dp;
587 const std::vector<T>& value,
588 const std::int64_t timestamp) noexcept {
592 Set<T>(key, value, timestamp);
594 }
catch (
const std::exception& e) {
595 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of " << e.what());
598 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of unknown exception.");
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);
613 elt::mal::shared_vector<const T>& value,
614 const std::int64_t timestamp) noexcept {
617 std::vector<T> vec(value.begin(), value.end());
618 Set<T>(key, vec, timestamp);
620 }
catch (
const std::exception& e) {
621 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of " << e.what());
624 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of unknown exception.");
632 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp) {
634 auto triplet = OldbAdapter::GetInternal(key, dp);
636 value = triplet->GetValue();
642 std::shared_ptr<elt::oldb::CiiOldbDpValue<T>> OldbAdapter::GetInternal(
const std::string& key,
643 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp) {
647 if (m_oldb ==
nullptr) {
649 if (m_oldb ==
nullptr) {
656 elt::mal::Uri key_uri(key);
658 if (m_oldb->DataPointExists(key_uri) ==
false) {
659 LOG4CPLUS_DEBUG(
GetLogger(),
"Data point not existing " << key_uri);
663 dp = m_oldb->GetDataPoint<T>(key_uri);
665 LOG4CPLUS_DEBUG(
GetLogger(),
"Data point not existing " << key_uri);
670 return dp->ReadValue();
671 }
catch (
const std::exception& e) {
678 std::vector<T>& values,
679 std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp) {
680 auto triplet = OldbAdapter::GetInternal(key, dp);
682 values = triplet->GetValue();
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) {
692 if (m_oldb ==
nullptr) {
694 if (m_oldb ==
nullptr) {
701 elt::mal::Uri key_uri(key);
703 if (m_oldb->DataPointExists(key_uri) ==
false) {
704 LOG4CPLUS_DEBUG(
GetLogger(),
"Data point not existing " << key_uri);
708 dp = m_oldb->GetDataPoint<std::vector<T>>(key_uri);
710 LOG4CPLUS_DEBUG(
GetLogger(),
"Data point not existing " << key_uri);
715 return dp->ReadValue();
716 }
catch (
const std::exception& e) {
725 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp,
726 const std::int64_t timestamp) {
729 if (m_oldb ==
nullptr) {
731 if (m_oldb ==
nullptr)
return;
737 elt::mal::Uri key_uri(key);
738 if (m_oldb->DataPointExists(key_uri) ==
false) {
740 dp = m_oldb->CreateDataPointByValue<T>(key_uri, value);
742 dp = m_oldb->GetDataPoint<T>(key_uri);
744 dp->WriteValue(value, timestamp);
747 LOG4CPLUS_DEBUG(
GetLogger(),
"Cannot get data point for key: " << key_uri);
752 dp->WriteValue(value, timestamp);
754 }
catch (
const std::exception& e) {
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) {
767 if (m_oldb ==
nullptr) {
769 if (m_oldb ==
nullptr)
return;
775 elt::mal::Uri key_uri(key);
777 if (m_oldb->DataPointExists(key_uri) ==
false) {
778 dp = m_oldb->CreateDataPointByValue<T>(key_uri, values, is_matrix);
781 dp = m_oldb->GetDataPoint<std::vector<T>>(key_uri);
783 dp->WriteValue(values, timestamp);
785 LOG4CPLUS_DEBUG(
GetLogger(),
"Cannot get data point for key: " << key_uri);
790 dp->WriteValue(values, timestamp);
792 }
catch (
const std::exception& e) {
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);
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
#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