X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/be90c803e3bb041335ea0b8f40348965846f06b7..a1b1c1ea3adb864c8c2e3feb4eaed890a9b505cf:/src/t_set.c diff --git a/src/t_set.c b/src/t_set.c index 33e7bb38..fada1095 100644 --- a/src/t_set.c +++ b/src/t_set.c @@ -476,19 +476,21 @@ void srandmemberWithCountCommand(redisClient *c) { unsigned long added = 0; while(added < count) { - int retval; - encoding = setTypeRandomElement(set,&ele,&llele); if (encoding == REDIS_ENCODING_INTSET) { - retval = dictAdd(d,createStringObjectFromLongLong(llele),NULL); + ele = createStringObjectFromLongLong(llele); } else if (ele->encoding == REDIS_ENCODING_RAW) { - retval = dictAdd(d,dupStringObject(ele),NULL); + ele = dupStringObject(ele); } else if (ele->encoding == REDIS_ENCODING_INT) { - retval = dictAdd(d, - createStringObjectFromLongLong((long)ele->ptr),NULL); + ele = createStringObjectFromLongLong((long)ele->ptr); } - - if (retval == DICT_OK) added++; + /* Try to add the object to the dictionary. If it already exists + * free it, otherwise increment the number of objects we have + * in the result dictionary. */ + if (dictAdd(d,ele,NULL) == DICT_OK) + added++; + else + decrRefCount(ele); } }