]> git.saurik.com Git - redis.git/blame - src/t_string.c
Merge branch 'master' of github.com:antirez/redis
[redis.git] / src / t_string.c
CommitLineData
3c1bf495 1#include <limits.h>
e2641e09 2#include "redis.h"
3
4/*-----------------------------------------------------------------------------
5 * String Commands
6 *----------------------------------------------------------------------------*/
7
9f9e1cea
PN
8static int checkStringLength(redisClient *c, long long size) {
9 if (size > 512*1024*1024) {
10 addReplyError(c,"string exceeds maximum allowed size (512MB)");
11 return REDIS_ERR;
12 }
13 return REDIS_OK;
14}
15
e2641e09 16void setGenericCommand(redisClient *c, int nx, robj *key, robj *val, robj *expire) {
17 int retval;
18 long seconds = 0; /* initialized to avoid an harmness warning */
19
20 if (expire) {
21 if (getLongFromObjectOrReply(c, expire, &seconds, NULL) != REDIS_OK)
22 return;
23 if (seconds <= 0) {
3ab20376 24 addReplyError(c,"invalid expire time in SETEX");
e2641e09 25 return;
26 }
27 }
28
e2641e09 29 retval = dbAdd(c->db,key,val);
30 if (retval == REDIS_ERR) {
31 if (!nx) {
32 dbReplace(c->db,key,val);
33 incrRefCount(val);
34 } else {
35 addReply(c,shared.czero);
36 return;
37 }
38 } else {
39 incrRefCount(val);
40 }
5b4bff9c 41 touchWatchedKey(c->db,key);
e2641e09 42 server.dirty++;
43 removeExpire(c->db,key);
44 if (expire) setExpire(c->db,key,time(NULL)+seconds);
45 addReply(c, nx ? shared.cone : shared.ok);
46}
47
48void setCommand(redisClient *c) {
75b41de8 49 c->argv[2] = tryObjectEncoding(c->argv[2]);
e2641e09 50 setGenericCommand(c,0,c->argv[1],c->argv[2],NULL);
51}
52
53void setnxCommand(redisClient *c) {
75b41de8 54 c->argv[2] = tryObjectEncoding(c->argv[2]);
e2641e09 55 setGenericCommand(c,1,c->argv[1],c->argv[2],NULL);
56}
57
58void setexCommand(redisClient *c) {
75b41de8 59 c->argv[3] = tryObjectEncoding(c->argv[3]);
e2641e09 60 setGenericCommand(c,0,c->argv[1],c->argv[3],c->argv[2]);
61}
62
63int getGenericCommand(redisClient *c) {
64 robj *o;
65
66 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL)
67 return REDIS_OK;
68
69 if (o->type != REDIS_STRING) {
70 addReply(c,shared.wrongtypeerr);
71 return REDIS_ERR;
72 } else {
73 addReplyBulk(c,o);
74 return REDIS_OK;
75 }
76}
77
78void getCommand(redisClient *c) {
79 getGenericCommand(c);
80}
81
82void getsetCommand(redisClient *c) {
83 if (getGenericCommand(c) == REDIS_ERR) return;
75b41de8 84 c->argv[2] = tryObjectEncoding(c->argv[2]);
e2641e09 85 dbReplace(c->db,c->argv[1],c->argv[2]);
86 incrRefCount(c->argv[2]);
5b4bff9c 87 touchWatchedKey(c->db,c->argv[1]);
e2641e09 88 server.dirty++;
89 removeExpire(c->db,c->argv[1]);
3c1bf495
PN
90}
91
92static int getBitOffsetFromArgument(redisClient *c, robj *o, size_t *offset) {
93 long long loffset;
94 char *err = "bit offset is not an integer or out of range";
95
96 if (getLongLongFromObjectOrReply(c,o,&loffset,err) != REDIS_OK)
97 return REDIS_ERR;
98
586500c0 99 /* Limit offset to SIZE_T_MAX or 512MB in bytes */
3c1bf495
PN
100 if ((loffset < 0) ||
101 ((unsigned long long)loffset >= (unsigned)SIZE_T_MAX) ||
076f88d6 102 ((unsigned long long)loffset >> 3) >= (512*1024*1024))
3c1bf495
PN
103 {
104 addReplyError(c,err);
105 return REDIS_ERR;
106 }
107
108 *offset = (size_t)loffset;
109 return REDIS_OK;
110}
111
112void setbitCommand(redisClient *c) {
113 robj *o;
eae33c1c 114 char *err = "bit is not an integer or out of range";
3c1bf495 115 size_t bitoffset;
eae33c1c
PN
116 long long bitvalue;
117 int byte, bit, on;
3c1bf495
PN
118
119 if (getBitOffsetFromArgument(c,c->argv[2],&bitoffset) != REDIS_OK)
120 return;
121
eae33c1c
PN
122 if (getLongLongFromObjectOrReply(c,c->argv[3],&bitvalue,err) != REDIS_OK)
123 return;
124
125 /* A bit can only be set to be on or off... */
126 if (bitvalue & ~1) {
127 addReplyError(c,err);
3c1bf495
PN
128 return;
129 }
130
131 o = lookupKeyWrite(c->db,c->argv[1]);
132 if (o == NULL) {
eae33c1c 133 o = createObject(REDIS_STRING,sdsempty());
3c1bf495
PN
134 dbAdd(c->db,c->argv[1],o);
135 } else {
136 if (checkType(c,o,REDIS_STRING)) return;
137
138 /* Create a copy when the object is shared or encoded. */
139 if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) {
140 robj *decoded = getDecodedObject(o);
141 o = createStringObject(decoded->ptr, sdslen(decoded->ptr));
142 decrRefCount(decoded);
143 dbReplace(c->db,c->argv[1],o);
144 }
3c1bf495 145 }
eae33c1c
PN
146
147 byte = bitoffset >> 3;
148 bit = 7 - (bitoffset & 0x7);
149 on = bitvalue & 0x1;
cc209063 150 o->ptr = sdsgrowzero(o->ptr,byte+1);
eae33c1c
PN
151 ((char*)o->ptr)[byte] |= on << bit;
152 ((char*)o->ptr)[byte] &= ~((!on) << bit);
153
3c1bf495
PN
154 touchWatchedKey(c->db,c->argv[1]);
155 server.dirty++;
156 addReply(c,shared.cone);
157}
158
159void getbitCommand(redisClient *c) {
160 robj *o;
161 size_t bitoffset, byte, bitmask;
162 int on = 0;
163 char llbuf[32];
164
165 if (getBitOffsetFromArgument(c,c->argv[2],&bitoffset) != REDIS_OK)
166 return;
167
168 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
169 checkType(c,o,REDIS_STRING)) return;
170
171 byte = bitoffset >> 3;
172 bitmask = 1 << (7 - (bitoffset & 0x7));
173 if (o->encoding != REDIS_ENCODING_RAW) {
174 if (byte < (size_t)ll2string(llbuf,sizeof(llbuf),(long)o->ptr))
175 on = llbuf[byte] & bitmask;
176 } else {
177 if (byte < sdslen(o->ptr))
178 on = ((sds)o->ptr)[byte] & bitmask;
179 }
180 addReply(c, on ? shared.cone : shared.czero);
e2641e09 181}
182
9f9e1cea
PN
183void setrangeCommand(redisClient *c) {
184 robj *o;
185 long offset;
186 sds value = c->argv[3]->ptr;
187
188 if (getLongFromObjectOrReply(c,c->argv[2],&offset,NULL) != REDIS_OK)
189 return;
190
191 o = lookupKeyWrite(c->db,c->argv[1]);
192 if (o == NULL) {
193 /* Negative offset is always 0 for non-existing keys */
194 if (offset < 0) offset = 0;
195
196 /* Return 0 when setting nothing on a non-existing string */
197 if (sdslen(value) == 0) {
198 addReply(c,shared.czero);
199 return;
200 }
201
202 /* Return when the resulting string exceeds allowed size */
203 if (checkStringLength(c,offset+sdslen(value)) != REDIS_OK)
204 return;
205
206 o = createObject(REDIS_STRING,sdsempty());
207 dbAdd(c->db,c->argv[1],o);
208 } else {
209 int olen;
210
211 /* Key exists, check type */
212 if (checkType(c,o,REDIS_STRING))
213 return;
214
215 /* Find out existing value length */
216 if (o->encoding == REDIS_ENCODING_INT) {
217 char llbuf[32];
218 olen = ll2string(llbuf,sizeof(llbuf),(long)o->ptr);
219 } else {
220 olen = sdslen(o->ptr);
221 }
222
223 /* Return existing string length when setting nothing */
224 if (sdslen(value) == 0) {
225 addReplyLongLong(c,olen);
226 return;
227 }
228
229 /* Convert negative indexes. Note that for SETRANGE, the meaning of a
230 * negative index is a little different than for other commands.
231 * Here, an offset of -1 points to the trailing NULL byte of the
232 * string instead of the last character. */
233 if (offset < 0) {
234 offset = olen+1+offset;
235 if (offset < 0) offset = 0;
236 }
237
238 /* Return when the resulting string exceeds allowed size */
239 if (checkStringLength(c,offset+sdslen(value)) != REDIS_OK)
240 return;
241
242 /* Create a copy when the object is shared or encoded. */
243 if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) {
244 robj *decoded = getDecodedObject(o);
245 o = createStringObject(decoded->ptr, sdslen(decoded->ptr));
246 decrRefCount(decoded);
247 dbReplace(c->db,c->argv[1],o);
248 }
249 }
250
251 if (sdslen(value) > 0) {
252 o->ptr = sdsgrowzero(o->ptr,offset+sdslen(value));
253 memcpy((char*)o->ptr+offset,value,sdslen(value));
254 touchWatchedKey(c->db,c->argv[1]);
255 server.dirty++;
256 }
257 addReplyLongLong(c,sdslen(o->ptr));
258}
259
ef11bccc
PN
260void getrangeCommand(redisClient *c) {
261 robj *o;
262 long start, end;
263 char *str, llbuf[32];
264 size_t strlen;
265
266 if (getLongFromObjectOrReply(c,c->argv[2],&start,NULL) != REDIS_OK)
267 return;
268 if (getLongFromObjectOrReply(c,c->argv[3],&end,NULL) != REDIS_OK)
269 return;
270 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
271 checkType(c,o,REDIS_STRING)) return;
272
273 if (o->encoding == REDIS_ENCODING_INT) {
274 str = llbuf;
275 strlen = ll2string(llbuf,sizeof(llbuf),(long)o->ptr);
276 } else {
277 str = o->ptr;
278 strlen = sdslen(str);
279 }
280
281 /* Convert negative indexes */
282 if (start < 0) start = strlen+start;
283 if (end < 0) end = strlen+end;
284 if (start < 0) start = 0;
285 if (end < 0) end = 0;
286 if ((unsigned)end >= strlen) end = strlen-1;
287
288 /* Precondition: end >= 0 && end < strlen, so the only condition where
289 * nothing can be returned is: start > end. */
290 if (start > end) {
291 addReply(c,shared.nullbulk);
292 } else {
293 addReplyBulkCBuffer(c,(char*)str+start,end-start+1);
294 }
295}
296
e2641e09 297void mgetCommand(redisClient *c) {
298 int j;
299
0537e7bf 300 addReplyMultiBulkLen(c,c->argc-1);
e2641e09 301 for (j = 1; j < c->argc; j++) {
302 robj *o = lookupKeyRead(c->db,c->argv[j]);
303 if (o == NULL) {
304 addReply(c,shared.nullbulk);
305 } else {
306 if (o->type != REDIS_STRING) {
307 addReply(c,shared.nullbulk);
308 } else {
309 addReplyBulk(c,o);
310 }
311 }
312 }
313}
314
315void msetGenericCommand(redisClient *c, int nx) {
316 int j, busykeys = 0;
317
318 if ((c->argc % 2) == 0) {
3ab20376 319 addReplyError(c,"wrong number of arguments for MSET");
e2641e09 320 return;
321 }
322 /* Handle the NX flag. The MSETNX semantic is to return zero and don't
323 * set nothing at all if at least one already key exists. */
324 if (nx) {
325 for (j = 1; j < c->argc; j += 2) {
326 if (lookupKeyWrite(c->db,c->argv[j]) != NULL) {
327 busykeys++;
328 }
329 }
330 }
331 if (busykeys) {
332 addReply(c, shared.czero);
333 return;
334 }
335
336 for (j = 1; j < c->argc; j += 2) {
337 c->argv[j+1] = tryObjectEncoding(c->argv[j+1]);
338 dbReplace(c->db,c->argv[j],c->argv[j+1]);
339 incrRefCount(c->argv[j+1]);
340 removeExpire(c->db,c->argv[j]);
5b4bff9c 341 touchWatchedKey(c->db,c->argv[j]);
e2641e09 342 }
343 server.dirty += (c->argc-1)/2;
344 addReply(c, nx ? shared.cone : shared.ok);
345}
346
347void msetCommand(redisClient *c) {
348 msetGenericCommand(c,0);
349}
350
351void msetnxCommand(redisClient *c) {
352 msetGenericCommand(c,1);
353}
354
355void incrDecrCommand(redisClient *c, long long incr) {
356 long long value;
357 robj *o;
358
359 o = lookupKeyWrite(c->db,c->argv[1]);
360 if (o != NULL && checkType(c,o,REDIS_STRING)) return;
361 if (getLongLongFromObjectOrReply(c,o,&value,NULL) != REDIS_OK) return;
362
363 value += incr;
364 o = createStringObjectFromLongLong(value);
365 dbReplace(c->db,c->argv[1],o);
5b4bff9c 366 touchWatchedKey(c->db,c->argv[1]);
e2641e09 367 server.dirty++;
368 addReply(c,shared.colon);
369 addReply(c,o);
370 addReply(c,shared.crlf);
371}
372
373void incrCommand(redisClient *c) {
374 incrDecrCommand(c,1);
375}
376
377void decrCommand(redisClient *c) {
378 incrDecrCommand(c,-1);
379}
380
381void incrbyCommand(redisClient *c) {
382 long long incr;
383
384 if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != REDIS_OK) return;
385 incrDecrCommand(c,incr);
386}
387
388void decrbyCommand(redisClient *c) {
389 long long incr;
390
391 if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != REDIS_OK) return;
392 incrDecrCommand(c,-incr);
393}
394
395void appendCommand(redisClient *c) {
396 int retval;
397 size_t totlen;
076f88d6 398 robj *o, *append;
e2641e09 399
400 o = lookupKeyWrite(c->db,c->argv[1]);
75b41de8 401 c->argv[2] = tryObjectEncoding(c->argv[2]);
e2641e09 402 if (o == NULL) {
403 /* Create the key */
404 retval = dbAdd(c->db,c->argv[1],c->argv[2]);
405 incrRefCount(c->argv[2]);
406 totlen = stringObjectLen(c->argv[2]);
407 } else {
408 if (o->type != REDIS_STRING) {
409 addReply(c,shared.wrongtypeerr);
410 return;
411 }
076f88d6
PN
412
413 append = getDecodedObject(c->argv[2]);
414 if (o->encoding == REDIS_ENCODING_RAW &&
415 (sdslen(o->ptr) + sdslen(append->ptr)) > 512*1024*1024)
416 {
417 addReplyError(c,"string exceeds maximum allowed size (512MB)");
418 decrRefCount(append);
419 return;
420 }
421
422 /* If the object is shared or encoded, we have to make a copy */
e2641e09 423 if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) {
424 robj *decoded = getDecodedObject(o);
e2641e09 425 o = createStringObject(decoded->ptr, sdslen(decoded->ptr));
426 decrRefCount(decoded);
427 dbReplace(c->db,c->argv[1],o);
428 }
076f88d6
PN
429
430 /* Append the value */
431 o->ptr = sdscatlen(o->ptr,append->ptr,sdslen(append->ptr));
432 decrRefCount(append);
e2641e09 433 totlen = sdslen(o->ptr);
434 }
5b4bff9c 435 touchWatchedKey(c->db,c->argv[1]);
e2641e09 436 server.dirty++;
b70d3555 437 addReplyLongLong(c,totlen);
e2641e09 438}
439
80091bba 440void strlenCommand(redisClient *c) {
441 robj *o;
442
443 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
444 checkType(c,o,REDIS_STRING)) return;
e2641e09 445
7ecd4644
PN
446 if (o->encoding == REDIS_ENCODING_RAW) {
447 addReplyLongLong(c,sdslen(o->ptr));
448 } else if (o->encoding == REDIS_ENCODING_INT) {
449 char llbuf[32];
450 int len = ll2string(llbuf,sizeof(llbuf),(long)o->ptr);
451 addReplyLongLong(c,len);
452 } else {
453 redisPanic("Unknown string encoding");
454 }
80091bba 455}
7ecd4644 456