forked from OpenAtomFoundation/pika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pika_command.h
504 lines (458 loc) · 15.8 KB
/
pika_command.h
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#ifndef PIKA_COMMAND_H_
#define PIKA_COMMAND_H_
#include <deque>
#include <string>
#include <memory>
#include <unordered_map>
#include "slash/include/slash_string.h"
#include "pink/include/redis_conn.h"
const std::string kPikaBinlogMagic = "__PIKA_X#$SKGI";
//Constant for command name
//Admin
const std::string kCmdNameSlaveof = "slaveof";
const std::string kCmdNameTrysync = "trysync";
const std::string kCmdNameInternalTrysync = "internaltrysync";
const std::string kCmdNameAuth = "auth";
const std::string kCmdNameBgsave = "bgsave";
const std::string kCmdNameBgsaveoff = "bgsaveoff";
const std::string kCmdNameCompact = "compact";
const std::string kCmdNamePurgelogsto = "purgelogsto";
const std::string kCmdNamePing = "ping";
const std::string kCmdNameSelect = "select";
const std::string kCmdNameFlushall = "flushall";
const std::string kCmdNameFlushdb = "flushdb";
const std::string kCmdNameReadonly = "readonly";
const std::string kCmdNameClient = "client";
const std::string kCmdNameShutdown = "shutdown";
const std::string kCmdNameInfo = "info";
const std::string kCmdNameConfig = "config";
const std::string kCmdNameMonitor = "monitor";
const std::string kCmdNameDbsize = "dbsize";
const std::string kCmdNameTime = "time";
const std::string kCmdNameDelbackup = "delbackup";
#ifdef TCMALLOC_EXTENSION
const std::string kCmdNameTcmalloc = "tcmalloc";
#endif
//Migrate slot
const std::string kCmdNameSlotsMgrtSlot = "slotsmgrtslot";
const std::string kCmdNameSlotsMgrtTagSlot = "slotsmgrttagslot";
const std::string kCmdNameSlotsMgrtOne = "slotsmgrtone";
const std::string kCmdNameSlotsMgrtTagOne = "slotsmgrttagone";
const std::string kCmdNameSlotsInfo = "slotsinfo";
const std::string kCmdNameSlotsHashKey = "slotshashkey";
const std::string kCmdNameSlotsReload = "slotsreload";
const std::string kCmdNameSlotsReloadOff = "slotsreloadoff";
const std::string kCmdNameSlotsDel = "slotsdel";
const std::string kCmdNameSlotsScan = "slotsscan";
const std::string kCmdNameSlotsCleanup = "slotscleanup";
const std::string kCmdNameSlotsCleanupOff = "slotscleanupoff";
const std::string kCmdNameSlotsMgrtTagSlotAsync = "slotsmgrttagslot-async";
const std::string kCmdNameSlotsMgrtSlotAsync = "slotsmgrtslot-async";
const std::string kCmdNameSlotsMgrtExecWrapper = "slotsmgrt-exec-wrapper";
const std::string kCmdNameSlotsMgrtAsyncStatus = "slotsmgrt-async-status";
const std::string kCmdNameSlotsMgrtAsyncCancel = "slotsmgrt-async-cancel";
//Kv
const std::string kCmdNameSet = "set";
const std::string kCmdNameGet = "get";
const std::string kCmdNameDel = "del";
const std::string kCmdNameIncr = "incr";
const std::string kCmdNameIncrby = "incrby";
const std::string kCmdNameIncrbyfloat = "incrbyfloat";
const std::string kCmdNameDecr = "decr";
const std::string kCmdNameDecrby = "decrby";
const std::string kCmdNameGetset = "getset";
const std::string kCmdNameAppend = "append";
const std::string kCmdNameMget = "mget";
const std::string kCmdNameKeys = "keys";
const std::string kCmdNameSetnx = "setnx";
const std::string kCmdNameSetex = "setex";
const std::string kCmdNameMset = "mset";
const std::string kCmdNameMsetnx = "msetnx";
const std::string kCmdNameGetrange = "getrange";
const std::string kCmdNameSetrange = "setrange";
const std::string kCmdNameStrlen = "strlen";
const std::string kCmdNameExists = "exists";
const std::string kCmdNameExpire = "expire";
const std::string kCmdNamePexpire = "pexpire";
const std::string kCmdNameExpireat = "expireat";
const std::string kCmdNamePexpireat = "pexpireat";
const std::string kCmdNameTtl = "ttl";
const std::string kCmdNamePttl = "pttl";
const std::string kCmdNamePersist = "persist";
const std::string kCmdNameType = "type";
const std::string kCmdNameScan = "scan";
//Hash
const std::string kCmdNameHDel = "hdel";
const std::string kCmdNameHSet = "hset";
const std::string kCmdNameHGet = "hget";
const std::string kCmdNameHGetall = "hgetall";
const std::string kCmdNameHExists = "hexists";
const std::string kCmdNameHIncrby = "hincrby";
const std::string kCmdNameHIncrbyfloat = "hincrbyfloat";
const std::string kCmdNameHKeys = "hkeys";
const std::string kCmdNameHLen = "hlen";
const std::string kCmdNameHMget = "hmget";
const std::string kCmdNameHMset = "hmset";
const std::string kCmdNameHSetnx = "hsetnx";
const std::string kCmdNameHStrlen = "hstrlen";
const std::string kCmdNameHVals = "hvals";
const std::string kCmdNameHScan = "hscan";
//List
const std::string kCmdNameLIndex = "lindex";
const std::string kCmdNameLInsert = "linsert";
const std::string kCmdNameLLen = "llen";
const std::string kCmdNameLPop = "lpop";
const std::string kCmdNameLPush = "lpush";
const std::string kCmdNameLPushx = "lpushx";
const std::string kCmdNameLRange = "lrange";
const std::string kCmdNameLRem = "lrem";
const std::string kCmdNameLSet = "lset";
const std::string kCmdNameLTrim = "ltrim";
const std::string kCmdNameRPop = "rpop";
const std::string kCmdNameRPopLPush = "rpoplpush";
const std::string kCmdNameRPush = "rpush";
const std::string kCmdNameRPushx = "rpushx";
//BitMap
const std::string kCmdNameBitSet = "setbit";
const std::string kCmdNameBitGet = "getbit";
const std::string kCmdNameBitPos = "bitpos";
const std::string kCmdNameBitOp = "bitop";
const std::string kCmdNameBitCount = "bitcount";
//Zset
const std::string kCmdNameZAdd = "zadd";
const std::string kCmdNameZCard = "zcard";
const std::string kCmdNameZScan = "zscan";
const std::string kCmdNameZIncrby = "zincrby";
const std::string kCmdNameZRange = "zrange";
const std::string kCmdNameZRangebyscore = "zrangebyscore";
const std::string kCmdNameZCount = "zcount";
const std::string kCmdNameZRem = "zrem";
const std::string kCmdNameZUnionstore = "zunionstore";
const std::string kCmdNameZInterstore = "zinterstore";
const std::string kCmdNameZRank = "zrank";
const std::string kCmdNameZRevrank = "zrevrank";
const std::string kCmdNameZScore = "zscore";
const std::string kCmdNameZRevrange = "zrevrange";
const std::string kCmdNameZRevrangebyscore = "zrevrangebyscore";
const std::string kCmdNameZRangebylex = "zrangebylex";
const std::string kCmdNameZRevrangebylex = "zrevrangebylex";
const std::string kCmdNameZLexcount = "zlexcount";
const std::string kCmdNameZRemrangebyrank = "zremrangebyrank";
const std::string kCmdNameZRemrangebylex = "zremrangebylex";
const std::string kCmdNameZRemrangebyscore = "zremrangebyscore";
//Set
const std::string kCmdNameSAdd = "sadd";
const std::string kCmdNameSPop = "spop";
const std::string kCmdNameSCard = "scard";
const std::string kCmdNameSMembers = "smembers";
const std::string kCmdNameSScan = "sscan";
const std::string kCmdNameSRem = "srem";
const std::string kCmdNameSUnion = "sunion";
const std::string kCmdNameSUnionstore = "sunionstore";
const std::string kCmdNameSInter = "sinter";
const std::string kCmdNameSInterstore = "sinterstore";
const std::string kCmdNameSIsmember = "sismember";
const std::string kCmdNameSDiff = "sdiff";
const std::string kCmdNameSDiffstore = "sdiffstore";
const std::string kCmdNameSMove = "smove";
const std::string kCmdNameSRandmember = "srandmember";
//HyperLogLog
const std::string kCmdNamePfAdd = "pfadd";
const std::string kCmdNamePfCount = "pfcount";
const std::string kCmdNamePfMerge = "pfmerge";
//GEO
const std::string kCmdNameGeoAdd = "geoadd";
const std::string kCmdNameGeoPos = "geopos";
const std::string kCmdNameGeoDist = "geodist";
const std::string kCmdNameGeoHash = "geohash";
const std::string kCmdNameGeoRadius = "georadius";
const std::string kCmdNameGeoRadiusByMember = "georadiusbymember";
//Pub/Sub
const std::string kCmdNamePublish = "publish";
const std::string kCmdNameSubscribe = "subscribe";
const std::string kCmdNameUnSubscribe = "unsubscribe";
const std::string kCmdNamePubSub = "pubsub";
const std::string kCmdNamePSubscribe = "psubscribe";
const std::string kCmdNamePUnSubscribe = "punsubscribe";
typedef pink::RedisCmdArgsType PikaCmdArgsType;
static const int RAW_ARGS_LEN = 1024 * 1024;
enum CmdFlagsMask {
kCmdFlagsMaskRW = 1,
kCmdFlagsMaskType = 30,
kCmdFlagsMaskLocal = 32,
kCmdFlagsMaskSuspend = 64,
kCmdFlagsMaskPrior = 128,
kCmdFlagsMaskAdminRequire = 256
};
enum CmdFlags {
kCmdFlagsRead = 0, //default rw
kCmdFlagsWrite = 1,
kCmdFlagsAdmin = 0, //default type
kCmdFlagsKv = 2,
kCmdFlagsHash = 4,
kCmdFlagsList = 6,
kCmdFlagsSet = 8,
kCmdFlagsZset = 10,
kCmdFlagsBit = 12,
kCmdFlagsHyperLogLog = 14,
kCmdFlagsGeo = 16,
kCmdFlagsPubSub = 18,
kCmdFlagsNoLocal = 0, //default nolocal
kCmdFlagsLocal = 32,
kCmdFlagsNoSuspend = 0, //default nosuspend
kCmdFlagsSuspend = 64,
kCmdFlagsNoPrior = 0, //default noprior
kCmdFlagsPrior = 128,
kCmdFlagsNoAdminRequire = 0, //default no need admin
kCmdFlagsAdminRequire = 256
};
class CmdInfo {
public:
CmdInfo(const std::string _name, int _num, uint16_t _flag)
: name_(_name), arity_(_num), flag_(_flag) {}
bool CheckArg(int num) const {
if ((arity_ > 0 && num != arity_) || (arity_ < 0 && num < -arity_)) {
return false;
}
return true;
}
bool is_write() const {
return ((flag_ & kCmdFlagsMaskRW) == kCmdFlagsWrite);
}
uint16_t flag_type() const {
return flag_ & kCmdFlagsMaskType;
}
bool is_local() const {
return ((flag_ & kCmdFlagsMaskLocal) == kCmdFlagsLocal);
}
// Others need to be suspended when a suspend command run
bool is_suspend() const {
return ((flag_ & kCmdFlagsMaskSuspend) == kCmdFlagsSuspend);
}
bool is_prior() const {
return ((flag_ & kCmdFlagsMaskPrior) == kCmdFlagsPrior);
}
// Must with admin auth
bool is_admin_require() const {
return ((flag_ & kCmdFlagsMaskAdminRequire) == kCmdFlagsAdminRequire);
}
std::string name() const {
return name_;
}
private:
std::string name_;
int arity_;
uint16_t flag_;
CmdInfo(const CmdInfo&);
CmdInfo& operator=(const CmdInfo&);
};
void inline RedisAppendContent(std::string& str, const std::string& value);
void inline RedisAppendLen(std::string& str, int ori, const std::string &prefix);
const std::string kNewLine = "\r\n";
class CmdRes {
public:
enum CmdRet {
kNone = 0,
kOk,
kPong,
kSyntaxErr,
kInvalidInt,
kInvalidBitInt,
kInvalidBitOffsetInt,
kInvalidBitPosArgument,
kWrongBitOpNotNum,
kInvalidFloat,
kOverFlow,
kNotFound,
kOutOfRange,
kInvalidPwd,
kNoneBgsave,
kPurgeExist,
kInvalidParameter,
kWrongNum,
kInvalidIndex,
kInvalidDbType,
kErrOther,
};
CmdRes():ret_(kNone) {}
bool none() const {
return ret_ == kNone && message_.empty();
}
bool ok() const {
return ret_ == kOk || ret_ == kNone;
}
void clear() {
message_.clear();
ret_ = kNone;
}
std::string raw_message() const {
return message_;
}
std::string message() const {
std::string result;
switch (ret_) {
case kNone:
return message_;
case kOk:
return "+OK\r\n";
case kPong:
return "+PONG\r\n";
case kSyntaxErr:
return "-ERR syntax error\r\n";
case kInvalidInt:
return "-ERR value is not an integer or out of range\r\n";
case kInvalidBitInt:
return "-ERR bit is not an integer or out of range\r\n";
case kInvalidBitOffsetInt:
return "-ERR bit offset is not an integer or out of range\r\n";
case kWrongBitOpNotNum:
return "-ERR BITOP NOT must be called with a single source key.\r\n";
case kInvalidBitPosArgument:
return "-ERR The bit argument must be 1 or 0.\r\n";
case kInvalidFloat:
return "-ERR value is not an float\r\n";
case kOverFlow:
return "-ERR increment or decrement would overflow\r\n";
case kNotFound:
return "-ERR no such key\r\n";
case kOutOfRange:
return "-ERR index out of range\r\n";
case kInvalidPwd:
return "-ERR invalid password\r\n";
case kNoneBgsave:
return "-ERR No BGSave Works now\r\n";
case kPurgeExist:
return "-ERR binlog already in purging...\r\n";
case kInvalidParameter:
return "-ERR Invalid Argument\r\n";
case kWrongNum:
result = "-ERR wrong number of arguments for '";
result.append(message_);
result.append("' command\r\n");
break;
case kInvalidIndex:
result = "-ERR invalid DB index\r\n";
break;
case kInvalidDbType:
result = "-ERR invalid DB type\r\n";
break;
case kErrOther:
result = "-ERR ";
result.append(message_);
result.append(kNewLine);
break;
default:
break;
}
return result;
}
// Inline functions for Create Redis protocol
void AppendStringLen(int ori) {
RedisAppendLen(message_, ori, "$");
}
void AppendArrayLen(int ori) {
RedisAppendLen(message_, ori, "*");
}
void AppendInteger(int ori) {
RedisAppendLen(message_, ori, ":");
}
void AppendContent(const std::string &value) {
RedisAppendContent(message_, value);
}
void AppendString(const std::string &value) {
AppendStringLen(value.size());
AppendContent(value);
}
void AppendStringRaw(std::string &value) {
message_.append(value);
}
void SetRes(CmdRet _ret, const std::string content = "") {
ret_ = _ret;
if (!content.empty()) {
message_ = content;
}
}
private:
std::string message_;
CmdRet ret_;
};
class Cmd {
public:
Cmd() {}
virtual ~Cmd() {}
virtual void Do() = 0;
void Initial(PikaCmdArgsType &argvs, const CmdInfo* const ptr_info) {
res_.clear(); // Clear res content
Clear(); // Clear cmd, Derived class can has own implement
DoInitial(argvs, ptr_info);
};
CmdRes& res() {
return res_;
}
virtual std::string ToBinlog(
const PikaCmdArgsType& argv,
const std::string& server_id,
const std::string& binlog_info,
bool need_send_to_hub) {
std::string res;
res.reserve(RAW_ARGS_LEN);
RedisAppendLen(res, argv.size() + 4, "*");
for (auto& v : argv) {
RedisAppendLen(res, v.size(), "$");
RedisAppendContent(res, v);
}
AppendAffiliatedInfo(res, server_id, binlog_info, need_send_to_hub);
return res;
}
protected:
CmdRes res_;
void AppendAffiliatedInfo(
std::string& res,
const std::string& server_id,
const std::string& binlog_info,
bool need_send_to_hub) {
// kPikaBinlogMagic
RedisAppendLen(res, kPikaBinlogMagic.size(), "$");
RedisAppendContent(res, kPikaBinlogMagic);
// server_id
RedisAppendLen(res, server_id.size(), "$");
RedisAppendContent(res, server_id);
// binlog_info
RedisAppendLen(res, binlog_info.size(), "$");
RedisAppendContent(res, binlog_info);
// need_send_to_hub
std::string v = need_send_to_hub ? "1" : "0";
RedisAppendLen(res, v.size(), "$");
RedisAppendContent(res, v);
}
private:
virtual void DoInitial(PikaCmdArgsType &argvs, const CmdInfo* const ptr_info) = 0;
virtual void Clear() {};
Cmd(const Cmd&);
Cmd& operator=(const Cmd&);
};
typedef std::unordered_map<std::string, Cmd*> CmdTable;
// Method for CmdInfo Table
void InitCmdInfoTable();
const CmdInfo* GetCmdInfo(const std::string& opt);
void DestoryCmdInfoTable();
// Method for Cmd Table
void InitCmdTable(CmdTable* cmd_table);
Cmd* GetCmdFromTable(const std::string& opt, const CmdTable& cmd_table);
void DestoryCmdTable(CmdTable* cmd_table);
void RedisAppendContent(std::string& str, const std::string& value) {
str.append(value.data(), value.size());
str.append(kNewLine);
}
void RedisAppendLen(std::string& str, int ori, const std::string &prefix) {
char buf[32];
slash::ll2string(buf, 32, static_cast<long long>(ori));
str.append(prefix);
str.append(buf);
str.append(kNewLine);
}
#endif