zoukankan      html  css  js  c++  java
  • Redis源码分析(三十四)--- redis.h服务端的实现分析(1)

           上次刚刚分析过了客户端的结构体分析,思路比较简答,清晰,最后学习的是服务端的实现,服务端在Redis可是重中之重,里面基本上囊括了之前模块中涉及到的所有知识点,从redis的头文件就可以看出了,redis.h代码量就已经破1000+行了,而且都还只是一些变量,宏定义的声明,和一些方法原型的声明。所以,今天的总结跟昨天一样,先不做具体的实现学习,先从全局的角度思考,服务端的整体设计思路,这从头文件的声明正好可以学习。

    /* ----------------------- 声明了一下所需的头文件,主要为各种结构体的操作文件 -------------------- */
    #include "ae.h"      /* Event driven programming library  事件驱动库*/
    #include "sds.h"     /* Dynamic safe strings  动态字符串库 */
    #include "dict.h"    /* Hash tables 哈希字典 */
    #include "adlist.h"  /* Linked lists 普通双向链表 */
    #include "zmalloc.h" /* total memory usage aware version of malloc/free 内存申请管理库 */
    #include "anet.h"    /* Networking the easy way  网络操作库 */
    #include "ziplist.h" /* Compact list data structure  压缩列表 */
    #include "intset.h"  /* Compact integer set structure 整形set结构体 */
    #include "version.h" /* Version macro  版本号文件*/
    #include "util.h"    /* Misc functions useful in many places 同样方法类*/
    #include "latency.h" /* Latency monitor API 延时监视方法 */
    #include "sparkline.h" /* ASII graphs API  微线图库 */
    
    /* -----------------------------根据模块的不同,宏定义了不同的变量 ---------------- */
    /* 1.Error codes Redis错误码*/
    /* 2.Static server configuration  server中的一些静态变量值*/
    /* 3.Protocol and I/O related defines  协议和I/O相关变量的定义*/
    /* 4.Hash table parameters 哈希表的参数*/
    /* 5.Command flags 命令行操作的flag定义*/
    /* 6.Object types Object的类型,包括List,String,Hash等*/
    /* 7.Objects encoding Object的编码类型*/
    /* 8.Defines related to the dump file format RDB的保存格式,14位,32位等*/
    /* 9.AOF states  AOF文件的状态*/
    /* 10.Client flags 客户端的flag标示*/
    /* 11.Client request types 客户端的请求类型,INLINE和MULTIBULK*/
    /* 12.Client classes for client limits 客户端的类型*/
    /* 13.Slave replication state replication状态*/
    /* 14.List related stuff 列表位置,head或tail*/
    /* 15.Sort operations 排序操作类型,升序或是降序等等*/
    /* 16.Log levels 日志级别*/
    /* 17.Anti-warning macro... 警告信息*/
    /* 18.Append only defines 追加操作变量*/
    /* 19.Zip structure related defaults ziplist压缩列表变量*/
    /* 20.HyperLogLog defines HLLC的变量定义*/
    /* 21.Sets operations codes 设置操作的操作码*/
    /* 22.Redis maxmemory strategies Redis内存操作策略*/
    /* 23.Scripting */
    /* 24.Units 时间单位,微妙和毫秒*/
    /* 25.SHUTDOWN flags */
    /* 26.Command call flags, see call() function */
    /* 27.Command propagation flags, see propagate() function */
    /* 28.Keyspace changes notification classes. 通知类型*/
    	
    /*-----------------------------------------------------------------------------
     * Data types 数据类型的相关定义
     *----------------------------------------------------------------------------*/
    1.typedef struct redisObject /* Redis Object对象 */
    2.typedef struct redisDb
    3.typedef struct multiCmd
    4.typedef struct multiState
    5.typedef struct blockingState
    6.typedef struct readyList
    7.typedef struct redisClient  /* Redis客户端结构体 */
    8.struct saveparam
    9.struct sharedObjectsStruct
    10.typedef struct zskiplistNode
    11.typedef struct zskiplist
    12.typedef struct zset
    13.typedef struct clientBufferLimitsConfig 
    14.typedef struct redisOp
    15.typedef struct redisOpArray
    16.struct redisServer /* Redis服务端结构体的定义 */
    17.struct redisCommand /* Redis服务端Command命令结构体的定义 */
    	
    /*-----------------------------------------------------------------------------
     * Functions prototypes 方法原型
     *----------------------------------------------------------------------------*/
    /* 1.Utils 通用类的方法*/
    /* 2.networking.c -- Networking and Client related operations 网络操作类方法*/
    /* 3.List data type 列表操作方法*/
    /* 4.MULTI/EXEC/WATCH... 命令执行方法*/
    /* 5.Redis object implementation Redis Object对象方法*/
    /* 6.Synchronous I/O with timeout I/O同步类方法*/
    /* 7.Replication 主从复制方法*/
    /* 8.Generic persistence functions 持久化加载的一些方法*/
    /* 9.AOF persistence AOF日志文件持久化方法*/
    /* 10.Core functions 核心类方法*/
    /* 11.Sorted sets data type 排序set集合方法*/
    /* 12.Set data type set类型数据操作方法*/
    /* 13.Hash data type 哈希类型方法操作方法*/
    /* 14.Pub / Sub 发布订阅方法*/
    /* 15.Keyspace events notification ketSpace事件通知方法*/
    /* 16.Configuration 配置类方法*/
    /* 17.db.c -- Keyspace access API db相关的方法*/
    /* 18.Sentinel */
    /* 19.Scripting */
    /* 20.Git SHA1 */
    /* 21.Commands prototypes 命令原型方法*/
    主要4个大模块

    1.引用头文件声明

    2.宏定义变量定义

    3.数据结构体的声明

    4.方法原型声明

    在这里特别提出,在 代码中遍地出现的RedisObject,RedisClient,RedisServer的结构定义,都是在这个文件中定义的。

    /* The actual Redis Object */
    #define REDIS_LRU_BITS 24
    #define REDIS_LRU_CLOCK_MAX ((1<<REDIS_LRU_BITS)-1) /* Max value of obj->lru */
    #define REDIS_LRU_CLOCK_RESOLUTION 1 /* LRU clock resolution in seconds */
    typedef struct redisObject {
        unsigned type:4;
        unsigned encoding:4;
        unsigned lru:REDIS_LRU_BITS; /* lru time (relative to server.lruclock) */
        int refcount;
        void *ptr;
    } robj;
    RedisClient,RedisServer的结构定义非常类似,里面包含了一堆的属性,长长的一排下来。
  • 相关阅读:
    nginx 转发配置
    Rancher中httpd证书的管理和使用
    JDK-docker
    软路由
    rancher相关
    rancher部署
    电商 好文 知识积累
    SpringBlade 接口文档 请求token接口报错
    SpringBlade 接口文档 无法访问
    电商 好文
  • 原文地址:https://www.cnblogs.com/bianqi/p/12184187.html
Copyright © 2011-2022 走看看