From 5577130451e8ba6ceeac07137f45915a069706a9 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 20 Mar 2018 11:52:34 +0100 Subject: [PATCH] CG: Make XINFO Great Again (and more Redis-ish). With XINFO out of the blue I invented a new syntax for commands never used in Redis in the past... Let's fix it and make it Great Again!!11one (TM) --- src/server.c | 2 +- src/t_stream.c | 54 ++++++++++++++++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/server.c b/src/server.c index 803d609c..7c7a719f 100644 --- a/src/server.c +++ b/src/server.c @@ -312,7 +312,7 @@ struct redisCommand redisCommandTable[] = { {"xack",xackCommand,-3,"wF",0,NULL,1,1,1,0,0}, {"xpending",xpendingCommand,-3,"r",0,NULL,1,1,1,0,0}, {"xclaim",xclaimCommand,-5,"wF",0,NULL,1,1,1,0,0}, - {"xinfo",xinfoCommand,-2,"r",0,NULL,1,1,1,0,0}, + {"xinfo",xinfoCommand,-2,"r",0,NULL,2,2,1,0,0}, {"post",securityWarningCommand,-1,"lt",0,NULL,0,0,0,0,0}, {"host:",securityWarningCommand,-1,"lt",0,NULL,0,0,0,0,0}, {"latency",latencyCommand,-2,"aslt",0,NULL,0,0,0,0,0} diff --git a/src/t_stream.c b/src/t_stream.c index f2da0100..37f08572 100644 --- a/src/t_stream.c +++ b/src/t_stream.c @@ -1926,34 +1926,52 @@ void xclaimCommand(client *c) { preventCommandPropagation(c); } -/* XINFO [CONSUMERS group|GROUPS|STREAM]. STREAM is the default */ +/* XINFO CONSUMERS key group + * XINFO GROUPS + * XINFO STREAM + * XINFO (alias of XINFO STREAM key) + * XINFO HELP. */ void xinfoCommand(client *c) { const char *help[] = { -" CONSUMERS -- Show consumer groups of group .", -" GROUPS -- Show the stream consumer groups.", -" STREAM -- Show information about the stream.", -" (without subcommand) -- Alias for STREAM.", -" HELP -- Prints this help.", +"CONSUMERS -- Show consumer groups of group .", +"GROUPS -- Show the stream consumer groups.", +"STREAM -- Show information about the stream.", +" -- Alias for STREAM .", +"HELP -- Print this help.", NULL }; stream *s = NULL; - char *opt = c->argc > 2 ? c->argv[2]->ptr : "STREAM"; /* Subcommand. */ + char *opt; + robj *key; + + /* HELP is special. Handle it ASAP. */ + if (!strcasecmp(c->argv[1]->ptr,"HELP")) { + addReplyHelp(c, help); + return; + } + + /* Handle the fact that no subcommand means "STREAM". */ + if (c->argc == 2) { + opt = "STREAM"; + key = c->argv[1]; + } else { + opt = c->argv[1]->ptr; + key = c->argv[2]; + } /* Lookup the key now, this is common for all the subcommands but HELP. */ - if (c->argc >= 2 && strcasecmp(opt,"HELP")) { - robj *o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr); - if (o == NULL) return; - s = o->ptr; - } + robj *o = lookupKeyWriteOrReply(c,key,shared.nokeyerr); + if (o == NULL) return; + s = o->ptr; /* Dispatch the different subcommands. */ if (!strcasecmp(opt,"CONSUMERS") && c->argc == 4) { - /* XINFO CONSUMERS . */ + /* XINFO CONSUMERS . */ streamCG *cg = streamLookupCG(s,c->argv[3]->ptr); if (cg == NULL) { addReplyErrorFormat(c, "-NOGROUP No such consumer group '%s' " "for key name '%s'", - c->argv[3]->ptr, c->argv[1]->ptr); + c->argv[3]->ptr, key->ptr); return; } @@ -1977,7 +1995,7 @@ NULL } raxStop(&ri); } else if (!strcasecmp(opt,"GROUPS") && c->argc == 3) { - /* XINFO GROUPS. */ + /* XINFO GROUPS . */ if (s->cgroups == NULL) { addReplyMultiBulkLen(c,0); return; @@ -2001,7 +2019,7 @@ NULL } else if (c->argc == 2 || (!strcasecmp(opt,"STREAM") && c->argc == 3)) { - /* XINFO STREAM (or the alias XINFO ). */ + /* XINFO STREAM (or the alias XINFO ). */ addReplyMultiBulkLen(c,12); addReplyStatus(c,"length"); addReplyLongLong(c,s->length); @@ -2026,10 +2044,8 @@ NULL count = streamReplyWithRange(c,s,&start,&end,1,1,NULL,NULL, STREAM_RWR_RAWENTRIES,NULL); if (!count) addReply(c,shared.nullbulk); - } else if (!strcasecmp(opt,"HELP")) { - addReplyHelp(c, help); } else { - addReplyError(c,"syntax error, try 'XINFO anykey HELP'"); + addReplyError(c,"syntax error, try 'XINFO HELP'"); } }