UniSet 2.32.1
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// -------------------------------------------------------------------------
28namespace uniset
29{
30 //------------------------------------------------------------------------------------------
56 public DBServer
57 {
58 public:
59 DBServer_PostgreSQL( uniset::ObjectId id, const std::string& prefix );
61 virtual ~DBServer_PostgreSQL();
62
64 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" );
65
67 static void help_print( int argc, const char* const* argv );
68
69 inline std::shared_ptr<LogAgregator> logAggregator()
70 {
71 return loga;
72 }
73 inline std::shared_ptr<DebugStream> log()
74 {
75 return dblog;
76 }
77
78 bool isConnectOk() const;
79
80 static constexpr std::string_view tblcols = { "date,time,time_usec,sensor_id,value,node" };
81
82 protected:
83 typedef std::unordered_map<int, std::string> DBTableMap;
84
85 virtual void initDBServer() override;
86 virtual void onReconnect( std::unique_ptr<PostgreSQLInterface>& db ) {};
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
98 inline std::string tblName(int key)
99 {
100 return tblMap[key];
101 }
102
104 {
108 lastNumberOfTimer
109 };
110
111 std::unique_ptr<PostgreSQLInterface> db;
112 typedef std::queue<std::string> QueryBuffer;
113
114 void flushBuffer();
115
116 // writeBuffer
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 , std::string_view 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 MessageType.h:240
The DBServer_PostgreSQL class Реализация работы с PostgreSQL. Т.к. основная работа сервера - это част...
Definition DBServer_PostgreSQL.h:57
Timers
Definition DBServer_PostgreSQL.h:104
@ FlushInsertBuffer
Definition DBServer_PostgreSQL.h:107
@ PingTimer
Definition DBServer_PostgreSQL.h:105
@ ReconnectTimer
Definition DBServer_PostgreSQL.h:106
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition DBServer_PostgreSQL.cc:454
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:493
static void help_print(int argc, const char *const *argv)
Definition DBServer_PostgreSQL.cc:522
Definition DBServer.h:45
Definition MessageType.h:127
Definition MessageType.h:171
Definition MessageType.h:283
Definition MessageType.h:214
Definition Calibration.h:27
KeyType key(const uniset::ObjectId id, const uniset::ObjectId node)
Definition UniSetTypes.cc:980
long ObjectId
Definition UniSetTypes_i.idl:30