From 682a79f2cc82e31d37ca9db6bb5ec631c197d52c Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Thu, 15 Jul 2010 19:33:19 -0700 Subject: [PATCH] Merge in memcached_stat_execute(). --- ChangeLog | 3 ++ libmemcached/common.h | 8 ++++ libmemcached/server.c | 20 ++++++++++ libmemcached/stats.c | 89 ++++++++++++++++++++++++++++++++++++------- libmemcached/stats.h | 3 ++ libmemcached/types.h | 4 ++ tests/mem_functions.c | 33 ++++++++++++++++ 7 files changed, 146 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index e1761803..ca56868e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +0.43 + * memcached_stat_execute() merged. + 0.42 Tue Jul 6 12:29:50 PDT 2010 * Mistake in libtool caused issue with library version diff --git a/libmemcached/common.h b/libmemcached/common.h index b45bf807..d07edd6c 100644 --- a/libmemcached/common.h +++ b/libmemcached/common.h @@ -56,9 +56,17 @@ typedef struct memcached_server_st * memcached_server_write_instance_st; +typedef memcached_return_t (*memcached_server_execute_fn)(memcached_st *ptr, memcached_server_write_instance_st server, void *context); + LIBMEMCACHED_LOCAL memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key); +LIBMEMCACHED_LOCAL +memcached_return_t memcached_server_execute(memcached_st *ptr, + memcached_server_execute_fn callback, + void *context); + + /* These are private not to be installed headers */ #include "libmemcached/io.h" #include "libmemcached/do.h" diff --git a/libmemcached/server.c b/libmemcached/server.c index 0461c3c7..3e5e77a5 100644 --- a/libmemcached/server.c +++ b/libmemcached/server.c @@ -172,6 +172,26 @@ memcached_return_t memcached_server_cursor(const memcached_st *ptr, return MEMCACHED_SUCCESS; } +memcached_return_t memcached_server_execute(memcached_st *ptr, + memcached_server_execute_fn callback, + void *context) +{ + for (uint32_t x= 0; x < memcached_server_count(ptr); x++) + { + memcached_server_write_instance_st instance= + memcached_server_instance_fetch(ptr, x); + + unsigned int iferror; + + iferror= (*callback)(ptr, instance, context); + + if (iferror) + continue; + } + + return MEMCACHED_SUCCESS; +} + memcached_server_instance_st memcached_server_by_key(const memcached_st *ptr, const char *key, size_t key_length, diff --git a/libmemcached/stats.c b/libmemcached/stats.c index ff5416d4..731c1cc0 100644 --- a/libmemcached/stats.c +++ b/libmemcached/stats.c @@ -29,6 +29,13 @@ static const char *memcached_stat_keys[] = { NULL }; +struct local_context +{ + memcached_stat_fn func; + void *context; + const char *args; +}; + static memcached_return_t set_data(memcached_stat_st *memc_stat, char *key, char *value) { @@ -228,8 +235,9 @@ char *memcached_stat_get_value(const memcached_st *ptr, memcached_stat_st *memc_ } static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, - char *args, - memcached_server_write_instance_st instance) + const char *args, + memcached_server_write_instance_st instance, + struct local_context *check) { memcached_return_t rc; @@ -286,10 +294,23 @@ static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, return rc; } - unlikely((set_data(memc_stat, buffer, buffer + strlen(buffer) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY) + if (memc_stat) + { + unlikely((set_data(memc_stat, buffer, buffer + strlen(buffer) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY) + { + WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY); + WATCHPOINT_ASSERT(0); + } + } + + if (check && check->func) { - WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY); - WATCHPOINT_ASSERT(0); + size_t key_length= strlen(buffer); + + check->func(instance, + buffer, key_length, + buffer+key_length+1, strlen(buffer+key_length+1), + check->context); } } while (1); @@ -302,8 +323,9 @@ static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, } static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat, - char *args, - memcached_server_write_instance_st instance) + const char *args, + memcached_server_write_instance_st instance, + struct local_context *check) { memcached_return_t rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; @@ -343,10 +365,21 @@ static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat, value= string_ptr; value[(size_t)(end_ptr-string_ptr)]= 0; string_ptr= end_ptr + 2; - unlikely((set_data(memc_stat, key, value)) == MEMCACHED_UNKNOWN_STAT_KEY) + if (memc_stat) { - WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY); - WATCHPOINT_ASSERT(0); + unlikely((set_data(memc_stat, key, value)) == MEMCACHED_UNKNOWN_STAT_KEY) + { + WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY); + WATCHPOINT_ASSERT(0); + } + } + + if (check && check->func) + { + check->func(instance, + key, strlen(key), + value, strlen(value), + check->context); } } else @@ -394,11 +427,11 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur if (ptr->flags.binary_protocol) { - temp_return= binary_stats_fetch(stat_instance, args, instance); + temp_return= binary_stats_fetch(stat_instance, args, instance, NULL); } else { - temp_return= ascii_stats_fetch(stat_instance, args, instance); + temp_return= ascii_stats_fetch(stat_instance, args, instance, NULL); } if (temp_return != MEMCACHED_SUCCESS) @@ -428,11 +461,11 @@ memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char if (memc.flags.binary_protocol) { - rc= binary_stats_fetch(memc_stat, args, instance); + rc= binary_stats_fetch(memc_stat, args, instance, NULL); } else { - rc= ascii_stats_fetch(memc_stat, args, instance); + rc= ascii_stats_fetch(memc_stat, args, instance, NULL); } memcached_free(&memc); @@ -489,3 +522,31 @@ void memcached_stat_free(const memcached_st *ptr, memcached_stat_st *memc_stat) free(memc_stat); } } + +static memcached_return_t call_stat_fn(memcached_st *ptr, + memcached_server_write_instance_st instance, + void *context) +{ + memcached_return_t rc; + struct local_context *check= (struct local_context *)context; + + if (ptr->flags.binary_protocol) + { + rc= binary_stats_fetch(NULL, check->args, instance, check); + } + else + { + rc= ascii_stats_fetch(NULL, check->args, instance, check); + } + + return rc; +} + +memcached_return_t memcached_stat_execute(memcached_st *memc, const char *args, memcached_stat_fn func, void *context) +{ + memcached_version(memc); + + struct local_context check= { .func= func, .context= context, .args= args }; + + return memcached_server_execute(memc, call_stat_fn, (void *)&check); +} diff --git a/libmemcached/stats.h b/libmemcached/stats.h index 105e8910..a3040306 100644 --- a/libmemcached/stats.h +++ b/libmemcached/stats.h @@ -62,6 +62,9 @@ LIBMEMCACHED_API char ** memcached_stat_get_keys(const memcached_st *ptr, memcached_stat_st *memc_stat, memcached_return_t *error); +LIBMEMCACHED_API +memcached_return_t memcached_stat_execute(memcached_st *memc, const char *args, memcached_stat_fn func, void *context); + #ifdef __cplusplus } // extern "C" #endif diff --git a/libmemcached/types.h b/libmemcached/types.h index 897abaed..28a1535c 100644 --- a/libmemcached/types.h +++ b/libmemcached/types.h @@ -47,6 +47,10 @@ typedef void *(*memcached_calloc_fn)(const memcached_st *ptr, size_t nelem, cons typedef memcached_return_t (*memcached_execute_fn)(const memcached_st *ptr, memcached_result_st *result, void *context); typedef memcached_return_t (*memcached_server_fn)(const memcached_st *ptr, memcached_server_instance_st server, void *context); +typedef memcached_return_t (*memcached_stat_fn)(memcached_server_instance_st server, + const char *key, size_t key_length, + const char *value, size_t value_length, + void *context); /** Trigger functions. diff --git a/tests/mem_functions.c b/tests/mem_functions.c index f59cf01a..a94787bb 100644 --- a/tests/mem_functions.c +++ b/tests/mem_functions.c @@ -5725,6 +5725,38 @@ static test_return_t test_cull_servers(memcached_st *memc) return TEST_SUCCESS; } +static memcached_return_t stat_printer(memcached_server_instance_st server, + const char *key, size_t key_length, + const char *value, size_t value_length, + void *context) +{ + (void)server; + (void)context; + (void)key; + (void)key_length; + (void)value; + (void)value_length; + + return MEMCACHED_SUCCESS; +} + +static test_return_t memcached_stat_execute_test(memcached_st *memc) +{ + memcached_return_t rc= memcached_stat_execute(memc, NULL, stat_printer, NULL); + test_true(rc == MEMCACHED_SUCCESS); + + rc= memcached_stat_execute(memc, "slabs", stat_printer, NULL); + test_true(rc == MEMCACHED_SUCCESS); + + rc= memcached_stat_execute(memc, "items", stat_printer, NULL); + test_true(rc == MEMCACHED_SUCCESS); + + rc= memcached_stat_execute(memc, "sizes", stat_printer, NULL); + test_true(rc == MEMCACHED_SUCCESS); + + return TEST_SUCCESS; +} + /* * This test ensures that the failure counter isn't incremented during * normal termination of the memcached instance. @@ -6118,6 +6150,7 @@ test_st tests[] ={ {"verbosity", 1, (test_callback_fn)test_verbosity}, {"test_server_failure", 1, (test_callback_fn)test_server_failure}, {"cull_servers", 1, (test_callback_fn)test_cull_servers}, + {"memcached_stat_execute", 1, (test_callback_fn)memcached_stat_execute_test}, {0, 0, 0} }; -- 2.30.2