From: Date: Fri, 15 Feb 2008 05:32:18 +0000 (+0530) Subject: Added behavior method around testing of keys. X-Git-Tag: _20~16 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=8a86b578acc594d37a8638e3e0afba1286c4b6ca;p=m6w6%2Flibmemcached Added behavior method around testing of keys. --- diff --git a/ChangeLog b/ChangeLog index e6228373..35f91ed7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,3 @@ -0.17 - * More error messages on command line tools. - 0.16 * Work on the UDP protocol * Added get_by_key, set_by_key tests for C++ API @@ -10,9 +7,9 @@ clients with the same lists, will have same distribution. (Idea from Ross McFarland). MEMCACHED_BEHAVIOR_SORT_HOSTS was added to enable this support. - * Added MEMCACHED_BAD_KEY_PROVIDED error for auto, set, and get operations - when the library is compiled with --enable-debug. This additional code - will test for bad keys. + * Added MEMCACHED_BAD_KEY_PROVIDED error for auto, set, and get operations. + MEMCACHED_BEHAVIOR_VERIFY_KEY was added to enable this feature. + * More error messages on command line tools. 0.15 Tue Jan 29 14:55:44 PST 2008 * More work on the C++ API. diff --git a/docs/memcached_behavior.pod b/docs/memcached_behavior.pod index e71f1438..50e215f8 100755 --- a/docs/memcached_behavior.pod +++ b/docs/memcached_behavior.pod @@ -88,6 +88,11 @@ action that gets data causes this buffer to be be sent to the remote connection. Quiting the connection or closing down the connection will also cause the buffered data to be pushed to the remote connection. +=item MEMCACHED_BEHAVIOR_VERIFY_KEY + +Enabling this will cause libmemcached(3) to test all keys to verify that they +are valid keys. + =item MEMCACHED_BEHAVIOR_SORT_HOSTS Enabling this will cause hosts that are added to be placed in the host list in diff --git a/include/memcached.h b/include/memcached.h index 92000fe8..e0f829aa 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -101,6 +101,7 @@ typedef enum { MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, MEMCACHED_BEHAVIOR_USER_DATA, MEMCACHED_BEHAVIOR_SORT_HOSTS, + MEMCACHED_BEHAVIOR_VERIFY_KEY, } memcached_behavior; typedef enum { diff --git a/lib/common.h b/lib/common.h index ff54fe57..9e4cb5e1 100644 --- a/lib/common.h +++ b/lib/common.h @@ -66,6 +66,7 @@ typedef enum { MEM_SUPPORT_CAS= (1 << 7), MEM_BUFFER_REQUESTS= (1 << 8), MEM_USE_SORT_HOSTS= (1 << 9), + MEM_VERIFY_KEY= (1 << 10), } memcached_flags; /* Hashing algo */ @@ -111,13 +112,7 @@ memcached_return value_fetch(memcached_server_st *ptr, memcached_result_st *result); void server_list_free(memcached_st *ptr, memcached_server_st *servers); -memcached_return key_proof(char **keys, size_t *key_length, - unsigned int number_of_keys); - -#ifdef HAVE_DEBUG -#define key_test(A,B,C) key_proof(A,B,C) -#else -#define key_test(A,B,C) MEMCACHED_SUCCESS -#endif +memcached_return memcachd_key_test(char **keys, size_t *key_length, + unsigned int number_of_keys); #endif /* __COMMON_H__ */ diff --git a/lib/memcached_auto.c b/lib/memcached_auto.c index 36780dc5..0d0fc917 100644 --- a/lib/memcached_auto.c +++ b/lib/memcached_auto.c @@ -17,7 +17,7 @@ static memcached_return memcached_auto(memcached_st *ptr, if (ptr->hosts == NULL || ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; - if (key_test(&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED) + if ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test(&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) return MEMCACHED_BAD_KEY_PROVIDED; server_key= memcached_generate_hash(ptr, key, key_length); diff --git a/lib/memcached_behavior.c b/lib/memcached_behavior.c index 1458c1a9..42eacba9 100644 --- a/lib/memcached_behavior.c +++ b/lib/memcached_behavior.c @@ -11,7 +11,7 @@ void set_behavior_flag(memcached_st *ptr, memcached_flags temp_flag, void *data) { - unsigned int *truefalse= (unsigned int *)data; + unsigned int truefalse= *(unsigned int *)data; memcached_quit(ptr); if (truefalse) @@ -46,6 +46,9 @@ memcached_return memcached_behavior_set(memcached_st *ptr, case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS: set_behavior_flag(ptr, MEM_USE_CACHE_LOOKUPS, data); break; + case MEMCACHED_BEHAVIOR_VERIFY_KEY: + set_behavior_flag(ptr, MEM_VERIFY_KEY, data); + break; case MEMCACHED_BEHAVIOR_KETAMA: set_behavior_flag(ptr, MEM_USE_KETAMA, data); break; @@ -103,6 +106,9 @@ unsigned long long memcached_behavior_get(memcached_st *ptr, case MEMCACHED_BEHAVIOR_TCP_NODELAY: temp_flag= MEM_TCP_NODELAY; break; + case MEMCACHED_BEHAVIOR_VERIFY_KEY: + temp_flag= MEM_VERIFY_KEY; + break; case MEMCACHED_BEHAVIOR_DISTRIBUTION: return ptr->distribution; case MEMCACHED_BEHAVIOR_HASH: @@ -115,7 +121,6 @@ unsigned long long memcached_behavior_get(memcached_st *ptr, break; case MEMCACHED_BEHAVIOR_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_get.c b/lib/memcached_get.c index cfbb13f4..8cc7ce30 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -75,7 +75,7 @@ memcached_return memcached_mget_by_key(memcached_st *ptr, if (ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; - if (key_test(keys, key_length, number_of_keys) == MEMCACHED_BAD_KEY_PROVIDED) + if ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test(keys, key_length, number_of_keys) == MEMCACHED_BAD_KEY_PROVIDED)) return MEMCACHED_BAD_KEY_PROVIDED; if (ptr->flags & MEM_SUPPORT_CAS) diff --git a/lib/memcached_storage.c b/lib/memcached_storage.c index 30585095..443389e0 100644 --- a/lib/memcached_storage.c +++ b/lib/memcached_storage.c @@ -66,7 +66,7 @@ static inline memcached_return memcached_send(memcached_st *ptr, if (ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; - if (key_test(&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED) + if ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test(&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) return MEMCACHED_BAD_KEY_PROVIDED; server_key= memcached_generate_hash(ptr, master_key, master_key_length); diff --git a/tests/function.c b/tests/function.c index b971134e..7c61db78 100644 --- a/tests/function.c +++ b/tests/function.c @@ -463,19 +463,34 @@ uint8_t flush_test(memcached_st *memc) uint8_t bad_key_test(memcached_st *memc) { -#ifdef HAVE_DEBUG memcached_return rc; char *key= "foo bad"; char *string; size_t string_length; uint32_t flags; + memcached_st *clone; + unsigned int set= 1; - string= memcached_get(memc, key, strlen(key), + clone= memcached_clone(NULL, memc); + assert(clone); + + (void)memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, &set); + + string= memcached_get(clone, key, strlen(key), &string_length, &flags, &rc); assert(rc == MEMCACHED_BAD_KEY_PROVIDED); assert(string_length == 0); assert(!string); -#endif + + set= 0; + (void)memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, &set); + string= memcached_get(clone, key, strlen(key), + &string_length, &flags, &rc); + assert(rc == MEMCACHED_NOTFOUND); + assert(string_length == 0); + assert(!string); + + memcached_free(clone); return 0; }