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