UniSet  2.7.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 bool deactivateObject() override;
91  virtual std::string getMonitInfo( const std::string& params ) override;
92 
93  bool writeToBase( const std::string& query );
94  void createTables( const std::shared_ptr<PostgreSQLInterface>& db );
95 
96  inline std::string tblName(int key)
97  {
98  return tblMap[key];
99  }
100 
101  enum Timers
102  {
106  lastNumberOfTimer
107  };
108 
109  std::unique_ptr<PostgreSQLInterface> db;
110  typedef std::queue<std::string> QueryBuffer;
111 
112  void flushBuffer();
113 
114  // writeBuffer
115  const std::vector<std::string> tblcols = { "date", "time", "time_usec", "sensor_id", "value", "node" };
116 
117  typedef std::vector<PostgreSQLInterface::Record> InsertBuffer;
118  void flushInsertBuffer();
119  virtual void addRecord( const PostgreSQLInterface::Record&& rec );
120  virtual bool writeInsertBufferToDB( const std::string& table
121  , const std::vector<std::string>& colname
122  , const InsertBuffer& ibuf );
123 
124  private:
125  DBTableMap tblMap;
126 
127  int PingTime = { 15000 };
128  int ReconnectTime = { 30000 };
129 
130  bool connect_ok = { false };
132  QueryBuffer qbuf;
133  size_t qbufSize = { 200 }; // размер буфера сообщений.
134  bool lastRemove = { false };
135  std::mutex mqbuf;
136 
137  InsertBuffer ibuf;
138  size_t ibufSize = { 0 };
139  size_t ibufMaxSize = { 2000 };
140  timeout_t ibufSyncTimeout = { 15000 };
141  float ibufOverflowCleanFactor = { 0.5 }; // коэфициент {0...1} чистки буфера при переполнении
142  };
143  // ----------------------------------------------------------------------------------
144 } // end of namespace uniset
145 //------------------------------------------------------------------------------------------
146 #endif
Definition: DBServer_PostgreSQL.h:104
virtual bool deactivateObject() override
Деактивиция объекта (переопределяется для необходимых действий при завершении работы) ...
Definition: DBServer_PostgreSQL.cc:464
Definition: CallbackTimer.h:29
Definition: DBServer_PostgreSQL.h:105
Definition: DBServer.h:51
Definition: MessageType.h:168
Definition: MessageType.h:234
static std::shared_ptr< DBServer_PostgreSQL > init_dbserver(int argc, const char *const *argv, const std::string &prefix="pgsql")
Definition: DBServer_PostgreSQL.cc:503
Timers
Definition: DBServer_PostgreSQL.h:101
The DBServer_PostgreSQL class Реализация работы с PostgreSQL.
Definition: DBServer_PostgreSQL.h:54
Definition: DBServer_PostgreSQL.h:103
Definition: MessageType.h:124
static void help_print(int argc, const char *const *argv)
Definition: DBServer_PostgreSQL.cc:527
Definition: MessageType.h:211
static KeyType key(const uniset::ObjectId id, const uniset::ObjectId node)
Definition: UniSetTypes.h:84
long ObjectId
Definition: UniSetTypes_i.idl:30