From 0537e7bf8042cf9954d3b0abab567edf3b5c0516 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 2 Sep 2010 12:38:34 +0200 Subject: [PATCH] Use specialized function to add multi bulk reply length --- src/multi.c | 2 +- src/networking.c | 4 ++++ src/redis.h | 1 + src/sort.c | 2 +- src/t_hash.c | 2 +- src/t_list.c | 6 +++--- src/t_set.c | 2 +- src/t_string.c | 2 +- src/t_zset.c | 3 +-- 9 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/multi.c b/src/multi.c index def1dd67..c85516df 100644 --- a/src/multi.c +++ b/src/multi.c @@ -107,7 +107,7 @@ void execCommand(redisClient *c) { unwatchAllKeys(c); /* Unwatch ASAP otherwise we'll waste CPU cycles */ orig_argv = c->argv; orig_argc = c->argc; - addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->mstate.count)); + addReplyMultiBulkLen(c,c->mstate.count); for (j = 0; j < c->mstate.count; j++) { c->argc = c->mstate.commands[j].argc; c->argv = c->mstate.commands[j].argv; diff --git a/src/networking.c b/src/networking.c index 971cbfc1..d2a4e231 100644 --- a/src/networking.c +++ b/src/networking.c @@ -200,6 +200,10 @@ void addReplyUlong(redisClient *c, unsigned long ul) { _addReplyLongLong(c,(long long)ul,':'); } +void addReplyMultiBulkLen(redisClient *c, long length) { + _addReplyLongLong(c,length,'*'); +} + void addReplyBulkLen(redisClient *c, robj *obj) { size_t len; diff --git a/src/redis.h b/src/redis.h index 752d56c3..6ee1d2e3 100644 --- a/src/redis.h +++ b/src/redis.h @@ -617,6 +617,7 @@ void addReplySds(redisClient *c, sds s); void addReplyDouble(redisClient *c, double d); void addReplyLongLong(redisClient *c, long long ll); void addReplyUlong(redisClient *c, unsigned long ul); +void addReplyMultiBulkLen(redisClient *c, long length); void *dupClientReplyValue(void *o); /* List data type */ diff --git a/src/sort.c b/src/sort.c index aa1ce929..f53ad486 100644 --- a/src/sort.c +++ b/src/sort.c @@ -307,7 +307,7 @@ void sortCommand(redisClient *c) { outputlen = getop ? getop*(end-start+1) : end-start+1; if (storekey == NULL) { /* STORE option not specified, sent the sorting result to client */ - addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",outputlen)); + addReplyMultiBulkLen(c,outputlen); for (j = start; j <= end; j++) { listNode *ln; listIter li; diff --git a/src/t_hash.c b/src/t_hash.c index c8be72f2..ad5d3e1e 100644 --- a/src/t_hash.c +++ b/src/t_hash.c @@ -315,7 +315,7 @@ void hmgetCommand(redisClient *c) { /* Note the check for o != NULL happens inside the loop. This is * done because objects that cannot be found are considered to be * an empty hash. The reply should then be a series of NULLs. */ - addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->argc-2)); + addReplyMultiBulkLen(c,c->argc-2); for (i = 2; i < c->argc; i++) { if (o != NULL && (value = hashTypeGet(o,c->argv[i])) != NULL) { addReplyBulk(c,value); diff --git a/src/t_list.c b/src/t_list.c index 2a981033..db9ca18e 100644 --- a/src/t_list.c +++ b/src/t_list.c @@ -494,7 +494,7 @@ void lrangeCommand(redisClient *c) { rangelen = (end-start)+1; /* Return the result in form of a multi-bulk reply */ - addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",rangelen)); + addReplyMultiBulkLen(c,rangelen); listTypeIterator *li = listTypeInitIterator(o,start,REDIS_TAIL); for (j = 0; j < rangelen; j++) { redisAssert(listTypeNext(li,&entry)); @@ -772,7 +772,7 @@ int handleClientsWaitingListPush(redisClient *c, robj *key, robj *ele) { redisAssert(ln != NULL); receiver = ln->value; - addReplySds(receiver,sdsnew("*2\r\n")); + addReplyMultiBulkLen(receiver,2); addReplyBulk(receiver,key); addReplyBulk(receiver,ele); unblockClientWaitingData(receiver); @@ -811,7 +811,7 @@ void blockingPopGenericCommand(redisClient *c, int where) { * "real" command will add the last element (the value) * for us. If this souds like an hack to you it's just * because it is... */ - addReplySds(c,sdsnew("*2\r\n")); + addReplyMultiBulkLen(c,2); addReplyBulk(c,argv[1]); popGenericCommand(c,where); diff --git a/src/t_set.c b/src/t_set.c index d6041e72..17cac934 100644 --- a/src/t_set.c +++ b/src/t_set.c @@ -469,7 +469,7 @@ void sunionDiffGenericCommand(redisClient *c, robj **setkeys, int setnum, robj * /* Output the content of the resulting set, if not in STORE mode */ if (!dstkey) { - addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",cardinality)); + addReplyMultiBulkLen(c,cardinality); si = setTypeInitIterator(dstset); while((ele = setTypeNext(si)) != NULL) { addReplyBulk(c,ele); diff --git a/src/t_string.c b/src/t_string.c index 3b8a39bb..411687a5 100644 --- a/src/t_string.c +++ b/src/t_string.c @@ -79,7 +79,7 @@ void getsetCommand(redisClient *c) { void mgetCommand(redisClient *c) { int j; - addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->argc-1)); + addReplyMultiBulkLen(c,c->argc-1); for (j = 1; j < c->argc; j++) { robj *o = lookupKeyRead(c->db,c->argv[j]); if (o == NULL) { diff --git a/src/t_zset.c b/src/t_zset.c index d25b1a66..7de63158 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -782,8 +782,7 @@ void zrangeGenericCommand(redisClient *c, int reverse) { } /* Return the result in form of a multi-bulk reply */ - addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n", - withscores ? (rangelen*2) : rangelen)); + addReplyMultiBulkLen(c,withscores ? (rangelen*2) : rangelen); for (j = 0; j < rangelen; j++) { ele = ln->obj; addReplyBulk(c,ele); -- 2.45.2