ifw-core  5.0.0-pre2
dbInterface.hpp
Go to the documentation of this file.
1 
9 #ifndef CORE_UTILS_BAT_DB_INTERFACE_HPP_
10 #define CORE_UTILS_BAT_DB_INTERFACE_HPP_
11 
12 // System headers
13 #include <string>
14 #include <any>
15 #include <fmt/format.h>
16 #include <boost/lockfree/spsc_queue.hpp>
17 #include <boost/atomic.hpp>
18 
19 
20 // Local headers
21 #include <utils/bat/logger.hpp>
22 #include <utils/bat/taskOldb.hpp>
23 #include <rad/cii/oldbAdapter.hpp>
24 #include <rad/cii/oldbTypes.hpp>
25 
26 
27 namespace utils::bat {
28 
29  using DbPair = std::pair<std::string, std::any>;
30  using DbVector = std::vector<DbPair>;
31 
35  class DbInterface {
36  public:
43  DbInterface(const std::string& prefix,
44  std::chrono::seconds db_timeout,
45  std::chrono::milliseconds db_task_period,
46  bool with_worker_thread = true);
47 
51  virtual ~DbInterface();
52 
53  inline bool IsConnected() { return m_connected; }
54  inline std::string GetPrefix() { return m_prefix; }
55 
63  void SetStates(const std::string& prefix,
64  const std::string& state,
65  const std::string& substate);
66 
67 
74  template<typename T>
75  void Get(const std::string& key, T& value);
76 
83  template<typename T>
84  void Set(const std::string& key,
85  const T& value);
86 
87 
98  template<typename T>
99  void SetAsync(const std::string& key,
100  const T& value);
101 
111  void BatchSetAsync(const DbVector& vec);
112 
113 
114 
115  DbInterface(const DbInterface&) = delete;
116  DbInterface& operator=(const DbInterface&) = delete;
117 
118 
119 
120  private:
121  std::string m_prefix;
122  bool m_connected{false};
123 
124 
126  rad::DoubleMap<rad::cii::OldbType> m_oldb_map;
127 
129  rad::cii::OldbAdapter m_oldb_adapter;
130 
132  TaskOldb m_task_oldb;
133 
134  bool m_with_worker_thread {true};
135  log4cplus::Logger m_logger;
136 
137  };
138 
139 
140  template<typename T>
141  void DbInterface::Get(const std::string& key, T& value) {
142  LOG4CPLUS_DEBUG(m_logger, fmt::format("Reading attribute <{}> with value: {}", key, value));
143  m_oldb_adapter.Get<T>(m_prefix + key, value);
144  LOG4CPLUS_DEBUG(m_logger, fmt::format("Reading attribute <{}> fnished !", key));
145  }
146 
147  template<typename T>
148  void DbInterface::Set(const std::string& key, const T& value) {
149  LOG4CPLUS_DEBUG(m_logger, fmt::format("Writting attribute <{}> with value: {}", key, value));
150  m_oldb_adapter.Set<T>(m_prefix + key, value);
151  LOG4CPLUS_DEBUG(m_logger, fmt::format("Writting attribute <{}> fnished !", key));
152  }
153 
154  template<typename T>
155  void DbInterface::SetAsync(const std::string& key, const T& value) {
156  //LOG4CPLUS_DEBUG(m_logger, fmt::format("Async attribute for write <{}> with value: {}",
157  // m_prefix + key, value));
158  m_oldb_map.Set(m_prefix + key, value);
159  //LOG4CPLUS_DEBUG(m_logger, fmt::format("Async attribute for write <{}> fnished !",
160  // m_prefix +key));
161  }
162 
163 } // namespace
164 
165 #endif // CORE_UTILS_BAT_DB_INTERFACE_HPP_
Definition: dbInterface.hpp:35
void Set(const std::string &key, const T &value)
Definition: dbInterface.hpp:148
void SetAsync(const std::string &key, const T &value)
Aynchronous OLDB set for single attributes.
Definition: dbInterface.hpp:155
void Get(const std::string &key, T &value)
Definition: dbInterface.hpp:141
void BatchSetAsync(const DbVector &vec)
Aynchronous OLDB set for a vector.
Definition: dbInterface.cpp:80
DbInterface(const DbInterface &)=delete
Disable copy constructor.
bool IsConnected()
Definition: dbInterface.hpp:53
virtual ~DbInterface()
Definition: dbInterface.cpp:53
DbInterface & operator=(const DbInterface &)=delete
Disable assignment operator.
DbInterface(const std::string &prefix, std::chrono::seconds db_timeout, std::chrono::milliseconds db_task_period, bool with_worker_thread=true)
Definition: dbInterface.cpp:21
std::string GetPrefix()
Definition: dbInterface.hpp:54
void SetStates(const std::string &prefix, const std::string &state, const std::string &substate)
Definition: dbInterface.cpp:61
Definition: taskOldb.hpp:32
Logger header file.
log4cplus::Logger & Logger()
Definition: defines.cpp:13
Definition: config.cpp:31
std::pair< std::string, std::any > DbPair
Definition: config.hpp:80
std::vector< DbPair > DbVector
Definition: config.hpp:81
taskSetup class header file.