Redis Source Code Analysis (34) --- implementation analysis of the redis. h server (1)

Source: Internet
Author: User
Tags redis server

The last time I analyzed the structure analysis of the client, the idea was simple and clear. Finally, I learned how to implement the server. The server is the top priority in Redis, it basically contains all the knowledge points involved in the previous module. We can see from the redis header file that redis. the h code volume has already exceeded more than 1000 lines, and it is only some variables, macro definition declarations, and some method prototype declarations. Therefore, today's summary is the same as yesterday's summary. First, we will not study the specific implementation. First, we will consider the overall design concept of the server from a global perspective. This can be learned from the Declaration of the document from the beginning.


/ * ----------------------- declares the required header files, which are mainly operation files of various structures ------- ------------- * /
#include "ae.h" / * Event driven programming library * /
#include "sds.h" / * Dynamic safe strings dynamic string library * /
#include "dict.h" / * Hash tables * /
#include "adlist.h" / * Linked lists ordinary doubly linked lists * /
#include "zmalloc.h" / * total memory usage aware version of malloc / free memory application management library * /
#include "anet.h" / * Networking the easy way network operations library * /
#include "ziplist.h" / * Compact list data structure * /
#include "intset.h" / * Compact integer set structure * /
#include "version.h" / * Version macro version number file * /
#include "util.h" / * Misc functions useful in many places
#include "latency.h" / * Latency monitor API latency monitoring method * /
#include "sparkline.h" / * ASII graphs API microline gallery * /

/ * ----------------------------- Depending on the module, the macro defines different variables -------- -------- * /
/ * 1.Error codes Redis error code * /
/ * 2. Some static variable values in Static server configuration server * /
/ * 3.Protocol and I / O related defines * /
/ * 4.Hash table parameters * /
/ * 5.Command flags Flag definition for command line operations * /
/ * 6.Object types Object types, including List, String, Hash, etc. * /
/ * 7.Objects encoding Object encoding type * /
/ * 8.Defines related to the dump file format RDB save format, 14 bit, 32 bit, etc. * /
/ * 9.AOF states AOF file status * /
/ * 10.Client flags Client flags * /
/ * 11.Client request types Client request types, INLINE and MULTIBULK * /
/ * 12.Client classes for client limits * /
/ * 13.Slave replication state replication status * /
/ * 14.List related stuff List position, head or tail * /
/ * 15.Sort operations sort operation type, ascending or descending, etc. * /
/ * 16.Log levels * /
/ * 17.Anti-warning macro ... warning message * /
/ * 18.Append only defines additional operation variables * /
/ * 19.Zip structure related defaults ziplist compression list variables * /
/ * 20.HyperLogLog defines HLLC variable definition * /
/ * 21.Sets operations codes * /
/ * 22.Redis maxmemory strategies Redis memory operation strategy * /
/ * 23.Scripting * /
/ * 24.Units time units, subtle and milliseconds * /
/ * 25.SHUTDOWN flags * /
/ * 26.Command call flags, see call () function * /
/ * 27.Command propagation flags, see propagate () function * /
/ * 28. Keyspace changes notification classes. Notification types * /
Ranch
/ * ------------------------------------------------ -----------------------------
 * Data types definitions
 * ------------------------------------------------- --------------------------- * /
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 client structure * /
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 / * Definition of Redis server structure * /
17.struct redisCommand / * Definition of Redis server Command command structure * /
Ranch
/ * ------------------------------------------------ -----------------------------
 * Functions prototypes
 * ------------------------------------------------- --------------------------- * /
/ * 1.Utils generic methods * /
/ * 2.networking.c-Networking and Client related operations
/ * 3.List data type List operation method * /
/ * 4.MULTI / EXEC / WATCH ... command execution method * /
/ * 5.Redis object implementation Redis Object method * /
/ * 6.Synchronous I / O with timeout I / O synchronization method * /
/ * 7.Replication replication method * /
/ * 8.Generic persistence functions Some methods for persistent loading * /
/ * 9.AOF persistence AOF log file persistence method * /
/ * 10.Core functions Core class methods * /
/ * 11.Sorted sets data type
/ * 12.Set data type set type data operation method * /
/ * 13.Hash data type Hash type method operation method * /
/ * 14.Pub / Sub Publish and Subscribe Method * /
/ * 15.Keyspace events notification ketSpace event notification method * /
/ * 16.Configuration configuration method * /
/ * 17.db.c-Keyspace access API db related methods * /
/ * 18.Sentinel * /
/ * 19.Scripting * /
/ * 20.Git SHA1 * /
/ * 21.Commands prototypes * /
Four major modules


1. Reference header file declaration

2. Macro definition variable definition

3. Data structure declaration

4. Method prototype declaration

It is particularly pointed out that the structure definitions of RedisObject, RedisClient, and RedisServer appearing everywhere in the code are defined in this file.


/* 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;
The structure definition of RedisClient and RedisServer is very similar. It contains a bunch of attributes and is arranged in a long row.


Redis Source Code Analysis (34) --- implementation analysis of the redis. h server (1)

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.