Dynamic string Implementation of redis

Source: Internet
Author: User

The structure of SDS dynamic string data is as follows:

1 typedef char *sds;2 3 struct sdshdr {4     int len;5     int free;6     char buf[];7 };

Len records the length of the string, free records the remaining space of the SDS, and the Buf points to the space of the stored characters.

The corresponding memory space is as follows:

For example, to store the string "chenrancc" at the beginning ":

At the beginning, an extra space of \ 0 is applied for than the initial string. As shown in, the corresponding function isSdsnewlen.
After the following CC characters are deleted:

After the CC characters are deleted, the empty space is not recycled. Instead, the free space is used for record. If you want to recycle two free spaces, you must allocate a new SDS.ReallocReassign the new SDS. The corresponding function isSdsremovefreespace. If you want to increase the space of SDS, use the same methodReallocAllocates a new SDS. The corresponding function isSdsmakeroomfor.

To reclaim the memory space of SDS, you can use the FunctionSdsfreeWhich actually callsFreeFunction.

In addition to the functions mentioned above, SDS also defines many other functions to facilitate upper-layer usage:

1 SDS sdsnewlen (const void * init, size_t initlen); // create SDS 2 SDS sdsempty (void) with a string of initlen length ); // create an SDS 3 SDS sdsnew (const char * init) with a length of 0; // create SDS 4 SDS sdsdup (const sds s) with a string ending with NULL ); // copy an SDS 5 void sdsfree (sds s); // release the memory space occupied by SDS 6 void sdsupdatelen (sds s ); // update Len in SDS to the actual String Length 7 void sdsclear (sds s); // set the string in SDS to an empty string 8 SDS sdsmakeroomfor (sds s, size_t addlen ); // the space occupied by the SDS string is increased by addlen characters (including free characters) 9 SDS sdsremovefreespace (sds s ); // remove the free space in SDS 10 size_t sdsallocsize (sds s); // obtain the actual space occupied by SDS 11 void sdsincrlen (sds s, int incr ); // The length of the actual SDS string is increased by incr12 SDS sdsgrowzero (sds s, size_t Len); // increase the space occupied by SDS to Len, the added space is cleared by 13 SDS sdscatlen (sds s, const void * t, size_t Len); // a string of 14 SDS sdscat (SDS s, const char * t); // The end of SDS is connected to a null string 15 SDS sdscatsds (sds s, const SDS t ); // connect the end of SDS to another sds16 SDS sdscpylen (sds s, const char * t, size_t Len); // copy the string with the length of Len to 17 SDS sdscpy (SDS s, const char * t); // copy the string ending with null to 18 SDS sdscatvprintf (sds s, const char * FMT, va_list AP) in SDS ); // link a variable parameter string 19 SDS sdscatprintf (sds s, const char * FMT,...) at the end of SDS ,...); // The end of SDS is connected to a variable parameter string 20 SDS sdstrim (sds s, const char * cset); // remove the prefix and suffix of the SDS string, these characters are all 21 void sdsrange (sds s, int start, int end) that has been present in the cset; // obtain a string of the SDS string. the start and end values can be negative, negative number indicates that 22 void sdstolower (sds s) is indexed from the back to the front; // set the characters in the SDS string to lowercase 23 void sdstoupper (sds s ); // set the characters in the SDS string to uppercase 24 int sdscmp (const SDS S1, const SDS S2); // compare the values of the two strings to 25 SDS * sdssplitlen (const char * s, int Len, const char * SEP, int seplen, int * count); // use the string SDP to split an SDS into multiple sds26 void sdsfreesplitres (SDS * tokens, int count ); // release the SDS array space 27 SDS sdsfromlonglong (long value) returned by the sdssplitlen function; // convert the number of the long type into an sds28 SDS sdscatrepr (SDS s, const char * P, size_t Len); // link a string with the length of Len at the end of SDS, and display the unprintable characters 29 int is_hex_digit (char C ); // judge whether a character is released as a hexadecimal number 30 int hex_digit_to_int (char C); // convert a hexadecimal number to an integer 31 SDS * sdssplitargs (const char * line, int * argc); // use ', "in a string to form multiple sds32 SDS sdsmapchars (sds s, const char * From, const char * To, size_t setlen) // Replace the characters in SDS that appear in from with the character 33 SDS sdsjoin (char ** argv, int argc, char * SEP) corresponding ); // connect multiple strings with delimiters to form an SDS
View code

The vector in SDS is similar to that in C ++. The only difference is that when space is insufficient, the vector can automatically increase the space by two times.

Dynamic string Implementation of redis

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.