X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fbehavior.cc;h=fd096db24f5043cabceb7f2caae15f0b924205f5;hb=1d3ffcb8031a12f06404a0700d556901b773c409;hp=8ffbd665ec570ae29d76fddc7571d9804d6849fe;hpb=1054bac0d64844c968d1dcb6d5478315bfd4d7a8;p=m6w6%2Flibmemcached diff --git a/libmemcached/behavior.cc b/libmemcached/behavior.cc index 8ffbd665..fd096db2 100644 --- a/libmemcached/behavior.cc +++ b/libmemcached/behavior.cc @@ -42,6 +42,26 @@ #include #include +static bool __is_ketama(memcached_st *ptr) +{ + switch (ptr->distribution) + { + case MEMCACHED_DISTRIBUTION_CONSISTENT: + case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: + case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY: + case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED: + return true; + + case MEMCACHED_DISTRIBUTION_MODULA: + case MEMCACHED_DISTRIBUTION_RANDOM: + case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET: + case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX: + break; + } + + return false; +} + /* This function is used to modify the behavior of running client. @@ -162,13 +182,17 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr, case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED: { + if (bool(data) == false) + { + return memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_KETAMA, true); + } + (void)memcached_behavior_set_key_hash(ptr, MEMCACHED_HASH_MD5); (void)memcached_behavior_set_distribution_hash(ptr, MEMCACHED_HASH_MD5); - ptr->ketama.weighted= bool(data); /** @note We try to keep the same distribution going. This should be deprecated and rewritten. */ - return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA); + return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED); } case MEMCACHED_BEHAVIOR_HASH: @@ -287,7 +311,7 @@ bool _is_auto_eject_host(const memcached_st *ptr) uint64_t memcached_behavior_get(memcached_st *ptr, const memcached_behavior_t flag) { - if (not ptr) + if (ptr == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } @@ -331,13 +355,17 @@ uint64_t memcached_behavior_get(memcached_st *ptr, return ptr->flags.verify_key; case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED: - return ptr->ketama.weighted; + if (__is_ketama(ptr)) + { + return ptr->ketama.weighted; + } + return false; case MEMCACHED_BEHAVIOR_DISTRIBUTION: return ptr->distribution; case MEMCACHED_BEHAVIOR_KETAMA: - return (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA) ? (uint64_t) 1 : 0; + return __is_ketama(ptr); case MEMCACHED_BEHAVIOR_HASH: return hashkit_get_function(&ptr->hashkit); @@ -487,23 +515,37 @@ uint64_t memcached_behavior_get(memcached_st *ptr, memcached_return_t memcached_behavior_set_distribution(memcached_st *ptr, memcached_server_distribution_t type) { - if (type < MEMCACHED_DISTRIBUTION_CONSISTENT_MAX) + switch (type) { - if (MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED) - { - ptr->ketama.weighted= true; - } - else - { - ptr->ketama.weighted= false; - } + case MEMCACHED_DISTRIBUTION_MODULA: + break; + + case MEMCACHED_DISTRIBUTION_CONSISTENT: + case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: + ptr->ketama.weighted= false; + break; + + case MEMCACHED_DISTRIBUTION_RANDOM: + break; + + case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY: + break; + + case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED: + ptr->ketama.weighted= true; + break; + + case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET: + break; - ptr->distribution= type; - return run_distribution(ptr); + default: + case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX: + return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("Invalid memcached_server_distribution_t")); } + ptr->distribution= type; - return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, - memcached_literal_param("Invalid memcached_server_distribution_t")); + return run_distribution(ptr); } @@ -531,7 +573,9 @@ memcached_hash_t memcached_behavior_get_key_hash(memcached_st *ptr) memcached_return_t memcached_behavior_set_distribution_hash(memcached_st *ptr, memcached_hash_t type) { if (hashkit_success(hashkit_set_distribution_function(&ptr->hashkit, (hashkit_hash_algorithm_t)type))) + { return MEMCACHED_SUCCESS; + } return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid memcached_hash_t()"));