Пример #1
0
static p_link_node
hash_find_node(hash_table *table, uint64_t key)
{
    hash_node   *hn;
    link_list   *l  = get_link_list(table, key);
    p_link_node  ln  = link_list_first(l);

    table->total_visit++;

    while (ln) {

        hn = (hash_node *)ln->data;
        table->total_key_compared++;
        if (hn->key == key) {
            hn->access_time = time(0);
            hn->visit_cnt++;
            /* Put the lastest item to the head of the linked list */
            (void)link_list_remove(l, ln);
            link_list_push(l, ln);
            return ln;
        }
        ln = link_list_get_next(l, ln);
    }

    return NULL;
}
Пример #2
0
bool
hash_add(hash_table *table, tc_pool_t *pool, uint64_t key, void *data)
{
    hash_node   *hn, *tmp;
    link_list   *l;
    p_link_node  ln;

    ln = hash_find_node(table, key);
    if (ln == NULL) {
        tmp = hash_node_malloc(pool, key, data);
        if (tmp != NULL) {
            l   = get_link_list(table, key);
            ln  = link_node_malloc(pool, tmp);
            if (ln != NULL) {
                link_list_push(l, ln);
                table->total++;
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    } else {
        hn = (hash_node *) ln->data;
        hn->data = data;
        return false;
    }
}
Пример #3
0
static p_link_node
hash_find_node(hash_table *table, uint64_t key)
{
    bool         first = true;
    hash_node   *hn;
    link_list   *l  = get_link_list(table, key);
    p_link_node  ln = link_list_first(l);

    while (ln) {

        hn = (hash_node *) ln->data;
        if (hn->key == key) {
            if (!first) {
                /* put the lastest item to the head of the linked list */
                link_list_remove(l, ln);
                link_list_push(l, ln);
            }
            return ln;
        }
        ln = link_list_get_next(l, ln);
        first = false;
    }

    return NULL;
}
Пример #4
0
bool hash_del(hash_table *table, uint64_t key)
{
    link_list   *l = get_link_list(table, key); 
    p_link_node ln = hash_find_node(table, key);
    if(ln != NULL){
        table->total--;
        link_list_remove(l, ln);
        link_node_internal_free(ln);
        free(ln);
        return true;
    }else{
        return false;
    }
}
Пример #5
0
bool
hash_del(hash_table *table, tc_pool_t *pool, uint64_t key)
{
    link_list  *l  = get_link_list(table, key);
    p_link_node ln = hash_find_node(table, key);

    if (ln != NULL) {
        table->total--;
        link_list_remove(l, ln);
        tc_pfree(pool, ln->data);
        tc_pfree(pool, ln);
        return true;
    } else {

        return false;
    }
}
static void 
remove_obsolete_resources(int is_full) 
{
    time_t      thresh_access_tme;
    uint32_t    i, cnt = 0;
    link_list  *l;
    hash_node  *hn;
    p_link_node ln, next_ln;

    if (ctx.table == NULL || ctx.table->total == 0) {
        return;
    }

    if (is_full) {
        thresh_access_tme = tc_time() + 1;
    } else {
        thresh_access_tme = tc_time() - MAX_IDLE_TIME;
    }

    for (i = 0; i < ctx.table->size; i ++) {
        l  = get_link_list(ctx.table, i);
        if (l->size > 0) {
            ln = link_list_first(l);
            while (ln) {
                hn = (hash_node *) ln->data;
                next_ln = link_list_get_next(l, ln);
                if (hn->access_time < thresh_access_tme) {
                    release_resources(hn->key);
                }
                ln = next_ln;
            }
            
            cnt += l->size;

            if (ctx.table->total == cnt) {
                break;
            }
        }
    }
}