From 2abb95a9a849453eeb864e919ea0b8d6495a6a2a Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 21 Oct 2009 10:50:24 +0200 Subject: [PATCH] SRANDMEMBER added --- redis-cli.c | 1 + redis.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/redis-cli.c b/redis-cli.c index 17a9d037..a8d3eca9 100644 --- a/redis-cli.c +++ b/redis-cli.c @@ -81,6 +81,7 @@ static struct redisCommand cmdTable[] = { {"sismember",3,REDIS_CMD_BULK}, {"scard",2,REDIS_CMD_INLINE}, {"spop",2,REDIS_CMD_INLINE}, + {"srandmember",2,REDIS_CMD_INLINE}, {"sinter",-2,REDIS_CMD_INLINE}, {"sinterstore",-3,REDIS_CMD_INLINE}, {"sunion",-2,REDIS_CMD_INLINE}, diff --git a/redis.c b/redis.c index b934b40d..31402663 100644 --- a/redis.c +++ b/redis.c @@ -385,6 +385,7 @@ static void smoveCommand(redisClient *c); static void sismemberCommand(redisClient *c); static void scardCommand(redisClient *c); static void spopCommand(redisClient *c); +static void srandmemberCommand(redisClient *c); static void sinterCommand(redisClient *c); static void sinterstoreCommand(redisClient *c); static void sunionCommand(redisClient *c); @@ -436,6 +437,7 @@ static struct redisCommand cmdTable[] = { {"sismember",sismemberCommand,3,REDIS_CMD_BULK}, {"scard",scardCommand,2,REDIS_CMD_INLINE}, {"spop",spopCommand,2,REDIS_CMD_INLINE}, + {"srandmember",srandmemberCommand,2,REDIS_CMD_INLINE}, {"sinter",sinterCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM}, {"sinterstore",sinterstoreCommand,-3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM}, {"sunion",sunionCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM}, @@ -3387,6 +3389,31 @@ static void spopCommand(redisClient *c) { } } +static void srandmemberCommand(redisClient *c) { + robj *set; + dictEntry *de; + + set = lookupKeyRead(c->db,c->argv[1]); + if (set == NULL) { + addReply(c,shared.nullbulk); + } else { + if (set->type != REDIS_SET) { + addReply(c,shared.wrongtypeerr); + return; + } + de = dictGetRandomKey(set->ptr); + if (de == NULL) { + addReply(c,shared.nullbulk); + } else { + robj *ele = dictGetEntryKey(de); + + addReplyBulkLen(c,ele); + addReply(c,ele); + addReply(c,shared.crlf); + } + } +} + static int qsortCompareSetsByCardinality(const void *s1, const void *s2) { dict **d1 = (void*) s1, **d2 = (void*) s2; @@ -4629,6 +4656,7 @@ static struct redisFunctionSym symsTable[] = { {"sismemberCommand", (unsigned long)sismemberCommand}, {"scardCommand", (unsigned long)scardCommand}, {"spopCommand", (unsigned long)spopCommand}, +{"srandmemberCommand", (unsigned long)srandmemberCommand}, {"sinterCommand", (unsigned long)sinterCommand}, {"sinterstoreCommand", (unsigned long)sinterstoreCommand}, {"sunionCommand", (unsigned long)sunionCommand}, -- 2.47.2