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

Function zrandmemberCommand

src/t_zset.cpp:4210–4240  ·  view source on GitHub ↗

ZRANDMEMBER key [ [WITHSCORES]] */

Source from the content-addressed store, hash-verified

4208
4209/* ZRANDMEMBER key [<count> [WITHSCORES]] */
4210void zrandmemberCommand(client *c) {
4211 long l;
4212 int withscores = 0;
4213 robj_roptr zset;
4214 ziplistEntry ele;
4215
4216 if (c->argc >= 3) {
4217 if (getRangeLongFromObjectOrReply(c,c->argv[2],-LONG_MAX,LONG_MAX,&l,NULL) != C_OK) return;
4218 if (c->argc > 4 || (c->argc == 4 && strcasecmp(szFromObj(c->argv[3]),"withscores"))) {
4219 addReplyErrorObject(c,shared.syntaxerr);
4220 return;
4221 } else if (c->argc == 4) {
4222 withscores = 1;
4223 if (l < -g_pserver->rand_total_threshold || l > g_pserver->rand_total_threshold) {
4224 addReplyError(c,"value is out of range");
4225 return;
4226 }
4227 }
4228 zrandmemberWithCountCommand(c, l, withscores);
4229 return;
4230 }
4231
4232 /* Handle variant without <count> argument. Reply with simple bulk string */
4233 if ((zset = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp])) == nullptr ||
4234 checkType(c,zset,OBJ_ZSET)) {
4235 return;
4236 }
4237
4238 zsetTypeRandomElement(zset, zsetLength(zset), &ele,NULL);
4239 zsetReplyFromZiplistEntry(c,&ele);
4240}

Callers

nothing calls this directly

Calls 10

szFromObjFunction · 0.85
addReplyErrorObjectFunction · 0.85
addReplyErrorFunction · 0.85
lookupKeyReadOrReplyFunction · 0.85
checkTypeFunction · 0.85
zsetTypeRandomElementFunction · 0.85
zsetLengthFunction · 0.85

Tested by

no test coverage detected