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