From 6322eb58fa7a62e44a222b8901abe81cad9d8649 Mon Sep 17 00:00:00 2001 From: zeekling Date: Sun, 8 Nov 2020 20:30:35 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=85=E8=AF=BBappend=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/db.c | 1 + src/t_string.c | 14 ++++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/db.c b/src/db.c index 19b2c48..0cd8998 100644 --- a/src/db.c +++ b/src/db.c @@ -352,6 +352,7 @@ int dbDelete(redisDb *db, robj *key) { robj *dbUnshareStringValue(redisDb *db, robj *key, robj *o) { serverAssert(o->type == OBJ_STRING); if (o->refcount != 1 || o->encoding != OBJ_ENCODING_RAW) { + // 如果是共享的,则需要解除共享,创建新的字符串 robj *decoded = getDecodedObject(o); o = createRawStringObject(decoded->ptr, sdslen(decoded->ptr)); decrRefCount(decoded); diff --git a/src/t_string.c b/src/t_string.c index 37b2f95..8f6bbc7 100644 --- a/src/t_string.c +++ b/src/t_string.c @@ -31,7 +31,7 @@ #include /* isnan(), isinf() */ /*----------------------------------------------------------------------------- - * String Commands + * String Commands 检查字符串的长度是否大于255M *----------------------------------------------------------------------------*/ static int checkStringLength(client *c, long long size) { @@ -452,26 +452,28 @@ void incrbyfloatCommand(client *c) { rewriteClientCommandArgument(c,3,aux2); decrRefCount(aux2); } - +/** + * append命令实现函数 + **/ void appendCommand(client *c) { size_t totlen; robj *o, *append; - + // 查找key对应的数据 o = lookupKeyWrite(c->db,c->argv[1]); if (o == NULL) { - /* Create the key */ + /* 如果找不到,则添加 */ c->argv[2] = tryObjectEncoding(c->argv[2]); dbAdd(c->db,c->argv[1],c->argv[2]); incrRefCount(c->argv[2]); totlen = stringObjectLen(c->argv[2]); } else { - /* Key exists, check type */ + /* Key exists, check type,检查是否为string类型 */ if (checkType(c,o,OBJ_STRING)) return; /* "append" is an argument, so always an sds */ append = c->argv[2]; - totlen = stringObjectLen(o)+sdslen(append->ptr); + totlen = stringObjectLen(o)+sdslen(append->ptr); // 检查长度 if (checkStringLength(c,totlen) != C_OK) return;