|
UniSet
2.6.0
|
00001 /* 00002 * Copyright (c) 2015 Pavel Vainerman. 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as 00006 * published by the Free Software Foundation, version 2.1. 00007 * 00008 * This program is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * Lesser General Lesser Public License for more details. 00012 * 00013 * You should have received a copy of the GNU Lesser General Public License 00014 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00015 */ 00016 // -------------------------------------------------------------------------- 00017 #ifndef DBServer_PostgreSQL_H_ 00018 #define DBServer_PostgreSQL_H_ 00019 // -------------------------------------------------------------------------- 00020 #include <unordered_map> 00021 #include <queue> 00022 #include "UniSetTypes.h" 00023 #include "PostgreSQLInterface.h" 00024 #include "DBServer.h" 00025 // ------------------------------------------------------------------------- 00026 namespace uniset 00027 { 00028 //------------------------------------------------------------------------------------------ 00054 class DBServer_PostgreSQL: 00055 public DBServer 00056 { 00057 public: 00058 DBServer_PostgreSQL( uniset::ObjectId id, const std::string& prefix ); 00059 DBServer_PostgreSQL(); 00060 virtual ~DBServer_PostgreSQL(); 00061 00063 static std::shared_ptr<DBServer_PostgreSQL> init_dbserver( int argc, const char* const* argv, const std::string& prefix = "pgsql" ); 00064 00066 static void help_print( int argc, const char* const* argv ); 00067 00068 inline std::shared_ptr<LogAgregator> logAggregator() 00069 { 00070 return loga; 00071 } 00072 inline std::shared_ptr<DebugStream> log() 00073 { 00074 return dblog; 00075 } 00076 00077 protected: 00078 typedef std::unordered_map<int, std::string> DBTableMap; 00079 00080 virtual void initDBServer() override; 00081 virtual void initDB( std::shared_ptr<PostgreSQLInterface>& db ) {}; 00082 virtual void initDBTableMap(DBTableMap& tblMap) {}; 00083 00084 virtual void timerInfo( const uniset::TimerMessage* tm ) override; 00085 virtual void sysCommand( const uniset::SystemMessage* sm ) override; 00086 virtual void sensorInfo( const uniset::SensorMessage* sm ) override; 00087 virtual void confirmInfo( const uniset::ConfirmMessage* cmsg ) override; 00088 virtual void sigterm( int signo ) override; 00089 00090 bool writeToBase( const string& query ); 00091 void createTables( std::shared_ptr<PostgreSQLInterface>& db ); 00092 00093 inline std::string tblName(int key) 00094 { 00095 return tblMap[key]; 00096 } 00097 00098 enum Timers 00099 { 00100 PingTimer, 00101 ReconnectTimer, 00102 FlushInsertBuffer, 00103 lastNumberOfTimer 00104 }; 00105 00106 std::shared_ptr<PostgreSQLInterface> db; 00107 int PingTime = { 15000 }; 00108 int ReconnectTime; 00109 bool connect_ok; 00111 bool activate; 00112 00113 typedef std::queue<std::string> QueryBuffer; 00114 00115 QueryBuffer qbuf; 00116 size_t qbufSize; // размер буфера сообщений. 00117 bool lastRemove = { false }; 00118 00119 void flushBuffer(); 00120 std::mutex mqbuf; 00121 00122 // writeBuffer 00123 const std::list<std::string> tblcols = { "date", "time", "time_usec", "sensor_id", "value", "node" }; 00124 00125 typedef std::vector<PostgreSQLInterface::Record> InsertBuffer; 00126 InsertBuffer ibuf; 00127 size_t ibufSize = { 0 }; 00128 size_t ibufMaxSize = { 2000 }; 00129 timeout_t ibufSyncTimeout = { 15000 }; 00130 void flushInsertBuffer(); 00131 float ibufOverflowCleanFactor = { 0.5 }; // коэфициент {0...1} чистки буфера при переполнении 00132 00133 private: 00134 DBTableMap tblMap; 00135 00136 }; 00137 // ---------------------------------------------------------------------------------- 00138 } // end of namespace uniset 00139 //------------------------------------------------------------------------------------------ 00140 #endif
1.7.6.1