ifw-core 6.0.0
Loading...
Searching...
No Matches
dbInterface.hpp
Go to the documentation of this file.
1
8#ifndef CORE_UTILS_BAT_DB_INTERFACE_HPP_
9#define CORE_UTILS_BAT_DB_INTERFACE_HPP_
10
11#include <string>
12#include <any>
13#include <fmt/format.h>
14#include <boost/lockfree/spsc_queue.hpp>
15#include <boost/atomic.hpp>
16
19#include <rad/cii/oldbAdapter.hpp>
20#include <rad/cii/oldbTypes.hpp>
21
22
23namespace ifw::core::utils::bat {
24
25 using DbPair = std::pair<std::string, std::any>;
26 using DbVector = std::vector<DbPair>;
27
32 public:
39 DbInterface(const std::string& prefix,
40 std::chrono::seconds db_timeout,
41 std::chrono::milliseconds db_task_period,
42 bool with_worker_thread = true);
43
47 virtual ~DbInterface();
48
49 inline bool IsConnected() { return m_connected; }
50 inline std::string GetPrefix() { return m_prefix; }
51
59 void SetStates(const std::string& prefix,
60 const std::string& state,
61 const std::string& substate);
62
63
70 template<typename T>
71 void Get(const std::string& key, T& value);
72
79 template<typename T>
80 void Set(const std::string& key,
81 const T& value);
82
83
94 template<typename T>
95 void SetAsync(const std::string& key,
96 const T& value);
97
107 void BatchSetAsync(const DbVector& vec);
108
109
110
111 DbInterface(const DbInterface&) = delete;
113
114
115
116 private:
117 std::string m_prefix;
118 bool m_connected{false};
119
120
122 rad::DoubleMap<rad::cii::OldbType> m_oldb_map;
123
125 rad::cii::OldbAdapter m_oldb_adapter;
126
128 TaskOldb m_task_oldb;
129
130 bool m_with_worker_thread {true};
131 log4cplus::Logger m_logger;
132
133 };
134
135
136 template<typename T>
137 void DbInterface::Get(const std::string& key, T& value) {
138 LOG4CPLUS_DEBUG(m_logger, fmt::format("Reading attribute <{}> with value: {}", key, value));
139 m_oldb_adapter.Get<T>(m_prefix + key, value);
140 LOG4CPLUS_DEBUG(m_logger, fmt::format("Reading attribute <{}> finished !", key));
141 }
142
143 template<typename T>
144 void DbInterface::Set(const std::string& key, const T& value) {
145 LOG4CPLUS_DEBUG(m_logger, fmt::format("Writing attribute <{}>", key));
146 m_oldb_adapter.Set<T>(m_prefix + key, value);
147 LOG4CPLUS_DEBUG(m_logger, fmt::format("Writing attribute <{}> finished !", key));
148 }
149
150 template<typename T>
151 void DbInterface::SetAsync(const std::string& key, const T& value) {
152 //LOG4CPLUS_DEBUG(m_logger, fmt::format("Async attribute for write <{}> with value: {}",
153 // m_prefix + key, value));
154 m_oldb_map.Set(m_prefix + key, value);
155 //LOG4CPLUS_DEBUG(m_logger, fmt::format("Async attribute for write <{}> finished !",
156 // m_prefix +key));
157 }
158
159} // namespace
160
161#endif // CORE_UTILS_BAT_DB_INTERFACE_HPP_
Definition dbInterface.hpp:31
DbInterface(const std::string &prefix, std::chrono::seconds db_timeout, std::chrono::milliseconds db_task_period, bool with_worker_thread=true)
Definition dbInterface.cpp:19
std::string GetPrefix()
Definition dbInterface.hpp:50
void SetAsync(const std::string &key, const T &value)
Aynchronous OLDB set for single attributes.
Definition dbInterface.hpp:151
void SetStates(const std::string &prefix, const std::string &state, const std::string &substate)
Definition dbInterface.cpp:59
virtual ~DbInterface()
Definition dbInterface.cpp:51
void Set(const std::string &key, const T &value)
Definition dbInterface.hpp:144
void BatchSetAsync(const DbVector &vec)
Aynchronous OLDB set for a vector.
Definition dbInterface.cpp:78
DbInterface(const DbInterface &)=delete
Disable copy constructor.
void Get(const std::string &key, T &value)
Definition dbInterface.hpp:137
bool IsConnected()
Definition dbInterface.hpp:49
DbInterface & operator=(const DbInterface &)=delete
Disable assignment operator.
Definition taskOldb.hpp:29
Config class source file.
Definition config.cpp:26
std::pair< std::string, std::any > DbPair
Definition config.hpp:76
std::vector< DbPair > DbVector
Definition config.hpp:77