From ad41a7c40430bc043c2b95556197af2862a2bab5 Mon Sep 17 00:00:00 2001 From: Matt Stancliff Date: Sun, 16 Nov 2014 13:03:54 -0500 Subject: [PATCH] Add addReplyBulkSds() function Refactor a common pattern into one function so we don't end up with copy/paste programming. --- src/cluster.c | 5 +---- src/networking.c | 8 ++++++++ src/redis.c | 6 +----- src/redis.h | 1 + src/sentinel.c | 5 +---- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 6fc028627..a308fc507 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -3886,10 +3886,7 @@ void clusterCommand(redisClient *c) { server.cluster->stats_bus_messages_sent, server.cluster->stats_bus_messages_received ); - addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n", - (unsigned long)sdslen(info))); - addReplySds(c,info); - addReply(c,shared.crlf); + addReplyBulkSds(c, info); } else if (!strcasecmp(c->argv[1]->ptr,"saveconfig") && c->argc == 2) { int retval = clusterSaveConfig(1); diff --git a/src/networking.c b/src/networking.c index e4890588a..607d225fd 100644 --- a/src/networking.c +++ b/src/networking.c @@ -525,6 +525,14 @@ void addReplyBulkCBuffer(redisClient *c, void *p, size_t len) { addReply(c,shared.crlf); } +/* Add sds to reply (takes ownership of sds and frees it) */ +void addReplyBulkSds(redisClient *c, sds s) { + addReplySds(c,sdscatfmt(sdsempty(),"$%u\r\n", + (unsigned long)sdslen(s))); + addReplySds(c,s); + addReply(c,shared.crlf); +} + /* Add a C nul term string as bulk reply */ void addReplyBulkCString(redisClient *c, char *s) { if (s == NULL) { diff --git a/src/redis.c b/src/redis.c index d68065512..9077dd5e6 100644 --- a/src/redis.c +++ b/src/redis.c @@ -3058,11 +3058,7 @@ void infoCommand(redisClient *c) { addReply(c,shared.syntaxerr); return; } - sds info = genRedisInfoString(section); - addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n", - (unsigned long)sdslen(info))); - addReplySds(c,info); - addReply(c,shared.crlf); + addReplyBulkSds(c, genRedisInfoString(section)); } void monitorCommand(redisClient *c) { diff --git a/src/redis.h b/src/redis.h index d60eaf34a..87415967d 100644 --- a/src/redis.h +++ b/src/redis.h @@ -1043,6 +1043,7 @@ void addReplyBulkCBuffer(redisClient *c, void *p, size_t len); void addReplyBulkLongLong(redisClient *c, long long ll); void addReply(redisClient *c, robj *obj); void addReplySds(redisClient *c, sds s); +void addReplyBulkSds(redisClient *c, sds s); void addReplyError(redisClient *c, char *err); void addReplyStatus(redisClient *c, char *status); void addReplyDouble(redisClient *c, double d); diff --git a/src/sentinel.c b/src/sentinel.c index 01c811813..33d970e57 100644 --- a/src/sentinel.c +++ b/src/sentinel.c @@ -2908,10 +2908,7 @@ void sentinelInfoCommand(redisClient *c) { dictReleaseIterator(di); } - addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n", - (unsigned long)sdslen(info))); - addReplySds(c,info); - addReply(c,shared.crlf); + addReplyBulkSds(c, info); } /* Implements Sentinel verison of the ROLE command. The output is