MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / zsetTypeRandomElement

Function zsetTypeRandomElement

src/t_zset.cpp:1690–1712  ·  view source on GitHub ↗

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. */

Source from the content-addressed store, hash-verified

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. */
1690void 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

Callers 2

zrandmemberCommandFunction · 0.85

Calls 5

ptrFromObjFunction · 0.85
dictGetFairRandomKeyFunction · 0.85
sdslenFunction · 0.85
ziplistRandomPairFunction · 0.85
zzlStrtodFunction · 0.85

Tested by

no test coverage detected