UniSet  2.6.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  protected:
78  typedef std::unordered_map<int, std::string> DBTableMap;
79 
80  virtual void initDBServer() override;
81  virtual void initDB( std::shared_ptr<PostgreSQLInterface>& db ) {};
82  virtual void initDBTableMap(DBTableMap& tblMap) {};
83 
84  virtual void timerInfo( const uniset::TimerMessage* tm ) override;
85  virtual void sysCommand( const uniset::SystemMessage* sm ) override;
86  virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
87  virtual void confirmInfo( const uniset::ConfirmMessage* cmsg ) override;
88  virtual void sigterm( int signo ) override;
89 
90  bool writeToBase( const string& query );
91  void createTables( std::shared_ptr<PostgreSQLInterface>& db );
92 
93  inline std::string tblName(int key)
94  {
95  return tblMap[key];
96  }
97 
98  enum Timers
99  {
103  lastNumberOfTimer
104  };
105 
106  std::shared_ptr<PostgreSQLInterface> db;
107  int PingTime = { 15000 };
108  int ReconnectTime;
109  bool connect_ok;
111  bool activate;
112 
113  typedef std::queue<std::string> QueryBuffer;
114 
115  QueryBuffer qbuf;
116  size_t qbufSize; // размер буфера сообщений.
117  bool lastRemove = { false };
118 
119  void flushBuffer();
120  std::mutex mqbuf;
121 
122  // writeBuffer
123  const std::list<std::string> tblcols = { "date", "time", "time_usec", "sensor_id", "value", "node" };
124 
125  typedef std::vector<PostgreSQLInterface::Record> InsertBuffer;
126  InsertBuffer ibuf;
127  size_t ibufSize = { 0 };
128  size_t ibufMaxSize = { 2000 };
129  timeout_t ibufSyncTimeout = { 15000 };
130  void flushInsertBuffer();
131  float ibufOverflowCleanFactor = { 0.5 }; // коэфициент {0...1} чистки буфера при переполнении
132 
133  private:
134  DBTableMap tblMap;
135 
136  };
137  // ----------------------------------------------------------------------------------
138 } // end of namespace uniset
139 //------------------------------------------------------------------------------------------
140 #endif