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

Function spopCommand

src/t_set.cpp:605–654  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

603}
604
605void spopCommand(client *c) {
606 robj *set, *ele;
607 sds sdsele;
608 int64_t llele;
609 int encoding;
610
611 if (c->argc == 3) {
612 spopWithCountCommand(c);
613 return;
614 } else if (c->argc > 3) {
615 addReplyErrorObject(c,shared.syntaxerr);
616 return;
617 }
618
619 /* Make sure a key with the name inputted exists, and that it's type is
620 * indeed a set */
621 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.null[c->resp]))
622 == NULL || checkType(c,set,OBJ_SET)) return;
623
624 /* Get a random element from the set */
625 encoding = setTypeRandomElement(set,&sdsele,&llele);
626
627 /* Remove the element from the set */
628 if (encoding == OBJ_ENCODING_INTSET) {
629 ele = createStringObjectFromLongLong(llele);
630 set->m_ptr = intsetRemove((intset*)set->m_ptr,llele,NULL);
631 } else {
632 ele = createStringObject(sdsele,sdslen(sdsele));
633 setTypeRemove(set,szFromObj(ele));
634 }
635
636 notifyKeyspaceEvent(NOTIFY_SET,"spop",c->argv[1],c->db->id);
637
638 /* Replicate/AOF this command as an SREM operation */
639 rewriteClientCommandVector(c,3,shared.srem,c->argv[1],ele);
640
641 /* Add the element to the reply */
642 addReplyBulk(c,ele);
643 decrRefCount(ele);
644
645 /* Delete the set if it's empty */
646 if (setTypeSize(set) == 0) {
647 dbDelete(c->db,c->argv[1]);
648 notifyKeyspaceEvent(NOTIFY_GENERIC,"del",c->argv[1],c->db->id);
649 }
650
651 /* Set has been modified */
652 signalModifiedKey(c,c->db,c->argv[1]);
653 g_pserver->dirty++;
654}
655
656/* handle the "SRANDMEMBER key <count>" variant. The normal version of the
657 * command is handled by the srandmemberCommand() function itself. */

Callers

nothing calls this directly

Calls 15

spopWithCountCommandFunction · 0.85
addReplyErrorObjectFunction · 0.85
lookupKeyWriteOrReplyFunction · 0.85
checkTypeFunction · 0.85
setTypeRandomElementFunction · 0.85
intsetRemoveFunction · 0.85
sdslenFunction · 0.85
setTypeRemoveFunction · 0.85
szFromObjFunction · 0.85
notifyKeyspaceEventFunction · 0.85

Tested by

no test coverage detected