From: Date: Fri, 4 Apr 2008 07:10:42 +0000 (-0700) Subject: Fix for always sort. X-Git-Tag: 0.20~1^2~28 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=151d417315c7ef9a1eaa0fe21150d8e0ec9eb440;p=awesomized%2Flibmemcached Fix for always sort. --- diff --git a/ChangeLog b/ChangeLog index 3d363ffa..03b15024 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ 0.19 + * Fixed bug where sort was always occuring on hosts * Logic fix in branch prediction (thanks Jay!) * Read through cached support. * Fixed for cas by key operation. diff --git a/libmemcached/memcached_hosts.c b/libmemcached/memcached_hosts.c index b97476a5..f816dd5b 100644 --- a/libmemcached/memcached_hosts.c +++ b/libmemcached/memcached_hosts.c @@ -116,7 +116,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) + if (ptr->number_of_hosts > 1 && ptr->flags & MEMCACHED_BEHAVIOR_SORT_HOSTS) qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers); rebalance_wheel(ptr); @@ -180,7 +180,7 @@ static memcached_return server_add(memcached_st *ptr, char *hostname, host_reset(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, type); ptr->number_of_hosts++; - if (ptr->number_of_hosts > 1) + if (ptr->number_of_hosts > 1 && ptr->flags & MEMCACHED_BEHAVIOR_SORT_HOSTS) qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers); ptr->hosts[0].count= ptr->number_of_hosts; @@ -221,14 +221,9 @@ memcached_server_st *memcached_server_list_append(memcached_server_st *ptr, host_reset(NULL, &new_host_list[count-1], hostname, port, MEMCACHED_CONNECTION_TCP); - /* We alway sort lists by default */ - if (new_host_list[0].count > 1) - qsort(new_host_list, count, sizeof(memcached_server_st), compare_servers); - /* Backwards compatibility hack */ new_host_list[0].count= count; - *error= MEMCACHED_SUCCESS; return new_host_list; } diff --git a/tests/function.c b/tests/function.c index a306a4a4..826e6c82 100644 --- a/tests/function.c +++ b/tests/function.c @@ -102,6 +102,47 @@ uint8_t server_sort_test(memcached_st *ptr) return 0; } +memcached_return server_display_unsort_function(memcached_st *ptr, memcached_server_st *server, void *context) +{ + /* Do Nothing */ + uint32_t x= *((uint32_t *)(context)); + + assert(test_ports[x] == server->port); + *((uint32_t *)(context))= ++x; + + return MEMCACHED_SUCCESS; +} + +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 */ + memcached_return rc; + memcached_server_function callbacks[1]; + memcached_st *local_memc; + + local_memc= memcached_create(NULL); + assert(local_memc); + memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1); + + for (x= 0; x < TEST_PORT_COUNT; x++) + { + test_ports[x]= random() % 64000; + rc= memcached_server_add(local_memc, "localhost", test_ports[x]); + assert(local_memc->number_of_hosts == x+1); + assert(local_memc->hosts[0].count == x+1); + assert(rc == MEMCACHED_SUCCESS); + } + + callbacks[0]= server_display_unsort_function; + memcached_server_cursor(local_memc, callbacks, (void *)&counter, 1); + + + memcached_free(local_memc); + + return 0; +} + uint8_t allocation_test(memcached_st *not_used) { memcached_st *memc; @@ -2522,6 +2563,7 @@ test_st tests[] ={ {"init", 0, init_test }, {"allocation", 0, allocation_test }, {"server_list_null_test", 0, server_list_null_test}, + {"server_unsort", 0, server_unsort_test}, {"server_sort", 0, server_sort_test}, {"clone_test", 0, clone_test }, {"error", 0, error_test },