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. */
| 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. */ |
| 620 | void 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 | /*----------------------------------------------------------------------------- |
no test coverage detected