宏HASH_INSERT

 

调用 方法 

HASH_INSERT(lock_t, hash, lock_sys->rec_hash,lock_rec_fold(space, page_no), lock);
/*******************************************************************//**
Inserts a struct to a hash table. */

#define HASH_INSERT(TYPE, NAME, TABLE, FOLD, DATA)\
do {\
    hash_cell_t*    cell3333;\ //hash_cell_t* 定义
    TYPE*        struct3333;\
\
    HASH_ASSERT_OWNED(TABLE, FOLD)\
\
    (DATA)->NAME = NULL;\
\
    cell3333 = hash_get_nth_cell(TABLE, hash_calc_hash(FOLD, TABLE));\ //函数定义
\
    if (cell3333->node == NULL) {\
        cell3333->node = DATA;\
    } else {\
        struct3333 = (TYPE*) cell3333->node;\ //此时struct3333的类型是lock_t
\
        while (struct3333->NAME != NULL) {\  //NAME 为 hash_node_t  hash; typedef void* hash_node_t;
\       struct3333 = (TYPE*) struct3333->NAME;\ 
    }\ \
    struct3333
->NAME = DATA;\
}\
}
while (0)

这里好绕

对于下一个元素, 都习惯于使用struct  结构体名称 *next

/**************************************************************//**
Calculates the hash value from a folded value.
@return    hashed value */
UNIV_INLINE
ulint
hash_calc_hash(
/*===========*/
    ulint        fold,    /*!< in: folded value */
    hash_table_t*    table)    /*!< in: hash table */
{
    ut_ad(table);
    ut_ad(table->magic_n == HASH_TABLE_MAGIC_N);
    return(ut_hash_ulint(fold, table->n_cells));
}

ulint
ut_hash_ulint(
/*==========*/
    ulint     key,        /*!< in: value to be hashed */
    ulint     table_size)    /*!< in: hash table size */
{
    ut_ad(table_size);
    key = key ^ UT_HASH_RANDOM_MASK2;

    return(key % table_size);
}

 

 

HASH_INSERT(lock_t, hash, lock_sys->rec_hash,
            lock_rec_fold(space, page_no), lock);


/*********************************************************************//**
Calculates the fold value of a page file address: used in inserting or
searching for a lock in the hash table.
@return    folded value */
UNIV_INLINE
ulint
lock_rec_fold(
/*==========*/
    ulint    space,    /*!< in: space */
    ulint    page_no)/*!< in: page number */
{
    return(ut_fold_ulint_pair(space, page_no));
}


/*************************************************************//**
Folds a pair of ulints.
@return    folded value */
UNIV_INLINE
ulint
ut_fold_ulint_pair(
/*===============*/
    ulint    n1,    /*!< in: ulint */
    ulint    n2)    /*!< in: ulint */
{
    return(((((n1 ^ n2 ^ UT_HASH_RANDOM_MASK2) << 8) + n1)
        ^ UT_HASH_RANDOM_MASK) + n2);
}

 

posted @ 2015-11-04 17:14  taek  阅读(391)  评论(0编辑  收藏  举报