]>
git.saurik.com Git - redis.git/blob - src/t_string.c
4 /*-----------------------------------------------------------------------------
6 *----------------------------------------------------------------------------*/
8 static int checkStringLength(redisClient
*c
, long long size
) {
9 if (size
> 512*1024*1024) {
10 addReplyError(c
,"string exceeds maximum allowed size (512MB)");
16 void setGenericCommand(redisClient
*c
, int nx
, robj
*key
, robj
*val
, robj
*expire
) {
18 long seconds
= 0; /* initialized to avoid an harmness warning */
21 if (getLongFromObjectOrReply(c
, expire
, &seconds
, NULL
) != REDIS_OK
)
24 addReplyError(c
,"invalid expire time in SETEX");
29 retval
= dbAdd(c
->db
,key
,val
);
30 if (retval
== REDIS_ERR
) {
32 dbReplace(c
->db
,key
,val
);
35 addReply(c
,shared
.czero
);
41 touchWatchedKey(c
->db
,key
);
43 removeExpire(c
->db
,key
);
44 if (expire
) setExpire(c
->db
,key
,time(NULL
)+seconds
);
45 addReply(c
, nx
? shared
.cone
: shared
.ok
);
48 void setCommand(redisClient
*c
) {
49 c
->argv
[2] = tryObjectEncoding(c
->argv
[2]);
50 setGenericCommand(c
,0,c
->argv
[1],c
->argv
[2],NULL
);
53 void setnxCommand(redisClient
*c
) {
54 c
->argv
[2] = tryObjectEncoding(c
->argv
[2]);
55 setGenericCommand(c
,1,c
->argv
[1],c
->argv
[2],NULL
);
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]);
63 int getGenericCommand(redisClient
*c
) {
66 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
)
69 if (o
->type
!= REDIS_STRING
) {
70 addReply(c
,shared
.wrongtypeerr
);
78 void getCommand(redisClient
*c
) {
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]);
89 removeExpire(c
->db
,c
->argv
[1]);
92 static int getBitOffsetFromArgument(redisClient
*c
, robj
*o
, size_t *offset
) {
94 char *err
= "bit offset is not an integer or out of range";
96 if (getLongLongFromObjectOrReply(c
,o
,&loffset
,err
) != REDIS_OK
)
99 /* Limit offset to 512MB in bytes */
100 if ((loffset
< 0) || ((unsigned long long)loffset
>> 3) >= (512*1024*1024))
102 addReplyError(c
,err
);
106 *offset
= (size_t)loffset
;
110 void setbitCommand(redisClient
*c
) {
112 char *err
= "bit is not an integer or out of range";
117 if (getBitOffsetFromArgument(c
,c
->argv
[2],&bitoffset
) != REDIS_OK
)
120 if (getLongLongFromObjectOrReply(c
,c
->argv
[3],&bitvalue
,err
) != REDIS_OK
)
123 /* A bit can only be set to be on or off... */
125 addReplyError(c
,err
);
129 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
131 o
= createObject(REDIS_STRING
,sdsempty());
132 dbAdd(c
->db
,c
->argv
[1],o
);
134 if (checkType(c
,o
,REDIS_STRING
)) return;
136 /* Create a copy when the object is shared or encoded. */
137 if (o
->refcount
!= 1 || o
->encoding
!= REDIS_ENCODING_RAW
) {
138 robj
*decoded
= getDecodedObject(o
);
139 o
= createStringObject(decoded
->ptr
, sdslen(decoded
->ptr
));
140 decrRefCount(decoded
);
141 dbReplace(c
->db
,c
->argv
[1],o
);
145 byte
= bitoffset
>> 3;
146 bit
= 7 - (bitoffset
& 0x7);
148 o
->ptr
= sdsgrowzero(o
->ptr
,byte
+1);
149 ((char*)o
->ptr
)[byte
] |= on
<< bit
;
150 ((char*)o
->ptr
)[byte
] &= ~((!on
) << bit
);
152 touchWatchedKey(c
->db
,c
->argv
[1]);
154 addReply(c
,shared
.cone
);
157 void getbitCommand(redisClient
*c
) {
159 size_t bitoffset
, byte
, bitmask
;
163 if (getBitOffsetFromArgument(c
,c
->argv
[2],&bitoffset
) != REDIS_OK
)
166 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
167 checkType(c
,o
,REDIS_STRING
)) return;
169 byte
= bitoffset
>> 3;
170 bitmask
= 1 << (7 - (bitoffset
& 0x7));
171 if (o
->encoding
!= REDIS_ENCODING_RAW
) {
172 if (byte
< (size_t)ll2string(llbuf
,sizeof(llbuf
),(long)o
->ptr
))
173 on
= llbuf
[byte
] & bitmask
;
175 if (byte
< sdslen(o
->ptr
))
176 on
= ((sds
)o
->ptr
)[byte
] & bitmask
;
178 addReply(c
, on
? shared
.cone
: shared
.czero
);
181 void setrangeCommand(redisClient
*c
) {
184 sds value
= c
->argv
[3]->ptr
;
186 if (getLongFromObjectOrReply(c
,c
->argv
[2],&offset
,NULL
) != REDIS_OK
)
189 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
191 /* Negative offset is always 0 for non-existing keys */
192 if (offset
< 0) offset
= 0;
194 /* Return 0 when setting nothing on a non-existing string */
195 if (sdslen(value
) == 0) {
196 addReply(c
,shared
.czero
);
200 /* Return when the resulting string exceeds allowed size */
201 if (checkStringLength(c
,offset
+sdslen(value
)) != REDIS_OK
)
204 o
= createObject(REDIS_STRING
,sdsempty());
205 dbAdd(c
->db
,c
->argv
[1],o
);
209 /* Key exists, check type */
210 if (checkType(c
,o
,REDIS_STRING
))
213 /* Find out existing value length */
214 if (o
->encoding
== REDIS_ENCODING_INT
) {
216 olen
= ll2string(llbuf
,sizeof(llbuf
),(long)o
->ptr
);
218 olen
= sdslen(o
->ptr
);
221 /* Return existing string length when setting nothing */
222 if (sdslen(value
) == 0) {
223 addReplyLongLong(c
,olen
);
227 /* Convert negative indexes. Note that for SETRANGE, the meaning of a
228 * negative index is a little different than for other commands.
229 * Here, an offset of -1 points to the trailing NULL byte of the
230 * string instead of the last character. */
232 offset
= olen
+1+offset
;
233 if (offset
< 0) offset
= 0;
236 /* Return when the resulting string exceeds allowed size */
237 if (checkStringLength(c
,offset
+sdslen(value
)) != REDIS_OK
)
240 /* Create a copy when the object is shared or encoded. */
241 if (o
->refcount
!= 1 || o
->encoding
!= REDIS_ENCODING_RAW
) {
242 robj
*decoded
= getDecodedObject(o
);
243 o
= createStringObject(decoded
->ptr
, sdslen(decoded
->ptr
));
244 decrRefCount(decoded
);
245 dbReplace(c
->db
,c
->argv
[1],o
);
249 if (sdslen(value
) > 0) {
250 o
->ptr
= sdsgrowzero(o
->ptr
,offset
+sdslen(value
));
251 memcpy((char*)o
->ptr
+offset
,value
,sdslen(value
));
252 touchWatchedKey(c
->db
,c
->argv
[1]);
255 addReplyLongLong(c
,sdslen(o
->ptr
));
258 void getrangeCommand(redisClient
*c
) {
261 char *str
, llbuf
[32];
264 if (getLongFromObjectOrReply(c
,c
->argv
[2],&start
,NULL
) != REDIS_OK
)
266 if (getLongFromObjectOrReply(c
,c
->argv
[3],&end
,NULL
) != REDIS_OK
)
268 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
269 checkType(c
,o
,REDIS_STRING
)) return;
271 if (o
->encoding
== REDIS_ENCODING_INT
) {
273 strlen
= ll2string(llbuf
,sizeof(llbuf
),(long)o
->ptr
);
276 strlen
= sdslen(str
);
279 /* Convert negative indexes */
280 if (start
< 0) start
= strlen
+start
;
281 if (end
< 0) end
= strlen
+end
;
282 if (start
< 0) start
= 0;
283 if (end
< 0) end
= 0;
284 if ((unsigned)end
>= strlen
) end
= strlen
-1;
286 /* Precondition: end >= 0 && end < strlen, so the only condition where
287 * nothing can be returned is: start > end. */
289 addReply(c
,shared
.nullbulk
);
291 addReplyBulkCBuffer(c
,(char*)str
+start
,end
-start
+1);
295 void mgetCommand(redisClient
*c
) {
298 addReplyMultiBulkLen(c
,c
->argc
-1);
299 for (j
= 1; j
< c
->argc
; j
++) {
300 robj
*o
= lookupKeyRead(c
->db
,c
->argv
[j
]);
302 addReply(c
,shared
.nullbulk
);
304 if (o
->type
!= REDIS_STRING
) {
305 addReply(c
,shared
.nullbulk
);
313 void msetGenericCommand(redisClient
*c
, int nx
) {
316 if ((c
->argc
% 2) == 0) {
317 addReplyError(c
,"wrong number of arguments for MSET");
320 /* Handle the NX flag. The MSETNX semantic is to return zero and don't
321 * set nothing at all if at least one already key exists. */
323 for (j
= 1; j
< c
->argc
; j
+= 2) {
324 if (lookupKeyWrite(c
->db
,c
->argv
[j
]) != NULL
) {
330 addReply(c
, shared
.czero
);
334 for (j
= 1; j
< c
->argc
; j
+= 2) {
335 c
->argv
[j
+1] = tryObjectEncoding(c
->argv
[j
+1]);
336 dbReplace(c
->db
,c
->argv
[j
],c
->argv
[j
+1]);
337 incrRefCount(c
->argv
[j
+1]);
338 removeExpire(c
->db
,c
->argv
[j
]);
339 touchWatchedKey(c
->db
,c
->argv
[j
]);
341 server
.dirty
+= (c
->argc
-1)/2;
342 addReply(c
, nx
? shared
.cone
: shared
.ok
);
345 void msetCommand(redisClient
*c
) {
346 msetGenericCommand(c
,0);
349 void msetnxCommand(redisClient
*c
) {
350 msetGenericCommand(c
,1);
353 void incrDecrCommand(redisClient
*c
, long long incr
) {
357 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
358 if (o
!= NULL
&& checkType(c
,o
,REDIS_STRING
)) return;
359 if (getLongLongFromObjectOrReply(c
,o
,&value
,NULL
) != REDIS_OK
) return;
362 o
= createStringObjectFromLongLong(value
);
363 dbReplace(c
->db
,c
->argv
[1],o
);
364 touchWatchedKey(c
->db
,c
->argv
[1]);
366 addReply(c
,shared
.colon
);
368 addReply(c
,shared
.crlf
);
371 void incrCommand(redisClient
*c
) {
372 incrDecrCommand(c
,1);
375 void decrCommand(redisClient
*c
) {
376 incrDecrCommand(c
,-1);
379 void incrbyCommand(redisClient
*c
) {
382 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
383 incrDecrCommand(c
,incr
);
386 void decrbyCommand(redisClient
*c
) {
389 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
390 incrDecrCommand(c
,-incr
);
393 void appendCommand(redisClient
*c
) {
398 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
399 c
->argv
[2] = tryObjectEncoding(c
->argv
[2]);
402 retval
= dbAdd(c
->db
,c
->argv
[1],c
->argv
[2]);
403 incrRefCount(c
->argv
[2]);
404 totlen
= stringObjectLen(c
->argv
[2]);
406 if (o
->type
!= REDIS_STRING
) {
407 addReply(c
,shared
.wrongtypeerr
);
411 append
= getDecodedObject(c
->argv
[2]);
412 if (o
->encoding
== REDIS_ENCODING_RAW
&&
413 (sdslen(o
->ptr
) + sdslen(append
->ptr
)) > 512*1024*1024)
415 addReplyError(c
,"string exceeds maximum allowed size (512MB)");
416 decrRefCount(append
);
420 /* If the object is shared or encoded, we have to make a copy */
421 if (o
->refcount
!= 1 || o
->encoding
!= REDIS_ENCODING_RAW
) {
422 robj
*decoded
= getDecodedObject(o
);
423 o
= createStringObject(decoded
->ptr
, sdslen(decoded
->ptr
));
424 decrRefCount(decoded
);
425 dbReplace(c
->db
,c
->argv
[1],o
);
428 /* Append the value */
429 o
->ptr
= sdscatlen(o
->ptr
,append
->ptr
,sdslen(append
->ptr
));
430 decrRefCount(append
);
431 totlen
= sdslen(o
->ptr
);
433 touchWatchedKey(c
->db
,c
->argv
[1]);
435 addReplyLongLong(c
,totlen
);
438 void strlenCommand(redisClient
*c
) {
441 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
442 checkType(c
,o
,REDIS_STRING
)) return;
444 if (o
->encoding
== REDIS_ENCODING_RAW
) {
445 addReplyLongLong(c
,sdslen(o
->ptr
));
446 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
448 int len
= ll2string(llbuf
,sizeof(llbuf
),(long)o
->ptr
);
449 addReplyLongLong(c
,len
);
451 redisPanic("Unknown string encoding");