From 8f8117f78e004fa5f5808239dcaedf9acdb8c828 Mon Sep 17 00:00:00 2001 From: "Meir Shpilraien (Spielrein)" Date: Tue, 10 Aug 2021 10:19:21 +0300 Subject: [PATCH] Format fixes and naming. SentReplyOnKeyMiss -> addReplyOrErrorObject (#9346) Following the comments on #8659, this PR fix some formatting and naming issues. --- src/db.c | 14 +++----------- src/networking.c | 15 +++++++++++++++ src/object.c | 2 +- src/server.h | 2 +- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/db.c b/src/db.c index 635dc972f5..94a03e8e0d 100644 --- a/src/db.c +++ b/src/db.c @@ -165,24 +165,16 @@ robj *lookupKeyWriteWithFlags(redisDb *db, robj *key, int flags) { robj *lookupKeyWrite(redisDb *db, robj *key) { return lookupKeyWriteWithFlags(db, key, LOOKUP_NONE); } -void SentReplyOnKeyMiss(client *c, robj *reply){ - serverAssert(sdsEncodedObject(reply)); - sds rep = reply->ptr; - if (sdslen(rep) > 1 && rep[0] == '-'){ - addReplyErrorObject(c, reply); - } else { - addReply(c,reply); - } -} + robj *lookupKeyReadOrReply(client *c, robj *key, robj *reply) { robj *o = lookupKeyRead(c->db, key); - if (!o) SentReplyOnKeyMiss(c, reply); + if (!o) addReplyOrErrorObject(c, reply); return o; } robj *lookupKeyWriteOrReply(client *c, robj *key, robj *reply) { robj *o = lookupKeyWrite(c->db, key); - if (!o) SentReplyOnKeyMiss(c, reply); + if (!o) addReplyOrErrorObject(c, reply); return o; } diff --git a/src/networking.c b/src/networking.c index 3ab9b3e5bb..c18b8f2477 100644 --- a/src/networking.c +++ b/src/networking.c @@ -469,6 +469,21 @@ void addReplyErrorObject(client *c, robj *err) { afterErrorReply(c, err->ptr, sdslen(err->ptr)-2); /* Ignore trailing \r\n */ } +/* Sends either a reply or an error reply by checking the first char. + * If the first char is '-' the reply is considered an error. + * In any case the given reply is sent, if the reply is also recognize + * as an error we also perform some post reply operations such as + * logging and stats update. */ +void addReplyOrErrorObject(client *c, robj *reply) { + serverAssert(sdsEncodedObject(reply)); + sds rep = reply->ptr; + if (sdslen(rep) > 1 && rep[0] == '-') { + addReplyErrorObject(c, reply); + } else { + addReply(c, reply); + } +} + /* See addReplyErrorLength for expectations from the input string. */ void addReplyError(client *c, const char *err) { addReplyErrorLength(c,err,strlen(err)); diff --git a/src/object.c b/src/object.c index 90fa282544..a5705ea5a9 100644 --- a/src/object.c +++ b/src/object.c @@ -1403,7 +1403,7 @@ robj *objectCommandLookup(client *c, robj *key) { robj *objectCommandLookupOrReply(client *c, robj *key, robj *reply) { robj *o = objectCommandLookup(c,key); - if (!o) SentReplyOnKeyMiss(c, reply); + if (!o) addReplyOrErrorObject(c, reply); return o; } diff --git a/src/server.h b/src/server.h index f01ab4ba0e..177525aa40 100644 --- a/src/server.h +++ b/src/server.h @@ -1891,6 +1891,7 @@ void addReplySds(client *c, sds s); void addReplyBulkSds(client *c, sds s); void setDeferredReplyBulkSds(client *c, void *node, sds s); void addReplyErrorObject(client *c, robj *err); +void addReplyOrErrorObject(client *c, robj *reply); void addReplyErrorSds(client *c, sds err); void addReplyError(client *c, const char *err); void addReplyStatus(client *c, const char *status); @@ -2396,7 +2397,6 @@ robj *lookupKeyReadWithFlags(redisDb *db, robj *key, int flags); robj *lookupKeyWriteWithFlags(redisDb *db, robj *key, int flags); robj *objectCommandLookup(client *c, robj *key); robj *objectCommandLookupOrReply(client *c, robj *key, robj *reply); -void SentReplyOnKeyMiss(client *c, robj *reply); int objectSetLRUOrLFU(robj *val, long long lfu_freq, long long lru_idle, long long lru_clock, int lru_multiplier); #define LOOKUP_NONE 0