Redis Deep (-)

Source: Internet
Author: User

First, the Redis key value pair

Each key-value pair (key-value) inside a Redis database is made up of objects, the key is a string object, the value is a string object, a list object, a hash object, a collection object, one of five of the ordered collection objects.

Redis does not directly use the traditional C-language string representation, but instead constructs an abstract type called 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 a SDS that holds the string "MSG"

Value is a string object, and the underlying implementation of the object is a SDS that holds the string "Hello World"

Ii. Definition of SDS

Each SDS.H/SDSHDR structure represents an SDS value

struct SDSHDR

{

Records the number of bytes used in the BUF array, equal to the length of the string saved by SDS

int Len;

Record the number of unused bytes in the BUF array

int free;

byte array to hold the string

char buf [];

}

The value of the free property is 0, indicating that the SDS does not have any unused space assigned to it.

The value of the Len property is 5, which means that the SDS holds a five-byte long string

Three, the difference from the C string

1. The C language uses a character array of length n+1 to represent a string of length n, and the last element of the character array is always the null character '

C string does not record its own length information, so in order to get the length of a C string, the entire string must be traversed, O (N), SDS because the length of the SDS itself is recorded, so the complexity is only O (1).

2. Eliminate buffer overflow

C may cause a buffer overflow during string concatenation, and the SDS space allocation strategy completely eliminates the possibility of buffer overflow: When SDS API needs to be modified, the API will first check that the SDS space meets the requirements of the modification, and if not, The API automatically expands the space of the SDS to the size required to perform the modification before performing the actual modification operation.

3. Reduce the number of memory re-allocations that are caused by modifying strings

For C strings, if the program is performing an operation that increases the string or shortens the operation of the string, the program needs to allocate or free the portion of space that the string is no longer using through memory redistribution. SDS, the length of the BUF array is not necessarily the number of characters plus one, the array can contain unused bytes, and the number of these bytes is recorded by the SDS free attribute.

4. Binary security

The BUF attribute of SDS is called a byte array, and Redis does not use this array to hold characters, but instead uses it to hold a series of binary data.

Redis Deep (-)

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.