]>
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";
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 /* Return existing string length when setting nothing */
225 olen
= stringObjectLen(o
);
226 if (sdslen(value
) == 0) {
227 addReplyLongLong(c
,olen
);
231 /* Return when the resulting string exceeds allowed size */
232 if (checkStringLength(c
,offset
+sdslen(value
)) != REDIS_OK
)
235 /* Create a copy when the object is shared or encoded. */
236 if (o
->refcount
!= 1 || o
->encoding
!= REDIS_ENCODING_RAW
) {
237 robj
*decoded
= getDecodedObject(o
);
238 o
= createStringObject(decoded
->ptr
, sdslen(decoded
->ptr
));
239 decrRefCount(decoded
);
240 dbReplace(c
->db
,c
->argv
[1],o
);
244 if (sdslen(value
) > 0) {
245 o
->ptr
= sdsgrowzero(o
->ptr
,offset
+sdslen(value
));
246 memcpy((char*)o
->ptr
+offset
,value
,sdslen(value
));
247 touchWatchedKey(c
->db
,c
->argv
[1]);
250 addReplyLongLong(c
,sdslen(o
->ptr
));
253 void getrangeCommand(redisClient
*c
) {
256 char *str
, llbuf
[32];
259 if (getLongFromObjectOrReply(c
,c
->argv
[2],&start
,NULL
) != REDIS_OK
)
261 if (getLongFromObjectOrReply(c
,c
->argv
[3],&end
,NULL
) != REDIS_OK
)
263 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
264 checkType(c
,o
,REDIS_STRING
)) return;
266 if (o
->encoding
== REDIS_ENCODING_INT
) {
268 strlen
= ll2string(llbuf
,sizeof(llbuf
),(long)o
->ptr
);
271 strlen
= sdslen(str
);
274 /* Convert negative indexes */
275 if (start
< 0) start
= strlen
+start
;
276 if (end
< 0) end
= strlen
+end
;
277 if (start
< 0) start
= 0;
278 if (end
< 0) end
= 0;
279 if ((unsigned)end
>= strlen
) end
= strlen
-1;
281 /* Precondition: end >= 0 && end < strlen, so the only condition where
282 * nothing can be returned is: start > end. */
284 addReply(c
,shared
.nullbulk
);
286 addReplyBulkCBuffer(c
,(char*)str
+start
,end
-start
+1);
290 void mgetCommand(redisClient
*c
) {
293 addReplyMultiBulkLen(c
,c
->argc
-1);
294 for (j
= 1; j
< c
->argc
; j
++) {
295 robj
*o
= lookupKeyRead(c
->db
,c
->argv
[j
]);
297 addReply(c
,shared
.nullbulk
);
299 if (o
->type
!= REDIS_STRING
) {
300 addReply(c
,shared
.nullbulk
);
308 void msetGenericCommand(redisClient
*c
, int nx
) {
311 if ((c
->argc
% 2) == 0) {
312 addReplyError(c
,"wrong number of arguments for MSET");
315 /* Handle the NX flag. The MSETNX semantic is to return zero and don't
316 * set nothing at all if at least one already key exists. */
318 for (j
= 1; j
< c
->argc
; j
+= 2) {
319 if (lookupKeyWrite(c
->db
,c
->argv
[j
]) != NULL
) {
325 addReply(c
, shared
.czero
);
329 for (j
= 1; j
< c
->argc
; j
+= 2) {
330 c
->argv
[j
+1] = tryObjectEncoding(c
->argv
[j
+1]);
331 dbReplace(c
->db
,c
->argv
[j
],c
->argv
[j
+1]);
332 incrRefCount(c
->argv
[j
+1]);
333 removeExpire(c
->db
,c
->argv
[j
]);
334 touchWatchedKey(c
->db
,c
->argv
[j
]);
336 server
.dirty
+= (c
->argc
-1)/2;
337 addReply(c
, nx
? shared
.cone
: shared
.ok
);
340 void msetCommand(redisClient
*c
) {
341 msetGenericCommand(c
,0);
344 void msetnxCommand(redisClient
*c
) {
345 msetGenericCommand(c
,1);
348 void incrDecrCommand(redisClient
*c
, long long incr
) {
352 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
353 if (o
!= NULL
&& checkType(c
,o
,REDIS_STRING
)) return;
354 if (getLongLongFromObjectOrReply(c
,o
,&value
,NULL
) != REDIS_OK
) return;
357 o
= createStringObjectFromLongLong(value
);
358 dbReplace(c
->db
,c
->argv
[1],o
);
359 touchWatchedKey(c
->db
,c
->argv
[1]);
361 addReply(c
,shared
.colon
);
363 addReply(c
,shared
.crlf
);
366 void incrCommand(redisClient
*c
) {
367 incrDecrCommand(c
,1);
370 void decrCommand(redisClient
*c
) {
371 incrDecrCommand(c
,-1);
374 void incrbyCommand(redisClient
*c
) {
377 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
378 incrDecrCommand(c
,incr
);
381 void decrbyCommand(redisClient
*c
) {
384 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
385 incrDecrCommand(c
,-incr
);
388 void appendCommand(redisClient
*c
) {
392 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
395 c
->argv
[2] = tryObjectEncoding(c
->argv
[2]);
396 dbAdd(c
->db
,c
->argv
[1],c
->argv
[2]);
397 incrRefCount(c
->argv
[2]);
398 totlen
= stringObjectLen(c
->argv
[2]);
400 /* Key exists, check type */
401 if (checkType(c
,o
,REDIS_STRING
))
404 /* "append" is an argument, so always an sds */
406 totlen
= stringObjectLen(o
)+sdslen(append
->ptr
);
407 if (checkStringLength(c
,totlen
) != REDIS_OK
)
410 /* If the object is shared or encoded, we have to make a copy */
411 if (o
->refcount
!= 1 || o
->encoding
!= REDIS_ENCODING_RAW
) {
412 robj
*decoded
= getDecodedObject(o
);
413 o
= createStringObject(decoded
->ptr
, sdslen(decoded
->ptr
));
414 decrRefCount(decoded
);
415 dbReplace(c
->db
,c
->argv
[1],o
);
418 /* Append the value */
419 o
->ptr
= sdscatlen(o
->ptr
,append
->ptr
,sdslen(append
->ptr
));
420 totlen
= sdslen(o
->ptr
);
422 touchWatchedKey(c
->db
,c
->argv
[1]);
424 addReplyLongLong(c
,totlen
);
427 void strlenCommand(redisClient
*c
) {
429 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
430 checkType(c
,o
,REDIS_STRING
)) return;
431 addReplyLongLong(c
,stringObjectLen(o
));