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

Function hashTypeRandomElement

src/t_hash.cpp:620–636  ·  view source on GitHub ↗

Return random element from a non empty hash. * 'key' and 'val' will be set to hold the element. * The memory in them is not to be freed or modified by the caller. * 'val' can be NULL in which case it's not extracted. */

Source from the content-addressed store, hash-verified

618 * The memory in them is not to be freed or modified by the caller.
619 * 'val' can be NULL in which case it's not extracted. */
620void hashTypeRandomElement(robj_roptr hashobj, unsigned long hashsize, ziplistEntry *key, ziplistEntry *val) {
621 if (hashobj->encoding == OBJ_ENCODING_HT) {
622 dictEntry *de = dictGetFairRandomKey((dict*)ptrFromObj(hashobj));
623 sds s = (sds)dictGetKey(de);
624 key->sval = (unsigned char*)s;
625 key->slen = sdslen(s);
626 if (val) {
627 sds s = (sds)dictGetVal(de);
628 val->sval = (unsigned char*)s;
629 val->slen = sdslen(s);
630 }
631 } else if (hashobj->encoding == OBJ_ENCODING_ZIPLIST) {
632 ziplistRandomPair((unsigned char*)ptrFromObj(hashobj), hashsize, key, val);
633 } else {
634 serverPanic("Unknown hash encoding");
635 }
636}
637
638
639/*-----------------------------------------------------------------------------

Callers 2

hrandfieldCommandFunction · 0.85

Calls 4

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

Tested by

no test coverage detected