Return random element from a non empty zset. * 'key' and 'val' will be set to hold the element. * The memory in `key` is not to be freed or modified by the caller. * 'score' can be NULL in which case it's not extracted. */
| 1688 | * The memory in `key` is not to be freed or modified by the caller. |
| 1689 | * 'score' can be NULL in which case it's not extracted. */ |
| 1690 | void zsetTypeRandomElement(robj_roptr zsetobj, unsigned long zsetsize, ziplistEntry *key, double *score) { |
| 1691 | if (zsetobj->encoding == OBJ_ENCODING_SKIPLIST) { |
| 1692 | zset *zs = (zset*)ptrFromObj(zsetobj); |
| 1693 | dictEntry *de = dictGetFairRandomKey(zs->dict); |
| 1694 | sds s = (sds)dictGetKey(de); |
| 1695 | key->sval = (unsigned char*)s; |
| 1696 | key->slen = sdslen(s); |
| 1697 | if (score) |
| 1698 | *score = *(double*)dictGetVal(de); |
| 1699 | } else if (zsetobj->encoding == OBJ_ENCODING_ZIPLIST) { |
| 1700 | ziplistEntry val; |
| 1701 | ziplistRandomPair((unsigned char*)ptrFromObj(zsetobj), zsetsize, key, &val); |
| 1702 | if (score) { |
| 1703 | if (val.sval) { |
| 1704 | *score = zzlStrtod(val.sval,val.slen); |
| 1705 | } else { |
| 1706 | *score = (double)val.lval; |
| 1707 | } |
| 1708 | } |
| 1709 | } else { |
| 1710 | serverPanic("Unknown zset encoding"); |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | /*----------------------------------------------------------------------------- |
| 1715 | * Sorted set commands |
no test coverage detected