Пример #1
0
// Delete an object from the cache, if it's still there
void cache_delete(cache_t cache, key_type key){
        item_t item_ptr = hash_table_find_item(cache->hash_table, key);
        if (item_ptr!=NULL){
            linked_list_delete(cache->linked_list, item_ptr->node_ptr);
            hash_table_delete(cache->hash_table, item_ptr);
        }
}
Пример #2
0
// Add a <key, value> pair to the cache.
// If key already exists, it will overwrite the old value.
// If maxmem capacity is exceeded, sufficient values will be removed
// from the cache to accomodate the new value.
void cache_set(cache_t cache, key_type key, val_type val, uint32_t val_size){
    
    item_t item_to_set = hash_table_find_item(cache->hash_table, key);
    // If key already exists, it will overwrite the old value.  
    if (item_to_set!=NULL){
        item_to_set->val = val;
        return;
    }

    //If maxmem capacity is exceeded, sufficient values will be removed
    if (cache_space_used(cache)+val_size >= cache->maxmem){
        cache_evict(cache);
    }

    //printf("\ncurrent_size:%u,hash_table_buckets_num:%u\n",cache->hash_table->current_size+1,(uint32_t) ((double) cache->hash_table->buckets_num*LOAD_FACTOR));
    //Check if we need to resize hash table
    if (cache->hash_table->current_size+1 > (uint32_t) ((double) cache->hash_table->buckets_num*LOAD_FACTOR)){
        resize_hash_table(cache->hash_table);

    }


    item_t item = hash_table_set(cache->hash_table, key, val, val_size);   
    node_t node = linked_list_set(cache->linked_list);

    // //connect the item in hash_table with its corresponding node in linked list
    connect_node_and_item(item,node);
    
}
Пример #3
0
// Retrieve the value associated with key in the cache, or NULL if not found. It will move the item with key to the front of the linked_list
val_type cache_get(cache_t cache, key_type key, uint32_t * val_size){
    item_t item_ptr = hash_table_find_item(cache->hash_table, key);
    if (item_ptr!=NULL){
        //move the newly used node to the front of the linked_list
        linked_list_top(cache->linked_list,item_ptr->node_ptr);
        *val_size = item_ptr->val_size;
        return item_ptr->val;
    }
    //If key is not found, assign 0 to val_size
    *val_size = 0;
	return NULL;
    
}
Пример #4
0
/**
 * Look up a key in the map and return the associated data pointer.
 */
const void *
util_keymap_lookup(const struct keymap *map, const void *key)
{
   unsigned key_hash;
   struct keymap_item *item;

   assert(map);
   if (!map)
      return NULL;

   key_hash = hash(key, map->key_size);

   item = hash_table_find_item(map, key, key_hash);
   if (!item)
      return NULL;
   
   return item->value;
}
Пример #5
0
/**
 * Insert a new key + data pointer into the table.
 * Note: we create a copy of the key, but not the data!
 * If the key is already present in the table, replace the existing
 * entry (calling the delete callback on the previous entry).
 * If the maximum capacity of the map is reached an old entry
 * will be deleted (the delete callback will be called).
 */
boolean
util_keymap_insert(struct keymap *map, const void *key,
                   const void *data, void *user)
{
   unsigned key_hash;
   struct keymap_item *item;
   struct cso_hash_iter iter;

   assert(map);
   if (!map)
      return FALSE;

   key_hash = hash(key, map->key_size);

   item = hash_table_find_item(map, key, key_hash);
   if (item) {
      /* call delete callback for old entry/item */
      map->delete_func(map, item->key, item->value, user);
      item->value = (void *) data;
      return TRUE;
   }
   
   item = MALLOC_STRUCT(keymap_item);
   if (!item)
      return FALSE;

   item->key = mem_dup(key, map->key_size);
   item->value = (void *) data;
   
   iter = cso_hash_insert(map->cso, key_hash, item);
   if (cso_hash_iter_is_null(iter)) {
      FREE(item);
      return FALSE;
   }

   map->num_entries++;

   return TRUE;
}