/* * @brief Insère un couple key/val dans la base * * Insère un couple key/val dans la base en fonction de la méthode d'allocation * et de la fonction de hachage en argument (kv) * * @param kv descripteur d'accès à la base * @param key clé * @param val valeur */ int kv_put (KV *kv, const kv_datum *key, const kv_datum *val) { len_t offset_tmp; if(offset_cle(kv,key,&offset_tmp) == 1) // la clé existe déjà { if((kv_del(kv,key)) == -1) { return -1; } if((kv_put(kv,key,val)) == -1) { return -1; } return 42; } else // la clé n'existe pas { len_t offset; if(kv_put_dkv(kv, key, val, &offset) == -1) { return -1; // modification dans le .dkv } if(kv_put_blk(kv, key, &offset) == -1) { return -1; // modification dans le .blk } if(writeData(kv, key, val, offset) == -1) { return -1; // écriture du couple key/val } } return 42; }
map_t store_deserialize(const str *input) { map_t map; cJSON *json_map, *obj; str key; int_str_t value; map = map_create(AVLMAP_SHARED); if (!map) { LM_ERR("oom\n"); return NULL; } cJSON_InitHooks(&shm_hooks); json_map = cJSON_Parse(input->s); if (!json_map) { LM_ERR("bad JSON input or oom\n"); goto out; } if (json_map->type != cJSON_Object) { LM_BUG("non-cJSON_Object kv_store col type (%d)", json_map->type); goto out; } for (obj = json_map->child; obj; obj = obj->next) { init_str(&key, obj->string); switch (obj->type) { case cJSON_String: value.is_str = 1; init_str(&value.s, obj->valuestring); break; case cJSON_Number: value.is_str = 0; value.i = obj->valueint; break; default: LM_BUG("unknown obj type (%d)", obj->type); continue; } if (!kv_put(map, &key, &value)) LM_ERR("oom, map will be incomplete\n"); } out: cJSON_Delete(json_map); cJSON_InitHooks(NULL); return map; }
rkv_error_t r_kv_put(kv_store_t *store, const kv_key_t *key, const kv_value_t *value, kv_version_t ** ret_new_version) { kv_version_t *version = NULL; kv_error_t err; if (!store || !key || !value) { return RKV_INVALID_ARGUEMENTS; } err = kv_put(store, key, value, &version); RETURN_RERR_IF_ERR(err); if (ret_new_version) { *ret_new_version = version; } return RKV_SUCCESS; }
int main() { int i, state; freopen("input_file", "r", stdin); scanf("%d", &num); for (i = 0; i < num; i++) { scanf("%d %s %d %s", &in[i].key_size, in[i].key, &in[i].value_size, in[i].value); out[i].value_size = 1025; } //----------------KV_INIT------------- fill_kvs_env(); if (kv_init(&kvs_env) != 0) { printf("kvs_init fail\n"); return -1; } //----------------KV_PUT------------- for (i = 0; i < num / 2; i++) { state = kv_put(in[i].key, in[i].key_size, in[i].value, in[i].value_size); if (state != 0) { printf("kv_put %d fail\n", i); } else { printf("put: key_size:%d\t\tkey:%s\t\tvalue_size:%d\t\tvalue:%s\n", in[i].key_size, in[i].key, in[i].value_size, in[i].value); } } fflush(stdout); //----------------KV_GET------------- for (i = 0; i < num / 2; i++) { state = kv_get(in[i].key, in[i].key_size, out[i].value, &out[i].value_size); if (state != 0) { printf("kv_get %d fail\n", i); } else { printf("get: key_size:%d\t\tkey:%s\t\tvalue_size:%d\t\tvalue:%s\n", in[i].key_size, in[i].key, out[i].value_size, out[i].value); } } fflush(stdout); //----------------KV_DELETE------------- for (i = 0; i < num / 4; i += 2) { state = kv_delete(in[i].key, in[i].key_size); if (state != 0) { printf("kv_delete %d fail\n", i); } else { printf("delete: key_size:%d\t\tkey:%s\t\tvalue_size:%d\t\tvalue:%s\n", in[i].key_size, in[i].key, out[i].value_size, out[i].value); } } fflush(stdout); //----------------KV_GET------------- for (i = 0; i < num / 4; i += 2) { state = kv_get(in[i].key, in[i].key_size, out[i].value, &out[i].value_size); if (state != 0) { printf("kv_get %d fail\n", i); } else { printf("get: key_size:%d\t\tkey:%s\t\tvalue_size:%d\t\tvalue:%s\n", in[i].key_size, in[i].key, out[i].value_size, out[i].value); } } fflush(stdout); //----------------KV_PUT------------- for (i = num / 2; i < num; i++) { state = kv_put(in[i].key, in[i].key_size, in[i].value, in[i].value_size); if (state != 0) { printf("kv_put %d fail\n", i); } else { printf("put: key_size:%d\t\tkey:%s\t\tvalue_size:%d\t\tvalue:%s\n", in[i].key_size, in[i].key, in[i].value_size, in[i].value); } } fflush(stdout); //----------------KV_GET------------- for (i = num / 2; i < num; i++) { state = kv_get(in[i].key, in[i].key_size, out[i].value, &out[i].value_size); if (state != 0) { printf("kv_get %d fail\n", i); } else { printf("get: key_size:%d\t\tkey:%s\t\tvalue_size:%d\t\tvalue:%s\n", in[i].key_size, in[i].key, out[i].value_size, out[i].value); } } fflush(stdout); //----------------KV_EXIT------------- if (kv_exit(1) != 0) { printf("kvs_exit fail\n"); return -1; } return 0; }
void config_center::put(const string& ns, const string &key, const string &value) { kv_put(m_service + ns, key, value); }
int main() { long long start, end, duration, t1, t2; struct timeval tv; int i, j, key_size, value_buffer_size; kvs_env.init_type = INIT_TYPE_CREATE; kvs_env.disk_file_path = "disk_file"; kvs_env.IMAGE_file_path = "IMAGE_file"; kvs_env.log_file_path = "log_file"; kvs_env.buffer_sleep_time = 0; kvs_env.buffer_horizon_size = 100 * 1024 * 1024; kvs_env.buffer_size = 700 * 1024 * 1024; for (i = 0; i < VALUE_BUFFER_SIZE; i++) value_buffer[i] = 'a'; srand((unsigned) time(NULL)); gettimeofday(&tv, NULL); start = tv.tv_sec * 1000 * 1000 + tv.tv_usec; if (kv_init(&kvs_env) != 0) { printf("kvs_init fail\n"); return -1; } //first write 5GB for (i = 0; i < CNT; i++) { get_key(key_buffer, i, &key_size); if (kv_put(key_buffer, key_size, value_buffer, VALUE_BUFFER_SIZE) != 0) { printf("kv_put fail\n"); return -1; } } duration = 0; j = CNT; for (i = 0; i < CNT; i++) { get_key(key_buffer, j, &key_size); if (i % 200 == 0) { if (kv_put(key_buffer, key_size, value_buffer, VALUE_BUFFER_SIZE) != 0) { printf("kv_put fail\n"); return -1; } j++; } random_key(key_buffer, j - 1, &key_size); gettimeofday(&tv, NULL); t1 = tv.tv_sec * 1000 * 1000 + tv.tv_usec; if (kv_get(key_buffer, key_size, value_buffer, &value_buffer_size) != 0) { printf("kv_get fail\n"); return -1; } gettimeofday(&tv, NULL); t2 = tv.tv_sec * 1000 * 1000 + tv.tv_usec; duration += (t2 - t1) / 1000; } kv_exit(); printf("read time: %lld ms\n", duration); gettimeofday(&tv, NULL); end = tv.tv_sec * 1000 * 1000 + tv.tv_usec; duration = (end - start) / 1000; printf("total time: %lld ms\n", duration); return 0; }
int sp_map_rehash(map **incmap) { map *themap = *incmap; /* another level of indirection */ map *pnewmap = NULL; /* the bigger map */ kvpair *curr=NULL,*prev=NULL; bucket *bucketptr=NULL; status code; long i=0; #ifdef DEBUG printf("*********************************************************************************************\n"); printf("DEBUG: enter sp_map_rehash - map size %lu, bucketsize %lu, bucketsused %lu\n",themap->size, themap->bucketsize,themap->bucketsused); #endif pnewmap = (map*)malloc(sizeof(map)); /* allocate memory for the new map */ code = create_map(&pnewmap, themap->type, nextprobableprime((themap->bucketsize) * (themap->resize_factor)), themap->resize_factor, themap->load_factor); bucketptr = themap->start; for(i=0;i<themap->bucketsize;i++) { if((bucketptr->start) == NULL) { bucketptr++; continue; /* no kvpairs at this hashcode */ #ifdef DEBUG printf("DEBUG: sp_map_rehash - ** no kvpairs at this hashcode, continue **\n"); #endif } else { curr = bucketptr->start; /* set curr to the first kvpair */ while(curr!=NULL) { /* now iterate through all kvpairs at this hashcode */ #ifdef DEBUG printf("DEBUG: putting key: %s value: %s into new map\n",(char*)curr->key,(char*)curr->value); #endif kv_put(&pnewmap, (void*)curr->key, (void*)curr->value); /* put the key value pair into the new map */ prev = curr; curr = curr->next; if((prev->key)!=NULL) free((void*)prev->key); prev->key = NULL; if((prev->value)!=NULL) free((void*)prev->value); prev->value = NULL; if(prev!=NULL) free((void*)prev); prev = NULL; /* old kvpair is free */ } } bucketptr++; } if(themap->start!=NULL) free((void*)themap->start); /* free the buckets in the old map */ themap->start = NULL; if(themap!=NULL) free((void*)themap); /* old map is now free */ themap = NULL; *incmap = pnewmap; /* set the dereferenced map pointer passed in to the new map */ #ifdef DEBUG printf("DEBUG: leave sp_map_rehash - new map size: %lu, bucketsused: %lu, currentload: %f, loadfactor: %f bucketsize: %lu\n",(*incmap)->size,(*incmap)->bucketsused,(*incmap)->currentload,(*incmap)->load_factor,(*incmap)->bucketsize); printf("*******************************************************************************************************************\n"); #endif return SUCCESS; }