]>
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 SIZE_T_MAX or 512MB in bytes */
101 ((unsigned long long)loffset
>= (unsigned)SIZE_T_MAX
) ||
102 ((unsigned long long)loffset
>> 3) >= (512*1024*1024))
104 addReplyError(c
,err
);
108 *offset
= (size_t)loffset
;
112 void setbitCommand(redisClient
*c
) {
114 char *err
= "bit is not an integer or out of range";
119 if (getBitOffsetFromArgument(c
,c
->argv
[2],&bitoffset
) != REDIS_OK
)
122 if (getLongLongFromObjectOrReply(c
,c
->argv
[3],&bitvalue
,err
) != REDIS_OK
)
125 /* A bit can only be set to be on or off... */
127 addReplyError(c
,err
);
131 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
133 o
= createObject(REDIS_STRING
,sdsempty());
134 dbAdd(c
->db
,c
->argv
[1],o
);
136 if (checkType(c
,o
,REDIS_STRING
)) return;
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
);
147 byte
= bitoffset
>> 3;
148 bit
= 7 - (bitoffset
& 0x7);
150 o
->ptr
= sdsgrowzero(o
->ptr
,byte
+1);
151 ((char*)o
->ptr
)[byte
] |= on
<< bit
;
152 ((char*)o
->ptr
)[byte
] &= ~((!on
) << bit
);
154 touchWatchedKey(c
->db
,c
->argv
[1]);
156 addReply(c
,shared
.cone
);
159 void getbitCommand(redisClient
*c
) {
161 size_t bitoffset
, byte
, bitmask
;
165 if (getBitOffsetFromArgument(c
,c
->argv
[2],&bitoffset
) != REDIS_OK
)
168 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
169 checkType(c
,o
,REDIS_STRING
)) return;
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
;
177 if (byte
< sdslen(o
->ptr
))
178 on
= ((sds
)o
->ptr
)[byte
] & bitmask
;
180 addReply(c
, on
? shared
.cone
: shared
.czero
);
183 void setrangeCommand(redisClient
*c
) {
186 sds value
= c
->argv
[3]->ptr
;
188 if (getLongFromObjectOrReply(c
,c
->argv
[2],&offset
,NULL
) != REDIS_OK
)
191 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
193 /* Negative offset is always 0 for non-existing keys */
194 if (offset
< 0) offset
= 0;
196 /* Return 0 when setting nothing on a non-existing string */
197 if (sdslen(value
) == 0) {
198 addReply(c
,shared
.czero
);
202 /* Return when the resulting string exceeds allowed size */
203 if (checkStringLength(c
,offset
+sdslen(value
)) != REDIS_OK
)
206 o
= createObject(REDIS_STRING
,sdsempty());
207 dbAdd(c
->db
,c
->argv
[1],o
);
211 /* Key exists, check type */
212 if (checkType(c
,o
,REDIS_STRING
))
215 /* Find out existing value length */
216 if (o
->encoding
== REDIS_ENCODING_INT
) {
218 olen
= ll2string(llbuf
,sizeof(llbuf
),(long)o
->ptr
);
220 olen
= sdslen(o
->ptr
);
223 /* Return existing string length when setting nothing */
224 if (sdslen(value
) == 0) {
225 addReplyLongLong(c
,olen
);
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. */
234 offset
= olen
+1+offset
;
235 if (offset
< 0) offset
= 0;
238 /* Return when the resulting string exceeds allowed size */
239 if (checkStringLength(c
,offset
+sdslen(value
)) != REDIS_OK
)
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
);
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]);
257 addReplyLongLong(c
,sdslen(o
->ptr
));
260 void getrangeCommand(redisClient
*c
) {
263 char *str
, llbuf
[32];
266 if (getLongFromObjectOrReply(c
,c
->argv
[2],&start
,NULL
) != REDIS_OK
)
268 if (getLongFromObjectOrReply(c
,c
->argv
[3],&end
,NULL
) != REDIS_OK
)
270 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
271 checkType(c
,o
,REDIS_STRING
)) return;
273 if (o
->encoding
== REDIS_ENCODING_INT
) {
275 strlen
= ll2string(llbuf
,sizeof(llbuf
),(long)o
->ptr
);
278 strlen
= sdslen(str
);
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;
288 /* Precondition: end >= 0 && end < strlen, so the only condition where
289 * nothing can be returned is: start > end. */
291 addReply(c
,shared
.nullbulk
);
293 addReplyBulkCBuffer(c
,(char*)str
+start
,end
-start
+1);
297 void mgetCommand(redisClient
*c
) {
300 addReplyMultiBulkLen(c
,c
->argc
-1);
301 for (j
= 1; j
< c
->argc
; j
++) {
302 robj
*o
= lookupKeyRead(c
->db
,c
->argv
[j
]);
304 addReply(c
,shared
.nullbulk
);
306 if (o
->type
!= REDIS_STRING
) {
307 addReply(c
,shared
.nullbulk
);
315 void msetGenericCommand(redisClient
*c
, int nx
) {
318 if ((c
->argc
% 2) == 0) {
319 addReplyError(c
,"wrong number of arguments for MSET");
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. */
325 for (j
= 1; j
< c
->argc
; j
+= 2) {
326 if (lookupKeyWrite(c
->db
,c
->argv
[j
]) != NULL
) {
332 addReply(c
, shared
.czero
);
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
]);
343 server
.dirty
+= (c
->argc
-1)/2;
344 addReply(c
, nx
? shared
.cone
: shared
.ok
);
347 void msetCommand(redisClient
*c
) {
348 msetGenericCommand(c
,0);
351 void msetnxCommand(redisClient
*c
) {
352 msetGenericCommand(c
,1);
355 void incrDecrCommand(redisClient
*c
, long long incr
) {
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;
364 o
= createStringObjectFromLongLong(value
);
365 dbReplace(c
->db
,c
->argv
[1],o
);
366 touchWatchedKey(c
->db
,c
->argv
[1]);
368 addReply(c
,shared
.colon
);
370 addReply(c
,shared
.crlf
);
373 void incrCommand(redisClient
*c
) {
374 incrDecrCommand(c
,1);
377 void decrCommand(redisClient
*c
) {
378 incrDecrCommand(c
,-1);
381 void incrbyCommand(redisClient
*c
) {
384 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
385 incrDecrCommand(c
,incr
);
388 void decrbyCommand(redisClient
*c
) {
391 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
392 incrDecrCommand(c
,-incr
);
395 void appendCommand(redisClient
*c
) {
400 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
401 c
->argv
[2] = tryObjectEncoding(c
->argv
[2]);
404 retval
= dbAdd(c
->db
,c
->argv
[1],c
->argv
[2]);
405 incrRefCount(c
->argv
[2]);
406 totlen
= stringObjectLen(c
->argv
[2]);
408 if (o
->type
!= REDIS_STRING
) {
409 addReply(c
,shared
.wrongtypeerr
);
413 append
= getDecodedObject(c
->argv
[2]);
414 if (o
->encoding
== REDIS_ENCODING_RAW
&&
415 (sdslen(o
->ptr
) + sdslen(append
->ptr
)) > 512*1024*1024)
417 addReplyError(c
,"string exceeds maximum allowed size (512MB)");
418 decrRefCount(append
);
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
);
430 /* Append the value */
431 o
->ptr
= sdscatlen(o
->ptr
,append
->ptr
,sdslen(append
->ptr
));
432 decrRefCount(append
);
433 totlen
= sdslen(o
->ptr
);
435 touchWatchedKey(c
->db
,c
->argv
[1]);
437 addReplyLongLong(c
,totlen
);
440 void strlenCommand(redisClient
*c
) {
443 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
444 checkType(c
,o
,REDIS_STRING
)) return;
446 if (o
->encoding
== REDIS_ENCODING_RAW
) {
447 addReplyLongLong(c
,sdslen(o
->ptr
));
448 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
450 int len
= ll2string(llbuf
,sizeof(llbuf
),(long)o
->ptr
);
451 addReplyLongLong(c
,len
);
453 redisPanic("Unknown string encoding");