Skip to content

Commit

Permalink
CLEANUP: Refactor for loops in update_continuum functionn
Browse files Browse the repository at this point in the history
  • Loading branch information
ing-eoking committed Jan 22, 2024
1 parent ab6114c commit 07fcfb3
Showing 1 changed file with 30 additions and 62 deletions.
92 changes: 30 additions & 62 deletions libmemcached/hosts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -350,79 +350,47 @@ 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)"));
}

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;
}
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;
}
}

Expand Down

0 comments on commit 07fcfb3

Please sign in to comment.