]>
git.saurik.com Git - redis.git/blob - src/t_string.c
e50692183ec40fd1d62ee4f82fa9333140e82fe8
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";
118 if (getBitOffsetFromArgument(c
,c
->argv
[2],&bitoffset
) != REDIS_OK
)
121 if (getLongFromObjectOrReply(c
,c
->argv
[3],&on
,err
) != REDIS_OK
)
124 /* Bits can only be set or cleared... */
126 addReplyError(c
,err
);
130 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
132 o
= createObject(REDIS_STRING
,sdsempty());
133 dbAdd(c
->db
,c
->argv
[1],o
);
135 if (checkType(c
,o
,REDIS_STRING
)) return;
137 /* Create a copy when the object is shared or encoded. */
138 if (o
->refcount
!= 1 || o
->encoding
!= REDIS_ENCODING_RAW
) {
139 robj
*decoded
= getDecodedObject(o
);
140 o
= createStringObject(decoded
->ptr
, sdslen(decoded
->ptr
));
141 decrRefCount(decoded
);
142 dbReplace(c
->db
,c
->argv
[1],o
);
146 /* Grow sds value to the right length if necessary */
147 byte
= bitoffset
>> 3;
148 o
->ptr
= sdsgrowzero(o
->ptr
,byte
+1);
150 /* Get current values */
151 byteval
= ((char*)o
->ptr
)[byte
];
152 bit
= 7 - (bitoffset
& 0x7);
153 bitval
= byteval
& (1 << bit
);
155 /* Update byte with new bit value and return original value */
156 byteval
&= ~(1 << bit
);
157 byteval
|= ((on
& 0x1) << bit
);
158 ((char*)o
->ptr
)[byte
] = byteval
;
159 touchWatchedKey(c
->db
,c
->argv
[1]);
161 addReply(c
, bitval
? shared
.cone
: shared
.czero
);
164 void getbitCommand(redisClient
*c
) {
171 if (getBitOffsetFromArgument(c
,c
->argv
[2],&bitoffset
) != REDIS_OK
)
174 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
175 checkType(c
,o
,REDIS_STRING
)) return;
177 byte
= bitoffset
>> 3;
178 bit
= 7 - (bitoffset
& 0x7);
179 if (o
->encoding
!= REDIS_ENCODING_RAW
) {
180 if (byte
< (size_t)ll2string(llbuf
,sizeof(llbuf
),(long)o
->ptr
))
181 bitval
= llbuf
[byte
] & (1 << bit
);
183 if (byte
< sdslen(o
->ptr
))
184 bitval
= ((char*)o
->ptr
)[byte
] & (1 << bit
);
187 addReply(c
, bitval
? shared
.cone
: shared
.czero
);
190 void setrangeCommand(redisClient
*c
) {
193 sds value
= c
->argv
[3]->ptr
;
195 if (getLongFromObjectOrReply(c
,c
->argv
[2],&offset
,NULL
) != REDIS_OK
)
199 addReplyError(c
,"offset is out of range");
203 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
205 /* Return 0 when setting nothing on a non-existing string */
206 if (sdslen(value
) == 0) {
207 addReply(c
,shared
.czero
);
211 /* Return when the resulting string exceeds allowed size */
212 if (checkStringLength(c
,offset
+sdslen(value
)) != REDIS_OK
)
215 o
= createObject(REDIS_STRING
,sdsempty());
216 dbAdd(c
->db
,c
->argv
[1],o
);
220 /* Key exists, check type */
221 if (checkType(c
,o
,REDIS_STRING
))
224 /* Find out existing value length */
225 if (o
->encoding
== REDIS_ENCODING_INT
) {
227 olen
= ll2string(llbuf
,sizeof(llbuf
),(long)o
->ptr
);
229 olen
= sdslen(o
->ptr
);
232 /* Return existing string length when setting nothing */
233 if (sdslen(value
) == 0) {
234 addReplyLongLong(c
,olen
);
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
) {
399 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
402 c
->argv
[2] = tryObjectEncoding(c
->argv
[2]);
403 dbAdd(c
->db
,c
->argv
[1],c
->argv
[2]);
404 incrRefCount(c
->argv
[2]);
405 totlen
= stringObjectLen(c
->argv
[2]);
407 /* Key exists, check type */
408 if (checkType(c
,o
,REDIS_STRING
))
411 /* "append" is an argument, so always an sds */
413 totlen
= stringObjectLen(o
)+sdslen(append
->ptr
);
414 if (checkStringLength(c
,totlen
) != REDIS_OK
)
417 /* If the object is shared or encoded, we have to make a copy */
418 if (o
->refcount
!= 1 || o
->encoding
!= REDIS_ENCODING_RAW
) {
419 robj
*decoded
= getDecodedObject(o
);
420 o
= createStringObject(decoded
->ptr
, sdslen(decoded
->ptr
));
421 decrRefCount(decoded
);
422 dbReplace(c
->db
,c
->argv
[1],o
);
425 /* Append the value */
426 o
->ptr
= sdscatlen(o
->ptr
,append
->ptr
,sdslen(append
->ptr
));
427 totlen
= sdslen(o
->ptr
);
429 touchWatchedKey(c
->db
,c
->argv
[1]);
431 addReplyLongLong(c
,totlen
);
434 void strlenCommand(redisClient
*c
) {
437 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
438 checkType(c
,o
,REDIS_STRING
)) return;
440 if (o
->encoding
== REDIS_ENCODING_RAW
) {
441 addReplyLongLong(c
,sdslen(o
->ptr
));
442 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
444 int len
= ll2string(llbuf
,sizeof(llbuf
),(long)o
->ptr
);
445 addReplyLongLong(c
,len
);
447 redisPanic("Unknown string encoding");