search
HomeDatabaseMysql TutorialRedis源码分析(八)---t_hash哈希转换

在上次的zipmap分析完之后,其实关于redis源代码结构体部分的内容其实已经全部结束了,因为下面还有几个和结构体相关的操作类,就页把他们归并到struct包下了。这类的文件有:t_hash.c,z_list,z_set.c,t_string.c,t_zset.c,这些文件的功能其实都差不多,就是

在上次的zipmap分析完之后,其实关于redis源代码结构体部分的内容其实已经全部结束了,因为下面还有几个和结构体相关的操作类,就页把他们归并到struct包下了。这类的文件有:t_hash.c,z_list,z_set.c,t_string.c,t_zset.c,这些文件的功能其实都差不多,就是用来实现Client和Server之间的命令处理的操作类,通过robj的形式,把dict,ziplist等存入robj中,进行各个转换,实现命令操作。避开了结构体原先的复杂结构,相当于是封装了结构体的操作类,今天我所讲的是t_hash,是dict哈希字典,ziplist压缩列表与robj之间的转换。统称hashType类型。由于此文件无头文件,只有.c文件,所以为了方便学习,我把方法拉了出来。

/* 下面是方法的归类 */
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的转换 */

hashType的相关操作命令类,其实就是对上面方法的结合调用:
/* 哈希命令类型 */
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的操作实现转换的原理很简单,rob通过里面的ptr指针,存的就是真实的ziplist或者dict哈希总类,然后后面的操作都是基于此进行的,比如说下面的方法: 
/* 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;
}
所有关于robj的相关结构体操作都会分成为2种情况处理,ZIPLIST和HASH类型就是dict类型,而且操作ziplist类型的时候要进行转码处理,当然在进行ziplist存入robj的时候要进行编码操作,可见,设计者在考虑到命令传输的时候想得还是很周到了,也考虑了安全的问题。 
/* 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;
}
在这个过程中,redis代码中还用到了一个引用计数的东西,应该是为了合理的内存释放控制,在很多地方可以看到这样的操作; 
/* 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;
}
客户端的命令操作其实是基于一个叫redisClient的对象,这个其实也就是robj对象,命令传输时,这个robj->ptr存着,具体的数据,robj->args[]存放了各种参数,后面就是调用前面的方法了,唯一不一样的是,命令调用后要有回复和更新通知操作。,下面是一个设置的命令; 
/* 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++;
}
其他命令与此类似,就不说了。可以看见,现在慢慢的能够略微向逻辑层的代码靠近了,后面的代码也一定非常精彩。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you create and manage user accounts in MySQL?How do you create and manage user accounts in MySQL?Apr 22, 2025 pm 06:05 PM

The steps to create and manage user accounts in MySQL are as follows: 1. Create a user: Use CREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password'; 2. Assign permissions: Use GRANTSELECT, INSERT, UPDATEONmydatabase.TO'newuser'@'localhost'; 3. Fix permission error: Use REVOKEALLPRIVILEGESONmydatabase.FROM'newuser'@'localhost'; then reassign permissions; 4. Optimization permissions: Use SHOWGRA

How does MySQL differ from Oracle?How does MySQL differ from Oracle?Apr 22, 2025 pm 05:57 PM

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

What are the disadvantages of using MySQL compared to other relational databases?What are the disadvantages of using MySQL compared to other relational databases?Apr 22, 2025 pm 05:49 PM

The disadvantages of MySQL compared to other relational databases include: 1. Performance issues: You may encounter bottlenecks when processing large-scale data, and PostgreSQL performs better in complex queries and big data processing. 2. Scalability: The horizontal scaling ability is not as good as Google Spanner and Amazon Aurora. 3. Functional limitations: Not as good as PostgreSQL and Oracle in advanced functions, some functions require more custom code and maintenance.

How do you perform a JOIN operation in MySQL?How do you perform a JOIN operation in MySQL?Apr 22, 2025 pm 05:41 PM

MySQL supports four JOIN types: INNERJOIN, LEFTJOIN, RIGHTJOIN and FULLOUTERJOIN. 1.INNERJOIN is used to match rows in two tables and return results that meet the criteria. 2.LEFTJOIN returns all rows in the left table, even if the right table does not match. 3. RIGHTJOIN is opposite to LEFTJOIN and returns all rows in the right table. 4.FULLOUTERJOIN returns all rows in the two tables that meet or do not meet the conditions.

How does MySQL's performance compare to other RDBMS under high load?How does MySQL's performance compare to other RDBMS under high load?Apr 22, 2025 pm 05:37 PM

MySQL's performance under high load has its advantages and disadvantages compared with other RDBMSs. 1) MySQL performs well under high loads through the InnoDB engine and optimization strategies such as indexing, query cache and partition tables. 2) PostgreSQL provides efficient concurrent read and write through the MVCC mechanism, while Oracle and Microsoft SQLServer improve performance through their respective optimization strategies. With reasonable configuration and optimization, MySQL can perform well in high load environments.

Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor