Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#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