C programming: hashtable insertion/search

Posted by Ricardo Campos on Stack Overflow See other posts from Stack Overflow or by Ricardo Campos
Published on 2013-11-02T18:02:10Z Indexed on 2013/11/03 3:54 UTC
Read the original article Hit count: 187

Filed under:
|

Hello i have a problem with my hash table its implemented like this:

#define HT_SIZE 10 
typedef struct _list_t_ {
    char key[20];
    char string[20];
    char prevValue[20];
    struct _list_t_ *next;
} list_t;

typedef struct _hash_table_t_ {
    int size;       /* the size of the table */
    list_t ***table; /* first */
    sem_t lock;
} hash_table_t;

I have a Linked list with 3 pointers because i want a hash table with several partitions (shards), here is my initialization of my Hash table:

hash_table_t *create_hash_table(int NUM_SERVER_THREADS, int num_shards){
    hash_table_t *new_table;    
    int j,i;
    if (HT_SIZE<1) return NULL; /* invalid size for table */
    /* Attempt to allocate memory for the hashtable structure */
    new_table = (hash_table_t*)malloc(sizeof(hash_table_t)*HT_SIZE);
    /* Attempt to allocate memory for the table itself */
    new_table->table = (list_t ***)calloc(1,sizeof(list_t **));
    /* Initialize the elements of the table */
    for(j=0; j<num_shards; j++){
        new_table->table[j] = (list_t **)calloc(1,sizeof(list_t *));
        for(i=0; i<HT_SIZE; i++){
            new_table->table[j][i] = (list_t *)calloc(1,sizeof(list_t ));
        }
    }
    /* Set the table's size */
    new_table->size = HT_SIZE;
    sem_init(&new_table->lock, 0, 1);
    return new_table;
}

Here is my search function to search in the hash table

list_t *lookup_string(hash_table_t *hashtable, char *key, int shardId){

    list_t *list ;

    int hashval = hash(key);
    /* Go to the correct list based on the hash value and see if key is
     * in the list.  If it is, return return a pointer to the list element.
     * If it isn't, the item isn't in the table, so return NULL.
     */
    sem_wait(&hashtable->lock);

    for(list = hashtable->table[shardId][hashval]; list != NULL; list =list->next) {
        if (strcmp(key, list->key) == 0){
                sem_post(&hashtable->lock);
                return list;
        }
    }
    sem_post(&hashtable->lock);

    return NULL;
}

And my insert function:

char *add_string(hash_table_t *hashtable, char *str,char *key, int shardId){
    list_t *new_list;
    list_t *current_list;
    unsigned int hashval = hash(key);
    /*printf("|%d|%d|%s|\n",hashval,shardId,key);*/
    /* Lock for concurrency */
    sem_wait(&hashtable->lock);
    /* Attempt to allocate memory for list */
    new_list = (list_t*)malloc(sizeof(list_t));

    /* Does item already exist? */
    sem_post(&hashtable->lock);
    current_list = lookup_string(hashtable, key,shardId);
    sem_wait(&hashtable->lock);
        /* item already exists, don't insert it again. */
    if (current_list != NULL){
        strcpy(new_list->prevValue,current_list->string);
        strcpy(new_list->string,str);
        strcpy(new_list->key,key);
        new_list->next = hashtable->table[shardId][hashval];
        hashtable->table[shardId][hashval] = new_list;
        sem_post(&hashtable->lock);
        return new_list->prevValue;
    }
    /* Insert into list */
    strcpy(new_list->string,str);
    strcpy(new_list->key,key);
    new_list->next = hashtable->table[shardId][hashval];
    hashtable->table[shardId][hashval] = new_list;
    /* Unlock */
    sem_post(&hashtable->lock);
    return new_list->prevValue;
}

My main class runs some of tests by executing the insertion / reading / delete from the elements of the hash table the problem is when i have more than 4 partitions/shards the tests stop at the first reading element saying it returned the wrong value NULL on the search function, when its less than 4 it runs perfectly well and passes all the tests.

You can see my main.c in here if you want to give a look:

http://hostcode.sourceforge.net/view/1105

My complete Hash table code:

http://hostcode.sourceforge.net/view/1103

And other functions where hash table code is executed:

.c file http://hostcode.sourceforge.net/view/1104

.h file http://hostcode.sourceforge.net/view/1106

Thank for you time, i appreciate any help you can give to me this is a college important project that I'm trying to solve and I'm stuck here for 2 days.

© Stack Overflow or respective owner

Related posts about c

    Related posts about hashtable