Do you really understand data structure redis do? redis internal data structures and an external data structure Secret

What data structures Redis there?


String String, Dictionary Hash, List List, a collection of Set, an ordered set of SortedSet.

Many people have encountered this scene, right during the interview?

In fact, in addition to several common data structures above, also you need to add a data structure HyperLogLog, Geo.

But many people do not know is not only above redis several data structures, also built the internal data structure. I.e. redis can be divided into an external data structure and the internal data structure.

(I want to learn programming from a small partner search circle T community , more and more industry-related industry information about free video tutorials. Oh, absolutely free!)

1. How to view the data structure of redis?

#### 1.1 How to view external data structure of redis?
Type command may be used to return the key type, such asstring, list, set, zset, hash 和stream,实例如下:

redis> SET key1 "value"
"OK"
redis> LPUSH key2 "value"
(integer) 1
redis> SADD key3 "value"
(integer) 1
redis> TYPE key1
"string"
redis> TYPE key2
"list"
redis> TYPE key3
"set"
redis> 

1.2 How do I view redis internal data structures
can be viewed by Object command. command object allows viewing the object from a given key inside of a Redis.

It is usually used in the case of debugging (the debugging) or learn to save space using a special encoding the key.

When Redis as a cache program, you can also order it through the information in the decision to expel the key strategies (eviction policies).

Data structure defines redisObject 2.redis


Internal data types server.h

typedef struct redisObject {
    unsigned type:4;
    unsigned encoding:4;
    unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
                            * LFU data (least significant 8 bits frequency
                            * and most significant 16 bits access time). */
    int refcount;
    void *ptr;
} robj;

Here, type internal data structure is implemented as an external data structure is redis the redis, encoding

Type values ​​as follows:

/*-----------------------------------------------------------------------------
 * Data types
 *----------------------------------------------------------------------------*/

/* A redis object, that is a type able to hold a string / list / set */

/* The actual Redis Object */
#define OBJ_STRING 0    /* String object. */
#define OBJ_LIST 1      /* List object. */
#define OBJ_SET 2       /* Set object. */
#define OBJ_ZSET 3      /* Sorted set object. */
#define OBJ_HASH 4      /* Hash object. */

/* The "module" object type is a special one that signals that the object
 * is one directly managed by a Redis module. In this case the value points
 * to a moduleValue struct, which contains the object value (which is only
 * handled by the module itself) and the RedisModuleType struct which lists
 * function pointers in order to serialize, deserialize, AOF-rewrite and
 * free the object.
 *
 * Inside the RDB file, module types are encoded as OBJ_MODULE followed
 * by a 64 bit module type ID, which has a 54 bits module-specific signature
 * in order to dispatch the loading to the right module, plus a 10 bits
 * encoding version. */
#define OBJ_MODULE 5    /* Module object. */
#define OBJ_STREAM 6    /* Stream object. */

Encoding the value as follows: server.h

/* Objects encoding. Some kind of objects like Strings and Hashes can be
 * internally represented in multiple ways. The 'encoding' field of the object
 * is set to one of this fields for this object. */
#define OBJ_ENCODING_RAW 0     /* Raw representation */
#define OBJ_ENCODING_INT 1     /* Encoded as integer */
#define OBJ_ENCODING_HT 2      /* Encoded as hash table */
#define OBJ_ENCODING_ZIPMAP 3  /* Encoded as zipmap */
#define OBJ_ENCODING_LINKEDLIST 4 /* No longer used: old list encoding. */
#define OBJ_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
#define OBJ_ENCODING_INTSET 6  /* Encoded as intset */
#define OBJ_ENCODING_SKIPLIST 7  /* Encoded as skiplist */
#define OBJ_ENCODING_EMBSTR 8  /* Embedded sds string encoding */
#define OBJ_ENCODING_QUICKLIST 9 /* Encoded as linked list of ziplists */
#define OBJ_ENCODING_STREAM 10 /* Encoded as a radix tree of listpacks */

Internal Type Summary
Here Insert Picture Description

Server.h 3. limit data structure

