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