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