Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#ifndef LIBDGRID_H
#define LIBDGRID_H
 
#include <string>
#include <vector>
#include <map>
#include <memory>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdint>
 
namespace DGrid {
 
typedef std::map<std::string, std::string> stringmap;
 
#define SELECT_TIMEOUT_SEC  1
#define SELECT_TIMEOUT_USEC 0
 
uint32_t maxPayloadSize();
 
typedef std::vector<char> NodeID;
typedef std::vector<char> TableID;
typedef uint64_t RecordID;
 
inline bool operator!(const TableID& id) { return id.empty(); }
 
enum AuthType {
    AUTH_UNKNOWN = 0,
    AUTH_NONE    = 1,
    AUTH_KEY     = 2,
    AUTH_USER    = 3
};
AuthType int2authType(const uint8_t);
const std::string authType2string(const uint8_t);
 
enum AuthState {
    AUTH_STATE_NONE = 0,
    AUTH_STATE_START = 1,
    AUTH_STATE_FAILED = 2,
    AUTH_STATE_OK = 3
};
 
enum TableScope {
    TABLE_GLOBAL = 0,
    TABLE_LOCAL  = 1
};
TableScope int2tableScope(const uint8_t);
const std::string tableScope2string(const uint8_t);
 
enum TableType {
    TABLE_TYPE_INVALID = 0,
    TABLE_STANDARD = 1,
    TABLE_HASHED   = 2,
    TABLE_SORTED   = 3,
    TABLE_SORTED_UNIQ = 4
};
TableType int2tableType(const uint8_t);
TableType string2tableType(const std::string&);
const std::string tableType2string(const uint8_t);
 
enum KeyFlags {
    KEY_FLAG_UNIQUE = 0x0001,
    KEY_FLAG_HASHED = 0x0002
};
 
enum FieldType {
    FIELD_TYPE_RAW = 0,
    FIELD_TYPE_INTEGER = 1,
    FIELD_TYPE_FLOAT   = 2,
    FIELD_TYPE_NUMERIC = 3,
    FIELD_TYPE_PACKED  = 4
};
FieldType int2fieldType(const uint8_t);
const std::string fieldType2string(const uint8_t);
 
enum SortFlag {
    SORT_FLAG_DESC   = 0x01,
    SORT_FLAG_STABLE = 0x02,
    SORT_FLAG_ASTEXT = 0x04
};
 
enum SortOrder {
    SORT_ASC = 0,
    SORT_DESC = SORT_FLAG_DESC
};
 
SortOrder int2sortOrder(const uint8_t);
const std::string sortOrder2string(const uint8_t);
 
struct SortOrderItem {
    std::string field;
    uint16_t    flags;
};
using SortOrderList = std::vector<SortOrderItem>;
 
#define DEFAULT_DGRIDD_LOCAL_SOCKET "/var/run/dgridd.sock"
#define DEFAULT_DGRID_SERVER_PORT 4030
#define DEFAULT_DGRID_NODE_PORT   4040
#define DEFAULT_DGRID_TIMEOUT    60000 // in msecs
#define MAX_SOCKET_PATH_LEN        108
//#define DEBUG_TRANSPORT
 
enum DGridEventType {
    ConnectionEstablished = 1,
    ConnectionFailed = 2,
    ConnectionClosed = 3,
    ConnectionBroken = 4
};
std::string eventType2string(DGridEventType);
 
class DGridEvent {
public:
    DGridEvent() = delete;
    DGridEvent(const DGridEvent&) = delete;
    DGridEvent(void* sender, DGridEventType type, std::string message);
    ~DGridEvent();
 
