From: Padraig O'Sullivan Date: Sat, 11 Jul 2009 19:10:39 +0000 (-0400) Subject: Modified the fetch() function in the C++ interface to return a bool instead X-Git-Tag: 0.32~17^2~1 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=bc9b4e408fbd3be5448136155870dcdbb8b65d58;p=m6w6%2Flibmemcached Modified the fetch() function in the C++ interface to return a bool instead of a std::string. This makes looping using the fetch() function easier. Also updated the C++ test cases to test more of the C++ interface's functionality. --- diff --git a/libmemcached/memcached.hh b/libmemcached/memcached.hh index 651c4a2c..689a3bce 100644 --- a/libmemcached/memcached.hh +++ b/libmemcached/memcached.hh @@ -28,19 +28,23 @@ public: memcached_free(&memc); } - std::string fetch(std::string &key, size_t *key_length, size_t *value_length) + bool fetch(std::string &key, + std::string &ret_val, + size_t *key_length, + size_t *value_length, + uint32_t *flags, + memcached_return *rc) { - uint32_t flags; - memcached_return rc; - std::string ret_val; - - char *value= memcached_fetch(&memc, const_cast(key.c_str()), key_length, - value_length, &flags, &rc); + char ret_key[MEMCACHED_MAX_KEY]; + char *value= memcached_fetch(&memc, ret_key, key_length, + value_length, flags, rc); if (value) { ret_val.assign(value); + key.assign(ret_key); + return true; } - return ret_val; + return false; } std::string get(const std::string &key, size_t *value_length) diff --git a/tests/plus.cpp b/tests/plus.cpp index fdc7e916..23b5cf17 100644 --- a/tests/plus.cpp +++ b/tests/plus.cpp @@ -21,21 +21,18 @@ using namespace std; extern "C" { - test_return basic_test(memcached_st *memc); - test_return increment_test(memcached_st *memc); - test_return basic_master_key_test(memcached_st *memc); void *world_create(void); void world_destroy(void *p); } -test_return basic_test(memcached_st *memc) +static test_return basic_test(memcached_st *memc) { Memcached foo(memc); const string value_set("This is some data"); string value; size_t value_length; - foo.set("mine", value_set); + foo.set("mine", value_set, 0, 0); value= foo.get("mine", &value_length); assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0)); @@ -43,7 +40,7 @@ test_return basic_test(memcached_st *memc) return TEST_SUCCESS; } -test_return increment_test(memcached_st *memc) +static test_return increment_test(memcached_st *memc) { Memcached mcach(memc); bool rc; @@ -54,7 +51,7 @@ test_return increment_test(memcached_st *memc) uint64_t int_ret_value; size_t value_length; - mcach.set(key, inc_value); + mcach.set(key, inc_value, 0, 0); ret_value= mcach.get(key, &value_length); printf("\nretvalue %s\n",ret_value.c_str()); int_inc_value= uint64_t(atol(inc_value.c_str())); @@ -76,7 +73,7 @@ test_return increment_test(memcached_st *memc) return TEST_SUCCESS; } -test_return basic_master_key_test(memcached_st *memc) +static test_return basic_master_key_test(memcached_st *memc) { Memcached foo(memc); const string value_set("Data for server A"); @@ -86,7 +83,7 @@ test_return basic_master_key_test(memcached_st *memc) string value; size_t value_length; - foo.set_by_key(master_key_a, key, value_set); + foo.set_by_key(master_key_a, key, value_set, 0, 0); value= foo.get_by_key(master_key_a, key, &value_length); assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0)); @@ -97,11 +94,106 @@ test_return basic_master_key_test(memcached_st *memc) return TEST_SUCCESS; } +/* Count the results */ +static memcached_return callback_counter(memcached_st *ptr __attribute__((unused)), + memcached_result_st *result __attribute__((unused)), + void *context) +{ + unsigned int *counter= static_cast(context); + + *counter= *counter + 1; + + return MEMCACHED_SUCCESS; +} + +static test_return mget_result_function(memcached_st *memc) +{ + Memcached mc(memc); + bool rc; + string key1("fudge"); + string key2("son"); + string key3("food"); + vector keys; + keys.reserve(3); + keys.push_back(key1); + keys.push_back(key2); + keys.push_back(key3); + unsigned int counter; + memcached_execute_function callbacks[1]; + + /* We need to empty the server before we continue the test */ + rc= mc.flush(0); + rc= mc.set_all(keys, keys, 50, 9); + assert(rc == true); + + rc= mc.mget(keys); + assert(rc == true); + + callbacks[0]= &callback_counter; + counter= 0; + rc= mc.fetch_execute(callbacks, static_cast(&counter), 1); + + assert(counter == 3); + + return TEST_SUCCESS; +} + +static test_return mget_test(memcached_st *memc) +{ + Memcached mc(memc); + bool rc; + memcached_return mc_rc; + vector keys; + keys.reserve(3); + keys.push_back("fudge"); + keys.push_back("son"); + keys.push_back("food"); + uint32_t flags; + + string return_key; + size_t return_key_length; + string return_value; + size_t return_value_length; + + /* We need to empty the server before we continue the test */ + rc= mc.flush(0); + assert(rc == true); + + rc= mc.mget(keys); + assert(rc == true); + + while (mc.fetch(return_key, return_value, &return_key_length, + &return_value_length, &flags, &mc_rc)) + { + assert(return_value.length() != 0); + } + assert(return_value_length == 0); + assert(mc_rc == MEMCACHED_END); + + rc= mc.set_all(keys, keys, 50, 9); + assert(rc == true); + + rc= mc.mget(keys); + assert(rc == true); + + while ((mc.fetch(return_key, return_value, &return_key_length, + &return_value_length, &flags, &mc_rc))) + { + assert(return_value.length() != 0); + assert(mc_rc == MEMCACHED_SUCCESS); + assert(return_key_length == return_value_length); + assert(!memcmp(return_value.c_str(), return_key.c_str(), return_value_length)); + } + + return TEST_SUCCESS; +} test_st tests[] ={ { "basic", 0, basic_test }, { "basic_master_key", 0, basic_master_key_test }, { "increment_test", 0, increment_test }, + { "mget", 1, mget_test }, + { "mget_result_function", 1, mget_result_function }, {0, 0, 0} };