1. 程式人生 > >Redis原始碼分析(三十四)--- redis.h服務端的實現分析(1)

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的結構定義非常類似,裡面包含了一堆的屬性,長長的一排下來。