Redis in-depth (-)

Source: Internet
Author: User
I. Each key pair (key-value) in the Redis database is composed of objects. Keys are a String object, a string object, a list object, and a hash object, set object, one of the five ordered set objects. Redis does not directly use a traditional string representation in the C language, but instead builds a simple dynamic string named SD.

I. Each key pair (key-value) in the Redis database is composed of objects. Keys are a String object, a string object, a list object, and a hash object, set object, one of the five ordered set objects. Redis does not directly use a traditional string representation in the C language, but instead builds a simple dynamic string named SD.

I. Redis key-value pairs

Each key-value pair in the Redis database is composed of objects. Keys are a String object, and values are string objects, List objects, hash objects, and collection objects, one of the five types of Ordered Set objects.

Redis does not directly use a traditional string representation in the C language. Instead, it builds an abstract type named simple dynamic string SDS and uses SDS as the default string representation of Redis.

SET msg "helloworld"

The key is a string object, and the underlying Implementation of the object is an SDS that stores the string "msg"

The value is a string object. The underlying Implementation of the object is an SDS that stores the string "hello world ".

Ii. SDS Definition

Each sds. h/sdshdr structure represents an SDS Value

Struct sdshdr

{

// Record the number of bytes used in the buf array, which is equal to the length of the string stored by SDS

Int len;

// Record the number of unused bytes in the buf Array

Int free;

// Byte array, used to save strings

Char buf [];

}

The value of the free attribute is 0, indicating that this SDS is not allocated any unused space.

The value of the Len attribute is 5, indicating that the SDS stores a five-byte long string.

Iii. Differences with C strings

1. The C language uses an array of characters with a length of N + 1 to represent a string with a length of N, and the last element of the character array is always a null character '\ 0'

C string does not record its own length information. Therefore, to obtain the length of a C string, We must traverse the entire string, O (N). Because SDS records the length of SDS, therefore, the complexity is only O (1 ).

2. Prevent Buffer Overflow

C may cause buffer overflow during String concatenation. The space allocation policy of SDS completely eliminates the possibility of buffer overflow: When the sds api needs to modify the SDS, the API first checks whether the SDS space meets the modification requirements. If not, the API automatically expands the SDS space to the size required for modification, and then perform the actual modification operation.

3. Reduce the number of times the memory is reallocated when the string is modified.

For a C string, if the program executes an operation to increase the string or shorten the string, the program needs to re-allocate the memory to allocate or release the space that the string is no longer used. In SDS, the length of the buf array is not necessarily the plus one character count. The array can contain unused bytes, and the number of these bytes is recorded by the free attribute of SDS.

4. Binary Security

The buf attribute of SDS is called a byte array. Redis uses it not to store characters, but to store a series of binary data.

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.