Five kinds of data types supported by Redis and their underlying implementations

Source: Internet
Author: User
Tags compact objet redis


Introduction to Redis object types






Redis is a key/value database in which each key and value are represented using objects. For example, we execute the following code: The key is a message and is an object that contains the string "message". And value is an object that contains the "Hello Redis".



Redis A total of five types of objects, respectively:


Type Constants the name of the object
Redis_string String Object
Redis_list List objects
Redis_hash Hash object
Redis_set Collection objects
Redis_zset Ordered collection objects

The structure body of an object in Redis is represented as follows:


/*
* Redis Object
*/
typedef struct REDISOBJECT {

Type
unsigned type:4;

Do not use (Snap to bit)
unsigned notused:2;

Encoding method
unsigned encoding:4;

LRU time (relative to Server.lruclock)
unsigned lru:22;

Reference count
int refcount;

Point to value of object
void *ptr;

} robj;



Type represents the object type of the object, one of the top five. However, in order to improve storage efficiency and program execution efficiency, the underlying data structure of each object can be implemented in more than one way. Encoding represents the encoding used at the bottom of the object. The following is an introduction to each of the underlying data structure implementations, and then to the underlying structure of each object type and analysis of their relationships.






Redis Object underlying data structure






There are eight types of underlying data structures, as shown in the following table:


Encoding Constants the underlying data structure corresponding to the encoding
Redis_encoding_int Integer of type Long
Redis_encoding_embstr EMBSTR encoded simple dynamic strings
Redis_encoding_raw Simple dynamic string
Redis_encoding_ht Dictionary
Redis_encoding_linkedlist Double-end Linked list
Redis_encoding_ziplist Compress list
Redis_encoding_intset Integer collection
Redis_encoding_skiplist Jumping Tables and dictionaries





String Object






The encoding of a string object can be int, raw, or embstr.



If the contents of a string can be converted to long, the string is converted to a long type, and the object's PTR points to that long and the object type is represented by an int type.



There are two types of ordinary strings, embstr and Raw. EMBSTR should be the new data structure for Redis 3.0, which is not in 2.8. If the length of the string object is less than 39 bytes, the Embstr object is used. Otherwise, use a traditional raw object. The benefits of EMBSTR are as follows: Embstr is created by allocating only one memory, and Raw to two times (one for SDS allocations, another for objet, Embstr for the first time). In contrast, the amount of memory freed is changed from two to one time. Embstr's objet and SDS are put together to better exploit the benefits of caching. It should be noted that Redis does not provide any way to modify the Embstr, that is, EMBSTR is a read-only form. Changes to EMBSTR are actually converted to raw before modification.






List Objects






The encoding of a list object can be either Ziplist or LinkedList.



Ziplist is a compact list, and its advantage is that it saves more memory space because it stores content in contiguous memory areas. When the list object element is small and each element is small, the Ziplist store is used. But when the amount of data is too large, ziplist is not so useful. Because in order to ensure the continuity of his storage content in memory, the insertion complexity is O (N), that is, each insertion will be realloc again. As shown in the following illustration, PTR in the object structure is pointing to a ziplist. The entire ziplist only needs to be malloc once, and they are a contiguous area in memory.












LinkedList is a two-way linked list. Its structure is relatively simple, the node holds the pre and next two pointers, as well as node-related information. When each node is added, it is necessary to malloc a piece of memory again.




Hash Object






The underlying implementation of a hash object can be either Ziplist or Hashtable.



Hash objects in the ziplist are stored in the order in which they are key1,value1,key2,value2. This approach is highly efficient when the number of objects is small and the content is not large.



Hashtable is a collection object that is implemented by the DICT structure.



The encoding of the collection object can be either Intset or Hashtable.



Intset is an integer set, which is stored as an integer of the same type, supporting integers of three lengths: [CPP] view plain copy #define INTSET_ENC_INT16 (sizeof (int16_t)) # Define INTSET_ENC_INT32 (sizeof (int32_t)) #define Intset_enc_int64 (sizeof (int64_t)) Intset is an ordered collection that finds the complexity of an element O (Logn), However, the insertion is not necessarily O (Logn), as it may involve an upgrade operation. For example, when the collection is full of int16_t-type integers, then to insert a int32_t, so in order to maintain the consistency of the data type in the collection, then all the data will be converted to the int32_t type, involving the reallocation of memory, when the insertion complexity is O (N). Intset is not supported for degraded operations.
ordered collection objects



