redis t_hash

6810 ワード

1.作用
t_hash.c,z_list,z_set.c,t_string.c,t_zset.c,これらのファイルの機能は実際にはそれほど悪くなく,ClientとServer間のコマンド処理を実現するための操作クラスであり,robjの形式でdict,ziplistなどをrobjに格納し,各変換を行い,コマンド操作を実現する.構造体の元の複雑な構造を避け,構造体をカプセル化した操作クラスに相当する.
t_hash長さが64未満の場合は構造体ziplistを使用し、そうでない場合はdictクラスを使用します.
/*          */  
void hashTypeTryConversion(robj *o, robj **argv, int start, int end) /*  hashType ziplist ,                  ziplist    ,           */  
void hashTypeTryObjectEncoding(robj *subject, robj **o1, robj **o2) /*  robj             ,        */  
int hashTypeGetFromZiplist(robj *o, robj *field,unsigned char **vstr,unsigned int *vlen,long long *vll) /*   ziplist                */  
int hashTypeGetFromHashTable(robj *o, robj *field, robj **value) /*             */  
robj *hashTypeGetObject(robj *o, robj *field) /*     key        */  
int hashTypeExists(robj *o, robj *field)   /* hastType            */  
int hashTypeSet(robj *o, robj *field, robj *value) /* hashType    , 2   ,ziplist,   hashtable */  
int hashTypeDelete(robj *o, robj *field)  /* hashType    ,  ziplist     , hashtable      */  
unsigned long hashTypeLength(robj *o)   /* hashType      */  
hashTypeIterator *hashTypeInitIterator(robj *subject)  /*   hashType    */  
void hashTypeReleaseIterator(hashTypeIterator *hi) /*   hashType    */  
int hashTypeNext(hashTypeIterator *hi) /*   hashType           */  
void hashTypeCurrentFromZiplist(hashTypeIterator *hi, int what,unsigned char **vstr,unsigned int *vlen,long long *vll) /*           ,    ziplist      key  , value       */  
void hashTypeCurrentFromHashTable(hashTypeIterator *hi, int what, robj **dst) /*           ,    dict      key  , value       */  
robj *hashTypeCurrentObject(hashTypeIterator *hi, int what) /*           ,    key   */  
robj *hashTypeLookupWriteOrCreate(redisClient *c, robj *key) /*   c     ,  key    ,           */  
void hashTypeConvertZiplist(robj *o, int enc) /*  ziplist    hashtable    */  
void hashTypeConvert(robj *o, int enc) /*       ,   ziplist dict    */

hashTypeの関連操作コマンドクラスは,実は上の方法の結合呼び出しである.
/*        */  
void hsetCommand(redisClient *c)  /*         */  
void hsetnxCommand(redisClient *c) /*              */  
void hmsetCommand(redisClient *c) /*        ,    key,       */  
void hincrbyCommand(redisClient *c) /*      value    */  
void hincrbyfloatCommand(redisClient *c) /*      float  value    */  
static void addHashFieldToReply(redisClient *c, robj *o, robj *field) /*  */  
void hgetCommand(redisClient *c) /*        ,     ,         */  
void hmgetCommand(redisClient *c) /*      key  ,    ,      NULL  */  
void hdelCommand(redisClient *c) /*         */  
void hlenCommand(redisClient *c) /*          */  
static void addHashIteratorCursorToReply(redisClient *c, hashTypeIterator *hi, int what) /*      hashType      */  
void genericHgetallCommand(redisClient *c, int flags) /*            ,    flag   */  
void hkeysCommand(redisClient *c) /*      key    */  
void hvalsCommand(redisClient *c) /*      val    */  
void hgetallCommand(redisClient *c) /*      key;value 2      */  
void hexistsCommand(redisClient *c) /*               */  
void hscanCommand(redisClient *c) /*         */  

2.検索
//  key       
robj *hashTypeGetObject(robj *o, robj *field) {
    robj *value = NULL;

    if (o->encoding == OBJ_ENCODING_ZIPLIST) {
        unsigned char *vstr = NULL;
        unsigned int vlen = UINT_MAX;
        long long vll = LLONG_MAX;

        if (hashTypeGetFromZiplist(o, field, &vstr, &vlen, &vll) == 0) {
            if (vstr) {
                value = createStringObject((char*)vstr, vlen);
            } else {
                value = createStringObjectFromLongLong(vll);
            }
        }
    } else if (o->encoding == OBJ_ENCODING_HT) {
        robj *aux;

        if (hashTypeGetFromHashTable(o, field, &aux) == 0) {
            incrRefCount(aux);
            value = aux;
        }
    } else {
        serverPanic("Unknown hash encoding");
    }
    return value;
}

3.更新
/* Add an element, discard the old if the key already exists.
 * Return 0 on insert and 1 on update.
 * This function will take care of incrementing the reference count of the
 * retained fields and value objects. */
//      ,      ,  key    ,  1updata,0insert
//     ,hash   dict
int hashTypeSet(robj *o, robj *field, robj *value) {
    int update = 0;

    if (o->encoding == OBJ_ENCODING_ZIPLIST) {
        unsigned char *zl, *fptr, *vptr;
        //    
        field = getDecodedObject(field);
        value = getDecodedObject(value);

        zl = o->ptr;
        fptr = ziplistIndex(zl, ZIPLIST_HEAD);
        if (fptr != NULL) {
            fptr = ziplistFind(fptr, field->ptr, sdslen(field->ptr), 1);//   
            if (fptr != NULL) {
                /* Grab pointer to the value (fptr points to the field) */
                vptr = ziplistNext(zl, fptr);
                serverAssert(vptr != NULL);
                update = 1;

                /* Delete value   */
                zl = ziplistDelete(zl, &vptr);

                /* Insert new value   */
                zl = ziplistInsert(zl, vptr, value->ptr, sdslen(value->ptr));
            }
        }

        if (!update) {//          
            /* Push new field/value pair onto the tail of the ziplist */
            zl = ziplistPush(zl, field->ptr, sdslen(field->ptr), ZIPLIST_TAIL);
            zl = ziplistPush(zl, value->ptr, sdslen(value->ptr), ZIPLIST_TAIL);
        }
        o->ptr = zl;
        decrRefCount(field);
        decrRefCount(value);

        /* Check if the ziplist needs to be converted to a hash table */
        if (hashTypeLength(o) > server.hash_max_ziplist_entries)
            hashTypeConvert(o, OBJ_ENCODING_HT);
    } else if (o->encoding == OBJ_ENCODING_HT) {
        if (dictReplace(o->ptr, field, value)) { /* Insert */
            incrRefCount(field);
        } else { /* Update */
            update = 1;
        }
        incrRefCount(value);
    } else {
        serverPanic("Unknown hash encoding");
    }
    return update;
}

4.操作
//hashType        ,              :
void hsetCommand(client *c) {
    int update;
    robj *o;

    if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
    hashTypeTryConversion(o,c->argv,2,3);
    //         
    hashTypeTryObjectEncoding(o,&c->argv[2], &c->argv[3]);
    //  
    update = hashTypeSet(o,c->argv[2],c->argv[3]);
    //       
    addReply(c, update ? shared.czero : shared.cone);
    signalModifiedKey(c->db,c->argv[1]);
    notifyKeyspaceEvent(NOTIFY_HASH,"hset",c->argv[1],c->db->id);
    server.dirty++;
}