In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
In this issue, the editor will bring you about the implementation principle of the Redis source code type. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
What is the implementation principle of Redis source code type?
Redis implements its own string type internally. The implementation details are contained in the sds.c file (sds is Simple Dynamic Strings).
Struct sdshdr {
Long len
Long free
Char buf []
}
Buf: the actual string stored
Len field: the length of the buff is stored. This field makes the operation complexity of Redis fetching the length of a string O (1).
Free field: stores 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.
Create a new Redis string
A new data type called sds is defined in sds.h, which is actually a string pointer:
Typedef char * sds
The function sdsnewslen for creating a new Redis string pointer is defined in sds.c:
Sds sdsnewlen (const void * init, size_t initlen) {
Struct sdshdr * sh
Sh = zmalloc (sizeof (struct sdshdr) + initlen+1)
# ifdef SDS_ABORT_ON_OOM
If (sh = = NULL) sdsOomAbort ()
# else
If (sh = = NULL) return NULL
# endif
Sh- > len = initlen
Sh- > free = 0
If (initlen) {
If (init) memcpy (sh- > buf, init, initlen)
Else memset (sh- > buf,0,initlen)
}
Sh- > buf [initlen] =''
Return (char*) sh- > buf
}
What is the implementation principle of Redis source code type?
As mentioned above, the Redis string is of type struct sdshdr. 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 struct sdshdr and allocates space for both the len,free and buf fields. The code for allocating space is as follows:
Sh = zmalloc (sizeof (struct sdshdr) + initlen+1); / / initlen is length of init argument.
When sdsnewlen returns successfully, the resulting Redis string looks something like this:
-
| | 5 | 0 | redis |
-
^ ^
Sh sh- > 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 struct sdshdr. (note: declaring buf as char buf [] is a common programming technique for variable-length structures.)
Let's take a look at how the sdslen function works:
Size_t sdslen (const sds s) {
Struct sdshdr * sh = (void*) (s-(sizeof (struct sdshdr)
Return sh- > len
}
Once you understand this tip, you should be able to understand everything in the entire sds.c file.
The implementation of the Redis string is hidden behind the interface, which accepts only string parameters. Users of Redis strings don't need to care about how it is implemented, just think of it as a string pointer.
The above is the implementation principle of the Redis source code type shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.