Example #1
0
int main(){
	
	char read_val[MAX_BUFFER];
	int ret;
	
	printf("Connecting to server\n");
	int kv_descriptor = kv_connect("127.0.0.1",9998); // This descriptor is what the API will use from now on

 	ret = kv_write(kv_descriptor, 1, "a", (strlen("a") + 1) * sizeof(char), 0);
 	printf("kv_write returned %d \n", ret);
	
	ret = kv_write(kv_descriptor, 2, "ab", (strlen("ab") + 1) * sizeof(char), 0);
 	printf("kv_write returned %d \n", ret);
	
	ret = kv_write(kv_descriptor, 3, "abc", (strlen("abc") + 1) * sizeof(char), 0);
	printf("kv_write returned %d \n", ret);

	ret = kv_delete(kv_descriptor, 2);
	printf("kv_delete returned %d \n", ret);
	
 	printf("Reading from server\n");
 	kv_read(kv_descriptor, 3, read_val, MAX_BUFFER);
 	
 	printf("Closing connection to server\n");
 	kv_close(kv_descriptor);

	exit(0);
}
Example #2
0
int main(){
	char linha[100];
	
	
	if(fork() == 0){
			
		int kv = kv_connect("127.0.0.1", 9999);
		printf("CLIENT 0 connected to data-server\n"); fflush(stdout);

		for (uint32_t i = 0; i < MAX_VALUES; i +=2){
			sprintf(linha, "%u", i);
			printf("CLIENT 0 writing key %d with value %s\n", i, linha); fflush(stdout);
			kv_write(kv, i , linha, strlen(linha)+1, 0);
		}

		printf("reading values\n");
		for (uint32_t i = 0; i < MAX_VALUES; i ++){
			if(kv_read(kv, i , linha, 100) == 0){
				printf ("key - %10u value %s\n", i, linha);
			}else {
				printf("key - %10u does not exist in dictionary\n", i);
			}
		}

		kv_close(kv);


	}else{
		int kv = kv_connect("127.0.0.1", 9999);
		printf("CLIENT 1 connected to data-server\n"); fflush(stdout);

		printf("reading values\n");
		for (uint32_t i = 0; i < MAX_VALUES; i ++){
			if(kv_read(kv, i , linha, 100) == 0){
				printf ("key - %10u value %s\n", i, linha);
			}else {
				printf("key - %10u does not exist in dictionary\n", i);
			}
		}

		printf("writing values\n");
		for (uint32_t i = 1; i < MAX_VALUES; i +=2){
			sprintf(linha, "%u", i);
			printf("CLIENT 1 writing key %d with value %s\n", i, linha); fflush(stdout);
			kv_write(kv, i , linha, strlen(linha)+1, 0);
		}

		printf("press enter to delete even values\n");
		for (uint32_t i = 0; i < MAX_VALUES; i +=2){		
			printf("deleting key %d\n", i);
			kv_delete(kv, i);
		}
		
		kv_close(kv);

	}
	
	
	
}
Example #3
0
int r_kv_delete(kv_store_t *store, const kv_key_t *key) {
    int ret;

    if (!store || !key) {
        return RKV_INVALID_ARGUEMENTS;
    }

    ret = kv_delete(store, key);
    if (ret > 0) {
        return ret;
    }
    RETURN_MAP_TO_RERR(ret);
}
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;
}
int test(void * kv_obj){

    int buffersize = 1000;
    char buffer[buffersize];

    //compare tests
    kv_set(kv_obj, "blah", "100");

    assert(!kv_cmp(kv_obj, "blah", "100")); 
    assert(!kv_cmp_int(kv_obj, "blah", 100));

    assert(kv_cmp(kv_obj, "blah", "200"));
    assert(kv_cmp_int(kv_obj, "blah", 200));

    kv_set(kv_obj, "blah", "31415");


    assert(kv_cmp(kv_obj, "blah", "100")); 
    assert(kv_cmp_int(kv_obj, "blah", 100));
    assert(kv_cmp(kv_obj, "blah", "200"));
    assert(kv_cmp_int(kv_obj, "blah", 200));

    assert(!kv_cmp(kv_obj, "blah", "31415"));
    assert(!kv_cmp_int(kv_obj, "blah", 31415));

    kv_set_int(kv_obj, "other", 150);
    assert(!kv_cmp_int(kv_obj, "other", 150));
    assert(kv_cmp_int(kv_obj, "other", 0));
    assert(kv_cmp_int(kv_obj, "other", -1));
    assert(!kv_cmp(kv_obj, "other", "150"));
    assert(kv_cmp(kv_obj, "other", "asdkflajef"));
    assert(kv_cmp(kv_obj, "other", "150sdfl"));

    //what is the defined kv_cmp behavior when a key is not set?
    //it should return nonzero
    assert(kv_cmp(kv_obj, "fake key", ""));
    assert(kv_cmp_int(kv_obj, "fake key", 0));
    assert(kv_cmp_int(kv_obj, "fake key", 1));
    assert(kv_cmp_int(kv_obj, "fake key", -1));



    // delete and keyexists  tests
    assert(kv_keyexists(kv_obj, "blah"));
    assert(!kv_keyexists(kv_obj, "fake key"));
    assert(!kv_keyexists(kv_obj, "other fake key"));

    kv_delete(kv_obj, "blah");
    assert(!kv_keyexists(kv_obj, "blah"));



    kv_set(kv_obj, "blah", "31415");

    //test and set
    assert(kv_test_and_set(kv_obj, "blah", "200"));
    assert(kv_test_and_set_int(kv_obj, "blah", 200));
    assert(!kv_test_and_set(kv_obj, "new", "200"));
    assert(!kv_test_and_set_int(kv_obj, "new2", 200));
    assert(kv_test_and_set(kv_obj, "new", "200"));
    assert(kv_test_and_set_int(kv_obj, "new2", 200));

    
    kv_set_int(kv_obj, "x", 30);
    kv_inc(kv_obj, "x");
    assert(!kv_cmp_int(kv_obj, "x", 31));

    kv_inc(kv_obj, "x");
    assert(!kv_cmp_int(kv_obj, "x", 32));

    kv_inc(kv_obj, "x");
    assert(!kv_cmp_int(kv_obj, "x", 33));
    assert(kv_cmp_int(kv_obj, "x", 333));
    assert(kv_cmp(kv_obj, "x", "33 "));
    assert(kv_cmp(kv_obj, "x", " 33"));

    kv_offset(kv_obj, "x", -10);
    assert(!kv_cmp_int(kv_obj, "x", 23));
    kv_offset(kv_obj, "x", 99999900);
    assert(!kv_cmp(kv_obj, "x", "99999923"));
    kv_dec(kv_obj, "x");
    assert(!kv_cmp(kv_obj, "x", "99999922"));
    kv_dec(kv_obj, "x");
    kv_dec(kv_obj, "x");
    kv_dec(kv_obj, "x");
    kv_inc(kv_obj, "x");
    kv_dec(kv_obj, "x");
    kv_offset(kv_obj, "x", -99999900);
    assert(!kv_cmp_int(kv_obj, "x", 19));


    kv_set(kv_obj, "append test", "one");
    kv_append(kv_obj, "append test", ", two");
    assert(!kv_cmp(kv_obj, "append test", "one, two"));
    assert(kv_cmp(kv_obj, "append test", "elephants"));
    kv_append(kv_obj, "append test", ", three");
    assert(!kv_cmp(kv_obj, "append test", "one, two, three"));


    // TODO check behavior on corner cases such as appending to a value that hasn't been set etc.


    printf("Tests finished.  Tests passed if assertions were checked.");
    return 0;
}