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

Function bitopCommand

src/bitops.cpp:599–862  ·  view source on GitHub ↗

BITOP op_name target_key src_key1 src_key2 src_key3 ... src_keyN */

Source from the content-addressed store, hash-verified

597
598/* BITOP op_name target_key src_key1 src_key2 src_key3 ... src_keyN */
599void bitopCommand(client *c) {
600 char *opname = szFromObj(c->argv[1]);
601 robj *targetkey = c->argv[2];
602 robj_roptr o;
603 int op;
604 unsigned long j, numkeys;
605 robj_roptr *objects; /* Array of source objects. */
606 unsigned char **src; /* Array of source strings pointers. */
607 unsigned long *len, maxlen = 0; /* Array of length of src strings,
608 and max len. */
609 unsigned long minlen = 0; /* Min len among the input keys. */
610 unsigned char *res = NULL; /* Resulting string. */
611
612 /* Parse the operation name. */
613 if ((opname[0] == 'a' || opname[0] == 'A') && !strcasecmp(opname,"and"))
614 op = BITOP_AND;
615 else if((opname[0] == 'o' || opname[0] == 'O') && !strcasecmp(opname,"or"))
616 op = BITOP_OR;
617 else if((opname[0] == 'x' || opname[0] == 'X') && !strcasecmp(opname,"xor"))
618 op = BITOP_XOR;
619 else if((opname[0] == 'n' || opname[0] == 'N') && !strcasecmp(opname,"not"))
620 op = BITOP_NOT;
621 else if (!strcasecmp(opname, "lshift"))
622 op = BITOP_LSHIFT;
623 else if (!strcasecmp(opname, "rshift"))
624 op = BITOP_RSHIFT;
625 else {
626 addReplyErrorObject(c,shared.syntaxerr);
627 return;
628 }
629
630 /* Sanity check: NOT accepts only a single key argument. */
631 if (op == BITOP_NOT && c->argc != 4) {
632 addReplyError(c,"BITOP NOT must be called with a single source key.");
633 return;
634 }
635
636 bool fShiftOp = (op == BITOP_LSHIFT) || (op == BITOP_RSHIFT);
637 long long shift = 0;
638
639 /* Sanity check: SHIFTS only accept a single arg and an integer */
640 if (fShiftOp) {
641 if (c->argc != 5) {
642 addReplyError(c,"BITOP SHIFT must be called with a single source key and an integer shift.");
643 return;
644 }
645 if (getLongLongFromObject(c->argv[4], &shift) != C_OK) {
646 addReplyError(c, "BITOP SHIFT's last parameter must be an integer");
647 return;
648 }
649 if (shift > BITOP_SHIFT_MAX) {
650 addReplyError(c, "BITOP SHIFT value is too large.");
651 return;
652 }
653
654 if (op == BITOP_RSHIFT)
655 shift = -shift;
656 }

Callers

nothing calls this directly

Calls 15

szFromObjFunction · 0.85
addReplyErrorObjectFunction · 0.85
addReplyErrorFunction · 0.85
getLongLongFromObjectFunction · 0.85
zmallocFunction · 0.85
lookupKeyReadFunction · 0.85
checkTypeFunction · 0.85
decrRefCountFunction · 0.85
zfreeFunction · 0.85
getDecodedObjectFunction · 0.85
ptrFromObjFunction · 0.85
sdslenFunction · 0.85

Tested by

no test coverage detected