From: Brian Aker Date: Sun, 7 Oct 2007 20:43:31 +0000 (-0700) Subject: Finished up memcached_behavior_set/get() method. Tests and examples found in X-Git-Tag: 0.5~8 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=8040887dc668c712a5aafc8483fc5470fbc846a5;p=awesomized%2Flibmemcached Finished up memcached_behavior_set/get() method. Tests and examples found in test.c under behavior test. --- diff --git a/include/memcached.h b/include/memcached.h index 42cfe77b..1633872f 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -71,9 +71,7 @@ typedef enum { typedef enum { MEMCACHED_BEHAVIOR_NO_BLOCK, - MEMCACHED_BEHAVIOR_BLOCK, MEMCACHED_BEHAVIOR_TCP_NODELAY, - MEMCACHED_BEHAVIOR_TCP_DELAY, MEMCACHED_BEHAVIOR_MD5_HASHING, } memcached_behavior; @@ -163,6 +161,7 @@ memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity); void memcached_quit(memcached_st *ptr); char *memcached_strerror(memcached_st *ptr, memcached_return rc); memcached_return memcached_behavior_set(memcached_st *ptr, memcached_behavior flag, void *data); +unsigned long long memcached_behavior_get(memcached_st *ptr, memcached_behavior flag); /* All of the functions for adding data to the server */ memcached_return memcached_set(memcached_st *ptr, char *key, size_t key_length, diff --git a/lib/common.h b/lib/common.h index 607b6373..baf75242 100644 --- a/lib/common.h +++ b/lib/common.h @@ -18,10 +18,12 @@ #include "libmemcached_probes.h" -#define MEM_NO_BLOCK (1 << 0) -#define MEM_TCP_NODELAY (1 << 1) -#define MEM_REUSE_MEMORY (1 << 2) -#define MEM_USE_MD5 (1 << 3) +typedef enum { + MEM_NO_BLOCK= (1 << 0), + MEM_TCP_NODELAY= (1 << 1), + MEM_REUSE_MEMORY= (1 << 2), + MEM_USE_MD5= (1 << 3), +} memcached_flags; void md5_signature(unsigned char *key, unsigned int length, unsigned char *result); memcached_return memcached_connect(memcached_st *ptr); diff --git a/lib/memcached_behavior.c b/lib/memcached_behavior.c index 38b1e9bb..bf90d75e 100644 --- a/lib/memcached_behavior.c +++ b/lib/memcached_behavior.c @@ -3,6 +3,12 @@ #include #include +/* + This function is used to modify the behabior of running client. + + We quit all connections so we can reset the sockets. +*/ + memcached_return memcached_behavior_set(memcached_st *ptr, memcached_behavior flag, void *data) @@ -10,24 +16,27 @@ memcached_return memcached_behavior_set(memcached_st *ptr, switch (flag) { case MEMCACHED_BEHAVIOR_NO_BLOCK: - /* We quit all connections so we can reset the sockets */ - memcached_quit(ptr); - ptr->flags|= MEM_NO_BLOCK; - break; - case MEMCACHED_BEHAVIOR_BLOCK: - /* We quit all connections so we can reset the sockets */ - memcached_quit(ptr); - ptr->flags+= MEM_NO_BLOCK; - break; + { + unsigned int *truefalse= (unsigned int *)data; + memcached_quit(ptr); + if (truefalse) + ptr->flags|= MEM_NO_BLOCK; + else + ptr->flags+= MEM_NO_BLOCK; + break; + } + case MEMCACHED_BEHAVIOR_TCP_NODELAY: - /* We quit all connections so we can reset the sockets */ - memcached_quit(ptr); - ptr->flags|= MEM_TCP_NODELAY; - break; - case MEMCACHED_BEHAVIOR_TCP_DELAY: - /* We quit all connections so we can reset the sockets */ - memcached_quit(ptr); - ptr->flags+= MEM_TCP_NODELAY; + { + unsigned int *truefalse= (unsigned int *)data; + memcached_quit(ptr); + if (truefalse) + ptr->flags|= MEM_TCP_NODELAY; + else + ptr->flags+= MEM_TCP_NODELAY; + break; + } + case MEMCACHED_BEHAVIOR_MD5_HASHING: ptr->flags+= MEM_USE_MD5; break; @@ -35,3 +44,28 @@ memcached_return memcached_behavior_set(memcached_st *ptr, return MEMCACHED_SUCCESS; } + +unsigned long long memcached_behavior_get(memcached_st *ptr, + memcached_behavior flag) +{ + switch (flag) + { + case MEMCACHED_BEHAVIOR_NO_BLOCK: + if (ptr->flags & MEM_NO_BLOCK) + return 1; + else + return 0; + case MEMCACHED_BEHAVIOR_TCP_NODELAY: + if (ptr->flags & MEM_TCP_NODELAY) + return 1; + else + return 0; + case MEMCACHED_BEHAVIOR_MD5_HASHING: + if (ptr->flags & MEM_USE_MD5) + return 1; + else + return 0; + } + + return MEMCACHED_SUCCESS; +} diff --git a/lib/memcached_quit.c b/lib/memcached_quit.c index 64bfe27e..00b454c0 100644 --- a/lib/memcached_quit.c +++ b/lib/memcached_quit.c @@ -18,8 +18,6 @@ void memcached_quit(memcached_st *ptr) { if (ptr->hosts[x].fd != -1) { - char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - if (ptr->flags & MEM_NO_BLOCK) memcached_io_write(ptr, x, "quit\r\n", 6, 1); close(ptr->hosts[x].fd); diff --git a/src/memslap.c b/src/memslap.c index 280e4c9d..3de38f30 100644 --- a/src/memslap.c +++ b/src/memslap.c @@ -309,12 +309,13 @@ void *run_task(void *p) { thread_context_st *context= (thread_context_st *)p; memcached_st *memc; + unsigned int value= 1; memc= memcached_create(NULL); if (opt_non_blocking_io) - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL ); + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &value); if (opt_tcp_nodelay) - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, NULL ); + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &value); memcached_server_push(memc, context->servers); diff --git a/tests/output.res b/tests/output.res index dd73ee14..c9171642 100644 --- a/tests/output.res +++ b/tests/output.res @@ -1,4 +1,4 @@ -servers localhost:5555,localhost +servers localhost Error 0 -> SUCCESS Error 1 -> FAILURE Error 2 -> HOSTNAME LOOKUP FAILURE diff --git a/tests/test.c b/tests/test.c index 8da111b3..2c870d87 100644 --- a/tests/test.c +++ b/tests/test.c @@ -452,6 +452,31 @@ void add_host_test(memcached_st *memc) memcached_server_list_free(servers); } +/* We don't test the behavior itself, we test the switches */ +void behavior_test(memcached_st *memc) +{ + unsigned long long value; + unsigned int set= 1; + + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &set); + value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK); + assert(value == 1); + + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &set); + value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY); + assert(value == 1); + + set= 0; + + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &set); + value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK); + assert(value == 0); + + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &set); + value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY); + assert(value == 0); +} + void add_host_test1(memcached_st *memc) { unsigned int x; @@ -528,6 +553,7 @@ int main(int argc, char *argv[]) {"get_stats", 0, get_stats }, {"add_host_test", 0, add_host_test }, {"get_stats_keys", 0, get_stats_keys }, + {"behavior_test", 0, get_stats_keys }, {0, 0, 0} };