forked from OpenAtomFoundation/pika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pika_hash.cc
467 lines (427 loc) · 11.7 KB
/
pika_hash.cc
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
#include "slash_string.h"
#include "nemo.h"
#include "pika_hash.h"
#include "pika_server.h"
extern PikaServer *g_pika_server;
void HDelCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHDel);
return;
}
key_ = argv[1];
PikaCmdArgsType::iterator iter = argv.begin();
iter++;
iter++;
fields_.assign(iter, argv.end());
return;
}
void HDelCmd::Do() {
int64_t num = 0;
nemo::Status s;
size_t index = 0, fields_num = fields_.size();
for (; index != fields_num; index++) {
s = g_pika_server->db()->HDel(key_, fields_[index]);
if (s.ok()) {
num++;
}
}
res_.AppendInteger(num);
return;
}
void HSetCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHSet);
return;
}
key_ = argv[1];
field_ = argv[2];
value_ = argv[3];
return;
}
void HGetCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHGet);
return;
}
key_ = argv[1];
field_ = argv[2];
return;
}
void HGetCmd::Do() {
std::string value;
nemo::Status s = g_pika_server->db()->HGet(key_, field_, &value);
if (s.ok()) {
res_.AppendStringLen(value.size());
res_.AppendContent(value);
} else if (s.IsNotFound()) {
res_.AppendContent("$-1");
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void HGetallCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHGetall);
return;
}
key_ = argv[1];
return;
}
void HGetallCmd::Do() {
std::vector<nemo::FV> fvs;
nemo::Status s = g_pika_server->db()->HGetall(key_, fvs);
if (s.ok()) {
res_.AppendArrayLen(fvs.size()*2);
std::vector<nemo::FV>::const_iterator iter;
for (iter = fvs.begin(); iter != fvs.end(); iter++) {
res_.AppendStringLen(iter->field.size());
res_.AppendContent(iter->field);
res_.AppendStringLen(iter->val.size());
res_.AppendContent(iter->val);
}
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
return;
}
static void DoHSet(const std::string &key, const std::string &field, const std::string &value, CmdRes& res, const std::string &exp_res) {
nemo::Status s = g_pika_server->db()->HSet(key, field, value);
if (s.ok()) {
res.AppendContent(exp_res);
} else {
res.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void HSetCmd::Do() {
std::string tmp;
nemo::Status s = g_pika_server->db()->HGet(key_, field_, &tmp);
if (s.ok()) {
if (tmp != value_) {
DoHSet(key_, field_, value_, res_, ":0");
} else {
res_.AppendContent(":0");
}
} else if (s.IsNotFound()) {
DoHSet(key_, field_, value_, res_, ":1");
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
return;
}
void HExistsCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHExists);
return;
}
key_ = argv[1];
field_ = argv[2];
return;
}
void HExistsCmd::Do() {
std::string value;
bool exist = g_pika_server->db()->HExists(key_, field_);
if (exist) {
res_.AppendContent(":1");
} else {
res_.AppendContent(":0");
}
}
void HIncrbyCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHIncrby);
return;
}
key_ = argv[1];
field_ = argv[2];
if (argv[3].find(" ") != std::string::npos || !slash::string2l(argv[3].data(), argv[3].size(), &by_)) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
return;
}
void HIncrbyCmd::Do() {
std::string new_value;
nemo::Status s = g_pika_server->db()->HIncrby(key_, field_, by_, new_value);
if (s.ok() || s.IsNotFound()) {
res_.AppendContent(":" + new_value);
} else if (s.IsCorruption() && s.ToString() == "Corruption: value is not integer") {
res_.SetRes(CmdRes::kInvalidInt);
} else if (s.IsInvalidArgument()) {
res_.SetRes(CmdRes::kOverFlow);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
return;
}
void HIncrbyfloatCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHIncrbyfloat);
return;
}
key_ = argv[1];
field_ = argv[2];
if (argv[3].find(" ") != std::string::npos || !slash::string2d(argv[3].data(), argv[3].size(), &by_)) {
res_.SetRes(CmdRes::kInvalidFloat);
return;
}
return;
}
void HIncrbyfloatCmd::Do() {
std::string new_value;
nemo::Status s = g_pika_server->db()->HIncrbyfloat(key_, field_, by_, new_value);
if (s.ok()) {
res_.AppendStringLen(new_value.size());
res_.AppendContent(new_value);
} else if (s.IsCorruption() && s.ToString() == "Corruption: value is not float") {
res_.SetRes(CmdRes::kInvalidFloat);
} else if (s.IsInvalidArgument()) {
res_.SetRes(CmdRes::kOverFlow);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
return;
}
void HKeysCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHKeys);
return;
}
key_ = argv[1];
return;
}
void HKeysCmd::Do() {
std::vector<std::string> fields;
nemo::Status s = g_pika_server->db()->HKeys(key_, fields);
if (s.ok()) {
res_.AppendArrayLen(fields.size());
std::vector<std::string>::const_iterator iter;
for (iter = fields.begin(); iter != fields.end(); iter++) {
res_.AppendStringLen(iter->size());
res_.AppendContent(*iter);
}
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
return;
}
void HLenCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHLen);
return;
}
key_ = argv[1];
return;
}
void HLenCmd::Do() {
int64_t len = 0;
len = g_pika_server->db()->HLen(key_);
if (len >= 0) {
res_.AppendInteger(len);
} else {
res_.SetRes(CmdRes::kErrOther, "something wrong in hlen");
}
return;
}
void HMgetCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHMget);
return;
}
key_ = argv[1];
PikaCmdArgsType::iterator iter = argv.begin();
iter++;
iter++;
fields_.assign(iter, argv.end());
return;
}
void HMgetCmd::Do() {
std::vector<nemo::FVS> fvs_v;
nemo::Status s = g_pika_server->db()->HMGet(key_, fields_, fvs_v);
if (s.ok()) {
res_.AppendArrayLen(fvs_v.size());
std::vector<nemo::FVS>::const_iterator iter = fvs_v.begin();
for (; iter != fvs_v.end(); iter++) {
if ((iter->status).ok()) {
res_.AppendStringLen(iter->val.size());
res_.AppendContent(iter->val);
} else {
res_.AppendContent("$-1");
}
}
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
return;
}
void HMsetCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHMset);
return;
}
key_ = argv[1];
size_t argc = argv.size();
if (argc % 2 != 0) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHMset);
return;
}
size_t index = 2;
fv_v_.clear();
for (; index < argc; index += 2) {
fv_v_.push_back({argv[index], argv[index+1]});
}
return;
}
void HMsetCmd::Do() {
nemo::Status s = g_pika_server->db()->HMSet(key_, fv_v_);
if (s.ok()) {
res_.SetRes(CmdRes::kOk);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
return;
}
void HSetnxCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHSetnx);
return;
}
key_ = argv[1];
field_ = argv[2];
value_ = argv[3];
return;
}
void HSetnxCmd::Do() {
nemo::Status s = g_pika_server->db()->HSetnx(key_, field_, value_);
if (s.ok()) {
res_.AppendContent(":1");
} else if (s.IsCorruption() && s.ToString() == "Corruption: Already Exist") {
res_.AppendContent(":0");
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void HStrlenCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHStrlen);
return;
}
key_ = argv[1];
field_ = argv[2];
return;
}
void HStrlenCmd::Do() {
int64_t len = g_pika_server->db()->HStrlen(key_, field_);
if (len >= 0) {
res_.AppendInteger(len);
} else {
res_.SetRes(CmdRes::kErrOther, "something wrong in hstrlen");
}
return;
}
void HValsCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHVals);
return;
}
key_ = argv[1];
return;
}
void HValsCmd::Do() {
std::vector<std::string> vals;
nemo::Status s = g_pika_server->db()->HVals(key_, vals);
if (s.ok()) {
res_.AppendArrayLen(vals.size());
std::vector<std::string>::const_iterator iter = vals.begin();
for (; iter != vals.end(); iter++) {
res_.AppendStringLen(iter->size());
res_.AppendContent(*iter);
}
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
return;
}
void HScanCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHScan);
return;
}
key_ = argv[1];
if (!slash::string2l(argv[2].data(), argv[2].size(), &cursor_)) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
size_t index = 3, argc = argv.size();
while (index < argc) {
std::string opt = slash::StringToLower(argv[index]);
if (opt == "match" || opt == "count") {
index++;
if (index >= argc) {
res_.SetRes(CmdRes::kSyntaxErr);
return;
}
if (opt == "match") {
pattern_ = argv[index];
} else if (!slash::string2l(argv[index].data(), argv[index].size(), &count_)) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
} else {
res_.SetRes(CmdRes::kSyntaxErr);
return;
}
index++;
}
if (count_ < 0) {
res_.SetRes(CmdRes::kSyntaxErr);
return;
}
return;
}
void HScanCmd::Do() {
int64_t hlen = g_pika_server->db()->HLen(key_);
if (hlen >= 0 && cursor_ >= hlen) {
cursor_ = 0;
}
if (hlen <= 512) {
count_ = 512;
}
nemo::HIterator *iter = g_pika_server->db()->HScan(key_, "", "", -1);
iter->Skip(cursor_);
if (!iter->Valid()) {
delete iter;
iter = g_pika_server->db()->HScan(key_, "", "", -1);
cursor_ = 0;
}
std::vector<nemo::FV> fv_v;
bool use_pattern = false;
if (pattern_ != "*") {
use_pattern = true;
}
for (; iter->Valid() && count_; iter->Next()) {
if (use_pattern && !slash::stringmatchlen(pattern_.data(), pattern_.size(), iter->field().data(), iter->field().size(), 0)) {
continue;
}
count_--;
cursor_++;
fv_v.push_back({iter->field(), iter->value()});
}
if (fv_v.size() <= 0 || !(iter->Valid())) {
cursor_ = 0;
}
delete iter;
res_.AppendContent("*2");
char buf[32];
int32_t len = slash::ll2string(buf, sizeof(buf), cursor_);
res_.AppendStringLen(len);
res_.AppendContent(buf);
res_.AppendArrayLen(fv_v.size()*2);
std::vector<nemo::FV>::const_iterator iter_fv = fv_v.begin();
for (; iter_fv != fv_v.end(); iter_fv++) {
res_.AppendStringLen(iter_fv->field.size());
res_.AppendContent(iter_fv->field);
res_.AppendStringLen(iter_fv->val.size());
res_.AppendContent(iter_fv->val);
}
return;
}