-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrxstrings.c
More file actions
661 lines (585 loc) · 22.9 KB
/
rxstrings.c
File metadata and controls
661 lines (585 loc) · 22.9 KB
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*
* rxstrings - extended Redis String commands module.
* Copyright (C) 2016 Redis Labs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "../redismodule.h"
#include "../rmutil/util.h"
#include "../rmutil/test_util.h"
#include "../rmutil/strings.h"
#define RM_MODULE_NAME "rxstrings"
/*
* CHECKAND key value [XX] <command> [arg1] [...]
* Checks a String key for value equality and sets it.
* Command can be any of the following Redis String commands: APPEND, DECR[BY]
* GETSET, INCR[BY], INCRBYFLOAT, PSETEX, SET[EX|NX].
* TODO: Rename to IF, add operators other than EQ, an optional ELSE &
* recursive, all Redis commands, multi-key :)
* Note: the key shouldn't be repeated for the executed command.
* Reply: nil if not equal or for non existing key when the XX flag is used.
* On success, the reply depends on the actual command executed.
*/
int CheckAndCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc < 4) {
return RedisModule_WrongArity(ctx);
}
RedisModule_AutoMemory(ctx);
/* Extract value, any flags and the command. */
size_t vallen, cmdlen;
int xxflag = 0, cmdidx = 3;
const char *val = RedisModule_StringPtrLen(argv[2], &vallen);
const char *cmd = RedisModule_StringPtrLen(argv[cmdidx], &cmdlen);
if (!strcasecmp("xx", cmd)) {
xxflag++;
cmdidx++;
cmd = RedisModule_StringPtrLen(argv[cmdidx], &cmdlen);
}
/* Only allow these commands. */
if (strncasecmp(cmd, "set", cmdlen) && strncasecmp(cmd, "setex", cmdlen) &&
strncasecmp(cmd, "psetex", cmdlen) && strncasecmp(cmd, "setnx", cmdlen) &&
strncasecmp(cmd, "incrby", cmdlen) &&
strncasecmp(cmd, "incrbyfloat", cmdlen) &&
strncasecmp(cmd, "incr", cmdlen) && strncasecmp(cmd, "decr", cmdlen) &&
strncasecmp(cmd, "decrby", cmdlen) &&
strncasecmp(cmd, "getset", cmdlen) &&
strncasecmp(cmd, "append", cmdlen)) {
RedisModule_ReplyWithError(ctx, "ERR invalid target command");
return REDISMODULE_ERR;
}
/* Get the target command arity. */
RedisModuleCallReply *rep =
RedisModule_Call(ctx, "COMMAND", "cs", "INFO", argv[cmdidx]);
RMUTIL_ASSERT_NOERROR(rep)
int cmdarity = RedisModule_CallReplyInteger(
RedisModule_CallReplyArrayElementByPath(rep, "1 2"));
int cmdargc = argc - cmdidx + 1;
if ((cmdarity > 0 && cmdarity != cmdargc) || (cmdargc < -cmdarity)) {
RedisModule_ReplyWithError(
ctx, "ERR wrong number of arguments for target command");
return REDISMODULE_ERR;
}
RedisModuleKey *key =
RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE);
if ((RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_EMPTY &&
RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_STRING)) {
RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
return REDISMODULE_ERR;
}
/* If XX is used, proceed only of the key exists. */
if ((RedisModule_KeyType(key) == REDISMODULE_KEYTYPE_EMPTY) && xxflag) {
RedisModule_ReplyWithNull(ctx);
return REDISMODULE_OK;
}
/* Check equality with existing value, if any. */
if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_EMPTY) {
rep = RedisModule_Call(ctx, "GET", "s", argv[1]);
RMUTIL_ASSERT_NOERROR(rep)
size_t curlen;
const char *curval = RedisModule_CallReplyStringPtr(rep, &curlen);
if (strncmp(val, curval, curlen)) {
RedisModule_ReplyWithNull(ctx);
return REDISMODULE_OK;
}
}
/* Prepare the arguments for the command. */
int i;
cmdargc--; /* -1 because of command name. */
RedisModuleString *cmdargv[argc];
cmdargv[0] = argv[1];
for (i = 1; i < cmdargc; i++) {
cmdargv[i] = argv[cmdidx + i];
}
/* Call the command and pass back the reply. */
rep = RedisModule_Call(ctx, cmd, "v", cmdargv, cmdargc);
RMUTIL_ASSERT_NOERROR(rep)
switch (RedisModule_CallReplyType(rep)) {
case REDISMODULE_REPLY_NULL:
RedisModule_ReplyWithNull(ctx);
break;
case REDISMODULE_REPLY_INTEGER:
RedisModule_ReplyWithLongLong(ctx, RedisModule_CallReplyInteger(rep));
break;
case REDISMODULE_REPLY_STRING:
RedisModule_ReplyWithString(ctx,
RedisModule_CreateStringFromCallReply(rep));
break;
}
return REDISMODULE_OK;
}
/*
* CHOP key count
* Removes characters from a value of a String key.
* If 'count' is positive, it removes from the right of the string;
* if 'count' is negative, it removes from the left of the string.
* If |count| is greater than the string length, the value is set as an empty string.
* If 'count' is zero, then the value remains unchanged.
* It is an error if the value is not a String.
* Integer Reply: the length of the string after the chop operation.
*/
int ChopCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 3) {
return RedisModule_WrongArity(ctx);
}
RedisModule_AutoMemory(ctx);
/* Obtain key and extract offset argument. */
RedisModuleKey *key =
RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE);
long long count;
if (RedisModule_StringToLongLong(argv[2], &count) != REDISMODULE_OK) {
RedisModule_ReplyWithError(ctx, "ERR invalid count");
return REDISMODULE_ERR;
}
/* Key must be a string. */
if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_STRING) {
RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
return REDISMODULE_ERR;
}
/* Calculate new length, protecting against overchop */
size_t valLen = RedisModule_ValueLength(key);
if (count == 0) {
RedisModule_ReplyWithLongLong(ctx, valLen);
return REDISMODULE_OK;
}
size_t absCount = (count < 0) ? -count : count;
size_t newLen = (valLen < absCount) ? 0 : (valLen - absCount);
/* Work around https://github.com/antirez/redis/issues/3717
* We use RM_StringSet to an empty string instead */
if (newLen == 0) {
RedisModuleString* emptyStr = RedisModule_CreateString(ctx, "", 0);
if (RedisModule_StringSet(key, emptyStr) != REDISMODULE_OK) {
RedisModule_ReplyWithError(ctx, "ERR RM_SetString empty failed");
return REDISMODULE_ERR;
}
RedisModule_ReplyWithLongLong(ctx, 0);
return REDISMODULE_OK;
}
/* If we chop from the left, we need to shift the characters,
* but only bother if we aren't zeroing the value */
if (count < 0 && newLen != 0) {
size_t dmaLen;
char* valPtr = RedisModule_StringDMA(key, &dmaLen, REDISMODULE_READ|REDISMODULE_WRITE);
memmove(valPtr, valPtr + absCount, newLen);
}
/* Truncate to new length */
if (RedisModule_StringTruncate(key, newLen) != REDISMODULE_OK) {
RedisModule_ReplyWithError(ctx, "ERR RM_StringTruncate failed");
return REDISMODULE_ERR;
}
RedisModule_ReplyWithLongLong(ctx, newLen);
return REDISMODULE_OK;
}
/*
* PREPEND key value
* Prepends a value to a String key.
* If key does not exist it is created and set as an empty string,
* so PREPEND will be similar to SET in this special case.
* Integer Reply: the length of the string after the prepend operation.
*/
int PrependCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 3) {
return RedisModule_WrongArity(ctx);
}
RedisModule_AutoMemory(ctx);
/* Obtain key and extract arg. */
RedisModuleKey *key =
RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE);
size_t argLen;
const char* argPtr = RedisModule_StringPtrLen(argv[2], &argLen);
/* SET if empty */
if (RedisModule_KeyType(key) == REDISMODULE_KEYTYPE_EMPTY) {
if (RedisModule_StringSet(key, argv[2]) == REDISMODULE_OK) {
RedisModule_ReplyWithLongLong(ctx, argLen);
return REDISMODULE_OK;
}
RedisModule_ReplyWithError(ctx, "ERR RM_StringSet failed");
return REDISMODULE_OK;
}
/* Otherwise key must be a string. */
if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_STRING) {
RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
return REDISMODULE_ERR;
}
/* Prepend the string: 1) expand string, 2) shift oldVal via memmove, 3) prepend arg */
size_t valLen = RedisModule_ValueLength(key);
size_t newLen = argLen + valLen;
if (RedisModule_StringTruncate(key, newLen) != REDISMODULE_OK) {
RedisModule_ReplyWithError(ctx, "ERR RM_StringTruncate failed");
return REDISMODULE_OK;
}
size_t dmaLen;
char* valPtr = RedisModule_StringDMA(key, &dmaLen, REDISMODULE_READ|REDISMODULE_WRITE);
memmove(valPtr + argLen, valPtr, valLen);
memcpy(valPtr, argPtr, argLen);
RedisModule_ReplyWithLongLong(ctx, newLen);
return REDISMODULE_OK;
}
/* Helper function for SETRANGERAND: uppercases a string in place. */
void stoupper(char *s) {
size_t l = strlen(s);
while (l--) s[l] = (char)toupper(s[l]);
}
/*
* SETRANGERAND key offset length
* [ALPHA|DIGIT|ALNUM|PUNC|HEX|CURSE|BINARY|READABLE|TEXT]
* [MIXEDCASE|UPPERCASE|LOWERCASE]
* Generates a random string, starting at 'offset' and of length 'length'. An
* optional character set may be provided:
* 'ALPHA' - letters only: a-z
* 'DIGIT' - digits only: 0-9
* 'ALNUM' - letters and digits
* 'PUNC' - all printable characters other than alphanumerics
* 'HEX' - hexadecimal: a-f, 0-9
* 'CURSE' - censored profanity (!@#$%^&*?)
* 'BINARY' - all characters between 0 and 255
* 'READABLE' - letters only, but more pronouncable
* 'TEXT' - this is the default, any printable character (equiv to 'ALPHA'
* + 'DIGIT' + 'PUNC')
* Additionally, an optional case argument can be provided:
* 'MIXEDCASE' - this is the default, a mix of upper and lower case. Treated
* as 'LOWERCASE' for 'HEX' and 'READABLE'.
* 'LOWERCASE' - uses only lowercase letters
* 'UPPERCASE' - uses only uppercase letters
* Reply: Integer, the length of the String after it was modified.
*/
int SetRangeRandCommand(RedisModuleCtx *ctx, RedisModuleString **argv,
int argc) {
if ((argc < 4) || (argc > 6)) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
/* Obtain key. */
RedisModuleKey *key =
RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE);
/* Key must be empty or a string. */
if ((RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_STRING &&
RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_EMPTY)) {
RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
return REDISMODULE_ERR;
}
/* Get offset and length. */
long long offset, length;
if (RedisModule_StringToLongLong(argv[2], &offset) != REDISMODULE_OK) {
RedisModule_ReplyWithError(ctx, "ERR invalid offset");
return REDISMODULE_ERR;
}
if ((offset < 0) || (offset > 512 * 1024 * 1024 - 1)) {
RedisModule_ReplyWithError(ctx, "ERR offset is out of range");
return REDISMODULE_ERR;
}
if (RedisModule_StringToLongLong(argv[3], &length) != REDISMODULE_OK) {
RedisModule_ReplyWithError(ctx, "ERR invalid length");
return REDISMODULE_ERR;
}
if (length < 1) {
RedisModule_ReplyWithError(ctx, "ERR length is out of range");
return REDISMODULE_ERR;
}
if (offset + length > 512 * 1024 * 1024) {
RedisModule_ReplyWithError(
ctx, "ERR string exceeds maximum allowed size (512MB)");
return REDISMODULE_ERR;
}
/* Character setthings. */
enum Charcase { defaultcase, mixed, lower, upper } charcase = defaultcase;
enum Chartype {
defaulttype,
alpha,
digit,
alnum,
punc,
hex,
curse,
binary,
readable,
text
} chartype = defaulttype;
static const char *charset_alpha = "aeioubcdfghjklmnpqrstvwxyz";
static const char *charset_digit = "0123456789";
static const char *charset_hex = "abcdef";
static const char *charset_punc = "!@#$%^&*?()[]{}<>_-~=+|;:,.\\/\"`'";
static const size_t maxsetlen = 97;
char *charset;
if ((charset = calloc(maxsetlen, sizeof(char))) == NULL) {
RedisModule_ReplyWithError(ctx, "ERR could not allocate memory");
return REDISMODULE_ERR;
}
/* Parse subcommands. */
int i;
for (i = 4; i < argc; i++) {
size_t subcmdlen;
const char *subcmd = RedisModule_StringPtrLen(argv[i], &subcmdlen);
if ((!strncasecmp(subcmd, "alpha", subcmdlen)) && (chartype == defaulttype))
chartype = alpha;
else if ((!strncasecmp(subcmd, "digit", subcmdlen)) &&
(chartype == defaulttype))
chartype = digit;
else if ((!strncasecmp(subcmd, "alnum", subcmdlen)) &&
(chartype == defaulttype))
chartype = alnum;
else if ((!strncasecmp(subcmd, "punc", subcmdlen)) &&
(chartype == defaulttype))
chartype = punc;
else if ((!strncasecmp(subcmd, "hex", subcmdlen)) &&
(chartype == defaulttype))
chartype = hex;
else if ((!strncasecmp(subcmd, "curse", subcmdlen)) &&
(chartype == defaulttype))
chartype = curse;
else if ((!strncasecmp(subcmd, "binary", subcmdlen)) &&
(chartype == defaulttype))
chartype = binary;
else if ((!strncasecmp(subcmd, "binary", subcmdlen)) &&
(chartype == defaulttype))
chartype = binary;
else if ((!strncasecmp(subcmd, "readable", subcmdlen)) &&
(chartype == defaulttype))
chartype = readable;
else if ((!strncasecmp(subcmd, "text", subcmdlen)) &&
(chartype == defaulttype))
chartype = text;
else if ((!strncasecmp(subcmd, "mixedcase", subcmdlen)) &&
(charcase == defaultcase))
charcase = mixed;
else if ((!strncasecmp(subcmd, "lowercase", subcmdlen)) &&
(charcase == defaultcase))
charcase = lower;
else if ((!strncasecmp(subcmd, "uppercase", subcmdlen)) &&
(charcase == defaultcase))
charcase = upper;
else {
RedisModule_ReplyWithError(
ctx, "ERR invalid character set and/or case subcommand");
return REDISMODULE_ERR;
}
}
/* Prepare the charset, start by setting the defaults. */
if (chartype == defaulttype) chartype = text;
if (charcase == defaultcase) charcase = mixed;
/* Mix the alpha elements. */
if ((chartype == alpha) || (chartype == alnum) || (chartype == text)) {
charset = strcat(charset, charset_alpha);
if ((charcase == mixed) || (charcase == upper)) stoupper(charset);
if (charcase == mixed) charset = strcat(charset, charset_alpha);
} else if (chartype == hex) {
charset = strcat(charset, charset_hex);
if (charcase == upper) stoupper(charset);
} else if (chartype == readable) {
charset = strcat(charset, charset_alpha);
if (charcase == upper) stoupper(charset);
}
/* Add the digits. */
if ((chartype == digit) || (chartype == alnum) || (chartype == text) ||
(chartype == hex)) {
charset = strcat(charset, charset_digit);
}
/* Finish with symbols if needed. */
if ((chartype == text) || (chartype == punc))
charset = strcat(charset, charset_punc);
else if (chartype == curse)
charset = strncat(charset, charset_punc, 9);
/* Get DMA pointer, truncate to new length if needed. */
size_t len;
char *val = RedisModule_StringDMA(key, &len, REDISMODULE_WRITE);
if (len < offset + length) {
RedisModule_StringTruncate(key, offset + length);
val = RedisModule_StringDMA(key, &len, REDISMODULE_WRITE);
}
/* Generate the random string. */
size_t charlen = strlen(charset);
if (chartype == binary) {
while (length--) {
val[offset++] = (char)(rand() % 256);
}
} else if (chartype == readable) {
unsigned isVowel = 0;
while (length--) {
val[offset++] =
(isVowel ? charset[rand() % 5] : charset[5 + rand() % 21]);
isVowel = !(isVowel);
}
} else
while (length--) {
unsigned i = rand() % charlen;
val[offset++] = charset[i];
}
free(charset);
RedisModule_ReplyWithLongLong(ctx, len);
return REDISMODULE_OK;
}
/*
* STRPOP key
* Gets the value at key, deletes the key, then return the value.
* Equivalent to an atomic GET and DEL.
* An error is returned if there is a value stored that is not a string.
* String Reply: the value of key, or nil if key does not exist.
*/
int StrPopCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 2) {
return RedisModule_WrongArity(ctx);
}
RedisModule_AutoMemory(ctx);
/* Obtain key */
RedisModuleKey *key =
RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE);
if (RedisModule_KeyType(key) == REDISMODULE_KEYTYPE_EMPTY) {
RedisModule_ReplyWithNull(ctx);
return REDISMODULE_OK;
}
/* Key must be a string */
if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_STRING) {
RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
return REDISMODULE_ERR;
}
/* GET the string */
size_t dmaLen;
char* valPtr = RedisModule_StringDMA(key, &dmaLen, REDISMODULE_READ);
RedisModuleString* valStr = RedisModule_CreateString(ctx, valPtr, dmaLen);
/* DELete the key */
if (RedisModule_DeleteKey(key) != REDISMODULE_OK) {
RedisModule_ReplyWithError(ctx, "ERR DeleteKey failed");
return REDISMODULE_ERR;
}
RedisModule_ReplyWithString(ctx, valStr);
return REDISMODULE_OK;
}
int testCheckAnd(RedisModuleCtx *ctx) {
RedisModuleCallReply *r;
r = RedisModule_Call(ctx, "checkand", "ccccc", "foo", "", "XX", "SET", "bar");
RMUtil_Assert(RedisModule_CallReplyType(r) == REDISMODULE_REPLY_NULL);
r = RedisModule_Call(ctx, "checkand", "cccc", "foo", "", "SET", "bar");
RMUtil_AssertReplyEquals(r,"OK");
r = RedisModule_Call(ctx, "checkand", "cccc", "foo", "", "SET", "baz");
RMUtil_Assert(RedisModule_CallReplyType(r) == REDISMODULE_REPLY_NULL);
r = RedisModule_Call(ctx, "checkand", "cccc", "foo", "bar", "SET", "baz");
RMUtil_AssertReplyEquals(r,"OK");
r = RedisModule_Call(ctx, "FLUSHALL", "");
return 0;
}
int testChop(RedisModuleCtx *ctx) {
RedisModuleCallReply *r;
r = RedisModule_Call(ctx, "set", "cc", "foo", "abcde");
RMUtil_AssertReplyEquals(r,"OK");
r = RedisModule_Call(ctx, "chop", "cc", "foo", "-2");
RMUtil_Assert(RedisModule_CallReplyInteger(r) == 3);
r = RedisModule_Call(ctx, "get", "c", "foo");
RMUtil_AssertReplyEquals(r,"cde");
r = RedisModule_Call(ctx, "FLUSHALL", "");
r = RedisModule_Call(ctx, "set", "cc", "foo", "abcde");
RMUtil_AssertReplyEquals(r,"OK");
r = RedisModule_Call(ctx, "chop", "cc", "foo", "2");
RMUtil_Assert(RedisModule_CallReplyInteger(r) == 3);
r = RedisModule_Call(ctx, "get", "c", "foo");
RMUtil_AssertReplyEquals(r,"abc");
r = RedisModule_Call(ctx, "FLUSHALL", "");
r = RedisModule_Call(ctx, "set", "cc", "foo", "abcde");
RMUtil_AssertReplyEquals(r,"OK");
r = RedisModule_Call(ctx, "chop", "cc", "foo", "-10");
RMUtil_Assert(RedisModule_CallReplyInteger(r) == 0);
r = RedisModule_Call(ctx, "get", "c", "foo");
RMUtil_AssertReplyEquals(r,"");
r = RedisModule_Call(ctx, "FLUSHALL", "");
r = RedisModule_Call(ctx, "set", "cc", "foo", "abcde");
RMUtil_AssertReplyEquals(r,"OK");
r = RedisModule_Call(ctx, "chop", "cc", "foo", "10");
RMUtil_Assert(RedisModule_CallReplyInteger(r) == 0);
r = RedisModule_Call(ctx, "get", "c", "foo");
RMUtil_AssertReplyEquals(r,"");
r = RedisModule_Call(ctx, "FLUSHALL", "");
r = RedisModule_Call(ctx, "set", "cc", "foo", "abcde");
RMUtil_AssertReplyEquals(r,"OK");
r = RedisModule_Call(ctx, "chop", "cc", "foo", "0");
RMUtil_Assert(RedisModule_CallReplyInteger(r) == 5);
r = RedisModule_Call(ctx, "get", "c", "foo");
RMUtil_AssertReplyEquals(r,"abcde");
r = RedisModule_Call(ctx, "FLUSHALL", "");
return 0;
}
int testPrepend(RedisModuleCtx *ctx) {
RedisModuleCallReply *r;
r = RedisModule_Call(ctx, "set", "cc", "foo", "fghij");
RMUtil_AssertReplyEquals(r,"OK");
r = RedisModule_Call(ctx, "prepend", "cc", "foo", "abcde");
RMUtil_Assert(RedisModule_CallReplyInteger(r) == 10);
r = RedisModule_Call(ctx, "get", "c", "foo");
RMUtil_AssertReplyEquals(r,"abcdefghij");
r = RedisModule_Call(ctx, "FLUSHALL", "");
return 0;
}
int testSetRangeRand(RedisModuleCtx *ctx) {
RedisModuleCallReply *r;
r = RedisModule_Call(ctx, "setrangerand", "ccc", "s", "0", "10");
RMUtil_Assert(RedisModule_CallReplyInteger(r) == 10);
r = RedisModule_Call(ctx, "FLUSHALL", "");
return 0;
}
int testStrPop(RedisModuleCtx *ctx) {
RedisModuleCallReply *r;
r = RedisModule_Call(ctx, "set", "cc", "foo", "abcdef");
RMUtil_AssertReplyEquals(r,"OK");
r = RedisModule_Call(ctx, "strpop", "c", "foo");
RMUtil_AssertReplyEquals(r,"abcdef");
r = RedisModule_Call(ctx, "exists", "c", "foo");
RMUtil_Assert(RedisModule_CallReplyInteger(r) == 0);
r = RedisModule_Call(ctx, "strpop", "c", "bar");
RMUtil_Assert(RedisModule_CallReplyType(r) == REDISMODULE_REPLY_NULL);
r = RedisModule_Call(ctx, "FLUSHALL", "");
return 0;
}
int TestModule(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
RedisModule_AutoMemory(ctx);
/* TODO: calling flushall but checking only for db 0. */
RedisModuleCallReply *r = RedisModule_Call(ctx, "DBSIZE", "");
if (RedisModule_CallReplyInteger(r) != 0) {
RedisModule_ReplyWithError(ctx,
"ERR test must be run on an empty instance");
return REDISMODULE_ERR;
}
RMUtil_Test(testCheckAnd);
RMUtil_Test(testChop);
RMUtil_Test(testPrepend);
RMUtil_Test(testSetRangeRand);
RMUtil_Test(testStrPop);
RedisModule_ReplyWithSimpleString(ctx, "PASS");
return REDISMODULE_OK;
}
int RedisModule_OnLoad(RedisModuleCtx *ctx) {
if (RedisModule_Init(ctx, RM_MODULE_NAME, 1, REDISMODULE_APIVER_1) ==
REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "checkand", CheckAndCommand,
"write deny-oom", 1, 1, 1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "chop", ChopCommand,
"write fast deny-oom", 1, 1,
1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "prepend", PrependCommand,
"write fast deny-oom", 1, 1,
1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "setrangerand", SetRangeRandCommand,
"write fast deny-oom", 1, 1,
1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "strpop", StrPopCommand,
"fast deny-oom", 1, 1,
1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "rxstrings.test", TestModule, "write", 0,
0, 0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
return REDISMODULE_OK;
}