int insert(Hash h, char *key, char *type, int lineno, int colno, int value) { int index = index_access(key); HList l = h->head[index]; hlist_add_back(l, hnode_new(key, type, lineno, colno, value)); return index; }
void hashtable_put( HashTable * h_table, void * key, size_t key_len, void * value, size_t value_len, bool thr_safe) { pthread_mutex_t* ht_mutex = NULL; if(thr_safe){ //slog(DEBUG,LOG_UTIL,"Before mutex lock in put"); ht_mutex = h_table->ht_mutex; //try to replace existing key's value if possible pthread_mutex_lock(ht_mutex); } int res = _hashtable_replace(h_table, key, key_len, value, value_len); if (res == 0) { // resize if needed. double load = ((double) h_table->count) / ((double) h_table->size); if (load > h_table->max_load) { _hashtable_resize(h_table); } //code to add key and value in O(1) time. uint32_t hash_val = h_table->hash(key, key_len); uint32_t index = hash_val % h_table->size; HNode * prev_head = h_table->table[index]; HNode * new_head = hnode_new( key, key_len, value, value_len, h_table->deleteKey, h_table->deleteValue ); new_head->next = prev_head; h_table->table[index] = new_head; h_table->count = h_table->count + 1; } if(thr_safe){ pthread_mutex_unlock(ht_mutex); //slog(DEBUG,LOG_UTIL,"After mutex unlock in put"); } }