#include <libhashkit/common.h>
-static inline bool _hashkit_init(hashkit_st *self)
+static inline void _hashkit_init(hashkit_st *self)
{
self->base_hash.function= hashkit_one_at_a_time;
self->base_hash.context= NULL;
- self->distribution_hash.function= self->base_hash.function;
- self->flags.is_base_same_distributed= false;
- return true;
+ self->distribution_hash.function= hashkit_one_at_a_time;
+ self->distribution_hash.context= NULL;
+
+ self->flags.is_base_same_distributed= true;
}
static inline hashkit_st *_hashkit_create(hashkit_st *self)
{
- if (self == NULL)
+ if (not self)
{
- self= (hashkit_st *)malloc(sizeof(hashkit_st));
- if (self == NULL)
+ self= new hashkit_st;
+ if (not self)
{
return NULL;
}
hashkit_st *hashkit_create(hashkit_st *self)
{
self= _hashkit_create(self);
- if (! self)
+ if (not self)
return self;
- if (! _hashkit_init(self))
- {
- hashkit_free(self);
- }
+ _hashkit_init(self);
return self;
}
{
if (hashkit_is_allocated(self))
{
- free(self);
+ delete self;
}
}
bool hashkit_compare(const hashkit_st *first, const hashkit_st *second)
{
- if (first->base_hash.function == second->base_hash.function &&
- first->base_hash.context == second->base_hash.context &&
- first->distribution_hash.function == second->distribution_hash.function &&
- first->distribution_hash.context == second->distribution_hash.context &&
+ if (not first or not second)
+ return false;
+
+ if (first->base_hash.function == second->base_hash.function and
+ first->base_hash.context == second->base_hash.context and
+ first->distribution_hash.function == second->distribution_hash.function and
+ first->distribution_hash.context == second->distribution_hash.context and
first->flags.is_base_same_distributed == second->flags.is_base_same_distributed)
{
return true;
#define hashkit_is_allocated(__object) ((__object)->options.is_allocated)
#define hashkit_is_initialized(__object) ((__object)->options.is_initialized)
+
+#define hashkit_success(X) (X) == HASHKIT_SUCCESS
+#define hashkit_failed(X) (X) != HASHKIT_SUCCESS
+
#ifdef __cplusplus
} // extern "C"
#endif
case MEMCACHED_BEHAVIOR_HASH:
return hashkit_get_function(&ptr->hashkit);
case MEMCACHED_BEHAVIOR_KETAMA_HASH:
- return hashkit_get_function(&ptr->distribution_hashkit);
+ return hashkit_get_function(&ptr->hashkit);
case MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS:
case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT:
return ptr->server_failure_limit;
memcached_return_t memcached_behavior_set_distribution_hash(memcached_st *ptr, memcached_hash_t type)
{
- if (hashkit_set_function(&ptr->distribution_hashkit, (hashkit_hash_algorithm_t)type) == HASHKIT_SUCCESS)
+ if (hashkit_success(hashkit_set_distribution_function(&ptr->hashkit, (hashkit_hash_algorithm_t)type)))
return MEMCACHED_SUCCESS;
return memcached_set_error_string(ptr, MEMCACHED_INVALID_ARGUMENTS,
memcached_hash_t memcached_behavior_get_distribution_hash(memcached_st *ptr)
{
- return (memcached_hash_t)hashkit_get_function(&ptr->distribution_hashkit);
+ return (memcached_hash_t)hashkit_get_function(&ptr->hashkit);
}
const char *libmemcached_string_behavior(const memcached_behavior_t flag)
}
else
{
- uint32_t value= hashkit_digest(&ptr->distribution_hashkit, sort_host, (size_t)sort_host_length);
+ uint32_t value= hashkit_digest(&ptr->hashkit, sort_host, (size_t)sort_host_length);
ptr->ketama.continuum[continuum_index].index= host_index;
ptr->ketama.continuum[continuum_index++].value= value;
}
}
else
{
- uint32_t value= hashkit_digest(&ptr->distribution_hashkit, sort_host, (size_t)sort_host_length);
+ uint32_t value= hashkit_digest(&ptr->hashkit, sort_host, (size_t)sort_host_length);
ptr->ketama.continuum[continuum_index].index= host_index;
ptr->ketama.continuum[continuum_index++].value= value;
}
self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
- hashkit_st *hash_ptr;
- hash_ptr= hashkit_create(&self->hashkit);
- if (! hash_ptr)
+ if (not hashkit_create(&self->hashkit))
return false;
self->ketama.continuum= NULL;
self->user_data= NULL;
self->number_of_replicas= 0;
- hash_ptr= hashkit_create(&self->distribution_hashkit);
- if (! hash_ptr)
- return false;
self->allocators= memcached_allocators_return_default();
new_clone->retry_timeout= source->retry_timeout;
new_clone->distribution= source->distribution;
- hashkit_st *hash_ptr;
-
- hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
- if (! hash_ptr)
- {
- memcached_free(new_clone);
- return NULL;
- }
-
- hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
- if (! hash_ptr)
+ if (not hashkit_clone(&new_clone->hashkit, &source->hashkit))
{
memcached_free(new_clone);
return NULL;
void *user_data;
uint64_t query_id;
uint32_t number_of_replicas;
- hashkit_st distribution_hashkit;
memcached_result_st result;
struct {
}
test_true(memc_clone->get_key_failure == memc->get_key_failure);
test_true(hashkit_compare(&memc_clone->hashkit, &memc->hashkit));
- test_true(hashkit_compare(&memc_clone->distribution_hashkit, &memc->distribution_hashkit));
test_true(memc_clone->io_bytes_watermark == memc->io_bytes_watermark);
test_true(memc_clone->io_msg_watermark == memc->io_msg_watermark);
test_true(memc_clone->io_key_prefetch == memc->io_key_prefetch);