]>
git.saurik.com Git - redis.git/blob - src/t_string.c
3 /*-----------------------------------------------------------------------------
5 *----------------------------------------------------------------------------*/
7 static int checkStringLength(redisClient
*c
, long long size
) {
8 if (size
> 512*1024*1024) {
9 addReplyError(c
,"string exceeds maximum allowed size (512MB)");
15 void setGenericCommand(redisClient
*c
, int nx
, robj
*key
, robj
*val
, robj
*expire
) {
16 long seconds
= 0; /* initialized to avoid an harmness warning */
19 if (getLongFromObjectOrReply(c
, expire
, &seconds
, NULL
) != REDIS_OK
)
22 addReplyError(c
,"invalid expire time in SETEX");
27 if (lookupKeyWrite(c
->db
,key
) != NULL
&& nx
) {
28 addReply(c
,shared
.czero
);
31 setKey(c
->db
,key
,val
);
33 if (expire
) setExpire(c
->db
,key
,time(NULL
)+seconds
);
34 addReply(c
, nx
? shared
.cone
: shared
.ok
);
37 void setCommand(redisClient
*c
) {
38 c
->argv
[2] = tryObjectEncoding(c
->argv
[2]);
39 setGenericCommand(c
,0,c
->argv
[1],c
->argv
[2],NULL
);
42 void setnxCommand(redisClient
*c
) {
43 c
->argv
[2] = tryObjectEncoding(c
->argv
[2]);
44 setGenericCommand(c
,1,c
->argv
[1],c
->argv
[2],NULL
);
47 void setexCommand(redisClient
*c
) {
48 c
->argv
[3] = tryObjectEncoding(c
->argv
[3]);
49 setGenericCommand(c
,0,c
->argv
[1],c
->argv
[3],c
->argv
[2]);
52 int getGenericCommand(redisClient
*c
) {
55 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
)
58 if (o
->type
!= REDIS_STRING
) {
59 addReply(c
,shared
.wrongtypeerr
);
67 void getCommand(redisClient
*c
) {
71 void getsetCommand(redisClient
*c
) {
72 if (getGenericCommand(c
) == REDIS_ERR
) return;
73 c
->argv
[2] = tryObjectEncoding(c
->argv
[2]);
74 setKey(c
->db
,c
->argv
[1],c
->argv
[2]);
78 static int getBitOffsetFromArgument(redisClient
*c
, robj
*o
, size_t *offset
) {
80 char *err
= "bit offset is not an integer or out of range";
82 if (getLongLongFromObjectOrReply(c
,o
,&loffset
,err
) != REDIS_OK
)
85 /* Limit offset to 512MB in bytes */
86 if ((loffset
< 0) || ((unsigned long long)loffset
>> 3) >= (512*1024*1024))
92 *offset
= (size_t)loffset
;
96 void setbitCommand(redisClient
*c
) {
98 char *err
= "bit is not an integer or out of range";
104 if (getBitOffsetFromArgument(c
,c
->argv
[2],&bitoffset
) != REDIS_OK
)
107 if (getLongFromObjectOrReply(c
,c
->argv
[3],&on
,err
) != REDIS_OK
)
110 /* Bits can only be set or cleared... */
112 addReplyError(c
,err
);
116 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
118 o
= createObject(REDIS_STRING
,sdsempty());
119 dbAdd(c
->db
,c
->argv
[1],o
);
121 if (checkType(c
,o
,REDIS_STRING
)) return;
123 /* Create a copy when the object is shared or encoded. */
124 if (o
->refcount
!= 1 || o
->encoding
!= REDIS_ENCODING_RAW
) {
125 robj
*decoded
= getDecodedObject(o
);
126 o
= createStringObject(decoded
->ptr
, sdslen(decoded
->ptr
));
127 decrRefCount(decoded
);
128 dbOverwrite(c
->db
,c
->argv
[1],o
);
132 /* Grow sds value to the right length if necessary */
133 byte
= bitoffset
>> 3;
134 o
->ptr
= sdsgrowzero(o
->ptr
,byte
+1);
136 /* Get current values */
137 byteval
= ((char*)o
->ptr
)[byte
];
138 bit
= 7 - (bitoffset
& 0x7);
139 bitval
= byteval
& (1 << bit
);
141 /* Update byte with new bit value and return original value */
142 byteval
&= ~(1 << bit
);
143 byteval
|= ((on
& 0x1) << bit
);
144 ((char*)o
->ptr
)[byte
] = byteval
;
145 signalModifiedKey(c
->db
,c
->argv
[1]);
147 addReply(c
, bitval
? shared
.cone
: shared
.czero
);
150 void getbitCommand(redisClient
*c
) {
157 if (getBitOffsetFromArgument(c
,c
->argv
[2],&bitoffset
) != REDIS_OK
)
160 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
161 checkType(c
,o
,REDIS_STRING
)) return;
163 byte
= bitoffset
>> 3;
164 bit
= 7 - (bitoffset
& 0x7);
165 if (o
->encoding
!= REDIS_ENCODING_RAW
) {
166 if (byte
< (size_t)ll2string(llbuf
,sizeof(llbuf
),(long)o
->ptr
))
167 bitval
= llbuf
[byte
] & (1 << bit
);
169 if (byte
< sdslen(o
->ptr
))
170 bitval
= ((char*)o
->ptr
)[byte
] & (1 << bit
);
173 addReply(c
, bitval
? shared
.cone
: shared
.czero
);
176 void setrangeCommand(redisClient
*c
) {
179 sds value
= c
->argv
[3]->ptr
;
181 if (getLongFromObjectOrReply(c
,c
->argv
[2],&offset
,NULL
) != REDIS_OK
)
185 addReplyError(c
,"offset is out of range");
189 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
191 /* Return 0 when setting nothing on a non-existing string */
192 if (sdslen(value
) == 0) {
193 addReply(c
,shared
.czero
);
197 /* Return when the resulting string exceeds allowed size */
198 if (checkStringLength(c
,offset
+sdslen(value
)) != REDIS_OK
)
201 o
= createObject(REDIS_STRING
,sdsempty());
202 dbAdd(c
->db
,c
->argv
[1],o
);
206 /* Key exists, check type */
207 if (checkType(c
,o
,REDIS_STRING
))
210 /* Return existing string length when setting nothing */
211 olen
= stringObjectLen(o
);
212 if (sdslen(value
) == 0) {
213 addReplyLongLong(c
,olen
);
217 /* Return when the resulting string exceeds allowed size */
218 if (checkStringLength(c
,offset
+sdslen(value
)) != REDIS_OK
)
221 /* Create a copy when the object is shared or encoded. */
222 if (o
->refcount
!= 1 || o
->encoding
!= REDIS_ENCODING_RAW
) {
223 robj
*decoded
= getDecodedObject(o
);
224 o
= createStringObject(decoded
->ptr
, sdslen(decoded
->ptr
));
225 decrRefCount(decoded
);
226 dbOverwrite(c
->db
,c
->argv
[1],o
);
230 if (sdslen(value
) > 0) {
231 o
->ptr
= sdsgrowzero(o
->ptr
,offset
+sdslen(value
));
232 memcpy((char*)o
->ptr
+offset
,value
,sdslen(value
));
233 signalModifiedKey(c
->db
,c
->argv
[1]);
236 addReplyLongLong(c
,sdslen(o
->ptr
));
239 void getrangeCommand(redisClient
*c
) {
242 char *str
, llbuf
[32];
245 if (getLongFromObjectOrReply(c
,c
->argv
[2],&start
,NULL
) != REDIS_OK
)
247 if (getLongFromObjectOrReply(c
,c
->argv
[3],&end
,NULL
) != REDIS_OK
)
249 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.emptybulk
)) == NULL
||
250 checkType(c
,o
,REDIS_STRING
)) return;
252 if (o
->encoding
== REDIS_ENCODING_INT
) {
254 strlen
= ll2string(llbuf
,sizeof(llbuf
),(long)o
->ptr
);
257 strlen
= sdslen(str
);
260 /* Convert negative indexes */
261 if (start
< 0) start
= strlen
+start
;
262 if (end
< 0) end
= strlen
+end
;
263 if (start
< 0) start
= 0;
264 if (end
< 0) end
= 0;
265 if ((unsigned)end
>= strlen
) end
= strlen
-1;
267 /* Precondition: end >= 0 && end < strlen, so the only condition where
268 * nothing can be returned is: start > end. */
270 addReply(c
,shared
.emptybulk
);
272 addReplyBulkCBuffer(c
,(char*)str
+start
,end
-start
+1);
276 void mgetCommand(redisClient
*c
) {
279 addReplyMultiBulkLen(c
,c
->argc
-1);
280 for (j
= 1; j
< c
->argc
; j
++) {
281 robj
*o
= lookupKeyRead(c
->db
,c
->argv
[j
]);
283 addReply(c
,shared
.nullbulk
);
285 if (o
->type
!= REDIS_STRING
) {
286 addReply(c
,shared
.nullbulk
);
294 void msetGenericCommand(redisClient
*c
, int nx
) {
297 if ((c
->argc
% 2) == 0) {
298 addReplyError(c
,"wrong number of arguments for MSET");
301 /* Handle the NX flag. The MSETNX semantic is to return zero and don't
302 * set nothing at all if at least one already key exists. */
304 for (j
= 1; j
< c
->argc
; j
+= 2) {
305 if (lookupKeyWrite(c
->db
,c
->argv
[j
]) != NULL
) {
310 addReply(c
, shared
.czero
);
315 for (j
= 1; j
< c
->argc
; j
+= 2) {
316 c
->argv
[j
+1] = tryObjectEncoding(c
->argv
[j
+1]);
317 setKey(c
->db
,c
->argv
[j
],c
->argv
[j
+1]);
319 server
.dirty
+= (c
->argc
-1)/2;
320 addReply(c
, nx
? shared
.cone
: shared
.ok
);
323 void msetCommand(redisClient
*c
) {
324 msetGenericCommand(c
,0);
327 void msetnxCommand(redisClient
*c
) {
328 msetGenericCommand(c
,1);
331 void incrDecrCommand(redisClient
*c
, long long incr
) {
332 long long value
, oldvalue
;
335 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
336 if (o
!= NULL
&& checkType(c
,o
,REDIS_STRING
)) return;
337 if (getLongLongFromObjectOrReply(c
,o
,&value
,NULL
) != REDIS_OK
) return;
341 if ((incr
< 0 && value
> oldvalue
) || (incr
> 0 && value
< oldvalue
)) {
342 addReplyError(c
,"increment or decrement would overflow");
345 new = createStringObjectFromLongLong(value
);
347 dbOverwrite(c
->db
,c
->argv
[1],new);
349 dbAdd(c
->db
,c
->argv
[1],new);
350 signalModifiedKey(c
->db
,c
->argv
[1]);
352 addReply(c
,shared
.colon
);
354 addReply(c
,shared
.crlf
);
357 void incrCommand(redisClient
*c
) {
358 incrDecrCommand(c
,1);
361 void decrCommand(redisClient
*c
) {
362 incrDecrCommand(c
,-1);
365 void incrbyCommand(redisClient
*c
) {
368 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
369 incrDecrCommand(c
,incr
);
372 void decrbyCommand(redisClient
*c
) {
375 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
376 incrDecrCommand(c
,-incr
);
379 void appendCommand(redisClient
*c
) {
383 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
386 c
->argv
[2] = tryObjectEncoding(c
->argv
[2]);
387 dbAdd(c
->db
,c
->argv
[1],c
->argv
[2]);
388 incrRefCount(c
->argv
[2]);
389 totlen
= stringObjectLen(c
->argv
[2]);
391 /* Key exists, check type */
392 if (checkType(c
,o
,REDIS_STRING
))
395 /* "append" is an argument, so always an sds */
397 totlen
= stringObjectLen(o
)+sdslen(append
->ptr
);
398 if (checkStringLength(c
,totlen
) != REDIS_OK
)
401 /* If the object is shared or encoded, we have to make a copy */
402 if (o
->refcount
!= 1 || o
->encoding
!= REDIS_ENCODING_RAW
) {
403 robj
*decoded
= getDecodedObject(o
);
404 o
= createStringObject(decoded
->ptr
, sdslen(decoded
->ptr
));
405 decrRefCount(decoded
);
406 dbOverwrite(c
->db
,c
->argv
[1],o
);
409 /* Append the value */
410 o
->ptr
= sdscatlen(o
->ptr
,append
->ptr
,sdslen(append
->ptr
));
411 totlen
= sdslen(o
->ptr
);
413 signalModifiedKey(c
->db
,c
->argv
[1]);
415 addReplyLongLong(c
,totlen
);
418 void strlenCommand(redisClient
*c
) {
420 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
421 checkType(c
,o
,REDIS_STRING
)) return;
422 addReplyLongLong(c
,stringObjectLen(o
));