UniSet  2.24.2
DBServer_PostgreSQL.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // --------------------------------------------------------------------------
17 #ifndef DBServer_PostgreSQL_H_
18 #define DBServer_PostgreSQL_H_
19 // --------------------------------------------------------------------------
20 #include <string_view>
21 #include <unordered_map>
22 #include <queue>
23 #include "UniSetTypes.h"
24 #include "PostgreSQLInterface.h"
25 #include "DBServer.h"
26 #include "SharedMemory.h"
27 // -------------------------------------------------------------------------
28 namespace uniset
29 {
30  //------------------------------------------------------------------------------------------
57  public DBServer
58  {
59  public:
60  DBServer_PostgreSQL( uniset::ObjectId id, const std::string& prefix );
62  virtual ~DBServer_PostgreSQL();
63 
65  static std::shared_ptr<DBServer_PostgreSQL> init_dbserver( int argc, const char* const* argv, const std::shared_ptr<uniset::SharedMemory>& ic = nullptr, const std::string& prefix = "pgsql" );
66 
68  static void help_print( int argc, const char* const* argv );
69 
70  inline std::shared_ptr<LogAgregator> logAggregator()
71  {
72  return loga;
73  }
74  inline std::shared_ptr<DebugStream> log()
75  {
76  return dblog;
77  }
78 
79  bool isConnectOk() const;
80 
81  protected:
82  typedef std::unordered_map<int, std::string> DBTableMap;
83 
84  virtual void initDBServer() override;
85  virtual void initDB( std::unique_ptr<PostgreSQLInterface>& db ) {};
86  virtual void initDBTableMap( DBTableMap& tblMap ) {};
87 
88  virtual void timerInfo( const uniset::TimerMessage* tm ) override;
89  virtual void sysCommand( const uniset::SystemMessage* sm ) override;
90  virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
91  virtual void confirmInfo( const uniset::ConfirmMessage* cmsg ) override;
92  virtual void onTextMessage( const uniset::TextMessage* msg ) override;
93  virtual bool deactivateObject() override;
94  virtual std::string getMonitInfo( const std::string& params ) override;
95 
96  bool writeToBase( const std::string& query );
97  void createTables( const std::shared_ptr<PostgreSQLInterface>& db );
98 
99  inline std::string tblName(int key)
100  {
101  return tblMap[key];
102  }
103 
104  enum Timers
105  {
109  lastNumberOfTimer
110  };
111 
112  std::unique_ptr<PostgreSQLInterface> db;
113  typedef std::queue<std::string> QueryBuffer;
114 
115  void flushBuffer();
116 
117  // writeBuffer
118  const std::initializer_list<std::string_view> tblcols = { "date", "time", "time_usec", "sensor_id", "value", "node" };
119 
120  typedef std::vector<PostgreSQLInterface::Record> InsertBuffer;
121  void flushInsertBuffer();
122  virtual void addRecord( const PostgreSQLInterface::Record&& rec );
123  virtual bool writeInsertBufferToDB( const std::string& table
124  , const std::initializer_list<std::string_view> colname
125  , const InsertBuffer& ibuf );
126 
127  private:
128  DBTableMap tblMap;
129 
130  int PingTime = { 15000 };
131  int ReconnectTime = { 30000 };
132 
133  bool connect_ok = { false };
135  QueryBuffer qbuf;
136  size_t qbufSize = { 200 }; // размер буфера сообщений.
137  bool lastRemove = { false };
138  std::mutex mqbuf;
139 
140  InsertBuffer ibuf;
141  size_t ibufSize = { 0 };
142  size_t ibufMaxSize = { 2000 };
143  timeout_t ibufSyncTimeout = { 15000 };
144  float ibufOverflowCleanFactor = { 0.5 }; // коэффициент {0...1} чистки буфера при переполнении
145  };
146  // ----------------------------------------------------------------------------------
147 } // end of namespace uniset
148 //------------------------------------------------------------------------------------------
149 #endif
Definition: MessageType.h:237
The DBServer_PostgreSQL class Реализация работы с PostgreSQL.
Definition: DBServer_PostgreSQL.h:58
Timers
Definition: DBServer_PostgreSQL.h:105
@ FlushInsertBuffer
Definition: DBServer_PostgreSQL.h:108
@ PingTimer
Definition: DBServer_PostgreSQL.h:106
@ ReconnectTimer
Definition: DBServer_PostgreSQL.h:107
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition: DBServer_PostgreSQL.cc:494
static std::shared_ptr< DBServer_PostgreSQL > init_dbserver(int argc, const char *const *argv, const std::shared_ptr< uniset::SharedMemory > &ic=nullptr, const std::string &prefix="pgsql")
Definition: DBServer_PostgreSQL.cc:533
static void help_print(int argc, const char *const *argv)
Definition: DBServer_PostgreSQL.cc:560
Definition: DBServer.h:45
Definition: MessageType.h:127
Definition: MessageType.h:171
Definition: MessageType.h:280
Definition: MessageType.h:214
Definition: CommonEventLoop.h:15
KeyType key(const uniset::ObjectId id, const uniset::ObjectId node)
Definition: UniSetTypes.cc:746
long ObjectId
Definition: UniSetTypes_i.idl:30