Redis Source Code Analysis (28)---object to create and release Redisobject objects

Source: Internet
Author: User

Today's study is more efficient. The Rio analysis over, learning another way between redisobject files, just want to say redisobject some build and convert. are very similar to each other. Lists the long list of APIs inside:

/*------------API---------------------*/robj *createobject (int type, void *ptr)/* Initial Create RobJ object method, followed by creation method similar to this */robj * Createstringobject (char *ptr, size_t len) robj *createstringobjectfromlonglong (Long Long value) RobJ * Createstringobjectfromlongdouble (long Double value) robj *dupstringobject (robj *o) robj *createlistobject (void) RobJ * Createziplistobject (void) robj *createsetobject (void) robj *createintsetobject (void) robj *createhashobject (void) RobJ *createzsetobject (void) robj *createzsetziplistobject (void) void Freestringobject (RobJ *o)/* Specific object in free obj, Here free is r->ptr */void freelistobject (robj *o) void Freesetobject (RobJ *o) void Freezsetobject (RobJ *o) void Freehashobject (RobJ *o)/* Release Hashobject There are 2 forms, one is O-ptr Dictionary object, there are 1 things to compress table O->ptr */void incrrefcount (robj *o)/* The RobJ object increases or decreases the reference count, increments the value of RefCount in RobJ */void decrrefcount (robj *o)/* decrements the reference count in RobJ. Referred to after 0. Release Object */void decrrefcountvoid (void *o) RobJ *resetrefcount (robj *obj) int Checktype (redisclient *c, robj *o, int type)/* Check R Whether the type of obj is of the given type */int Isobjectrepresentableaslonglong (RobJ *o, Long long *llval) robj *tryobjectencoding (robj *o)/* Encode a robj in the frontal character object, Mainly to save space */robj *getdecodedobject (robj *o)/* Get decoded RobJ */int comparestringobjectswithflags (robj *a, robj *b, int flags) I NT Comparestringobjects (robj *a, robj *b) int collatestringobjects (RobJ *a, robj *b) int equalstringobjects (RobJ *a, RobJ *b ) size_t Stringobjectlen (robj *o) int getdoublefromobject (RobJ *o, double *target)/* Gets a double value from RobJ */int Getdoublefromobjectorreply (redisclient *c, RobJ *o, double *target, const char *msg) int Getlongdoublefromobject (RobJ *o, Long double *target) int getlongdoublefromobjectorreply (redisclient *c, RobJ *o, long double *target, const char *msg) int g Etlonglongfromobject (RobJ *o, Long long *target) int getlonglongfromobjectorreply (redisclient *c, RobJ *o, Long long *targ ET, const char *msg) int getlongfromobjectorreply (redisclient *c, RobJ *o, long *target, const char *msg) Char *strencoding ( int encoding) unsigned long estimateobjectidletIME (robj *o) robj *objectcommandlookup (redisclient *c, RobJ *key)/* obj lookup command, */robj *objectcommandlookuporreply ( Redisclient *c, RobJ *key, robj *reply) void Objectcommand (Redisclient *c)
Look back from the past. The first one to create obj:

/* Initial Create RobJ object method */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;    /* Set The LRU to the current lruclock (minutes resolution). */    O->LRU = Server.lruclock;    return o;}
There is a free method that must be released to create it:

/* Specific object in */void freestringobject (RobJ *o) {    if (o->encoding = Redis_encoding_raw) {        sdsfree (O- >PTR);}    }
The free method has very many derivative methods. See what kind of space you want to release, be able to, set,dict,ziplist and so on. There are some of the following types:

Switch (o->type) {case        redis_string:freestringobject (o);        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;        }
This article focuses on the relevant methods of reference counting, which are controlled by Robj->refcount values:

/* RobJ Object Increase or decrease reference count, increment value of refcount in RobJ */void incrrefcount (robj *o) {//Increment robj value RefCount    ;}
Adding a reference count is a line of code, but decrements the word. We can also guess that when the reference count becomes 0, it means that no one is using it, and that it frees up space;

/* Decrements the reference count in RobJ, referred to after 0. Releasing the object */void Decrrefcount (RobJ *o) {//Assuming the previous reference count has been <=0, indicating an exception occurred    if (o->refcount <= 0) redispanic (" Decrrefcount against RefCount <= 0 ");    if (O->refcount = = 1) {    //assuming that the reference count before is 1 and then decremented once. A        switch (o->type) {case        redis_string:freestringobject (o)        can be released if the object is referenced in exactly the same context; 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 {    //other case of reference count for >1, it is only necessary to        o->refcount--;}} by the regular descending reference count.    
The standard reference counting method controls the management of memory. (It is a reminder that the life cycle management of objects in the JVM is rooted in the search method.) is not a reference count). Another implementation of the encoding method in RobJ is also defined in this file:

/* Try to encode a string object with order to save space *//* encode a robj in the frontal character objects.    The main purpose is to save space */robj *tryobjectencoding (RobJ *o) {Long value;    SDS s = o->ptr;    size_t Len; if (o->encoding! = Redis_encoding_raw)//Assuming the robj has been encoded, return directly to O; /* ALREADY encoded */* IT's not safe to encode gkfx objects:shared objects can be gkfx * everywhere in the " Object Space "of Redis. Encoded objects can only * appear as "values" (and not, for instance, as keys)/* * If the reference count of RobJ exceeds 1 references.    is unsafe to encode obj, because the object is shared */if (O->refcount > 1) return o;    /* Currently we try to encode only strings */redisassertwithinfo (Null,o,o->type = = redis_string);    /* Check If we can represent this string as a long integer */len = Sdslen (s); if (Len > | |!string2l (s,len,&value)) {/* We can ' t encode the object ... * the last Try, and at least optimize the SDS string inside * The string object to require littleSpace, in case there * was more than 10% of free space at the end of the SDS string. * * We do this for larger strings, using the arbitrary value * of the bytes.         This code was backported from the unstable branch * where this is performed when the object is too large * Encoded as EMBSTR. */if (len > && o->encoding = = Redis_encoding_raw && Sdsavail (s) ; LEN/10) {//Call Sdsremovefreespace to remove a space from a string in 0->ptr o->ptr = Sdsremovefreespace (o->ptr)        ; }/* Return the original object.    */return o; }    .....
Is the space occupied by removing spaces from the string.

There is also a getdecodeobject () to the corresponding:

/* Get A decoded version of an encoded object (returned as a new object). * If The object is already raw-encoded just increment the ref count. *//* gets the decoded robj */robj *getdecodedobject (robj *o) {    robj *dec;    if (o->encoding = = Redis_encoding_raw) {    //Assume there is no encoding method. The reference count is added directly. and return to        Incrrefcount (o);        return o;    }    if (O->type = = redis_string && o->encoding = = redis_encoding_int) {        char buf[32];//is assumed to be a STRING. Type and is Encoding_int mode, first step conversion        ll2string (buf,32, (long) o->ptr);        Dec = Createstringobject (Buf,strlen (BUF));        return Dec;    } else {        redispanic ("Unknown encoding Type");}    }
The above is a simple analysis of redisobject.

Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Redis Source Code Analysis (28)---object to create and release Redisobject objects

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.