Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement string types in Redis

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

It is believed that many inexperienced people have no idea about how to implement string type in Redis. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Redis is a database based on key-value storage. Redis uses a string as its key, and the string is the most basic data type used by the value. Of course, there are more complex types, such as lists, collections, ordered collections, and hash tables, but even these complex types are implemented using strings.

Redis implements its own string type internally. The implementation details are contained in the sds.c file (sds is SimpleDynamicStrings).

Structsdshdr {

Longlen

Longfree

Charbuf []

}

The actual string stored in buf

The len field holds the length of the buff. This field makes the operation complexity of Redis fetching the length of a string O (1).

The free field holds the remaining space in the buff.

The len and free fields can be thought of as meta-information that holds an array of buf strings.

How to implement string types inside Redis

Create a new Redis string

A new data type called sds is defined in sds.h, which is actually a string pointer:

Typedefchar*sds

The function sdsnewslen for creating a new Redis string pointer is defined in sds.c:

Sdssdsnewlen (constvoid*init,size_tinitlen) {

Structsdshdr*sh

Sh=zmalloc (sizeof (structsdshdr) + initlen+1)

# ifdefSDS_ABORT_ON_OOM

If (sh==NULL) sdsOomAbort ()

# else

If (sh==NULL) returnNULL

# endif

Sh- > len=initlen

Sh- > free=0

If (initlen) {

If (init) memcpy (sh- > buf,init,initlen)

Elsememset (sh- > buf,0,initlen)

}

Sh- > buf [initlen] ='\ 0'

Return (char*) sh- > buf

}

As mentioned above, the Redis string is of type structsdshdr. But the sdsnewlen function returns a string pointer!

This is just a trick. Let's explain it here. Suppose we create a new Redis string with the sdsnewlen function as follows:

Sdsnewlen ("redis", 5)

This function creates a new variable of type structsdshdr and allocates space for both the len,free and buf fields. The code for allocating space is as follows:

Sh=zmalloc (sizeof (structsdshdr) + initlen+1); / / initlenislengthofinitargument.

When sdsnewlen returns successfully, the resulting Redis string looks something like this:

-

| | 5 | 0 | redis |

-

^ ^

Shsh- > buf

The sdsnewlen function returns sh- > buf to the caller.

So what if you want to free up the space occupied by the Redis string that sh points to?

What you want at this point is a pointer to sh, and what you get is a pointer to sh- > buf.

So can you get a pointer to sh from a pointer to sh- > buf?

Yes, it's just a pointer operation. Notice the memory diagram above. When we subtract two long lengths from the address of sh- > buf, we get the address of sh.

And coincidentally, the lengths of the two longs add up to exactly the length of structsdshdr. Note: declaring buf as charbuf [] is a common programming technique for variable-length structures. )

Let's take a look at how the sdslen function works:

Size_tsdslen (constsdss) {

Structsdshdr*sh= (void*) (s-(sizeof (structsdshdr)

Returnsh- > len

}

After reading the above, have you mastered how to implement string types in Redis? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report