Redis source code analysis (8) --- t_hash hash Conversion

Source: Internet
Author: User

After the last zipmap analysis, the content about the redis source code struct has actually ended, because there are several operation classes related to the struct below, they are merged into the struct package. These types of files include t_hash.c, z_list, z_set.c, t_string.c, and t_zset.c. The functions of these files are similar, that is, the operation class used to implement command processing between the client and server, store dict and ziplist into robj in the form of robj, and perform various conversions to implement command operations. Avoiding the complex structure of the struct, it encapsulates the operation class of the struct. Today I am talking about t_hash, which is the dict hash dictionary, and the conversion between ziplist compression list and robj. Hashtype. Because this file has no header file and only the. c file, I pulled out the method for convenience of learning.

/* 下面是方法的归类 */void hashTypeTryConversion(robj *o, robj **argv, int start, int end) /* 当hashType为ziplist时,判断对象长度是否超出了服务端可接受的ziplist最大长度,超过则转成哈希字典类型*/void hashTypeTryObjectEncoding(robj *subject, robj **o1, robj **o2) /* 当robj用的是字典的编码方式的时候,则经过编码转换 */int hashTypeGetFromZiplist(robj *o, robj *field,unsigned char **vstr,unsigned int *vlen,long long *vll) /* 获取ziplist压缩列表中的某个索引位置上的值 */int hashTypeGetFromHashTable(robj *o, robj *field, robj **value) /* 获取哈希字典中的某个值 */robj *hashTypeGetObject(robj *o, robj *field) /* 获取某个key对应的对象类型 */int hashTypeExists(robj *o, robj *field)   /* hastType类型判断某个键是否存在 */int hashTypeSet(robj *o, robj *field, robj *value) /* hashType设置操作,分2种情况,ziplist,和字典hashtable */int hashTypeDelete(robj *o, robj *field)  /* hashType删除操作,分为ziplist的删除操作,和hashtable的删除操作 */unsigned long hashTypeLength(robj *o)   /* hashType求长度操作 */hashTypeIterator *hashTypeInitIterator(robj *subject)  /* 获取hashType迭代器 */void hashTypeReleaseIterator(hashTypeIterator *hi) /* 释放hashType迭代器 */int hashTypeNext(hashTypeIterator *hi) /* 通过hashType迭代器获取下一个元素 */void hashTypeCurrentFromZiplist(hashTypeIterator *hi, int what,unsigned char **vstr,unsigned int *vlen,long long *vll) /* 根据当前迭代器的位置,获取当前ziplist的所在位置的key位置,或value该位置上的值 */void hashTypeCurrentFromHashTable(hashTypeIterator *hi, int what, robj **dst) /* 根据当前迭代器的位置,获取当前dict的所在位置的key位置,或value该位置上的值 */robj *hashTypeCurrentObject(hashTypeIterator *hi, int what) /* 根据当前迭代器的位置,获取当前key对象 */robj *hashTypeLookupWriteOrCreate(redisClient *c, robj *key) /* 根据c客户端对象,找到key是否存在,创建或实现添加操作  */void hashTypeConvertZiplist(robj *o, int enc) /* 从ziplist压缩表到hashtable的转换 */void hashTypeConvert(robj *o, int enc) /* 对象转换操作,例如从ziplist到dict的转换 */
The relevant Operation Command class of hashtype is actually a combined call to the above method:

/* 哈希命令类型 */void hsetCommand(redisClient *c)  /* 客户端设置指令 */void hsetnxCommand(redisClient *c) /* 客户端设置下一个位置指令 */void hmsetCommand(redisClient *c) /* 客户单设置命令,如果没有key,还有后续操作 */void hincrbyCommand(redisClient *c) /* 客户端添加value值操作 */void hincrbyfloatCommand(redisClient *c) /* 客户端添加float类型value值操作 */static void addHashFieldToReply(redisClient *c, robj *o, robj *field) /*  */void hgetCommand(redisClient *c) /* 客户端获取操作,如果没找到,直接不做任何操作 */void hmgetCommand(redisClient *c) /* 客户端获取key操作,如果为空,会返回一些了NULL值 */void hdelCommand(redisClient *c) /* 客户端删除操作 */void hlenCommand(redisClient *c) /* 客户端求长度命令 */static void addHashIteratorCursorToReply(redisClient *c, hashTypeIterator *hi, int what) /* 客户端添加hashType迭代器操作 */void genericHgetallCommand(redisClient *c, int flags) /* 客户端获取操作原始方法,可以添加flag参数 */void hkeysCommand(redisClient *c) /* 客户端获取key值命令 */void hvalsCommand(redisClient *c) /* 客户端获取val值命令 */void hgetallCommand(redisClient *c) /* 客户端获取key;value 2个值都获取 */void hexistsCommand(redisClient *c) /* 客户端判断记录是否存在操作 */void hscanCommand(redisClient *c) /* 客户端扫描操作 */

Robj's operation is very simple. Rob uses the PTR pointer in it to store the real ziplist or dict hash class, and the subsequent operations are based on this, for example, the following method:

