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

Function setTypeRandomElement

src/t_set.cpp:211–223  ·  view source on GitHub ↗

Return random element from a non empty set. * The returned element can be an int64_t value if the set is encoded * as an "intset" blob of integers, or an SDS string if the set * is a regular set. * * The caller provides both pointers to be populated with the right * object. The return value of the function is the object->encoding * field of the object and is used by the caller to check if t

Source from the content-addressed store, hash-verified

209 * be NULL since the function will try to defensively populate the non
210 * used field with values which are easy to trap if misused. */
211int setTypeRandomElement(robj *setobj, sds *sdsele, int64_t *llele) {
212 if (setobj->encoding == OBJ_ENCODING_HT) {
213 dictEntry *de = dictGetFairRandomKey((dict*)setobj->m_ptr);
214 *sdsele = (sds)dictGetKey(de);
215 *llele = -123456789; /* Not needed. Defensive. */
216 } else if (setobj->encoding == OBJ_ENCODING_INTSET) {
217 *llele = intsetRandom((intset*)setobj->m_ptr);
218 *sdsele = NULL; /* Not needed. Defensive. */
219 } else {
220 serverPanic("Unknown set encoding");
221 }
222 return setobj->encoding;
223}
224
225int setTypeRandomElement(robj_roptr setobj, const char **sdsele, int64_t *llele)
226{

Callers 4

spopWithCountCommandFunction · 0.85
spopCommandFunction · 0.85
srandmemberCommandFunction · 0.85

Calls 3

dictGetFairRandomKeyFunction · 0.85
intsetRandomFunction · 0.85
unsafe_robjcastMethod · 0.80

Tested by

no test coverage detected