UniSet  2.8.0
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 <unordered_map>
21 #include <queue>
22 #include "UniSetTypes.h"
23 #include "PostgreSQLInterface.h"
24 #include "DBServer.h"
25 // -------------------------------------------------------------------------
26 namespace uniset
27 {
28  //------------------------------------------------------------------------------------------
55  public DBServer
56  {
57  public:
58  DBServer_PostgreSQL( uniset::ObjectId id, const std::string& prefix );
60  virtual ~DBServer_PostgreSQL();
61 
63  static std::shared_ptr<DBServer_PostgreSQL> init_dbserver( int argc, const char* const* argv, const std::string& prefix = "pgsql" );
64 
66  static void help_print( int argc, const char* const* argv );
67 
68  inline std::shared_ptr<LogAgregator> logAggregator()
69  {
70  return loga;
71  }
72  inline std::shared_ptr<DebugStream> log()
73  {
74  return dblog;
75  }
76 
77  bool isConnectOk() const;
78 
79  protected:
80  typedef std::unordered_map<int, std::string> DBTableMap;
81 
82  virtual void initDBServer() override;
83  virtual void initDB( std::unique_ptr<PostgreSQLInterface>& db ) {};
84  virtual void initDBTableMap( DBTableMap& tblMap ) {};
85 
86  virtual void timerInfo( const uniset::TimerMessage* tm ) override;
87  virtual void sysCommand( const uniset::SystemMessage* sm ) override;
88  virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
89  virtual void confirmInfo( const uniset::ConfirmMessage* cmsg ) override;
90  virtual void onTextMessage( const uniset::TextMessage* msg ) override;
91  virtual bool deactivateObject() override;
92  virtual std::string getMonitInfo( const std::string& params ) override;
93 
94  bool writeToBase( const std::string& query );
95  void createTables( const std::shared_ptr<PostgreSQLInterface>& db );
96 
97  inline std::string tblName(int key)
98  {
99  return tblMap[key];
100  }
101 
102  enum Timers
103  {
107  lastNumberOfTimer
108  };
109 
110  std::unique_ptr<PostgreSQLInterface> db;
111  typedef std::queue<std::string> QueryBuffer;
112 
113  void flushBuffer();
114 
115  // writeBuffer
116  const std::vector<std::string> tblcols = { "date", "time", "time_usec", "sensor_id", "value", "node" };
117 
118  typedef std::vector<PostgreSQLInterface::Record> InsertBuffer;
119  void flushInsertBuffer();
120  virtual void addRecord( const PostgreSQLInterface::Record&& rec );
121  virtual bool writeInsertBufferToDB( const std::string& table
122  , const std::vector<std::string>& colname
123  , const InsertBuffer& ibuf );
124 
125  private:
126  DBTableMap tblMap;
127 
128  int PingTime = { 15000 };
129  int ReconnectTime = { 30000 };
130 
131  bool connect_ok = { false };
133  QueryBuffer qbuf;
134  size_t qbufSize = { 200 }; // размер буфера сообщений.
135  bool lastRemove = { false };
136  std::mutex mqbuf;
137 
138  InsertBuffer ibuf;
139  size_t ibufSize = { 0 };
140  size_t ibufMaxSize = { 2000 };
141  timeout_t ibufSyncTimeout = { 15000 };
142  float ibufOverflowCleanFactor = { 0.5 }; // коэффициент {0...1} чистки буфера при переполнении
143  };
144  // ----------------------------------------------------------------------------------
145 } // end of namespace uniset
146 //------------------------------------------------------------------------------------------
147 #endif
Definition: DBServer_PostgreSQL.h:105
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition: DBServer_PostgreSQL.cc:494
Definition: CommonEventLoop.h:14
Definition: DBServer_PostgreSQL.h:106
Definition: DBServer.h:51
Definition: MessageType.h:170
Definition: MessageType.h:236
static std::shared_ptr< DBServer_PostgreSQL > init_dbserver(int argc, const char *const *argv, const std::string &prefix="pgsql")
Definition: DBServer_PostgreSQL.cc:533
Timers
Definition: DBServer_PostgreSQL.h:102
The DBServer_PostgreSQL class Реализация работы с PostgreSQL.
Definition: DBServer_PostgreSQL.h:54
KeyType key(const uniset::ObjectId id, const uniset::ObjectId node)
Definition: UniSetTypes.cc:718
Definition: DBServer_PostgreSQL.h:104
Definition: MessageType.h:126
Definition: MessageType.h:279
static void help_print(int argc, const char *const *argv)
Definition: DBServer_PostgreSQL.cc:557
Definition: MessageType.h:213
long ObjectId
Definition: UniSetTypes_i.idl:30