Ejemplo n.º 1
0
//k and v both are in stack
//k td
//v mvalue.data
int
insert_kv_mem(struct rbtree *rbt, struct htable *ds, uchar * k, int klen, 
              int type, uchar * v, int vlen, int hijack, packet_type *lowerdomain)
{
    uchar *val = NULL;
    struct mvalue *mv = NULL, tmp = {0};
    int ret = -1;
    struct rbnode *pn = NULL;
    struct ttlnode tn = { 0 }, *tmp_tn = NULL;
    int idx;
    if (vlen < 0 || vlen > MAX_RECORD_SIZE)
        return -1;
    hashval_t *hash = &(lowerdomain->hash[0]);
    idx = get_pre_mem_hash(k, klen, hash);
    val = malloc(vlen);
    if (val == NULL)
        return -1;
    memcpy(val, v, vlen);
    mv = (struct mvalue *) v;
    ret = htable_insert(ds + idx, k, klen, type, val, 1, &tmp, hash);    //mem, replace
    if (ret >= HTABLE_INSERT_RET_NEVER_EXPIRE) {
        free(val);
    }
    if (rbt) {
        if (ret == HTABLE_INSERT_RET_REPLACE) {
            pthread_spin_lock(&rbt->lock);
            tn.dlen = klen;
            //tmp get old data
            tn.exp = tmp.ttl;
            tn.type = type;
            tn.lowerdomain = NULL;
            tn.data = k;
            pn = find_node(rbt, &tn);
            //if update, we had delete tn in rbt
            //else update tn in rbt
            if (pn != NULL) {
                tmp_tn = delete_node(rbt, pn);
                if (tmp_tn) {
                    free(tmp_tn->lowerdomain);
                    free(tmp_tn);
                }
            }
            pthread_spin_unlock(&rbt->lock);
        }
    }
    if (mv->ttl == (MAX_TTL + 1)) {       //never expired
        return 0;
    }
    if (rbt == NULL) {
        return 0;
    }
    //data exists in htable, delete it in ttl tree, then insert
    pthread_spin_lock(&rbt->lock);
    if (type != NS && ret == HTABLE_INSERT_RET_REPLACE)
        ret = insert_into_ttltree(rbt, k, klen, type, mv->ttl, lowerdomain); //ttl expired tree
    else
        ret = insert_into_ttltree(rbt, k, klen, type, tmp.ttl, lowerdomain); //ttl expired tree
    pthread_spin_unlock(&rbt->lock);
    return 0;
}
Ejemplo n.º 2
0
//k and v both are in stack
//k td
//v mvalue.data
int insert_kv_mem(struct rbtree *rbt,struct htable *ds,uchar *k,uchar *v,int vlen)
{
 uchar *val = NULL;
 struct mvalue *mv = NULL,tmp;
 int ret = -1;
 struct rbnode *pn = NULL;
 struct ttlnode tn = {0};
 if(vlen < 0 || vlen > MAX_RECORD_SIZE)
	return -1;
 hashval_t hash = nocase_char_hash_function(k);
 hash = get_pre_mem_hash(k);
 val = malloc(vlen);
 if(val == NULL)
	return -1;
 memcpy(val,v,vlen);
 mv = (struct mvalue*)v;
 ret = htable_insert(ds + hash,k,val,1,&tmp); //mem, replace
 if(ret == 2)
	free(val);
 if(mv->ttl == (MAX_TTL + 1))//never expired
	 return 0;
 if(rbt == NULL)
	 return 0;
 //data exists in htable, delete it in ttl tree, then insert
 pthread_mutex_lock(&rbt->lock);
 if(ret != 0)
	{
	 tn.dlen = strlen(k) + 1;
	 //tmp get old data
	 tn.exp = tmp.ttl;
	 tn.data = k;
	 pn = find_node(rbt,&tn);
	 //if update, we had delete tn in rbt
	 //else update tn in rbt
	 if(pn != NULL)
		 delete_node(rbt,pn);
	}
 ret = insert_into_ttltree(rbt,k,mv->ttl);//ttl expired tree
 pthread_mutex_unlock(&rbt->lock);
 return 0;
}