forked from pikiwidb/rediscache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_string.c
393 lines (321 loc) · 11.1 KB
/
t_string.c
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
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include "redis.h"
#include "commondef.h"
#include "commonfunc.h"
#include "zmalloc.h"
#include "object.h"
#include "sds.h"
#include "db.h"
#include "solarisfixes.h"
#include "util.h"
/*-----------------------------------------------------------------------------
* String Commands
*----------------------------------------------------------------------------*/
static int checkStringLength(long long size)
{
return (size > 512*1024*1024) ? C_ERR : C_OK;
}
/* The setGenericCommand() function implements the SET operation with different
* options and variants. This function is called in order to implement the
* following commands: SET, SETEX, PSETEX, SETNX.
*
* 'flags' changes the behavior of the command (NX or XX, see belove).
*
* 'expire' represents an expire to set in form of a Redis object as passed
* by the user. It is interpreted according to the specified 'unit'.
*
* 'ok_reply' and 'abort_reply' is what the function will reply to the client
* if the operation is performed, or when it is not because of NX or
* XX flags.
*
* If ok_reply is NULL "+OK" is used.
* If abort_reply is NULL, "$-1" is used. */
#define OBJ_SET_NO_FLAGS 0
#define OBJ_SET_NX (1<<0) /* Set if key not exists. */
#define OBJ_SET_XX (1<<1) /* Set if key exists. */
#define OBJ_SET_EX (1<<2) /* Set if time in seconds is given */
#define OBJ_SET_PX (1<<3) /* Set if time in ms in given */
static int setGenericCommand(redisCache *redis_cache, robj *kobj, robj *vobj, robj *expire, int unit, int flags) {
long long milliseconds = 0; /* initialized to avoid any harmness warning */
if (expire) {
if (getLongLongFromObject(expire, &milliseconds) != C_OK)
return REDIS_INVALID_ARG;
if (milliseconds <= 0) {
return REDIS_INVALID_ARG;
}
if (unit == UNIT_SECONDS) milliseconds *= 1000;
}
if ((flags & OBJ_SET_NX && lookupKeyWrite(redis_cache,kobj) != NULL) ||
(flags & OBJ_SET_XX && lookupKeyWrite(redis_cache,kobj) == NULL)) {
return C_ERR;
}
setKey(redis_cache, kobj, vobj);
if (expire) setExpire(redis_cache, kobj, mstime()+milliseconds);
return C_OK;
}
static int incrDecrCommand(redisCache *redis_cache, robj *kobj, long long incr, long long *ret) {
long long value, oldvalue;
robj *o, *new;
o = lookupKeyWrite(redis_cache,kobj);
if (o != NULL && checkType(o,OBJ_STRING)) return REDIS_INVALID_TYPE;
if (getLongLongFromObject(o,&value) != C_OK) return REDIS_INVALID_TYPE;
oldvalue = value;
if ((incr < 0 && oldvalue < 0 && incr < (LLONG_MIN-oldvalue)) ||
(incr > 0 && oldvalue > 0 && incr > (LLONG_MAX-oldvalue))) {
return REDIS_OVERFLOW;
}
value += incr;
*ret = value;
if (o && o->refcount == 1 && o->encoding == OBJ_ENCODING_INT &&
(value < 0 || value >= OBJ_SHARED_INTEGERS) &&
value >= LONG_MIN && value <= LONG_MAX)
{
new = o;
o->ptr = (void*)((long)value);
} else {
new = createStringObjectFromLongLong(value);
if (o) {
dbOverwrite(redis_cache,kobj,new);
} else {
dbAdd(redis_cache,kobj,new);
}
}
return C_OK;
}
static int incrbyfloatCommand(redisCache *redis_cache, robj *kobj, long double incr, long double *ret)
{
long double value;
robj *o, *new;
o = lookupKeyWrite(redis_cache,kobj);
if (o != NULL && checkType(o,OBJ_STRING)) return REDIS_INVALID_TYPE;
if (getLongDoubleFromObject(o,&value) != C_OK) return REDIS_INVALID_TYPE;
value += incr;
*ret = value;
if (isnan(value) || isinf(value)) {
return REDIS_OVERFLOW;
}
new = createStringObjectFromLongDouble(value, 1);
if (o)
dbOverwrite(redis_cache,kobj,new);
else
dbAdd(redis_cache,kobj,new);
return C_OK;
}
static int appendCommand(redisCache *redis_cache, robj *kobj, robj *vobj, unsigned long *ret)
{
size_t totlen;
robj *o, *append;
o = lookupKeyWrite(redis_cache,kobj);
if (o == NULL) {
/* Create the key */
// c->argv[2] = tryObjectEncoding(c->argv[2]);
dbAdd(redis_cache,kobj,vobj);
incrRefCount(vobj);
totlen = stringObjectLen(vobj);
} else {
/* Key exists, check type */
if (checkType(o,OBJ_STRING))
return REDIS_INVALID_TYPE;
/* "append" is an argument, so always an sds */
append = vobj;
totlen = stringObjectLen(o)+sdslen(append->ptr);
if (checkStringLength(totlen) != C_OK)
return REDIS_OVERFLOW;
/* Append the value */
o = dbUnshareStringValue(redis_cache,kobj,o);
o->ptr = sdscatlen(o->ptr,append->ptr,sdslen(append->ptr));
totlen = sdslen(o->ptr);
}
*ret = totlen;
return C_OK;
}
static int getrangeCommand(redisCache *redis_cache,
robj *kobj,
long start,
long end,
sds *val)
{
robj *o;
char *str, llbuf[32];
size_t strlen;
if ((o = lookupKeyRead(redis_cache, kobj)) == NULL) return REDIS_KEY_NOT_EXIST;
if (checkType(o,OBJ_STRING)) return REDIS_INVALID_TYPE;
if (o->encoding == OBJ_ENCODING_INT) {
str = llbuf;
strlen = ll2string(llbuf,sizeof(llbuf),(long)o->ptr);
} else {
str = o->ptr;
strlen = sdslen(str);
}
/* Convert negative indexes */
if (start < 0 && end < 0 && start > end) {
*val = sdsempty();
return C_OK;
}
if (start < 0) start = strlen+start;
if (end < 0) end = strlen+end;
if (start < 0) start = 0;
if (end < 0) end = 0;
if ((unsigned)end >= strlen) end = strlen-1;
/* Precondition: end >= 0 && end < strlen, so the only condition where
* nothing can be returned is: start > end. */
if (start > end || strlen == 0) {
*val = sdsempty();
} else {
*val = sdsnewlen((char*)str+start, end-start+1);
}
return C_OK;
}
static int setrangeCommand(redisCache *redis_cache, robj *kobj, long offset, robj *vobj, unsigned long *ret)
{
robj *o;
sds value = vobj->ptr;
if (offset < 0) return REDIS_INVALID_ARG;
o = lookupKeyWrite(redis_cache,kobj);
if (o == NULL) {
/* Return 0 when setting nothing on a non-existing string */
if (sdslen(value) == 0) {
*ret = 0;
return C_OK;
}
/* Return when the resulting string exceeds allowed size */
if (checkStringLength(offset+sdslen(value)) != C_OK) return REDIS_OVERFLOW;
o = createObject(OBJ_STRING,sdsempty());
dbAdd(redis_cache,kobj,o);
} else {
/* Key exists, check type */
if (checkType(o,OBJ_STRING)) return REDIS_INVALID_TYPE;
/* Return existing string length when setting nothing */
if (sdslen(value) == 0) {
*ret = stringObjectLen(o);
return C_OK;
}
/* Return when the resulting string exceeds allowed size */
if (checkStringLength(offset+sdslen(value)) != C_OK) return REDIS_OVERFLOW;
/* Create a copy when the object is shared or encoded. */
o = dbUnshareStringValue(redis_cache,kobj,o);
}
if (sdslen(value) > 0) {
o->ptr = sdsgrowzero(o->ptr,offset+sdslen(value));
memcpy((char*)o->ptr+offset,value,sdslen(value));
}
*ret = sdslen(o->ptr);
return C_OK;
}
int RcSet(redisCache cache, robj *key, robj *val, robj *expire)
{
if (NULL == cache || NULL == key || NULL == val) {
return REDIS_INVALID_ARG;
}
redisCache *redis_cache = (redisCache*)cache;
return setGenericCommand(redis_cache, key, val, expire, UNIT_SECONDS, OBJ_SET_NO_FLAGS);
}
int RcSetnx(redisCache cache, robj *key, robj *val, robj *expire)
{
if (NULL == cache || NULL == key || NULL == val) {
return REDIS_INVALID_ARG;
}
redisCache *redis_cache = (redisCache*)cache;
return setGenericCommand(redis_cache, key, val, expire, UNIT_SECONDS, OBJ_SET_NX);;
}
int RcSetxx(redisCache cache, robj *key, robj *val, robj *expire)
{
if (NULL == cache || NULL == key || NULL == val) {
return REDIS_INVALID_ARG;
}
redisCache *redis_cache = (redisCache*)cache;
return setGenericCommand(redis_cache, key, val, expire, UNIT_SECONDS, OBJ_SET_XX);;
}
int RcGet(redisCache cache, robj *key, robj **val)
{
if (NULL == cache || NULL == key) {
return REDIS_INVALID_ARG;
}
redisCache *redis_cache = (redisCache*)cache;
robj *vobj = lookupKeyRead(redis_cache, key);
if (NULL == vobj || OBJ_STRING != vobj->type) {
return REDIS_KEY_NOT_EXIST;
}
*val = vobj;
return C_OK;
}
int RcIncr(redisCache cache, robj *key, long long *ret)
{
if (NULL == cache || NULL == key) {
return REDIS_INVALID_ARG;
}
redisCache *redis_cache = (redisCache*)cache;
return incrDecrCommand(redis_cache, key, 1, ret);
}
int RcDecr(redisCache cache, robj *key, long long *ret)
{
if (NULL == cache || NULL == key) {
return REDIS_INVALID_ARG;
}
redisCache *redis_cache = (redisCache*)cache;
return incrDecrCommand(redis_cache, key, -1, ret);
}
int RcIncrBy(redisCache cache, robj *key, long long incr, long long *ret)
{
if (NULL == cache || NULL == key) {
return REDIS_INVALID_ARG;
}
redisCache *redis_cache = (redisCache*)cache;
return incrDecrCommand(redis_cache, key, incr, ret);
}
int RcDecrBy(redisCache cache, robj *key, long long incr, long long *ret)
{
if (NULL == cache || NULL == key) {
return REDIS_INVALID_ARG;
}
redisCache *redis_cache = (redisCache*)cache;
return incrDecrCommand(redis_cache, key, incr * (-1), ret);
}
int RcIncrByFloat(redisCache cache, robj *key, long double incr, long double *ret)
{
if (NULL == cache || NULL == key) {
return REDIS_INVALID_ARG;
}
redisCache *redis_cache = (redisCache*)cache;
return incrbyfloatCommand(redis_cache, key, incr, ret);
}
int RcAppend(redisCache cache, robj *key, robj *val, unsigned long *ret)
{
if (NULL == cache || NULL == key) {
return REDIS_INVALID_ARG;
}
redisCache *redis_cache = (redisCache*)cache;
return appendCommand(redis_cache, key, val, ret);
}
int RcGetRange(redisCache cache, robj *key, long start, long end, sds *val)
{
if (NULL == cache || NULL == key) {
return REDIS_INVALID_ARG;
}
redisCache *redis_cache = (redisCache*)cache;
return getrangeCommand(redis_cache, key, start, end, val);
}
int RcSetRange(redisCache cache, robj *key, long start, robj *val, unsigned long *ret)
{
if (NULL == cache || NULL == key) {
return REDIS_INVALID_ARG;
}
redisCache *redis_cache = (redisCache*)cache;
return setrangeCommand(redis_cache, key, start, val, ret);
}
int RcStrlen(redisCache cache, robj *key, int *val_len)
{
if (NULL == cache || NULL == key) {
return REDIS_INVALID_ARG;
}
redisCache *redis_cache = (redisCache*)cache;
robj *vobj = lookupKeyRead(redis_cache, key);
if (NULL == vobj || OBJ_STRING != vobj->type) {
return REDIS_KEY_NOT_EXIST;
}
*val_len = stringObjectLen(vobj);
return C_OK;
}