diff --git a/core b/core new file mode 100644 index 0000000..dab870a Binary files /dev/null and b/core differ diff --git a/src/object.c b/src/object.c index 1bc400e..e971ef5 100644 --- a/src/object.c +++ b/src/object.c @@ -465,7 +465,11 @@ robj *tryObjectEncoding(robj *o) { /* This object is encodable as a long. Try to use a shared object. * Note that we avoid using shared integers when maxmemory is used * because every object needs to have a private LRU field for the LRU - * algorithm to work well. */ + * algorithm to work well. + * + * 如果长度小于20,则考虑将value转换long类型 + * + * */ if ((server.maxmemory == 0 || !(server.maxmemory_policy & MAXMEMORY_FLAG_NO_SHARED_INTEGERS)) && value >= 0 && @@ -490,7 +494,9 @@ robj *tryObjectEncoding(robj *o) { /* If the string is small and is still RAW encoded, * try the EMBSTR encoding which is more efficient. * In this representation the object and the SDS string are allocated - * in the same chunk of memory to save space and cache misses. */ + * in the same chunk of memory to save space and cache misses. + * 对于长度小于44的进行编码 + * */ if (len <= OBJ_ENCODING_EMBSTR_SIZE_LIMIT) { robj *emb; @@ -508,7 +514,9 @@ robj *tryObjectEncoding(robj *o) { * * We do that only for relatively large strings as this branch * is only entered if the length of the string is greater than - * OBJ_ENCODING_EMBSTR_SIZE_LIMIT. */ + * OBJ_ENCODING_EMBSTR_SIZE_LIMIT. + * 不能编码的字符串释放空闲字节 + * */ trimStringObjectIfNeeded(o); /* Return the original object. */ diff --git a/src/server.h b/src/server.h index 787930a..7191aef 100644 --- a/src/server.h +++ b/src/server.h @@ -645,15 +645,15 @@ typedef struct clientReplyBlock { * by integers from 0 (the default database) up to the max configured * database. The database number is the 'id' field in the structure. */ typedef struct redisDb { - dict *dict; /* The keyspace for this DB */ - dict *expires; /* Timeout of keys with a timeout set */ - dict *blocking_keys; /* Keys with clients waiting for data (BLPOP)*/ - dict *ready_keys; /* Blocked keys that received a PUSH */ - dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */ - int id; /* Database ID */ - long long avg_ttl; /* Average TTL, just for stats */ - unsigned long expires_cursor; /* Cursor of the active expire cycle. */ - list *defrag_later; /* List of key names to attempt to defrag one by one, gradually. */ + dict *dict; /* 键空间字典,存放所有键值对。 */ + dict *expires; /* key的超时时间字典,存放键的过期时间,注意dict和expires中的键都指向同一个键的sds。 */ + dict *blocking_keys; /* 阻塞的key:处于阻塞状态的键和对应的client。*/ + dict *ready_keys; /* 准备好的key:解除阻塞状态的键和对应的client,与blocking_keys属性相对,为了实现需要阻塞的命令设计 */ + dict *watched_keys; /* 执行事务的key:watch的键和对应的client,主要用于事务。 */ + int id; /* 数据库ID */ + long long avg_ttl; /* 数据库内所有键的平均生存时间,用于统计 */ + unsigned long expires_cursor; /* 活动过期周期的光标。 */ + list *defrag_later; /* 逐渐尝试逐个碎片整理的key列表 */ } redisDb; /* Client MULTI/EXEC state */ @@ -1786,6 +1786,7 @@ int collateStringObjects(robj *a, robj *b); int equalStringObjects(robj *a, robj *b); unsigned long long estimateObjectIdleTime(robj *o); void trimStringObjectIfNeeded(robj *o); +// 判断只有对string和raw类型进行优化 #define sdsEncodedObject(objptr) (objptr->encoding == OBJ_ENCODING_RAW || objptr->encoding == OBJ_ENCODING_EMBSTR) /* Synchronous I/O with timeout */ diff --git a/src/t_string.c b/src/t_string.c index 34be2f5..373252b 100644 --- a/src/t_string.c +++ b/src/t_string.c @@ -96,10 +96,12 @@ void setGenericCommand(client *c, int flags, robj *key, robj *val, robj *expire, /* SET key value [NX] [XX] [KEEPTTL] [EX ] [PX ] */ void setCommand(client *c) { int j; - robj *expire = NULL; + robj *expire = NULL; // 超时时间,robj类型 + // 字符串的超时时间单位有秒和毫秒两种,程序中根据此值来确认超时的单位,此值只有两个取值,分别为UNIT_SECONDS,UNIT_MILLISECONDS int unit = UNIT_SECONDS; + // int类型,它是一个二进制串,程序中根据此值来确定key是否应该被设置到数据库。 int flags = OBJ_SET_NO_FLAGS; - // 解析set命令的val值 + // 解析set命令的NX、XX、EX、PX的值 for (j = 3; j < c->argc; j++) { char *a = c->argv[j]->ptr; robj *next = (j == c->argc-1) ? NULL : c->argv[j+1];