From: Date: Fri, 4 Apr 2008 07:49:51 +0000 (-0700) Subject: If you call sort, a quit occurs, and the current servers get sorted. X-Git-Tag: 0.20~1^2~26 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=8f4f53d570ffa167f08912242c1a41d6f314ac0b;p=awesomized%2Flibmemcached If you call sort, a quit occurs, and the current servers get sorted. --- diff --git a/libmemcached/common.h b/libmemcached/common.h index 7e21afff..de55dbbe 100644 --- a/libmemcached/common.h +++ b/libmemcached/common.h @@ -109,4 +109,6 @@ void server_list_free(memcached_st *ptr, memcached_server_st *servers); memcached_return memcachd_key_test(char **keys, size_t *key_length, unsigned int number_of_keys); +void sort_hosts(memcached_st *ptr); + #endif /* __COMMON_H__ */ diff --git a/libmemcached/memcached_behavior.c b/libmemcached/memcached_behavior.c index 677e022f..1de8f495 100644 --- a/libmemcached/memcached_behavior.c +++ b/libmemcached/memcached_behavior.c @@ -54,8 +54,17 @@ memcached_return memcached_behavior_set(memcached_st *ptr, set_behavior_flag(ptr, MEM_USE_KETAMA, data); break; case MEMCACHED_BEHAVIOR_SORT_HOSTS: - set_behavior_flag(ptr, MEM_USE_SORT_HOSTS, data); - break; + { + set_behavior_flag(ptr, MEM_USE_SORT_HOSTS, data); + + if (ptr->flags & MEM_USE_SORT_HOSTS) + { + memcached_quit(ptr); + sort_hosts(ptr); + } + + break; + } case MEMCACHED_BEHAVIOR_POLL_TIMEOUT: ptr->poll_timeout= (int32_t)data; break; diff --git a/libmemcached/memcached_hosts.c b/libmemcached/memcached_hosts.c index d6832ef3..87a8cbd3 100644 --- a/libmemcached/memcached_hosts.c +++ b/libmemcached/memcached_hosts.c @@ -49,6 +49,11 @@ static int compare_servers(const void *p1, const void *p2) return return_value; } +void sort_hosts(memcached_st *ptr) +{ + qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers); +} + static void host_reset(memcached_st *ptr, memcached_server_st *host, char *hostname, unsigned int port, memcached_connection type) @@ -117,7 +122,7 @@ memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *l ptr->hosts[0].count= ptr->number_of_hosts; if (ptr->number_of_hosts > 1 && ptr->flags & MEM_USE_SORT_HOSTS) - qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers); + sort_hosts(ptr); rebalance_wheel(ptr); @@ -181,7 +186,7 @@ static memcached_return server_add(memcached_st *ptr, char *hostname, ptr->number_of_hosts++; if (ptr->number_of_hosts > 1 && ptr->flags & MEM_USE_SORT_HOSTS) - qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers); + sort_hosts(ptr); ptr->hosts[0].count= ptr->number_of_hosts; diff --git a/tests/function.c b/tests/function.c index 777be74c..03df4da2 100644 --- a/tests/function.c +++ b/tests/function.c @@ -117,6 +117,7 @@ uint8_t server_unsort_test(memcached_st *ptr) { uint8_t x; uint32_t counter= 0; /* Prime the value for the assert in server_display_function */ + uint32_t bigger= 0; /* Prime the value for the assert in server_display_function */ memcached_return rc; memcached_server_function callbacks[1]; memcached_st *local_memc; @@ -136,6 +137,11 @@ uint8_t server_unsort_test(memcached_st *ptr) callbacks[0]= server_display_unsort_function; memcached_server_cursor(local_memc, callbacks, (void *)&counter, 1); + /* Now we sort old data! */ + memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1); + callbacks[0]= server_display_function; + memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1); + memcached_free(local_memc);