/* Zipped structures related defaults */
#define OBJ_HASH_MAX_ZIPLIST_ENTRIES 512
#define OBJ_HASH_MAX_ZIPLIST_VALUE 64
#define OBJ_SET_MAX_INTSET_ENTRIES 512
#define OBJ_ZSET_MAX_ZIPLIST_ENTRIES 128
#define OBJ_ZSET_MAX_ZIPLIST_VALUE 64
#define OBJ_STREAM_NODE_MAX_BYTES 4096
#define OBJ_STREAM_NODE_MAX_ENTRIES 100
/* HyperLogLog defines */
#define CONFIG_DEFAULT_HLL_SPARSE_MAX_BYTES 3000

4. Examples

4.1 String String

int: 8 bytes long integer

embstr: less than 44 byte strings (currently), the previous version is 3.0 39

raw: string is less than 39 bytes of 512MB

object.c

/* Create a string object with EMBSTR encoding if it is smaller than
 * OBJ_ENCODING_EMBSTR_SIZE_LIMIT, otherwise the RAW encoding is
 * used.
 *
 * The current limit of 44 is chosen so that the biggest string object
 * we allocate as EMBSTR will still fit into the 64 byte arena of jemalloc. */
#define OBJ_ENCODING_EMBSTR_SIZE_LIMIT 44

Verify:

Connected.
local:0>object encoding test1
"int"

local:0>object encoding test2
"embstr"

local:0>object encoding test3
"raw"

local:0>get test1
"10000"

local:0>get test2
"hello world!"

local:0>get test3
"Redis is not a plain key-value store, it is actually a data structures server, supporting different kinds of values. What this means is that, while in traditional key-value stores you associated string keys to string values, in Redis the value is not limited to a simple string, but can also hold more complex data structures. The following is the list of all the data structures supported by Redis, which will be covered separately in this tutorial:"

local:0>

4.2 Hash hash

When the filed number less than 512, and no value greater than 64 bytes, for the inner code ziplist

When the number is greater than 512 filed, or a value greater than 64 bytes, encoded internal hashtable

Connected.
local:0>hmset hashtest1 field1 value1 field2 value2 field3 value3
"OK"

local:0>object encoding hashtest1
"ziplist"

local:0>hset hashtest2 field1 "Redis modules can access Redis built-in data structures both at high level, by calling Redis commands, and at low level, by manipulating the data structures directly."
"1"

local:0>object encoding hashtest2
"hashtable"

local:0>

4.3 List list

Before redis 3.2

When the number of elements in list is less than 512, and a value no greater than 64 bytes, for the inner code ziplist

When the number of elements in list is greater than 512, or a value greater than 64 bytes, for the inner code linkedlist

After redis 3.2

Use quicklist

Connected.
local:0>rpush listtest1 value1 value2 value3 value4 value5
"5"

local:0>object encoding listtest1
"quicklist"

local:0>rpush listtest2 "Redis modules can access Redis built-in data structures both at high level, by calling Redis commands, and at low level, by manipulating the data structures directly."
"1"

local:0>object encoding listtest2
"quicklist"

local:0>

4.4 collection set

When the (default) set of elements in the set are integers smaller than 512 and the number of elements used intset

Other conditions of use hashtable

local:0>sadd settest1 1 2 3
"3"

local:0>object encoding settest1
"intset"

local:0>sadd settest2 "hello world!"
"1"

local:0>object encoding settest2
"hashtable"

local:0>

4.5 ordered set zset

When the number of elements of an ordered set zse less than 128 (the default), and a value no greater than 64 bytes, for the inner code ziplist

When the number of elements of an ordered set zse greater than 128 (default), or a value greater than 64 bytes, for the inner code skiplist

Connected.
local:0>zadd zsettest1 10 value1 20 value2 30 value3
"3"

local:0>object encoding zsettest1
"ziplist"

local:0>zadd zsettest2 60 "Redis modules can access Redis built-in data structures both at high level, by calling Redis commands, and at low level, by manipulating the data structures directly."
"1"

local:0>object encoding zsettest2
"skiplist"

local:0>

4.6 Geo

Connected.
local:0>GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
"2"

local:0>object encoding Sicily
"ziplist"

local:0

4.7 HyperLogLog

Connected.
local:0>PFADD hll a b c d e f g
"1"

local:0>object encoding h11
null

local:0>object encoding hll
"raw"

local:0>

5 summary


  1. External data structure types can be viewed by type

  2. Internal data structure types can be viewed object

  3. Achieve understanding of the internal data structure will help us to understand redis

  4. You can review the data structure and its implementation

Guess you like

Origin blog.csdn.net/wanghao112956/article/details/91374582