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

Function RM_StringTruncate

src/module.cpp:2611–2640  ·  view source on GitHub ↗

If the string is open for writing and is of string type, resize it, padding * with zero bytes if the new length is greater than the old one. * * After this call, RM_StringDMA() must be called again to continue * DMA access with the new pointer. * * The function returns REDISMODULE_OK on success, and REDISMODULE_ERR on * error, that is, the key is not open for writing, is not a string * or

Source from the content-addressed store, hash-verified

2609 * If the key is empty, a string key is created with the new string value
2610 * unless the new length value requested is zero. */
2611int RM_StringTruncate(RedisModuleKey *key, size_t newlen) {
2612 if (!(key->mode & REDISMODULE_WRITE)) return REDISMODULE_ERR;
2613 if (key->value && key->value->type != OBJ_STRING) return REDISMODULE_ERR;
2614 if (newlen > 512*1024*1024) return REDISMODULE_ERR;
2615
2616 /* Empty key and new len set to 0. Just return REDISMODULE_OK without
2617 * doing anything. */
2618 if (key->value == NULL && newlen == 0) return REDISMODULE_OK;
2619
2620 if (key->value == NULL) {
2621 /* Empty key: create it with the new size. */
2622 robj *o = createObject(OBJ_STRING,sdsnewlen(NULL, newlen));
2623 genericSetKey(key->ctx->client,key->db,key->key,o,0,0);
2624 key->value = o;
2625 decrRefCount(o);
2626 } else {
2627 /* Unshare and resize. */
2628 key->value = dbUnshareStringValue(key->db, key->key, key->value);
2629 size_t curlen = sdslen(szFromObj(key->value));
2630 if (newlen > curlen) {
2631 key->value->m_ptr = sdsgrowzero(szFromObj(key->value),newlen);
2632 } else if (newlen < curlen) {
2633 sdssubstr(szFromObj(key->value),0,newlen);
2634 /* If the string is too wasteful, reallocate it. */
2635 if (sdslen(szFromObj(key->value)) < sdsavail(szFromObj(key->value)))
2636 key->value->m_ptr = sdsRemoveFreeSpace(szFromObj(key->value));
2637 }
2638 }
2639 return REDISMODULE_OK;
2640}
2641
2642/* --------------------------------------------------------------------------
2643 * ## Key API for List type

Callers

nothing calls this directly

Calls 11

createObjectFunction · 0.85
sdsnewlenFunction · 0.85
genericSetKeyFunction · 0.85
decrRefCountFunction · 0.85
dbUnshareStringValueFunction · 0.85
sdslenFunction · 0.85
szFromObjFunction · 0.85
sdsgrowzeroFunction · 0.85
sdssubstrFunction · 0.85
sdsavailFunction · 0.85
sdsRemoveFreeSpaceFunction · 0.85

Tested by

no test coverage detected