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

Function setbitCommand

src/bitops.cpp:533–569  ·  view source on GitHub ↗

SETBIT key offset bitvalue */

Source from the content-addressed store, hash-verified

531
532/* SETBIT key offset bitvalue */
533void setbitCommand(client *c) {
534 robj *o;
535 const char *err = "bit is not an integer or out of range";
536 uint64_t bitoffset;
537 ssize_t byte, bit;
538 int byteval, bitval;
539 long on;
540
541 if (getBitOffsetFromArgument(c,c->argv[2],&bitoffset,0,0) != C_OK)
542 return;
543
544 if (getLongFromObjectOrReply(c,c->argv[3],&on,err) != C_OK)
545 return;
546
547 /* Bits can only be set or cleared... */
548 if (on & ~1) {
549 addReplyError(c,err);
550 return;
551 }
552
553 if ((o = lookupStringForBitCommand(c,bitoffset)) == NULL) return;
554
555 /* Get current values */
556 byte = bitoffset >> 3;
557 byteval = ((uint8_t*)ptrFromObj(o))[byte];
558 bit = 7 - (bitoffset & 0x7);
559 bitval = byteval & (1 << bit);
560
561 /* Update byte with new bit value and return original value */
562 byteval &= ~(1 << bit);
563 byteval |= ((on & 0x1) << bit);
564 ((uint8_t*)ptrFromObj(o))[byte] = byteval;
565 signalModifiedKey(c,c->db,c->argv[1]);
566 notifyKeyspaceEvent(NOTIFY_STRING,"setbit",c->argv[1],c->db->id);
567 g_pserver->dirty++;
568 addReply(c, bitval ? shared.cone : shared.czero);
569}
570
571/* GETBIT key offset */
572void getbitCommand(client *c) {

Callers

nothing calls this directly

Calls 8

getBitOffsetFromArgumentFunction · 0.85
getLongFromObjectOrReplyFunction · 0.85
addReplyErrorFunction · 0.85
ptrFromObjFunction · 0.85
signalModifiedKeyFunction · 0.85
notifyKeyspaceEventFunction · 0.85
addReplyFunction · 0.85

Tested by

no test coverage detected