Redis Source Analysis (iii): Redis data type string

Source: Internet
Author: User
Tags data structures redis

SDS (Simple dynamic string) is the Redis-bottom string representation, which has the function of string-object and substitution char*. Value types in a database include strings, hash tables, lists, collections, and ordered collections, but key types are always strings.

typedef char *SDS;
struct SDSHDR {
//buf occupied length
int len;
BUF remaining available length
int free;
Where the string data is actually stored
char buf[];

Redis constructs its own type system, all keys, values, and parameters are of type, expressed in the Redisobject structure in Redis:

* * Redis object
/
typedef struct REDISOBJECT {
//type
unsigned type:4;
Aligning the
unsigned notused:2;
Encoding method
unsigned encoding:4;
LRU time (relative to Server.lruclock)
unsigned lru:22;
Reference count
int refcount;
The value to point to the object is
void *ptr;
} robj;


Each specific value of the string type is described by a redisobject, which can represent a string or an integer, using Redis_encoding_int and Redis_encoding_raw respectively Two encodings (Redis do not directly store data structures but are converted into memory-mapped models using encoding). The former is used to hold the long type data, which is used to hold the Sds/char*,long long,long double,double type data. String type is basically through the SDS data structure of the operation function to achieve, here just a simple example, nothing else to say.

 * * Append string after specified string and indicate increased memory space * *
 SDS
Sdscatlen (SDS s, const void *t, size_t len) {
	//statement string sh
    struct SDSHDR *sh;
	Gets the length of the parameter s
    size_t Curlen = Sdslen (s);
	Add the memory space
    s = sdsmakeroomfor (S,len) to S;
	The unassigned success returns null
    if (s = = null) return null;
	Causes SH to point to the base address of the s after the reallocation of memory
    sh = (void*) (s (sizeof (struct SDSHDR));
	Copy T to the end of S
    memcpy (S+curlen, T, Len);
	Set the length variable of sh and the remaining available length
    Sh->len = Curlen+len;
    Sh->free = sh->free-len;
	At the end, add the end character
    s[curlen+len] = ';
    return s;
}


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.