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

Function setrangeCommand

src/t_string.cpp:432–490  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

430}
431
432void setrangeCommand(client *c) {
433 robj *o;
434 long offset;
435 sds value = (sds)ptrFromObj(c->argv[3]);
436
437 if (getLongFromObjectOrReply(c,c->argv[2],&offset,NULL) != C_OK)
438 return;
439
440 if (offset < 0) {
441 addReplyError(c,"offset is out of range");
442 return;
443 }
444
445 o = lookupKeyWrite(c->db,c->argv[1]);
446 if (o == NULL) {
447 /* Return 0 when setting nothing on a non-existing string */
448 if (sdslen(value) == 0) {
449 addReply(c,shared.czero);
450 return;
451 }
452
453 /* Return when the resulting string exceeds allowed size */
454 if (checkStringLength(c,offset,sdslen(value)) != C_OK)
455 return;
456
457 o = createObject(OBJ_STRING,sdsnewlen(NULL, offset+sdslen(value)));
458 dbAdd(c->db,c->argv[1],o);
459 } else {
460 size_t olen;
461
462 /* Key exists, check type */
463 if (checkType(c,o,OBJ_STRING))
464 return;
465
466 /* Return existing string length when setting nothing */
467 olen = stringObjectLen(o);
468 if (sdslen(value) == 0) {
469 addReplyLongLong(c,olen);
470 return;
471 }
472
473 /* Return when the resulting string exceeds allowed size */
474 if (checkStringLength(c,offset,sdslen(value)) != C_OK)
475 return;
476
477 /* Create a copy when the object is shared or encoded. */
478 o = dbUnshareStringValue(c->db,c->argv[1],o);
479 }
480
481 if (sdslen(value) > 0) {
482 o->m_ptr = sdsgrowzero(szFromObj(o),offset+sdslen(value));
483 memcpy((char*)o->m_ptr+offset,value,sdslen(value));
484 signalModifiedKey(c,c->db,c->argv[1]);
485 notifyKeyspaceEvent(NOTIFY_STRING,
486 "setrange",c->argv[1],c->db->id);
487 g_pserver->dirty++;
488 }
489 addReplyLongLong(c,sdslen((sds)ptrFromObj(o)));

Callers

nothing calls this directly

Calls 15

ptrFromObjFunction · 0.85
getLongFromObjectOrReplyFunction · 0.85
addReplyErrorFunction · 0.85
lookupKeyWriteFunction · 0.85
sdslenFunction · 0.85
addReplyFunction · 0.85
checkStringLengthFunction · 0.85
createObjectFunction · 0.85
sdsnewlenFunction · 0.85
dbAddFunction · 0.85
checkTypeFunction · 0.85
stringObjectLenFunction · 0.85

Tested by

no test coverage detected