The encoding of ordered sets may be two kinds, one is ziplist, the other is the combination of skiplist and dict.



Ziplist as a collection and as a hash object, the member and the score are stored sequentially. According to score from small to large order. Its structure is no longer repeated.



Skiplist is a jumping table that implements a fast lookup in an ordered set, and in most cases it can be as fast as a balanced tree. But its implementation is relatively simple, can be used as a substitute for the balance tree. Its structure is quite special. The following are the structure of the jump table skiplist and its internal node Skiplistnode:









Redis can use five data types: strings, hash lists, lists, collections, ordered collections












String Type





A string is the most basic data type in Redis, and it can store any type of string containing binary data. can be used to store mailboxes, JSON objects, even a picture, a string allows maximum storage capacity of512MB。 A string is the basis of four other types, and the difference between several other types is essentially just the way the string is organized.Basic Commands string Manipulation SETAssign value, usage: SET key value GetTake value, usage: Get keyINCRIncrementing numbers, only useful for numeric type keys, equivalent to Java i++ operations, usage: INCR keyIncrbyAdd the specified number, only for numeric types of keys useful, equivalent to Java i+=3, usage: Incrby key increment, meaning that the key increment,increment can be negative, indicating a decrease.DECRDescending number, only useful for numeric type keys, equivalent to Java I-Compact, usage: DECR keyDecrbyReduce the specified number, only for numeric types of keys useful, equivalent to Java i-=3, usage: Decrby key decrement, meaning that the key decrement,decrement can be positive, indicating increase.incrbyfloatIncreases the specified floating-point number, only useful for numeric type keys, usage: incrbyfloat key incrementAPPENDAppends a value to the tail, equivalent to "Hello" in java. append ("World"), Usage: Append key valueSTRLENGet string length, usage: STRLEN keyMsetSet values for multiple keys at the same time: Mset key1 value1 [Key2 value2 ...]MgetGets the value of multiple key at the same time: Mget Key1 [Key2 ...] bit Operation getbit Gets the value of the bits of a key value (0/1), used: Getbit key offset setbit Sets the value of the bits of a key value (0/1) at the specified position, using: setbit Key offset value bitcount Gets the number of 1 of the binary representation of a range of key values, using: Bitcount key [Start end] bitop This command can perform bitwise operations on multiple string type keys and store the results in the specified key, Bitop supported operations including:Or,and,xor,not, usage: bitop OP deskey key1 key2 bitpos Gets the position of the specified key with the first bit value of 0 or 1, usage: Bitpos key 0/1 [Start, end]





Hash Type









The


hash type is equivalent to HashMap in Java, whose value is a dictionary that holds many key,value pairs, and each pair of Key,value value keys are string types, in other words, the hash type cannot nest other data types. A hash type key can contain up to 2 of 32-1 fields. Basic Commands Hset   Assignment, Usage: Hset key field value Hmset   Assign multiple fields at once, usage: Hmset key field1 value1 [Field2 Values] hget   Value, Usage: Hset key field hmget   values for multiple fields at once: Hmset key field1 [Field2] Strong>hgetall   The value of all fields at once, using: Hgetall key hexists   To determine whether the field exists, Usage: hexists key field Strong>hsetnx   Assign values when a field does not exist, using: Hsetnx key field value Hincrby   Add a number that is useful only for values of numeric types, Usage: Hincrby key field increment Hdel   Delete field, usage: Hdel key field Hkeys   get all field names. Usage: Hkeys key hvals   Get all field values, usage: hvals key Hlen   Get number of fields, usage: Hlen key List Type




The


list type (list) is used to store an ordered list of strings, often by adding elements to both ends of the queue or by getting a fragment of the list. A two-way list (double linked list) is used internally, so the time complexity of adding elements to both ends of the list is O (1), getting closer to the elements at both ends of the list. However, the disadvantage is that using lists to access elements through the index is inefficient (you need to traverse the elements from the endpoint). So the use of the list is generally like: Friends circle New, only concerned about the latest content. With list types, Redis can also be used as Message Queuing.






