1.createObject
robj * createObject ( int type, void * ptr) {
robj * o = zmalloc ( sizeof ( * o) ) ;
o-> type = type;
o-> encoding = REDIS_ENCODING_RAW;
o-> ptr = ptr;
o-> refcount = 1 ;
o-> lru = LRU_CLOCK ( ) ;
return o;
}
2.createRawStringObject
robj * createRawStringObject ( char * ptr, size_t len) {
return createObject ( REDIS_STRING, sdsnewlen ( ptr, len) ) ;
}
3.createEmbeedStringObject
robj * createEmbeddedStringObject ( char * ptr, size_t len) {
robj * o = zmalloc ( sizeof ( robj) + sizeof ( struct sdshdr ) + len+ 1 ) ;
struct sdshdr * sh = ( void * ) ( o+ 1 ) ;
o-> type = REDIS_STRING;
o-> encoding = REDIS_ENCODING_EMBSTR;
o-> ptr = sh+ 1 ;
o-> refcount = 1 ;
o-> lru = LRU_CLOCK ( ) ;
sh-> len = len;
sh-> free = 0 ;
if ( ptr) {
memcpy ( sh-> buf, ptr, len) ;
sh-> buf[ len] = '\0' ;
} else {
memset ( sh-> buf, 0 , len+ 1 ) ;
}
return o;
}
4.createStringObject
robj * createStringObject ( char * ptr, size_t len) {
if ( len <= REDIS_ENCODING_EMBSTR_SIZE_LIMIT)
return createEmbeddedStringObject ( ptr, len) ;
else
return createRawStringObject ( ptr, len) ;
}
5.createStringObjectFromLongLong
robj * createStringObjectFromLongLong ( long long value) {
robj * o;
if ( value >= 0 && value < REDIS_SHARED_INTEGERS) {
incrRefCount ( shared. integers[ value] ) ;
o = shared. integers[ value] ;
} else {
if ( value >= LONG_MIN && value <= LONG_MAX) {
o = createObject ( REDIS_STRING, NULL ) ;
o-> encoding = REDIS_ENCODING_INT;
o-> ptr = ( void * ) ( ( long ) value) ;
} else {
o = createObject ( REDIS_STRING, sdsfromlonglong ( value) ) ;
}
}
return o;
}
6.createStringObjectFromLongDouble
robj * createStringObjectFromLongDouble ( long double value) {
char buf[ 256 ] ;
int len;
len = snprintf ( buf, sizeof ( buf) , "%.17Lf" , value) ;
if ( strchr ( buf, '.' ) != NULL ) {
char * p = buf+ len- 1 ;
while ( * p == '0' ) {
p-- ;
len-- ;
}
if ( * p == '.' ) len-- ;
}
return createStringObject ( buf, len) ;
}
7.dupstringObject
robj * dupStringObject ( robj * o) {
robj * d;
redisAssert ( o-> type == REDIS_STRING) ;
switch ( o-> encoding) {
case REDIS_ENCODING_RAW:
return createRawStringObject ( o-> ptr, sdslen ( o-> ptr) ) ;
case REDIS_ENCODING_EMBSTR:
return createEmbeddedStringObject ( o-> ptr, sdslen ( o-> ptr) ) ;
case REDIS_ENCODING_INT:
d = createObject ( REDIS_STRING, NULL ) ;
d-> encoding = REDIS_ENCODING_INT;
d-> ptr = o-> ptr;
return d;
default :
redisPanic ( "Wrong encoding." ) ;
break ;
}
}
8.createListObject
robj * createListObject ( void ) {
list * l = listCreate ( ) ;
robj * o = createObject ( REDIS_LIST, l) ;
listSetFreeMethod ( l, decrRefCountVoid) ;
o-> encoding = REDIS_ENCODING_LINKEDLIST;
return o;
}
9.createZiplistObject
robj * createZiplistObject ( void ) {
unsigned char * zl = ziplistNew ( ) ;
robj * o = createObject ( REDIS_LIST, zl) ;
o-> encoding = REDIS_ENCODING_ZIPLIST;
return o;
}
10.createSetObject
robj * createSetObject ( void ) {
dict * d = dictCreate ( & setDictType, NULL ) ;
robj * o = createObject ( REDIS_SET, d) ;
o-> encoding = REDIS_ENCODING_HT;
return o;
}
11.createIntsetObject
robj * createIntsetObject ( void ) {
intset * is = intsetNew ( ) ;
robj * o = createObject ( REDIS_SET, is) ;
o-> encoding = REDIS_ENCODING_INTSET;
return o;
}
12.createHashObject
robj * createHashObject ( void ) {
unsigned char * zl = ziplistNew ( ) ;
robj * o = createObject ( REDIS_HASH, zl) ;
o-> encoding = REDIS_ENCODING_ZIPLIST;
return o;
}
13.createZsetObject
robj * createZsetObject ( void ) {
zset * zs = zmalloc ( sizeof ( * zs) ) ;
robj * o;
zs-> dict = dictCreate ( & zsetDictType, NULL ) ;
zs-> zsl = zslCreate ( ) ;
o = createObject ( REDIS_ZSET, zs) ;
o-> encoding = REDIS_ENCODING_SKIPLIST;
return o;
}
14.createZestziplistObject
robj * createZsetZiplistObject ( void ) {
unsigned char * zl = ziplistNew ( ) ;
robj * o = createObject ( REDIS_ZSET, zl) ;
o-> encoding = REDIS_ENCODING_ZIPLIST;
return o;
}
15.freeStringObject
void freeStringObject ( robj * o) {
if ( o-> encoding == REDIS_ENCODING_RAW) {
sdsfree ( o-> ptr) ;
}
}
16.freeListObject
void freeListObject ( robj * o) {
switch ( o-> encoding) {
case REDIS_ENCODING_LINKEDLIST:
listRelease ( ( list* ) o-> ptr) ;
break ;
case REDIS_ENCODING_ZIPLIST:
zfree ( o-> ptr) ;
break ;
default :
redisPanic ( "Unknown list encoding type" ) ;
}
}
17.freeSetObject
void freeSetObject ( robj * o) {
switch ( o-> encoding) {
case REDIS_ENCODING_HT:
dictRelease ( ( dict* ) o-> ptr) ;
break ;
case REDIS_ENCODING_INTSET:
zfree ( o-> ptr) ;
break ;
default :
redisPanic ( "Unknown set encoding type" ) ;
}
}
18.freeZetObject
void freeZsetObject ( robj * o) {
zset * zs;
switch ( o-> encoding) {
case REDIS_ENCODING_SKIPLIST:
zs = o-> ptr;
dictRelease ( zs-> dict) ;
zslFree ( zs-> zsl) ;
zfree ( zs) ;
break ;
case REDIS_ENCODING_ZIPLIST:
zfree ( o-> ptr) ;
break ;
default :
redisPanic ( "Unknown sorted set encoding" ) ;
}
}
19.decrRefCount
void decrRefCount ( robj * o) {
if ( o-> refcount <= 0 ) redisPanic ( "decrRefCount against refcount <= 0" ) ;
if ( o-> refcount == 1 ) {
switch ( o-> type) {
case REDIS_STRING: freeStringObject ( o) ; break ;
case REDIS_LIST: freeListObject ( o) ; break ;
case REDIS_SET: freeSetObject ( o) ; break ;
case REDIS_ZSET: freeZsetObject ( o) ; break ;
case REDIS_HASH: freeHashObject ( o) ; break ;
default : redisPanic ( "Unknown object type" ) ; break ;
}
zfree ( o) ;
} else {
o-> refcount-- ;
}
}
20.compareStringObjectWithFlags
int compareStringObjectsWithFlags ( robj * a, robj * b, int flags) {
redisAssertWithInfo ( NULL , a, a-> type == REDIS_STRING && b-> type == REDIS_STRING) ;
char bufa[ 128 ] , bufb[ 128 ] , * astr, * bstr;
size_t alen, blen, minlen;
if ( a == b) return 0 ;
if ( sdsEncodedObject ( a) ) {
astr = a-> ptr;
alen = sdslen ( astr) ;
} else {
alen = ll2string ( bufa, sizeof ( bufa) , ( long ) a-> ptr) ;
astr = bufa;
}
if ( sdsEncodedObject ( b) ) {
bstr = b-> ptr;
blen = sdslen ( bstr) ;
} else {
blen = ll2string ( bufb, sizeof ( bufb) , ( long ) b-> ptr) ;
bstr = bufb;
}
if ( flags & REDIS_COMPARE_COLL) {
return strcoll ( astr, bstr) ;
} else {
int cmp;
minlen = ( alen < blen) ? alen : blen;
cmp = memcmp ( astr, bstr, minlen) ;
if ( cmp == 0 ) return alen- blen;
return cmp;
}
}
21.equalStringObjects
int equalStringObjects ( robj * a, robj * b) {
if ( a-> encoding == REDIS_ENCODING_INT &&
b-> encoding == REDIS_ENCODING_INT) {
return a-> ptr == b-> ptr;
} else {
return compareStringObjects ( a, b) == 0 ;
}
}
22.getDoubleFromObject
int getDoubleFromObject ( robj * o, double * target) {
double value;
char * eptr;
if ( o == NULL ) {
value = 0 ;
} else {
redisAssertWithInfo ( NULL , o, o-> type == REDIS_STRING) ;
if ( sdsEncodedObject ( o) ) {
errno = 0 ;
value = strtod ( o-> ptr, & eptr) ;
if ( isspace ( ( ( char * ) o-> ptr) [ 0 ] ) ||
eptr[ 0 ] != '\0' ||
( errno == ERANGE &&
( value == HUGE_VAL || value == - HUGE_VAL || value == 0 ) ) ||
errno == EINVAL ||
isnan ( value) )
return REDIS_ERR;
} else if ( o-> encoding == REDIS_ENCODING_INT) {
value = ( long ) o-> ptr;
} else {
redisPanic ( "Unknown string encoding" ) ;
}
}
* target = value;
return REDIS_OK;
}