]>
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 touchWatchedKey(c
->db
,key
);
21 if (nx
) deleteIfVolatile(c
->db
,key
);
22 retval
= dbAdd(c
->db
,key
,val
);
23 if (retval
== REDIS_ERR
) {
25 dbReplace(c
->db
,key
,val
);
28 addReply(c
,shared
.czero
);
35 removeExpire(c
->db
,key
);
36 if (expire
) setExpire(c
->db
,key
,time(NULL
)+seconds
);
37 addReply(c
, nx
? shared
.cone
: shared
.ok
);
40 void setCommand(redisClient
*c
) {
41 setGenericCommand(c
,0,c
->argv
[1],c
->argv
[2],NULL
);
44 void setnxCommand(redisClient
*c
) {
45 setGenericCommand(c
,1,c
->argv
[1],c
->argv
[2],NULL
);
48 void setexCommand(redisClient
*c
) {
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 dbReplace(c
->db
,c
->argv
[1],c
->argv
[2]);
74 incrRefCount(c
->argv
[2]);
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
]);
124 server
.dirty
+= (c
->argc
-1)/2;
125 addReply(c
, nx
? shared
.cone
: shared
.ok
);
128 void msetCommand(redisClient
*c
) {
129 msetGenericCommand(c
,0);
132 void msetnxCommand(redisClient
*c
) {
133 msetGenericCommand(c
,1);
136 void incrDecrCommand(redisClient
*c
, long long incr
) {
140 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
141 if (o
!= NULL
&& checkType(c
,o
,REDIS_STRING
)) return;
142 if (getLongLongFromObjectOrReply(c
,o
,&value
,NULL
) != REDIS_OK
) return;
145 o
= createStringObjectFromLongLong(value
);
146 dbReplace(c
->db
,c
->argv
[1],o
);
148 addReply(c
,shared
.colon
);
150 addReply(c
,shared
.crlf
);
153 void incrCommand(redisClient
*c
) {
154 incrDecrCommand(c
,1);
157 void decrCommand(redisClient
*c
) {
158 incrDecrCommand(c
,-1);
161 void incrbyCommand(redisClient
*c
) {
164 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
165 incrDecrCommand(c
,incr
);
168 void decrbyCommand(redisClient
*c
) {
171 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
172 incrDecrCommand(c
,-incr
);
175 void appendCommand(redisClient
*c
) {
180 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
183 retval
= dbAdd(c
->db
,c
->argv
[1],c
->argv
[2]);
184 incrRefCount(c
->argv
[2]);
185 totlen
= stringObjectLen(c
->argv
[2]);
187 if (o
->type
!= REDIS_STRING
) {
188 addReply(c
,shared
.wrongtypeerr
);
191 /* If the object is specially encoded or shared we have to make
193 if (o
->refcount
!= 1 || o
->encoding
!= REDIS_ENCODING_RAW
) {
194 robj
*decoded
= getDecodedObject(o
);
196 o
= createStringObject(decoded
->ptr
, sdslen(decoded
->ptr
));
197 decrRefCount(decoded
);
198 dbReplace(c
->db
,c
->argv
[1],o
);
201 if (c
->argv
[2]->encoding
== REDIS_ENCODING_RAW
) {
202 o
->ptr
= sdscatlen(o
->ptr
,
203 c
->argv
[2]->ptr
, sdslen(c
->argv
[2]->ptr
));
205 o
->ptr
= sdscatprintf(o
->ptr
, "%ld",
206 (unsigned long) c
->argv
[2]->ptr
);
208 totlen
= sdslen(o
->ptr
);
211 addReplySds(c
,sdscatprintf(sdsempty(),":%lu\r\n",(unsigned long)totlen
));
214 void substrCommand(redisClient
*c
) {
216 long start
= atoi(c
->argv
[2]->ptr
);
217 long end
= atoi(c
->argv
[3]->ptr
);
218 size_t rangelen
, strlen
;
221 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
222 checkType(c
,o
,REDIS_STRING
)) return;
224 o
= getDecodedObject(o
);
225 strlen
= sdslen(o
->ptr
);
227 /* convert negative indexes */
228 if (start
< 0) start
= strlen
+start
;
229 if (end
< 0) end
= strlen
+end
;
230 if (start
< 0) start
= 0;
231 if (end
< 0) end
= 0;
233 /* indexes sanity checks */
234 if (start
> end
|| (size_t)start
>= strlen
) {
235 /* Out of range start or start > end result in null reply */
236 addReply(c
,shared
.nullbulk
);
240 if ((size_t)end
>= strlen
) end
= strlen
-1;
241 rangelen
= (end
-start
)+1;
243 /* Return the result */
244 addReplySds(c
,sdscatprintf(sdsempty(),"$%zu\r\n",rangelen
));
245 range
= sdsnewlen((char*)o
->ptr
+start
,rangelen
);
246 addReplySds(c
,range
);
247 addReply(c
,shared
.crlf
);