]> git.saurik.com Git - redis.git/blob - src/t_string.c
Refactor and rename SUBSTR to GETRANGE
[redis.git] / src / t_string.c
1 #include <limits.h>
2 #include "redis.h"
3
4 /*-----------------------------------------------------------------------------
5 * String Commands
6 *----------------------------------------------------------------------------*/
7
8 static 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
16 void 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) {
24 addReplyError(c,"invalid expire time in SETEX");
25 return;
26 }
27 }
28
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 }
41 touchWatchedKey(c->db,key);
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
48 void setCommand(redisClient *c) {
49 c->argv[2] = tryObjectEncoding(c->argv[2]);
50 setGenericCommand(c,0,c->argv[1],c->argv[2],NULL);
51 }
52
53 void setnxCommand(redisClient *c) {
54 c->argv[2] = tryObjectEncoding(c->argv[2]);
55 setGenericCommand(c,1,c->argv[1],c->argv[2],NULL);
56 }
57
58 void setexCommand(redisClient *c) {
59 c->argv[3] = tryObjectEncoding(c->argv[3]);
60 setGenericCommand(c,0,c->argv[1],c->argv[3],c->argv[2]);
61 }
62
63 int 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
78 void getCommand(redisClient *c) {
79 getGenericCommand(c);
80 }
81
82 void getsetCommand(redisClient *c) {
83 if (getGenericCommand(c) == REDIS_ERR) return;
84 c->argv[2] = tryObjectEncoding(c->argv[2]);
85 dbReplace(c->db,c->argv[1],c->argv[2]);
86 incrRefCount(c->argv[2]);
87 touchWatchedKey(c->db,c->argv[1]);
88 server.dirty++;
89 removeExpire(c->db,c->argv[1]);
90 }
91
92 static 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
99 /* Limit offset to SIZE_T_MAX or 512MB in bytes */
100 if ((loffset < 0) ||
101 ((unsigned long long)loffset >= (unsigned)SIZE_T_MAX) ||
102 ((unsigned long long)loffset >> 3) >= (512*1024*1024))
103 {
104 addReplyError(c,err);
105 return REDIS_ERR;
106 }
107
108 *offset = (size_t)loffset;
109 return REDIS_OK;
110 }
111
112 void setbitCommand(redisClient *c) {
113 robj *o;
114 char *err = "bit is not an integer or out of range";
115 size_t bitoffset;
116 long long bitvalue;
117 int byte, bit, on;
118
119 if (getBitOffsetFromArgument(c,c->argv[2],&bitoffset) != REDIS_OK)
120 return;
121
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);
128 return;
129 }
130
131 o = lookupKeyWrite(c->db,c->argv[1]);
132 if (o == NULL) {
133 o = createObject(REDIS_STRING,sdsempty());
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 }
145 }
146
147 byte = bitoffset >> 3;
148 bit = 7 - (bitoffset & 0x7);
149 on = bitvalue & 0x1;
150 o->ptr = sdsgrowzero(o->ptr,byte+1);
151 ((char*)o->ptr)[byte] |= on << bit;
152 ((char*)o->ptr)[byte] &= ~((!on) << bit);
153
154 touchWatchedKey(c->db,c->argv[1]);
155 server.dirty++;
156 addReply(c,shared.cone);
157 }
158
159 void 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);
181 }
182
183 void 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
260 void 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
297 void mgetCommand(redisClient *c) {
298 int j;
299
300 addReplyMultiBulkLen(c,c->argc-1);
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
315 void msetGenericCommand(redisClient *c, int nx) {
316 int j, busykeys = 0;
317
318 if ((c->argc % 2) == 0) {
319 addReplyError(c,"wrong number of arguments for MSET");
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]);
341 touchWatchedKey(c->db,c->argv[j]);
342 }
343 server.dirty += (c->argc-1)/2;
344 addReply(c, nx ? shared.cone : shared.ok);
345 }
346
347 void msetCommand(redisClient *c) {
348 msetGenericCommand(c,0);
349 }
350
351 void msetnxCommand(redisClient *c) {
352 msetGenericCommand(c,1);
353 }
354
355 void 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);
366 touchWatchedKey(c->db,c->argv[1]);
367 server.dirty++;
368 addReply(c,shared.colon);
369 addReply(c,o);
370 addReply(c,shared.crlf);
371 }
372
373 void incrCommand(redisClient *c) {
374 incrDecrCommand(c,1);
375 }
376
377 void decrCommand(redisClient *c) {
378 incrDecrCommand(c,-1);
379 }
380
381 void 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
388 void 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
395 void appendCommand(redisClient *c) {
396 int retval;
397 size_t totlen;
398 robj *o, *append;
399
400 o = lookupKeyWrite(c->db,c->argv[1]);
401 c->argv[2] = tryObjectEncoding(c->argv[2]);
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 }
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 */
423 if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) {
424 robj *decoded = getDecodedObject(o);
425 o = createStringObject(decoded->ptr, sdslen(decoded->ptr));
426 decrRefCount(decoded);
427 dbReplace(c->db,c->argv[1],o);
428 }
429
430 /* Append the value */
431 o->ptr = sdscatlen(o->ptr,append->ptr,sdslen(append->ptr));
432 decrRefCount(append);
433 totlen = sdslen(o->ptr);
434 }
435 touchWatchedKey(c->db,c->argv[1]);
436 server.dirty++;
437 addReplyLongLong(c,totlen);
438 }
439
440 void 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;
445
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 }
455 }
456