summaryrefslogtreecommitdiff
path: root/src/t_string.c
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2014-03-30 18:32:17 +0200
committerantirez <antirez@gmail.com>2014-03-30 18:32:17 +0200
commit543ede03f2e54b45b22164e17c7f51de9cfc69fa (patch)
tree19c4beb012a814620ad90f5416915ba640638304 /src/t_string.c
parentaaf6db459b34dc2182fe1d84596733a9b9e97ec4 (diff)
downloadredis-543ede03f2e54b45b22164e17c7f51de9cfc69fa.tar.gz
String value unsharing refactored into proper function.
All the Redis functions that need to modify the string value of a key in a destructive way (APPEND, SETBIT, SETRANGE, ...) require to make the object unshared (if refcount > 1) and encoded in raw format (if encoding is not already REDIS_ENCODING_RAW). This was cut & pasted many times in multiple places of the code. This commit puts the small logic needed into a function called dbUnshareStringValue().
Diffstat (limited to 'src/t_string.c')
-rw-r--r--src/t_string.c16
1 files changed, 2 insertions, 14 deletions
diff --git a/src/t_string.c b/src/t_string.c
index 3645ae7c5..41e4b3b71 100644
--- a/src/t_string.c
+++ b/src/t_string.c
@@ -215,12 +215,7 @@ void setrangeCommand(redisClient *c) {
return;
/* Create a copy when the object is shared or encoded. */
- if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) {
- robj *decoded = getDecodedObject(o);
- o = createRawStringObject(decoded->ptr, sdslen(decoded->ptr));
- decrRefCount(decoded);
- dbOverwrite(c->db,c->argv[1],o);
- }
+ o = dbUnshareStringValue(c->db,c->argv[1],o);
}
if (sdslen(value) > 0) {
@@ -433,15 +428,8 @@ void appendCommand(redisClient *c) {
if (checkStringLength(c,totlen) != REDIS_OK)
return;
- /* If the object is shared or encoded, we have to make a copy */
- if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) {
- robj *decoded = getDecodedObject(o);
- o = createRawStringObject(decoded->ptr, sdslen(decoded->ptr));
- decrRefCount(decoded);
- dbOverwrite(c->db,c->argv[1],o);
- }
-
/* Append the value */
+ o = dbUnshareStringValue(c->db,c->argv[1],o);
o->ptr = sdscatlen(o->ptr,append->ptr,sdslen(append->ptr));
totlen = sdslen(o->ptr);
}