basic Commands Lpush   Adding elements to the left end of the list, usage: Lpush key value Rpush   Add elements to the right end of the list, using: Rpush key value lpop   Eject the element from the left end of the list using: Lpop key rpop   Popup element from the right end of the list, Usage: Rpop key Llen   Get the number of elements in the list, usage: Llen key Lrange   Get the elements of a fragment in the list, usage: lrange Key start Stop,index from 0,-1 for the last element Lrem   Delete the value specified in the list, use: Lrem key count value, Deletes the element with value from the first count in the list, the number from the left when Count>0, and the count<0 from the right when count=0 deletes all elements of value lindex   Get the element value of the specified index, usage: lindex key index LSET   Set the element value for the specified index, usage: LSET key index value LTRIM   Keep Only the list of specified fragments, usage: LTRIM key start stop, include start and stop Linsert   Insert element in the list, usage: Linsert key before| After Privot value, start at the left to look for the first element with the value Privot, and then before or after the second argument to insert value Rpoplpush before or after the element.   Escaping elements from one list to another list, usage: Rpoplpush source destination collection type









Set in the concept of the high school textbooks, the collection of each element is different, the number of elements in the collection of up to 2 of the 32 square-1, the elements in the collection are not in order.Basic Commands Saddadd element, Usage: Sadd key value1 [value2 value3 ...]SremDelete element, usage: Srem key value2 [value2 value3 ...]smembersGet all the elements in the collection, Usage: Smembers keySismemberDetermines whether an element is in the collection, Usage: Sismember key valueSdiffOn the set do the difference set operation, usage: Sdiff key1 key2 [Key3 ...], first calculates the difference set of Key1 and Key2, then uses the result and the Key3 to do the difference setsinterTo do the intersection operation of the set, usage: sinter key1 key2 [Key3 ...]sunionDo the set and set operation, usage: sunion key1 key2 [Key3 ...]SCardGets the number of elements in the collection, Usage: SCard keySdiffstoreMake a difference set to the collection and store the results, using: Sdiffstore destination key1 Key2 [Key3 ...]SinterstoreIntersects the set and stores the result, using: Sinterstore destination key1 Key2 [Key3 ...]SunionstoreDo a set operation on the collection and store the results, using: Sunionstore destination key1 Key2 [Key3 ...]SrandmemberRandom get the elements in the collection, usage: Srandmember key [Count], when count>0, will randomly get count of the elements in the collection, when count<0, randomly in the collection to get |count| and possibly duplicate elements.SpopRandomly pops an element from the collection, using: Spop keyordered collection type


The difference between an ordered set type and a collection type is that he is orderly. An ordered set is a fraction associated with each element on the basis of a collection, which allows the ordered set to not only support inserting, deleting, and determining whether an element exists, but also to obtain the top/bottom n elements of the score. Each element in an ordered set is different, but the score can be the same. An ordered set is implemented using a hash table and a jump list, even if the data in the middle is read quickly and the time complexity is O (log (N)), and the ordered set costs more memory than the list.Basic Commands Zaddadd element, Usage: Zadd key score1 value1 [score2 value2 score3 value3 ...]ZscoreGets the fraction of the element, usage: Zscore key valueZrangeGet the element that is ranked in a range: Zrange key start Stop [Withscore], sorted by element in order from small to large, numbering from 0, containing elements for start and stop, Withscore option indicates whether to return element fractionsZrevrangeGet the element that is ranked in a range, using: Zrevrange key start Stop [Withscore], just like the previous command usage, which is sorted in reverse order.ZrangebyscoreGets the element in the specified fraction range, using: Zrangebyscore key min Max, which contains Min and Max, (min means not including min, max means no Max,+inf representation infinityZincrbyAdd a fraction of an element, usage: Zincrby key increment valueZcardGets the number of elements in the collection, Usage: Zcard keyZcountGets the number of elements in the specified fraction range, usage: Zcount key min max,min and Max's usage is the same as in 5ZremDelete one or more elements, usage: Zrem key value1 [value2 ...]ZremrangebyrankTo delete an element by rank, usage: Zremrangebyrank key start stopZremrangebyscoreDelete elements by fraction range, usage: Zremrangebyscore key min max,min and Max's usage is the same as in 4ZrankGets the ranking of the elements in a positive order, usage: Zrank key valueZrevrankGets the rank of the element in reverse order, usage: Zrevrank key valueZinterstoreComputes the intersection of ordered sets and stores the results, using: Zinterstore destination numbers key1 key2 [Key3 key4 ...] WEIGHTS weight1 weight2 [weight3 weight4 ...] AGGREGATE SUM | MIN | Max,numbers represents the number of sets that participate in the operation, weight represents the weight, aggregate represents the result valueZunionstoreCompute ordered set of sets and store results, usage and 141, no longer repeat.


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.