From: Date: Fri, 18 Jan 2008 18:54:24 +0000 (-0500) Subject: This patch includes all of the callback work for setting clone() and cleanup() X-Git-Tag: 0.14~13 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=99803a66dda300ea7a5741fe8aae852e8b746366;p=m6w6%2Flibmemcached This patch includes all of the callback work for setting clone() and cleanup() operations. --- diff --git a/ChangeLog b/ChangeLog index 7fddc017..3434f8dd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * Added memcached_callback_get()/set() * First prototype of C++ interface * Updated docs for uint16_t changes in previous release diff --git a/docs/Makefile.am b/docs/Makefile.am index f92f77f9..0234e833 100644 --- a/docs/Makefile.am +++ b/docs/Makefile.am @@ -14,6 +14,7 @@ EXTRA_DIST = libmemcached.pod\ memcached_verbosity.pod\ memstat.pod\ memcached_behavior.pod\ + memcached_callback.pod\ memcached_server_st.pod\ memcat.pod\ memcached_create.pod\ @@ -38,6 +39,8 @@ man_MANS = libmemcached.3\ memcached_append_by_key.3\ memcached_behavior_get.3\ memcached_behavior_set.3\ + memcached_callback_get.3\ + memcached_callback_set.3\ memcached_cas.3\ memcached_cas_by_key.3\ memcached_clone.3\ @@ -205,6 +208,12 @@ memcached_behavior_get.3: memcached_behavior.pod memcached_behavior_set.3: memcached_behavior.pod pod2man -c "libmemcached" -r "" -s 3 memcached_behavior.pod > memcached_behavior_set.3 +memcached_callback_get.3: memcached_callback.pod + pod2man -c "libmemcached" -r "" -s 3 memcached_callback.pod > memcached_callback_get.3 + +memcached_callback_set.3: memcached_callback.pod + pod2man -c "libmemcached" -r "" -s 3 memcached_callback.pod > memcached_callback_set.3 + memcached_stat.3: memcached_stats.pod pod2man -c "libmemcached" -r "" -s 3 memcached_stats.pod > memcached_stat.3 diff --git a/docs/libmemcached.pod b/docs/libmemcached.pod index cb5083d5..c40072f6 100755 --- a/docs/libmemcached.pod +++ b/docs/libmemcached.pod @@ -104,7 +104,7 @@ Brian Aker, Ebrian@tangent.orgE =head1 SEE ALSO -memcached(1) libmemcached_examples(3) libmemcached(1) memcat(1) memcp(1) memflush(1) memrm(1) memslap(1) memstat(1) memcached_fetch(3) memcached_replace(3) memcached_server_list_free(3) libmemcached_examples(3) memcached_clone(3) memcached_free(3) memcached_server_add(3) memcached_server_push(3) memcached_add(3) memcached_get(3) memcached_server_count(3) memcached_servers_parse(3) memcached_create(3) memcached_increment(3) memcached_server_list(3) memcached_set(3) memcached_decrement(3) memcached_mget(3) memcached_server_list_append(3) memcached_strerror(3) memcached_delete(3) memcached_quit(3) memcached_server_list_count(3) memcached_verbosity(3) memcached_server_add_unix_socket(3) memcahed_result_create(3) memcached_result_free(3) memcached_result_key_value(3) memcached_result_key_length(3) memcached_result_value(3) memcached_result_length(3) memcached_result_flags(3) memcached_result_cas(3) memcached_result_st(3) memcached_append(3) memcached_prepend(3) memcached_fetch_result(3) memerror(1) memcached_get_by_key(3) memcached_mget_by_key(3) memcached_delete_by_key(3) memcached_fetch_execute(3) +memcached(1) libmemcached_examples(3) libmemcached(1) memcat(1) memcp(1) memflush(1) memrm(1) memslap(1) memstat(1) memcached_fetch(3) memcached_replace(3) memcached_server_list_free(3) libmemcached_examples(3) memcached_clone(3) memcached_free(3) memcached_server_add(3) memcached_server_push(3) memcached_add(3) memcached_get(3) memcached_server_count(3) memcached_servers_parse(3) memcached_create(3) memcached_increment(3) memcached_server_list(3) memcached_set(3) memcached_decrement(3) memcached_mget(3) memcached_server_list_append(3) memcached_strerror(3) memcached_delete(3) memcached_quit(3) memcached_server_list_count(3) memcached_verbosity(3) memcached_server_add_unix_socket(3) memcahed_result_create(3) memcached_result_free(3) memcached_result_key_value(3) memcached_result_key_length(3) memcached_result_value(3) memcached_result_length(3) memcached_result_flags(3) memcached_result_cas(3) memcached_result_st(3) memcached_append(3) memcached_prepend(3) memcached_fetch_result(3) memerror(1) memcached_get_by_key(3) memcached_mget_by_key(3) memcached_delete_by_key(3) memcached_fetch_execute(3) memcached_callback_get(3) memcached_callback_set(3) =cut diff --git a/docs/memcached_behavior.pod b/docs/memcached_behavior.pod index bd8c85a7..04d6e060 100755 --- a/docs/memcached_behavior.pod +++ b/docs/memcached_behavior.pod @@ -77,7 +77,8 @@ Modify the timeout value that is used by poll(). The default value is -1. An sig This allows you to store a pointer to a specifc piece of data. This can be retrieved from inside of memcached_fetch_exectue(). Cloning a memcached_st -will copy the pointer to the clone. +will copy the pointer to the clone. This was deprecated in 0.14 in favor +of memcached_callback_set(3). This will be removed in 0.15. =item MEMCACHED_BEHAVIOR_BUFFER_REQUESTS diff --git a/include/memcached.h b/include/memcached.h index 605c4a4c..05cb74a8 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -19,12 +19,6 @@ extern "C" { #endif -typedef struct memcached_st memcached_st; -typedef struct memcached_stat_st memcached_stat_st; -typedef struct memcached_result_st memcached_result_st; -typedef struct memcached_string_st memcached_string_st; -typedef struct memcached_server_st memcached_server_st; - /* These are Private and should not be used by applications */ #define MEMCACHED_VERSION_STRING_LENGTH 12 @@ -73,6 +67,15 @@ typedef enum { MEMCACHED_MAXIMUM_RETURN, /* Always add new error code before */ } memcached_return; +typedef struct memcached_st memcached_st; +typedef struct memcached_stat_st memcached_stat_st; +typedef struct memcached_result_st memcached_result_st; +typedef struct memcached_string_st memcached_string_st; +typedef struct memcached_server_st memcached_server_st; +typedef memcached_return (*clone_func)(memcached_st *parent, memcached_st *clone); +typedef memcached_return (*cleanup_func)(memcached_st *ptr); + + typedef enum { MEMCACHED_DISTRIBUTION_MODULA, MEMCACHED_DISTRIBUTION_CONSISTENT, @@ -93,6 +96,12 @@ typedef enum { MEMCACHED_BEHAVIOR_USER_DATA, } memcached_behavior; +typedef enum { + MEMCACHED_CALLBACK_USER_DATA, + MEMCACHED_CALLBACK_CLEANUP_FUNCTION, + MEMCACHED_CALLBACK_CLONE_FUNCTION, +} memcached_callback; + typedef enum { MEMCACHED_HASH_DEFAULT= 0, MEMCACHED_HASH_MD5, @@ -202,6 +211,8 @@ struct memcached_st { memcached_server_distribution distribution; void *user_data; unsigned int wheel[MEMCACHED_WHEEL_SIZE]; + clone_func on_clone; + cleanup_func on_cleanup; #ifdef NOT_USED /* Future Use */ uint8_t replicas; memcached_return warning; @@ -383,6 +394,13 @@ memcached_return memcached_fetch_execute(memcached_st *ptr, unsigned int number_of_callbacks ); +memcached_return memcached_callback_set(memcached_st *ptr, + memcached_callback flag, + void *data); +void *memcached_callback_get(memcached_st *ptr, + memcached_callback flag, + memcached_return *error); + /* Result Struct */ void memcached_result_free(memcached_result_st *result); memcached_result_st *memcached_result_create(memcached_st *ptr, diff --git a/lib/Makefile.am b/lib/Makefile.am index 7012db66..707da9bb 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -31,6 +31,7 @@ libmemcached_la_SOURCES = crc.c \ memcached.c \ memcached_auto.c \ memcached_behavior.c \ + memcached_callback.c \ memcached_connect.c \ memcached_delete.c \ memcached_do.c \ diff --git a/lib/memcached_behavior.c b/lib/memcached_behavior.c index 55b9da67..f7a89604 100644 --- a/lib/memcached_behavior.c +++ b/lib/memcached_behavior.c @@ -108,7 +108,8 @@ unsigned long long memcached_behavior_get(memcached_st *ptr, temp_flag= MEM_USE_KETAMA; break; case MEMCACHED_BEHAVIOR_USER_DATA: - return (unsigned long long)ptr->user_data; + return 0; + //return (unsigned long long)ptr->user_data; case MEMCACHED_BEHAVIOR_POLL_TIMEOUT: { return (unsigned long long)ptr->poll_timeout; diff --git a/lib/memcached_callback.c b/lib/memcached_callback.c new file mode 100644 index 00000000..7149e276 --- /dev/null +++ b/lib/memcached_callback.c @@ -0,0 +1,66 @@ +#include "common.h" +#include +#include +#include + +/* + These functions provide data and function callback support +*/ + +memcached_return memcached_callback_set(memcached_st *ptr, + memcached_callback flag, + void *data) +{ + switch (flag) + { + case MEMCACHED_CALLBACK_USER_DATA: + { + ptr->user_data= data; + break; + } + case MEMCACHED_CALLBACK_CLEANUP_FUNCTION: + { + cleanup_func func= (cleanup_func)data; + ptr->on_cleanup= func; + break; + } + case MEMCACHED_CALLBACK_CLONE_FUNCTION: + { + clone_func func= (clone_func)data; + ptr->on_clone= func; + break; + } + default: + return MEMCACHED_FAILURE; + } + + return MEMCACHED_SUCCESS; +} + +void *memcached_callback_get(memcached_st *ptr, + memcached_callback flag, + memcached_return *error) +{ + switch (flag) + { + case MEMCACHED_CALLBACK_USER_DATA: + { + *error= ptr->user_data ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE; + return (void *)ptr->user_data; + } + case MEMCACHED_CALLBACK_CLEANUP_FUNCTION: + { + *error= ptr->on_cleanup ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE; + return (void *)ptr->on_cleanup; + } + case MEMCACHED_CALLBACK_CLONE_FUNCTION: + { + *error= ptr->on_clone ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE; + return (void *)ptr->on_clone; + } + default: + WATCHPOINT_ASSERT(0); + *error= MEMCACHED_FAILURE; + return NULL; + } +} diff --git a/support/libmemcached.spec.in b/support/libmemcached.spec.in index 8e71d7da..88f3d477 100644 --- a/support/libmemcached.spec.in +++ b/support/libmemcached.spec.in @@ -74,6 +74,8 @@ memerror - Creates human readable messages from libmemecached error codes. %{_mandir}/man3/memcached_behavior_set.3.gz %{_mandir}/man3/memcached_cas.3.gz %{_mandir}/man3/memcached_cas_by_key.3.gz +%{_mandir}/man3/memcached_callback_get.3.gz +%{_mandir}/man3/memcached_callback_set.3.gz %{_mandir}/man3/memcached_clone.3.gz %{_mandir}/man3/memcached_create.3.gz %{_mandir}/man3/memcached_decrement.3.gz diff --git a/tests/function.c b/tests/function.c index 732f3b04..6ca61d7d 100644 --- a/tests/function.c +++ b/tests/function.c @@ -949,6 +949,55 @@ uint8_t add_host_test(memcached_st *memc) return 0; } +memcached_return clone_test_callback(memcached_st *parent, memcached_st *clone) +{ + return MEMCACHED_SUCCESS; +} + +memcached_return cleanup_test_callback(memcached_st *ptr) +{ + return MEMCACHED_SUCCESS; +} + +uint8_t callback_test(memcached_st *memc) +{ + /* Test User Data */ + { + int x= 5; + int *test_ptr; + memcached_return rc; + + rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_USER_DATA, &x); + assert(rc == MEMCACHED_SUCCESS); + test_ptr= (int *)memcached_callback_get(memc, MEMCACHED_CALLBACK_USER_DATA, &rc); + assert(*test_ptr == x); + } + + /* Test Clone Callback */ + { + clone_func temp_function; + memcached_return rc; + + rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, clone_test_callback); + assert(rc == MEMCACHED_SUCCESS); + temp_function= (clone_func)memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc); + assert(temp_function == clone_test_callback); + } + + /* Test Cleanup Callback */ + { + cleanup_func temp_function; + memcached_return rc; + + rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, cleanup_test_callback); + assert(rc == MEMCACHED_SUCCESS); + temp_function= (cleanup_func)memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc); + assert(temp_function == cleanup_test_callback); + } + + return 0; +} + /* We don't test the behavior itself, we test the switches */ uint8_t behavior_test(memcached_st *memc) { @@ -2084,6 +2133,7 @@ test_st tests[] ={ {"add_host_test", 0, add_host_test }, {"get_stats_keys", 0, get_stats_keys }, {"behavior_test", 0, get_stats_keys }, + {"callback_test", 0, get_stats_keys }, {0, 0, 0} };