From 72766e20e068799a28f1e597b533222ea72ad9d5 Mon Sep 17 00:00:00 2001 From: ing-eoking Date: Sun, 21 Jan 2024 21:55:02 -0500 Subject: [PATCH] CLEANUP: Refactor for loops in update_continuum function --- libmemcached/hosts.cc | 89 +++++++++++++------------------------------ 1 file changed, 26 insertions(+), 63 deletions(-) diff --git a/libmemcached/hosts.cc b/libmemcached/hosts.cc index 8e236ed6..3a1bce8e 100644 --- a/libmemcached/hosts.cc +++ b/libmemcached/hosts.cc @@ -350,79 +350,42 @@ static memcached_return_t update_continuum(memcached_st *ptr) } - if (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY) + + for (uint32_t pointer_index= 0; + pointer_index < pointer_per_server / pointer_per_hash; + pointer_index++) { - for (uint32_t pointer_index= 0; - pointer_index < pointer_per_server / pointer_per_hash; - pointer_index++) - { - char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= ""; - int sort_host_length; + char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= ""; + int sort_host_length; - // Spymemcached ketema key format is: hostname/ip:port-index - // If hostname is not available then: ip:port-index + if (list[host_index].port == MEMCACHED_DEFAULT_PORT && + ptr->distribution != MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY) + { + sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH, + "%s-%u", + list[host_index].hostname, + pointer_index); + } + else + { sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH, "%s:%u-%u", list[host_index].hostname, (uint32_t)list[host_index].port, pointer_index); - - if (sort_host_length >= MEMCACHED_MAX_HOST_SORT_LENGTH || sort_host_length < 0) - { - return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, - memcached_literal_param("snprintf(MEMCACHED_MAX_HOST_SORT_LENGTH)")); - } - - if (DEBUG) - { - fprintf(stdout, "update_continuum: key is %s\n", sort_host); - } - - for (uint32_t x= 0; x < pointer_per_hash; x++) - { - uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x); - new_continuum[continuum_index].index= host_index; - new_continuum[continuum_index++].value= value; - } } - } - else - { - for (uint32_t pointer_index= 0; - pointer_index < pointer_per_server / pointer_per_hash; - pointer_index++) - { - char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= ""; - int sort_host_length; - - if (list[host_index].port == MEMCACHED_DEFAULT_PORT) - { - sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH, - "%s-%u", - list[host_index].hostname, - pointer_index); - } - else - { - sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH, - "%s:%u-%u", - list[host_index].hostname, - (uint32_t)list[host_index].port, - pointer_index); - } - - if (sort_host_length >= MEMCACHED_MAX_HOST_SORT_LENGTH || sort_host_length < 0) - { - return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, - memcached_literal_param("snprintf(MEMCACHED_MAX_HOST_SORT_LENGTH)")); - } + if (sort_host_length >= MEMCACHED_MAX_HOST_SORT_LENGTH || sort_host_length < 0) + { + return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, + memcached_literal_param("snprintf(MEMCACHED_MAX_HOST_SORT_LENGTH)")); + } - for (uint32_t x = 0; x < pointer_per_hash; x++) { - uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x); - new_continuum[continuum_index].index= host_index; - new_continuum[continuum_index++].value= value; - } + for (uint32_t x= 0; x < pointer_per_hash; x++) + { + uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x); + new_continuum[continuum_index].index= host_index; + new_continuum[continuum_index++].value= value; } }