    void*          sender;
    DGridEventType type;
    std::string    message;
};
typedef std::function<void(std::shared_ptr<DGridEvent>)> DGridEventHandler;
 
std::string strtrim(const std::string& str);
std::wstring strtrim(const std::wstring& str);
 
template<typename T>
std::basic_string<T> strtrim(const std::basic_string<T>& str) {
    size_t first = str.find_first_not_of(' ');
    if (std::basic_string<T>::npos == first) {
        return str;
    }
    size_t last = str.find_last_not_of(' ');
    return str.substr(first, (last - first + 1));
}
 
template<class T>
T to_lower_case(const T&v)
{
    T str = v; 
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    return str; 
}
 
template<class T>
T to_upper_case(const T &v)
{
    T str = v; 
    std::transform(str.begin(), str.end(), str.begin(), ::toupper);
    return str; 
}
 
 
std::string raw_to_hexstring(const std::vector<char> &v);
std::vector<char> hexstring_to_raw(const std::string &v);
uint64_t getCurrentTime();
 
enum DGridResult {
    DGRID_UNKNOWN = 0,
    DGRID_OK   = 1,
    DGRID_FAIL = 2,
    DGRID_NOT_FOUND = 3,
    DGRID_NOT_IMPLEMENTED = 4,
    DGRID_NO_CONN   = 5,
    DGRID_TIMEOUT   = 6,
    DGRID_SEND_ERROR = 7,
    DGRID_INVALID_ARGUMENT = 8,
    DGRID_TOO_OLD   = 9,
    DGRID_NOT_ENOUGH_NODES = 10,
    DGRID_ALREADY_EXISTS   = 11,
    DGRID_OUT_OF_RANGE     = 12,
    DGRID_NOT_FOUND_INDEX_SUGGESTED = 13,
    DGRID_NOT_AUTHORIZED   = 14
};
DGridResult int2resultCode(const uint8_t);
std::string DGridResultString(DGridResult r);
 
// common packet header structure
struct Network_packet_header {
    uint8_t  magic[2]; // uint16_t big-endian
    uint8_t  type;
    uint32_t dlen; // uint32_t big-endian
    uint16_t crc;  // uint16_t big-endian
} __attribute__ (( __packed__ ));
static const uint16_t Network_packet_header_size = sizeof(Network_packet_header);
 
// common packet structure
struct Network_packet {
    Network_packet_header   header;
    char                    data[];
} __attribute__ (( __packed__ ));
 
// echo packet structure
struct Network_pkt_echo {
    uint8_t  seq;
} __attribute__ (( __packed__ ));
 
// ACK packet structure
struct Network_pkt_acknowledge {
    uint32_t req_id;   // request ID, uint32_t big-endian
    uint8_t  req_type; // request type
    uint8_t  req_subtype;
    uint8_t  result;
    char     data[];
} __attribute__ (( __packed__ ));
static const uint16_t Network_pkt_acknowledge_size = sizeof(Network_pkt_acknowledge);
 
// common request packet structure
struct Network_pkt_request {
    uint32_t id; // uint32_t big-endian
    uint8_t  type;
    uint8_t  subtype;
    char     data[];
} __attribute__ (( __packed__ ));
static const uint16_t Network_pkt_request_size = sizeof(Network_pkt_request);
 
// packet types
enum PacketType {
    PKT_INVALID  = 0,
    PKT_RESET    = 1,
    PKT_AUTH     = 2,
    PKT_AUTH_RPL = 3,
    PKT_ECHO     = 4,
    PKT_ECHO_RPL = 5,
    PKT_REQ      = 6,
    PKT_ACK      = 7
};
PacketType int2packetType(const uint8_t);
const std::string packetType2string(const uint8_t);
 
// request types
enum RequestType {
    REQ_NONE          = 0x00,
 
    REQ_KEY_VALUE     = 0x01,
    REQ_TABLE_CURSOR  = 0x05,
    REQ_TABLE         = 0x06,
    REQ_TABLE_MULTI   = 0x07,
    REQ_CLEAR_SESSION = 0x08,
    REQ_MIGRATION     = 0x09,
    REQ_STATS         = 0x0A,
    REQ_STATS_KV      = 0x0B,
    REQ_STATS_TABLE   = 0x0C,
    REQ_TABLE_CREATE  = 0x10,
    REQ_USER          = 0x11,
 
