It took a few days to read "Redis design and implementation" and put some of the ideas down for you to share.
2nd Chapter Simple Dynamic stringthe string objects inside the Redis are implemented using the SDS structure. SDS differs from the C-style character array and the Java string (fixed length). This structure is more like a C + + string or Java arraylist<character>. The length is dynamically variable. This structure is used for all the key values and string literals of Redis. This chapter spent more than 10 pages to talk about the SDS structure, feeling full of nonsense, there is time to suggest to see the source of ArrayList. 3rd Chapter linked listThe REDIS list structure is implemented using a doubly linked list. This chapter spends a few pages on the list, feeling the data structure. Free to skip this chapter, see Java LinkedList source Bar. 4th Chapter DictionaryRedis databases and hash keys are implemented using a dictionary data structure. The dictionary used by Redis is exactly the HashMap in Java or the hash_map in C + +. Have seen HashMap's children's shoes skip this chapter directly. 5th Chapter Jumping TableThe bottom of a Redis's ordered set key is a skip table. The lookup time complexity of a hop table is Logn, which can be comparable to a balanced binary tree. But the balanced binary tree implementation is slightly more complex, so Redis uses a slightly simpler implementation of the skiplist. This chapter is also in the "data structure", not interested in skipping. 6th Chapter Integer Setif all the elements in a collection are integers, then the Redis bottom layer takes an integer collection of the data structure.
127.0.0.1:6379> sadd Num 1 2 3 4 5 6 7 (integer) 7127.0.0.1:6379> object encoding num "Intset"
inset the type of an integer array in this struct structure can be 16-bit, 32-bit, 64-bit. When all integers in the array are 16-bit lengths, if a new 32-bit integer is added, the entire 16 array will be upgraded to a 32-bit array. Only the upgrade operation, there is no corresponding demotion operation. the data in an integer array is ordered. 8th Chapter ObjectRedis is a system implemented in C, and it is far fetched to talk to Redis about objects. Redis defines objects with struct struct bodies. The Redis database relies on string objects, list objects, Hashtable objects, ordered table objects, and collection objects that comprise 5 of objects. each object of the Redis species is represented by the redisobject structure. Defined as follows
typedef struct REDISOBJECT {unsigned type;unsigned encoding;void *ptr;}
type is just one of the 5 types of objects just said, and the PTR pointer executes the object's true position. The Type command allows you to find out which of the Redis keys belong to the 5 types of objects. Examples are as follows:
127.0.0.1:6379> hmset profile Addr Chengdu country China company Jdok127.0.0.1:6379> type Profilehash
There are 5 types of objects, each with its own special commands that cannot be used on other objects. For example, it is not possible to rpush a hash table. Therefore, each command executes, the server first performs a type check. The book says Type,del and other orders are polymorphic orders, it is really nonsense. C language is a functional language, with polymorphic unrelated. Memory Recyclingthe java/c# memory recovery mechanism is not available in C + +. Redis uses a method of reference counting to achieve memory recovery. But one of the Achilles ' heel of reference counting is that if a circular reference is encountered, the object that is referenced by the loop will never be recycled. Thus causing a memory leak. So the JVM abandons this memory recovery technique and uses the root path to reach the algorithm. It is possible that the Redis object is simple and not loop-dependent, so take this approach. There is very little in the book about memory recycling, and I will study it myself later. Object Sharingif Redis needs to produce two identical objects, Redis produces only one data, with pointers to two objects pointing to the data object. The object is then shared.
127.0.0.1:6379> set a 100ok127.0.0.1:6379> object refcount A (integer) 2127.0.0.1:6379> set B 100ok127.0.0.1 :6379> object refcount A (integer) 3
RefCount indicates that the object's data has been referenced several times.
The redisobject struct also has a field, LRU, which records when the object was last accessed by the client. Object Idletime Subtracts the LRU time of the objects with the current time.
127.0.0.1:6379> set a 100ok127.0.0.1:6379> object idletime A (integer) 4127.0.0.1:6379> object Idletime A ( Integer) 9
The first eight chapters of this book are about the most basic data structures in Redis. Share so much first.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Redis Design and implementation" Reading notes