APPEND command

This commit is contained in:
antirez 2010-02-04 15:55:52 +01:00
parent ed9e496634
commit 4b00bebd3d
3 changed files with 50 additions and 1 deletions

View File

@ -63,6 +63,7 @@ static struct redisCommand cmdTable[] = {
{"get",2,REDIS_CMD_INLINE},
{"set",3,REDIS_CMD_BULK},
{"setnx",3,REDIS_CMD_BULK},
{"append",3,REDIS_CMD_BULK},
{"del",-2,REDIS_CMD_INLINE},
{"exists",2,REDIS_CMD_INLINE},
{"incr",2,REDIS_CMD_INLINE},

48
redis.c
View File

@ -650,6 +650,7 @@ static void multiCommand(redisClient *c);
static void execCommand(redisClient *c);
static void blpopCommand(redisClient *c);
static void brpopCommand(redisClient *c);
static void appendCommand(redisClient *c);
/*================================= Globals ================================= */
@ -659,6 +660,7 @@ static struct redisCommand cmdTable[] = {
{"get",getCommand,2,REDIS_CMD_INLINE},
{"set",setCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
{"setnx",setnxCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
{"append",appendCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
{"del",delCommand,-2,REDIS_CMD_INLINE},
{"exists",existsCommand,2,REDIS_CMD_INLINE},
{"incr",incrCommand,2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM},
@ -3695,6 +3697,52 @@ static void decrbyCommand(redisClient *c) {
incrDecrCommand(c,-incr);
}
static void appendCommand(redisClient *c) {
int retval;
size_t totlen;
robj *o;
o = lookupKeyWrite(c->db,c->argv[1]);
if (o == NULL) {
/* Create the key */
retval = dictAdd(c->db->dict,c->argv[1],c->argv[2]);
incrRefCount(c->argv[1]);
incrRefCount(c->argv[2]);
totlen = stringObjectLen(c->argv[2]);
} else {
dictEntry *de;
de = dictFind(c->db->dict,c->argv[1]);
assert(de != NULL);
o = dictGetEntryVal(de);
if (o->type != REDIS_STRING) {
addReply(c,shared.wrongtypeerr);
return;
}
/* If the object is specially encoded or shared we have to make
* a copy */
if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) {
robj *decoded = getDecodedObject(o);
o = createStringObject(decoded->ptr, sdslen(decoded->ptr));
decrRefCount(decoded);
dictReplace(c->db->dict,c->argv[1],o);
}
/* APPEND! */
if (c->argv[2]->encoding == REDIS_ENCODING_RAW) {
o->ptr = sdscatlen(o->ptr,
c->argv[2]->ptr, sdslen(c->argv[2]->ptr));
} else {
o->ptr = sdscatprintf(o->ptr, "%ld",
(unsigned long) c->argv[2]->ptr);
}
totlen = sdslen(o->ptr);
}
server.dirty++;
addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",(unsigned long)totlen));
}
/* ========================= Type agnostic commands ========================= */
static void delCommand(redisClient *c) {

2
sds.c
View File

@ -159,7 +159,7 @@ sds sdscpy(sds s, char *t) {
sds sdscatprintf(sds s, const char *fmt, ...) {
va_list ap;
char *buf, *t;
size_t buflen = 32;
size_t buflen = 16;
while(1) {
buf = zmalloc(buflen);