    REQ_NODE_GET_LOAD          = 0x82,
    REQ_NODE_GET_THEME_SIMPLE  = 0x83,
    REQ_NODE_APPEND_THEME_SIMPLE = 0x84,
    REQ_NODE_SYNC_THEME_SIMPLE = 0x85,
    REQ_NODE_TABLE_CHECK       = 0x86,
    REQ_NODE_PASSTHRU_TABLE_CREATE = 0x87,
    REQ_NODE_PASSTHRU_TABLE        = 0x88,
    REQ_NODE_PASSTHRU_MIGRATION    = 0x89,
    REQ_NODE_PASSTHRU_TABLE_CURSOR = 0x8A,
    REQ_NODE_PASSTHRU_TABLE_MULTI  = 0x90,
 
    REQ_INVALID     = 0xFF
};
RequestType int2requestType(const uint8_t);
const std::string requestType2string(const uint8_t);
 
// request subtypes
enum RequestSubType {
    REQ_ST_NONE          = 0x00,
    REQ_ST_GET           = 0x01,
    REQ_ST_SET           = 0x02,
    REQ_ST_DEL           = 0x03,
    REQ_ST_CREATE        = 0x04,
    REQ_ST_LIST          = 0x05,
    REQ_ST_COUNT         = 0x06,
    REQ_ST_DROP          = 0x07,
    REQ_ST_APPEND        = 0x08,
    REQ_ST_INSERT        = 0x09,
    REQ_ST_CLEAR         = 0x0A,
    REQ_ST_GET_ALL       = 0x0B,
    REQ_ST_GET_STRUCT    = 0x0C,
    REQ_ST_GET_NEXT      = 0x0D,
    REQ_ST_LIST_SESSIONS = 0x0E,
    REQ_ST_GET_ID        = 0x0F,
    REQ_ST_DEL_ADJ_DUPES = 0x10,
    REQ_ST_SORT          = 0x11,
    REQ_ST_CREATE_CURSOR = 0x12,
    REQ_ST_SECKEY_RESET   = 0x13,
    REQ_ST_SECKEY_REBUILD = 0x14,
    REQ_ST_INVALID       = 0xFF
};
 
RequestSubType int2requestSubType(const uint8_t);
const std::string requestSubType2string(const uint8_t);
 
bool isTableModifyRequest(const RequestSubType req_subtype);
 
 
enum BooleanOperator {
    BOOL_AND   = 0,
    BOOL_OR    = 1,
    BOOL_EQUAL = 2,
    BOOL_NOT   = 3
};
BooleanOperator int2booleanOperator(const uint8_t);
const std::string booleanOperator2string(const uint8_t);
 
enum ComparisionType {
    COMPARE_EQ  = 0,
    COMPARE_NE  = 1,
    COMPARE_LT  = 2,
    COMPARE_LE  = 3,
    COMPARE_GT  = 4,
    COMPARE_GE  = 5
};
 
ComparisionType int2comparisionType(const uint8_t);
const std::string comparisionType2string(const uint8_t);
 
struct ACK_data {
    ACK_data() : result(DGRID_UNKNOWN), req_id(0) {}
    ACK_data(uint32_t i) : result(DGRID_UNKNOWN), req_id(i) {}
    ACK_data(DGridResult t, uint32_t i) : result(t), req_id(i) {}
    DGridResult  result;
    RequestType  req_type;
    RequestSubType req_subtype;
    uint32_t     req_id;
    std::vector<char> payload;
    std::string  err_details;
};
 
// packet send/receive methods
int32_t sendPacket(int sockfd, const PacketType type, const std::vector<char>& data);
int32_t sendPacket(int sockfd, const PacketType type, const char* data = NULL, const uint32_t dlen = 0);
int32_t recvPacket(int sockfd, PacketType& type, std::vector<char>& data, bool& crc_ok);
 
bool sendReset(int sockfd);
bool sendRequest(int sockfd, const uint32_t req_id, const RequestType type, const RequestSubType subtype, const char* data = NULL, const uint32_t dlen = 0);
bool sendAck(int sockfd, const Network_pkt_request* req, const DGridResult type, const char* data = NULL, const uint32_t dlen = 0);
 
} // namespace DGrid
 
#endif // LIBDGRID_H