From a3350442040b8127e442e4499ac84b5e24e44fcc Mon Sep 17 00:00:00 2001 From: zeekling Date: Sun, 8 Nov 2020 21:53:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=85=E8=AF=BBsetrange=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E6=BA=90=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/t_string.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/t_string.c b/src/t_string.c index 8f6bbc7..c75a40d 100644 --- a/src/t_string.c +++ b/src/t_string.c @@ -196,7 +196,9 @@ void getsetCommand(client *c) { notifyKeyspaceEvent(NOTIFY_STRING,"set",c->argv[1],c->db->id); server.dirty++; } - +/** + * setrange命令实现详情 + * */ void setrangeCommand(client *c) { robj *o; long offset; @@ -247,6 +249,11 @@ void setrangeCommand(client *c) { } if (sdslen(value) > 0) { + /** + * 考虑到当value的长度加offset会大于原值长度时,需要额外分配空间用于存储新值并返回。 + * 此时调用了sdsgrowzero函数。sdsgrowzero函数会进行识别,只有当offset+sdslen(value) + * 大于原值长度时才会扩充空间,否则直接返回原字符串。 + * */ o->ptr = sdsgrowzero(o->ptr,offset+sdslen(value)); memcpy((char*)o->ptr+offset,value,sdslen(value)); signalModifiedKey(c,c->db,c->argv[1]);