Redis Source code Parsing (iii): Redis data type string

Source: Internet
Author: User

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

typedef char *SDS;STRUCT SDSHDR {///buf occupied length int len;//buf remaining usable length int free;//where the string data is actually stored char buf[];};

Redis builds its own type system, with all keys, values, and parameters typed, represented in Redis with the Redisobject structure:

/** Redis Object */typedef struct Redisobject {//type unsigned type:4;//align bits unsigned notused:2;//encoding method unsigned encoding:4;//LRU Time (relative to Server.lruclock) unsigned lru:22;//reference count int refcount;//point to the value of the object void *ptr;} RobJ;


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

/* Appends a string to the specified string and indicates an increased memory space */sds Sdscatlen (SDS s, const void *t, size_t len) {//Declaration string sh    struct SDSHDR *sh;//Gets the length of the parameter s c1/>size_t Curlen = Sdslen (s);//For s new memory space    s = sdsmakeroomfor (S,len);//unassigned succeeded returns null    if (s = = null) return null;// Make SH point to the base address of S (void*) (s (sizeof (struct SDSHDR))) After the memory is reassigned, and    //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 of the add ' \ n ' end character    s[curlen+len] = ' + ';    return s;}


Redis Source code Parsing (iii): Redis data type string

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.