#ifndef PG_DGRID_H
|
#define PG_DGRID_H
|
|
enum class QDBScope { GLOBAL = 1, QDB_SHARED = 2, QDB_USERSCOPE = 3 };
|
|
#ifdef __cplusplus
|
extern "C" {
|
#endif
|
|
#include "postgres.h"
|
#include "fmgr.h"
|
#include "varatt.h"
|
|
#ifdef __cplusplus
|
}
|
#endif
|
|
#include <string>
|
#include <memory>
|
#include <mutex>
|
#include "dgrid_client.h"
|
#include "dgrid_config.h"
|
|
// #pragma GCC diagnostic push
|
// #pragma GCC diagnostic error "-Wall"
|
// #pragma GCC diagnostic error "-Wextra"
|
// #pragma GCC diagnostic error "-Werror"
|
|
class PGDGrid {
|
public:
|
static constexpr const char* DEFAULT_SCOPE = "user";
|
|
static PGDGrid& instance();
|
|
template <typename T>
|
bool putValue(const std::string& key, const T& value,
|
const std::string& scope);
|
template <typename T>
|
DGrid::DGridResult getValue(const std::string& key, T& value, const std::string& scope);
|
DGrid::DGridResult hasValue(const std::string& key, const std::string& scope);
|
DGrid::DGridResult delValue(const std::string& key, const std::string& scope);
|
|
// Установка параметров подключения
|
void setHost(const std::string& host, int port);
|
void setClusterKey(const std::string& key);
|
|
private:
|
PGDGrid();
|
~PGDGrid();
|
|
PGDGrid(const PGDGrid&) = delete;
|
PGDGrid& operator=(const PGDGrid&) = delete;
|
|
static constexpr const char* QDB_THEME = "qdb_theme";
|
static constexpr const char* PREFIX_QDB = "qdb::";
|
static constexpr const char* PREFIX_SHARED = "shared::";
|
static constexpr const char* PREFIX_USER_ID = "user_id::";
|
|
// Методы управления подключением
|
bool ensureConnected();
|
|
// Вспомогательные методы
|
QDBScope convertScope(const std::string& scope);
|
std::string prefixKeyWithScope(const std::string& key,
|
const std::string& scope);
|
|
// Методы работы с machine_id
|
bool getMachineIdFromDB(const std::string& username, std::string& machine_id);
|
bool saveMachineIdToDB(const std::string& username,
|
const std::string& machine_id);
|
std::string getMachineId(const std::string& username);
|
bool ensureTableExists();
|
std::string getCurrentUsername() const;
|
|
// Параметры подключения
|
std::string host_;
|
int port_;
|
std::string cluster_key_;
|
DGrid::Config* config_;
|
|
// Состояние подключения
|
bool initialized_;
|
std::unique_ptr<DGrid::DGridDataClient> client_;
|
std::string session_;
|
|
// Machine ID состояние
|
std::string cached_machine_id_; // Кэшированный идентификатор машины
|
bool table_initialized_;
|
};
|
|
template <typename T>
|
bool PGDGrid::putValue(const std::string& key, const T& value,
|
const std::string& scope) {
|
if (!ensureConnected()) {
|
return false;
|
}
|
|
std::string prefixed_key = prefixKeyWithScope(key, scope);
|
DGrid::DGridResult result;
|
|
switch (convertScope(scope)) {
|
case QDBScope::GLOBAL:
|
result = client_->set_global_value(QDB_THEME, prefixed_key, value);
|
break;
|
case QDBScope::QDB_SHARED:
|
case QDBScope::QDB_USERSCOPE:
|
result = client_->set_session_value(QDB_THEME, prefixed_key, value);
|
break;
|
}
|
|
return result == DGrid::DGRID_OK;
|
}
|
|
template <typename T>
|
DGrid::DGridResult PGDGrid::getValue(const std::string& key, T& value,
|
const std::string& scope) {
|
if (!ensureConnected()) {
|
return DGrid::DGRID_NO_CONN;
|
}
|
|
std::string prefixed_key = prefixKeyWithScope(key, scope);
|
DGrid::DGridResult result = DGrid::DGRID_NOT_FOUND;
|
|
switch (convertScope(scope)) {
|
case QDBScope::GLOBAL:
|
result = client_->get_global_value(QDB_THEME, prefixed_key, value);
|
break;
|
case QDBScope::QDB_SHARED:
|
case QDBScope::QDB_USERSCOPE:
|
result = client_->get_session_value(QDB_THEME, prefixed_key, value);
|
break;
|
}
|
|
return result;
|
}
|
|
// C-интерфейс
|
extern "C" {
|
extern Datum dgrid_put_text(PG_FUNCTION_ARGS);
|
extern Datum dgrid_put_bytea(PG_FUNCTION_ARGS);
|
extern Datum dgrid_get_text(PG_FUNCTION_ARGS);
|
extern Datum dgrid_get_bytea(PG_FUNCTION_ARGS);
|
extern Datum dgrid_has_value(PG_FUNCTION_ARGS);
|
extern Datum dgrid_del_value(PG_FUNCTION_ARGS);
|
extern Datum dgrid_set_host(PG_FUNCTION_ARGS);
|
extern Datum dgrid_set_cluster_key(PG_FUNCTION_ARGS);
|
}
|
|
// #pragma GCC diagnostic pop
|
|
#endif // PG_DGRID_H
|