//将item插入到哈希表和LRU队列中,hv为哈希值 int do_item_link(item *it, const uint32_t hv) { MEMCACHED_ITEM_LINK(ITEM_key(it), it->nkey, it->nbytes); //确保这个item已经从slab分配出去并且还没插入到LRU队列中 assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0); mutex_lock(&cache_lock); //加入link标记 it->it_flags |= ITEM_LINKED; it->time = current_time; STATS_LOCK(); stats.curr_bytes += ITEM_ntotal(it); stats.curr_items += 1; stats.total_items += 1; STATS_UNLOCK(); /* Allocate a new CAS ID on link. */ ITEM_set_cas(it, (settings.use_cas) ? get_cas_id() : 0); //插入到hash表中 assoc_insert(it, hv); //item插入到链表中 item_link_q(it); //引用计数加1 refcount_incr(&it->refcount); mutex_unlock(&cache_lock); return 1; }
static ENGINE_ERROR_CODE do_item_link(struct demo_engine *engine, hash_item *it) { const char *key = dm_item_get_key(it); size_t stotal = ITEM_stotal(engine, it); assert((it->iflag & ITEM_LINKED) == 0); assert(it->nbytes < (1024 * 1024)); /* 1MB max size */ MEMCACHED_ITEM_LINK(key, it->nkey, it->nbytes); /* Allocate a new CAS ID on link. */ dm_item_set_cas(it, get_cas_id()); /* link the item to the hash table */ it->iflag |= ITEM_LINKED; it->time = engine->server.core->get_current_time(); it->hval = engine->server.core->hash(key, it->nkey, 0); dm_assoc_insert(engine, it->hval, it); /* update item statistics */ pthread_mutex_lock(&engine->stats.lock); engine->stats.curr_bytes += stotal; engine->stats.curr_items += 1; engine->stats.total_items += 1; pthread_mutex_unlock(&engine->stats.lock); return ENGINE_SUCCESS; }
int do_item_link(item *it) { MEMCACHED_ITEM_LINK(ITEM_key(it), it->nbytes); assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0); assert(it->nbytes < (1024 * 1024)); /* 1MB max size */ it->it_flags |= ITEM_LINKED; it->time = current_time; assoc_insert(it); STATS_LOCK(); stats.curr_bytes += ITEM_ntotal(it); stats.curr_items += 1; stats.total_items += 1; STATS_UNLOCK(); #ifdef USE_REPLICATION /* Allocate a new CAS ID on link. */ if(!(it->it_flags & ITEM_REPDATA)) it->cas_id = get_cas_id(); #else /* Allocate a new CAS ID on link. */ it->cas_id = get_cas_id(); #endif /* USE_REPLICATION */ item_link_q(it); return 1; }
/* 形成了一个完成的 item 后, 就要把它放入两个数据结构中, 一是 memcached 的哈希表, memcached 运行过程中只有一个哈希表, 二是 item 所在的 slabclass 的 LRU 队列. */ int do_item_link(item *it, const uint32_t hv) { MEMCACHED_ITEM_LINK(ITEM_key(it), it->nkey, it->nbytes); assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0); mutex_lock(&cache_lock); it->it_flags |= ITEM_LINKED; it->time = current_time; STATS_LOCK(); stats.curr_bytes += ITEM_ntotal(it); stats.curr_items += 1; stats.total_items += 1; STATS_UNLOCK(); /* Allocate a new CAS ID on link. */ ITEM_set_cas(it, (settings.use_cas) ? get_cas_id() : 0); /* 把 item 放入哈希表 */ assoc_insert(it, hv); /* 把 item 放入 LRU 队列*/ item_link_q(it); refcount_incr(&it->refcount); mutex_unlock(&cache_lock); return 1; }
int do_item_link(item *it) { MEMCACHED_ITEM_LINK(ITEM_key(it), it->nkey, it->nbytes); assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0); assert(it->nbytes < (1024 * 1024)); /* 1MB max size */ it->it_flags |= ITEM_LINKED; it->time = current_time; assoc_insert(it); #ifdef MOXI_ITEM_MALLOC it->refcount++; #endif STATS_LOCK(); stats.curr_bytes += ITEM_ntotal(it); stats.curr_items += 1; stats.total_items += 1; STATS_UNLOCK(); /* Allocate a new CAS ID on link. */ ITEM_set_cas(it, (settings.use_cas) ? get_cas_id() : 0); item_link_q(it); return 1; }
int do_item_link(struct default_engine *engine, hash_item *it) { MEMCACHED_ITEM_LINK(item_get_key(it), it->nkey, it->nbytes); assert((it->iflag & (ITEM_LINKED|ITEM_SLABBED)) == 0); assert(it->nbytes < (1024 * 1024)); /* 1MB max size */ it->iflag |= ITEM_LINKED; it->time = engine->server.core->get_current_time(); assoc_insert(engine, engine->server.core->hash(item_get_key(it), it->nkey, 0), it); pthread_mutex_lock(&engine->stats.lock); engine->stats.curr_bytes += ITEM_ntotal(engine, it); engine->stats.curr_items += 1; engine->stats.total_items += 1; pthread_mutex_unlock(&engine->stats.lock); /* Allocate a new CAS ID on link. */ item_set_cas(NULL, NULL, it, get_cas_id()); item_link_q(engine, it); return 1; }
//将item加入到hashtable和LRU链中 int do_item_link(item *it, const uint32_t hv) { syslog(LOG_INFO, "[%s:%s:%d]", __FILE__, __func__, __LINE__); MEMCACHED_ITEM_LINK(ITEM_key(it), it->nkey, it->nbytes); assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0); //判断状态,即没有在hash表LRU链中或被释放 mutex_lock(&cache_lock); it->it_flags |= ITEM_LINKED; //设置linked状态 it->time = current_time;//设置最近访问的时间 STATS_LOCK(); stats.curr_bytes += ITEM_ntotal(it); //增加每个item所需要的字节大小,包括item结构体和item内容大小 stats.curr_items += 1; stats.total_items += 1; STATS_UNLOCK(); /* Allocate a new CAS ID on link. */ ITEM_set_cas(it, (settings.use_cas) ? get_cas_id() : 0); //设置新CAS,CAS是memcache用来处理并发请求的一种机制 assoc_insert(it, hv);//插入hashtable item_link_q(it); //加入LRU链 refcount_incr(&it->refcount); mutex_unlock(&cache_lock); return 1; }
int do_item_link(struct default_engine *engine, hash_item *it) { const hash_key* key = item_get_key(it); MEMCACHED_ITEM_LINK(hash_key_get_client_key(key), hash_key_get_client_key_len(key), it->nbytes); cb_assert((it->iflag & (ITEM_LINKED|ITEM_SLABBED)) == 0); it->iflag |= ITEM_LINKED; it->time = engine->server.core->get_current_time(); assoc_insert(engine, crc32c(hash_key_get_key(key), hash_key_get_key_len(key), 0), it); cb_mutex_enter(&engine->stats.lock); engine->stats.curr_bytes += ITEM_ntotal(engine, it); engine->stats.curr_items += 1; engine->stats.total_items += 1; cb_mutex_exit(&engine->stats.lock); /* Allocate a new CAS ID on link. */ item_set_cas(NULL, NULL, it, get_cas_id()); item_link_q(engine, it); return 1; }