Redis feature one: Data structures

Source: Internet
Author: User

A simple dynamic string
    • Redis did not directly use the traditional C-language string representation, but built a dynamic string SDS, when Redis needed more than just a string literal, but a can be the show IDE string value, Redis uses SDS to represent the string value, For example, in a Redis database, key-value pairs containing string values are implemented by SDS at the bottom.
redis  setname"bugall"ok1.键值对的键是一个字符串对象,对象的底层实现是一个保存着字符串"name"的SDS2.键值对的值也是一个字符串对象,对象的底层实现是一个保存这字符串"bugall"的SDS
    • In addition to saving string values in the database, SDS is also used as a buffer, the AOF buffer in the AOF module, and the input buffer in the client state.
structsdshdr{//Record the number of bytes used in the BUF array    //equals the length of the string saved by SDS    int Len;//Record the number of unused bytes in the BUF array    intFree//byte array to hold the stringChar buf[];}1. The value of the free propertyis 0, indicating that the SDS does not have any unused space assigned to it.2.LenThe value of the propertyis 5, which indicates that the SDS holds a five-byte long string3. The Buf property is an array of type char, an array ofTop 5Each byte is saved as ' R '.', 'E', 'D', 'I', 'S' five characters, and the last byte holds the null character ' /Note: The 1-byte space saved Kong is not counted in the Len attribute of the SDS, and an additional one-byte space is allocated for the null character. 
    • Unlike the C string, the SDS space allocation strategy completely eliminates the possibility of buffer overflow, and when SDS API needs to be modified, the API will first check if 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, so using SDS does not require manual modification of the SDS's space size, nor is there a case of buffer overflow.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Redis feature one: Data structures

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.