/* Get the value from a hash table encoded hash, identified by field. * Returns -1 when the field cannot be found. *//* 获取哈希字典中的某个值 */int hashTypeGetFromHashTable(robj *o, robj *field, robj **value) {    dictEntry *de;    redisAssert(o->encoding == REDIS_ENCODING_HT);        //通过robj->ptr里面存的dict总类或ziplist类开始寻找    de = dictFind(o->ptr, field);    if (de == NULL) return -1;    //获取其中的value值    *value = dictGetVal(de);    return 0;}

All struct operations related to robj will be processed in two cases. ziplist and hash are dict types, and transcoding is required when ziplist operations are performed, of course, the ziplist should be encoded when it is stored in robj. It can be seen that the designer still thinks well when considering the command transmission, and also considers the security issue.

/* Add an element, discard the old if the key already exists. * Return 0 on insert and 1 on update. * This function will take care of incrementing the reference count of the * retained fields and value objects. *//* hashType设置操作,分2种情况,ziplist,和字典hashtable */int hashTypeSet(robj *o, robj *field, robj *value) {    int update = 0;    if (o->encoding == REDIS_ENCODING_ZIPLIST) {        unsigned char *zl, *fptr, *vptr;                //首先对field和value进行解码        field = getDecodedObject(field);        value = getDecodedObject(value);        zl = o->ptr;        fptr = ziplistIndex(zl, ZIPLIST_HEAD);        if (fptr != NULL) {            fptr = ziplistFind(fptr, field->ptr, sdslen(field->ptr), 1);            if (fptr != NULL) {                /* Grab pointer to the value (fptr points to the field) */                vptr = ziplistNext(zl, fptr);                redisAssert(vptr != NULL);                update = 1;                //设置的操作,其实先删除,再插入语一个新值                /* Delete value */                zl = ziplistDelete(zl, &vptr);                /* Insert new value */                zl = ziplistInsert(zl, vptr, value->ptr, sdslen(value->ptr));            }        }        if (!update) {            /* Push new field/value pair onto the tail of the ziplist */            zl = ziplistPush(zl, field->ptr, sdslen(field->ptr), ZIPLIST_TAIL);            zl = ziplistPush(zl, value->ptr, sdslen(value->ptr), ZIPLIST_TAIL);        }        o->ptr = zl;        //用完之后,引用计数递减        decrRefCount(field);        decrRefCount(value);        /* Check if the ziplist needs to be converted to a hash table */        if (hashTypeLength(o) > server.hash_max_ziplist_entries)            hashTypeConvert(o, REDIS_ENCODING_HT);    } else if (o->encoding == REDIS_ENCODING_HT) {    //如果是字典,直接替换        if (dictReplace(o->ptr, field, value)) { /* Insert */            incrRefCount(field);        } else { /* Update */            update = 1;        }        //用完之后,引用计数递减        incrRefCount(value);    } else {        redisPanic("Unknown hash encoding");    }    return update;}

In this process, the redis Code also uses a reference counting item, which should be used for reasonable memory release control and can be seen in many places;

/* Higher level function of hashTypeGet*() that always returns a Redis * object (either new or with refcount incremented), so that the caller * can retain a reference or call decrRefCount after the usage. * * The lower level function can prevent copy on write so it is * the preferred way of doing read operations. *//* 获取某个key的对象 */robj *hashTypeGetObject(robj *o, robj *field) {    robj *value = NULL;    if (o->encoding == REDIS_ENCODING_ZIPLIST) {        unsigned char *vstr = NULL;        unsigned int vlen = UINT_MAX;        long long vll = LLONG_MAX;        if (hashTypeGetFromZiplist(o, field, &vstr, &vlen, &vll) == 0) {        //在ziplist中获取值            if (vstr) {                value = createStringObject((char*)vstr, vlen);            } else {                value = createStringObjectFromLongLong(vll);            }        }    } else if (o->encoding == REDIS_ENCODING_HT) {        robj *aux;        if (hashTypeGetFromHashTable(o, field, &aux) == 0) {        //对象被引用了,计数递增            incrRefCount(aux);            value = aux;        }    } else {        redisPanic("Unknown hash encoding");    }    return value;}

The client's command operation is actually based on an object called redisclient, which is actually a robj object. During Command Transmission, this robj-> PTR is stored, and the specific data, robj-> ARGs [] stores various parameters, followed by calling the previous method. The only difference is that there must be a reply and update notification operation after the command is called ., The following is a set command;

/* hashType处理客户端的命令请求 */void hsetCommand(redisClient *c) {    int update;    robj *o;    if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;    hashTypeTryConversion(o,c->argv,2,3);    hashTypeTryObjectEncoding(o,&c->argv[2], &c->argv[3]);    //命令的操作都是通过,客户端中的对象,和存在于里面的命令参数组成    update = hashTypeSet(o,c->argv[2],c->argv[3]);    //操作完添加回复    addReply(c, update ? shared.czero : shared.cone);    //发送通知表示命令执行完毕,预测者会触发窗口上的显示    signalModifiedKey(c->db,c->argv[1]);    notifyKeyspaceEvent(REDIS_NOTIFY_HASH,"hset",c->argv[1],c->db->id);        //客户端命令执行成功,因为客户单此时的数据时最新的,服务端的脏数据就自然多了一个,    server.dirty++;}

Other commands are similar. As you can see, the code at the logic layer is now slightly closer, and the code at the end must be wonderful.

Redis source code analysis (8) --- t_hash hash Conversion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.