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);
136 bool TryGet(
const std::string& key, T& value) noexcept;
145 void Get(
const std::string& key, std::vector<T>& value);
155 bool TryGet(
const std::string& key, std::vector<T>& value) noexcept;
164 void Set(
const std::string& key,
166 const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
176 bool TrySet(
const std::string& key,
178 const std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now()) noexcept;
187 void Set(const
std::
string& key,
188 const
std::vector<T>& value,
189 const
std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
200 const
std::vector<T>& value,
201 const
std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now()) noexcept;
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());
223 elt::mal::shared_vector<const T>& value,
224 const
std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now()) noexcept;
239 void Get(const
std::
string& key,
241 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp);
256 void Get(const
std::
string& key,
257 std::vector<T>& values,
258 std::shared_ptr<elt::oldb::CiiOldbDataPoint<
std::vector<T>>>& dp);
273 void Set(const
std::
string& key,
275 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp,
276 const
std::int64_t timestamp = elt::oldb::CiiOldbUtil::Now());
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());
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());
326 void Del(const
std::
string& key);
329 std::shared_ptr<elt::oldb::CiiOldb> m_oldb;
330 std::map<
std::
string,
std::shared_ptr<elt::oldb::CiiOldbTypedDataBase>> m_data_points;
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);
343 Get<T>(key, value, dp);
344 if (it == m_data_points.end() && dp !=
nullptr) {
345 m_data_points[key] = dp;
356 }
catch (
const std::exception& e) {
357 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of " << e.what());
360 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of unknown exception.");
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);
374 Get<T>(key, value,
false, dp);
375 if (it == m_data_points.end() && dp !=
nullptr) {
376 m_data_points[key] = dp;
387 }
catch (
const std::exception& e) {
388 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of " << e.what());
391 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed reading key " << key <<
" because of unknown exception.");
397 void OldbAdapter::Set(
const std::string& key,
const T& value,
const std::int64_t timestamp) {
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);
405 Set<T>(key, value, dp, timestamp);
406 if (it == m_data_points.end() && dp !=
nullptr) {
407 m_data_points[key] = dp;
413 const std::int64_t timestamp) noexcept {
417 Set<T>(key, value, timestamp);
419 }
catch (
const std::exception& e) {
420 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of " << e.what());
423 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of unknown exception.");
430 const std::vector<T>& value,
431 const std::int64_t timestamp) {
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);
439 Set<T>(key, value,
false, dp, timestamp);
440 if (it == m_data_points.end() && dp !=
nullptr) {
441 m_data_points[key] = dp;
447 const std::vector<T>& value,
448 const std::int64_t timestamp) noexcept {
452 Set<T>(key, value, timestamp);
454 }
catch (
const std::exception& e) {
455 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of " << e.what());
458 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of unknown exception.");
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);
473 elt::mal::shared_vector<const T>& value,
474 const std::int64_t timestamp) noexcept {
477 std::vector<T> vec(value.begin(), value.end());
478 Set<T>(key, vec, timestamp);
480 }
catch (
const std::exception& e) {
481 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of " << e.what());
484 LOG4CPLUS_DEBUG(
GetLogger(),
"Failed writing key " << key <<
" because of unknown exception.");
492 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp) {
495 if (m_oldb ==
nullptr) {
497 if (m_oldb ==
nullptr) {
504 elt::mal::Uri key_uri(key);
506 if (m_oldb->DataPointExists(key_uri) ==
false) {
507 LOG4CPLUS_DEBUG(
GetLogger(),
"Data point not existing " << key_uri);
511 dp = m_oldb->GetDataPoint<T>(key_uri);
513 LOG4CPLUS_DEBUG(
GetLogger(),
"Data point not existing " << key_uri);
519 if (dp->ReadValue()) {
520 value = dp->ReadValue()->GetValue();
522 }
catch (
const std::exception& e) {
529 std::vector<T>& values,
530 std::shared_ptr<elt::oldb::CiiOldbDataPoint<std::vector<T>>>& dp) {
533 if (m_oldb ==
nullptr) {
535 if (m_oldb ==
nullptr) {
542 elt::mal::Uri key_uri(key);
544 if (m_oldb->DataPointExists(key_uri) ==
false) {
545 LOG4CPLUS_DEBUG(
GetLogger(),
"Data point not existing " << key_uri);
549 dp = m_oldb->GetDataPoint<std::vector<T>>(key_uri);
551 LOG4CPLUS_DEBUG(
GetLogger(),
"Data point not existing " << key_uri);
556 if (dp->ReadValue()) {
557 values = dp->ReadValue()->GetValue();
559 }
catch (
const std::exception& e) {
567 std::shared_ptr<elt::oldb::CiiOldbDataPoint<T>>& dp,
568 const std::int64_t timestamp) {
571 if (m_oldb ==
nullptr) {
573 if (m_oldb ==
nullptr)
return;
579 elt::mal::Uri key_uri(key);
580 if (m_oldb->DataPointExists(key_uri) ==
false) {
582 dp = m_oldb->CreateDataPointByValue<T>(key_uri, value);
584 dp = m_oldb->GetDataPoint<T>(key_uri);
586 dp->WriteValue(value, timestamp);
589 LOG4CPLUS_DEBUG(
GetLogger(),
"Cannot get data point for key: " << key_uri);
594 dp->WriteValue(value, timestamp);
596 }
catch (
const std::exception& e) {
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) {
609 if (m_oldb ==
nullptr) {
611 if (m_oldb ==
nullptr)
return;
617 elt::mal::Uri key_uri(key);
619 if (m_oldb->DataPointExists(key_uri) ==
false) {
620 dp = m_oldb->CreateDataPointByValue<T>(key_uri, values, is_matrix);
623 dp = m_oldb->GetDataPoint<std::vector<T>>(key_uri);
625 dp->WriteValue(values, timestamp);
627 LOG4CPLUS_DEBUG(
GetLogger(),
"Cannot get data point for key: " << key_uri);
632 dp->WriteValue(values, timestamp);
634 }
catch (
const std::exception& e) {
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);
652 #endif // RAD_MAL_OLDB_ADAPTER_HPP