ifw-fcf  4.1.0-pre2
dbInterface.hpp
Go to the documentation of this file.
1 
8 #ifndef FCF_DEVMGR_COMMON_DB_INTERFACE_HPP_
9 #define FCF_DEVMGR_COMMON_DB_INTERFACE_HPP_
10 
11 // System headers
12 #include <string>
13 
14 #pragma GCC diagnostic push
15 #pragma GCC diagnostic ignored "-Wpedantic"
16 
17 #include <sw/redis++/redis++.h>
18 
19 #pragma GCC diagnostic pop
20 
21 // Local headers
22 #include <utils/bat/dbInterface.hpp>
23 #include <utils/bat/logger.hpp>
24 
25 namespace fcf::devmgr::common {
26 
27  // These parameters are required by the Redis++ library.
29  const std::size_t REDIS_CONNECTION_POOL_SIZE = 15;
33  const int64_t REDIS_WAIT_TIMEOUT_MS = 10000;
37  const int64_t REDIS_SOCKET_TIMEOUT_MS = 15000;
38 
42  class DbInterface {
43  public:
49  DbInterface(const std::string& prefix,
50  const std::string& db_endpoint,
51  const timeval& db_timeout);
52 
53  inline bool IsConnected() const {return m_connected;}
54  void SetStates(const std::string& prefix,
55  const std::string& state,
56  const std::string& substate);
57  void BatchSet(const utils::bat::DbVector& vec);
58  template<typename T>
59  void Set(const std::string& key,
60  const T& value);
61 
62  DbInterface(const DbInterface&) = delete;
63  DbInterface& operator=(const DbInterface&) = delete;
64 
65  private:
66  std::string m_prefix;
67  std::string m_db_endpoint;
68  timeval m_db_timeout;
69  bool m_connected{false};
70  std::unique_ptr<::sw::redis::Redis> m_runtime_db;
71 
72  log4cplus::Logger m_logger;
73  };
74 
75  template<typename T>
76  void DbInterface::Set(const std::string& key, const T& value) {
77  std::ostringstream os;
78  os << value;
79 
80  try {
81  LOG4CPLUS_DEBUG(m_logger, fmt::format("Writing attribute <{}> ...", m_prefix+key));
82  m_runtime_db->set(m_prefix+key, os.str());
83  LOG4CPLUS_DEBUG(m_logger, fmt::format("Writing attribute <{}> finished !", key));
84  } catch (const ::sw::redis::Error& err) {
85  LOG4CPLUS_ERROR(m_logger, fmt::format("Problems writing attribute <{}>: <> !",
86  key, err.what()));
87  throw std::runtime_error (fmt::format("Problems writing attribute <{}>: <> !", key,
88  err.what()));
89  }
90  }
91 
92 } // namespace devmgr
93 
94 #endif // FCF_DEVMGR_COMMON_DB_INTERFACE_HPP_
fcf::devmgr::common::REDIS_WAIT_TIMEOUT_MS
const int64_t REDIS_WAIT_TIMEOUT_MS
Definition: dbInterface.hpp:33
fcf::devmgr::common::REDIS_CONNECTION_POOL_SIZE
const std::size_t REDIS_CONNECTION_POOL_SIZE
Definition: dbInterface.hpp:29
fcf::devmgr::common::REDIS_CONNECTION_POOL_CONN_LIFETIME_MS
const int64_t REDIS_CONNECTION_POOL_CONN_LIFETIME_MS
Definition: dbInterface.hpp:31
fcf::devmgr::common::DbInterface::SetStates
void SetStates(const std::string &prefix, const std::string &state, const std::string &substate)
Definition: dbInterface.cpp:75
fcf::devmgr::common
Definition: actionMgr.cpp:29
fcf::devmgr::common::DbInterface
Definition: dbInterface.hpp:42
fcf::devmgr::common::DbInterface::IsConnected
bool IsConnected() const
Definition: dbInterface.hpp:53
fcf::devmgr::common::DbInterface::DbInterface
DbInterface(const DbInterface &)=delete
Disable copy constructor.
fcf::devmgr::common::REDIS_SOCKET_TIMEOUT_MS
const int64_t REDIS_SOCKET_TIMEOUT_MS
Definition: dbInterface.hpp:37
fcf::devmgr::common::DbInterface::Set
void Set(const std::string &key, const T &value)
Definition: dbInterface.hpp:76
fcf::devmgr::common::DbInterface::BatchSet
void BatchSet(const utils::bat::DbVector &vec)
Definition: dbInterface.cpp:93
fcf::devmgr::common::DbInterface::DbInterface
DbInterface(const std::string &prefix, const std::string &db_endpoint, const timeval &db_timeout)
Definition: dbInterface.cpp:24
fcf::devmgr::common::DbInterface::operator=
DbInterface & operator=(const DbInterface &)=delete
Disable assignment operator.