Redis source reading-sds string source reading

Source: Internet
Author: User

Redis uses SDS instead of char * strings,

It is defined as follows:

Char *SDS; struct SDSHDR {    int  len;     int  Free ;     Char buf[];};

SDS points to a char string

SDSHDR is the string header

More ingenious structure

Use char buf[] to store the actual contents of a string

Note Char *buf and Char buf[] are different

sizeof (SDSHDR) equals 8, not what I thought 12

The contiguous memory structure is as follows:

0----7 SDSHDR

8----After SDS, at the same time also buf position

Methods of establishing SDS

SDS Sdsnewlen (Const void*init, size_t Initlen) {    structSDSHDR *sh; if(init) {sh= Zmalloc (sizeof(structSDSHDR) +initlen+1); } Else{sh= Zcalloc (sizeof(structSDSHDR) +initlen+1); }    if(sh = = NULL)returnNULL; SH->len =Initlen; SH- Free=0; if(Initlen &&init) memcpy (Sh-buf, Init, Initlen); SH->buf[initlen] =' /'; return(Char*) sh->buf;}

Methods of releasing SDS

void Sdsfree (SDS s) {    ifreturn;    Zfree (S-sizeof(struct  sdshdr));}

Note that the SDS points to the actual string, not the SDSHDR.

There are many functions for processing SDS strings in SDS.C (after all, in order to replace char *)

 

Function name

Role

Complexity of

Sdsnewlen

Creates an SDS of a specified length, accepting a specified C string as the initialization value

O (N)

Sdsempty

Create an SDS that contains only the empty string ""

O (N)

Sdsnew

Creates a corresponding SDS based on the given C string.

O (N)

Sdsdup

Copy a given SDS

O (N)

Sdsfree

Releasing a given SDS

O (1)

Sdsupdatelen

Update the free and Len values for the SDSHDR corresponding to the given SDS

O (1)

Sdsclear

Clears the buf of the given SDS, initializes the BUF to "", and modifies the free and Len values of the corresponding SDSHDR

O (1)

Sdsmakeroomfor

Expansion of BUF for a given SDS corresponding to SDSHDR

O (N)

Sdsremovefreespace

Release the excess space of the BUF without altering the SDS

O (N)

Sdsallocsize

Calculates the memory size of a given SDS

O (1)

Sdsincrlen

Extend or shrink the right side of the BUF for a given SDS

O (1)

Sdsgrowzero

Expands the given SDS to the specified length, and the spare part is populated with the

O (N)

Sdscatlen

Appends a C string to the buf of the given SDS counterpart SDSHDR

O (N)

Sdscpylen

To copy a C string into SDS, it is necessary to determine whether an extension is required based on the total length of the SDS

O (N)

sdscatprintf

Append to the given SDS by formatting the output form

O (N)

Sdstrim

For a given SDS, delete the front/back end characters in the given C string

O (N)

Sdsrange

Intercept a given sds,[start,end] string

O (N)

sdscmp

Compare the size of two SDS

O (N)

Sdssplitlen

Cuts the given string s by a given Sep delimited string

O (N)

Redis source reading-sds string source reading

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.