]>
git.saurik.com Git - redis.git/blob - src/t_string.c
3 /*-----------------------------------------------------------------------------
5 *----------------------------------------------------------------------------*/
7 void setGenericCommand(redisClient
*c
, int nx
, robj
*key
, robj
*val
, robj
*expire
) {
9 long seconds
= 0; /* initialized to avoid an harmness warning */
12 if (getLongFromObjectOrReply(c
, expire
, &seconds
, NULL
) != REDIS_OK
)
15 addReplySds(c
,sdsnew("-ERR invalid expire time in SETEX\r\n"));
20 retval
= dbAdd(c
->db
,key
,val
);
21 if (retval
== REDIS_ERR
) {
23 dbReplace(c
->db
,key
,val
);
26 addReply(c
,shared
.czero
);
32 touchWatchedKey(c
->db
,key
);
34 removeExpire(c
->db
,key
);
35 if (expire
) setExpire(c
->db
,key
,time(NULL
)+seconds
);
36 addReply(c
, nx
? shared
.cone
: shared
.ok
);
39 void setCommand(redisClient
*c
) {
40 setGenericCommand(c
,0,c
->argv
[1],c
->argv
[2],NULL
);
43 void setnxCommand(redisClient
*c
) {
44 setGenericCommand(c
,1,c
->argv
[1],c
->argv
[2],NULL
);
47 void setexCommand(redisClient
*c
) {
48 setGenericCommand(c
,0,c
->argv
[1],c
->argv
[3],c
->argv
[2]);
51 int getGenericCommand(redisClient
*c
) {
54 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
)
57 if (o
->type
!= REDIS_STRING
) {
58 addReply(c
,shared
.wrongtypeerr
);
66 void getCommand(redisClient
*c
) {
70 void getsetCommand(redisClient
*c
) {
71 if (getGenericCommand(c
) == REDIS_ERR
) return;
72 dbReplace(c
->db
,c
->argv
[1],c
->argv
[2]);
73 incrRefCount(c
->argv
[2]);
74 touchWatchedKey(c
->db
,c
->argv
[1]);
76 removeExpire(c
->db
,c
->argv
[1]);
79 void mgetCommand(redisClient
*c
) {
82 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",c
->argc
-1));
83 for (j
= 1; j
< c
->argc
; j
++) {
84 robj
*o
= lookupKeyRead(c
->db
,c
->argv
[j
]);
86 addReply(c
,shared
.nullbulk
);
88 if (o
->type
!= REDIS_STRING
) {
89 addReply(c
,shared
.nullbulk
);
97 void msetGenericCommand(redisClient
*c
, int nx
) {
100 if ((c
->argc
% 2) == 0) {
101 addReplySds(c
,sdsnew("-ERR wrong number of arguments for MSET\r\n"));
104 /* Handle the NX flag. The MSETNX semantic is to return zero and don't
105 * set nothing at all if at least one already key exists. */
107 for (j
= 1; j
< c
->argc
; j
+= 2) {
108 if (lookupKeyWrite(c
->db
,c
->argv
[j
]) != NULL
) {
114 addReply(c
, shared
.czero
);
118 for (j
= 1; j
< c
->argc
; j
+= 2) {
119 c
->argv
[j
+1] = tryObjectEncoding(c
->argv
[j
+1]);
120 dbReplace(c
->db
,c
->argv
[j
],c
->argv
[j
+1]);
121 incrRefCount(c
->argv
[j
+1]);
122 removeExpire(c
->db
,c
->argv
[j
]);
123 touchWatchedKey(c
->db
,c
->argv
[j
]);
125 server
.dirty
+= (c
->argc
-1)/2;
126 addReply(c
, nx
? shared
.cone
: shared
.ok
);
129 void msetCommand(redisClient
*c
) {
130 msetGenericCommand(c
,0);
133 void msetnxCommand(redisClient
*c
) {
134 msetGenericCommand(c
,1);
137 void incrDecrCommand(redisClient
*c
, long long incr
) {
141 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
142 if (o
!= NULL
&& checkType(c
,o
,REDIS_STRING
)) return;
143 if (getLongLongFromObjectOrReply(c
,o
,&value
,NULL
) != REDIS_OK
) return;
146 o
= createStringObjectFromLongLong(value
);
147 dbReplace(c
->db
,c
->argv
[1],o
);
148 touchWatchedKey(c
->db
,c
->argv
[1]);
150 addReply(c
,shared
.colon
);
152 addReply(c
,shared
.crlf
);
155 void incrCommand(redisClient
*c
) {
156 incrDecrCommand(c
,1);
159 void decrCommand(redisClient
*c
) {
160 incrDecrCommand(c
,-1);
163 void incrbyCommand(redisClient
*c
) {
166 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
167 incrDecrCommand(c
,incr
);
170 void decrbyCommand(redisClient
*c
) {
173 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
174 incrDecrCommand(c
,-incr
);
177 void appendCommand(redisClient
*c
) {
182 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
185 retval
= dbAdd(c
->db
,c
->argv
[1],c
->argv
[2]);
186 incrRefCount(c
->argv
[2]);
187 totlen
= stringObjectLen(c
->argv
[2]);
189 if (o
->type
!= REDIS_STRING
) {
190 addReply(c
,shared
.wrongtypeerr
);
193 /* If the object is specially encoded or shared we have to make
195 if (o
->refcount
!= 1 || o
->encoding
!= REDIS_ENCODING_RAW
) {
196 robj
*decoded
= getDecodedObject(o
);
198 o
= createStringObject(decoded
->ptr
, sdslen(decoded
->ptr
));
199 decrRefCount(decoded
);
200 dbReplace(c
->db
,c
->argv
[1],o
);
203 if (c
->argv
[2]->encoding
== REDIS_ENCODING_RAW
) {
204 o
->ptr
= sdscatlen(o
->ptr
,
205 c
->argv
[2]->ptr
, sdslen(c
->argv
[2]->ptr
));
207 o
->ptr
= sdscatprintf(o
->ptr
, "%ld",
208 (unsigned long) c
->argv
[2]->ptr
);
210 totlen
= sdslen(o
->ptr
);
212 touchWatchedKey(c
->db
,c
->argv
[1]);
214 addReplySds(c
,sdscatprintf(sdsempty(),":%lu\r\n",(unsigned long)totlen
));
217 void substrCommand(redisClient
*c
) {
219 long start
= atoi(c
->argv
[2]->ptr
);
220 long end
= atoi(c
->argv
[3]->ptr
);
221 size_t rangelen
, strlen
;
224 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
225 checkType(c
,o
,REDIS_STRING
)) return;
227 o
= getDecodedObject(o
);
228 strlen
= sdslen(o
->ptr
);
230 /* convert negative indexes */
231 if (start
< 0) start
= strlen
+start
;
232 if (end
< 0) end
= strlen
+end
;
233 if (start
< 0) start
= 0;
234 if (end
< 0) end
= 0;
236 /* indexes sanity checks */
237 if (start
> end
|| (size_t)start
>= strlen
) {
238 /* Out of range start or start > end result in null reply */
239 addReply(c
,shared
.nullbulk
);
243 if ((size_t)end
>= strlen
) end
= strlen
-1;
244 rangelen
= (end
-start
)+1;
246 /* Return the result */
247 addReplySds(c
,sdscatprintf(sdsempty(),"$%zu\r\n",rangelen
));
248 range
= sdsnewlen((char*)o
->ptr
+start
,rangelen
);
249 addReplySds(c
,range
);
250 addReply(c
,shared
.crlf
);
254 void strlenCommand(redisClient
*c
) {
257 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
258 checkType(c
,o
,REDIS_STRING
)) return;
260 o
= getDecodedObject(o
);
261 addReplyLongLong(c
,sdslen(o
->ptr
));