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;
}