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