]> git.saurik.com Git - redis.git/blobdiff - src/t_set.c
Merge pull request #544 from dvirsky/2.6
[redis.git] / src / t_set.c
index 3ad75aef640b02c6dc904cb3d69b8c6cef7d468a..fada1095aef1f6317a976e6f665de022de0962b2 100644 (file)
@@ -187,7 +187,7 @@ unsigned long setTypeSize(robj *subject) {
 }
 
 /* Convert the set to specified encoding. The resulting dict (when converting
- * to a hashtable) is presized to hold the number of elements in the original
+ * to a hash table) is presized to hold the number of elements in the original
  * set. */
 void setTypeConvert(robj *setobj, int enc) {
     setTypeIterator *si;
@@ -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);
         }
     }