From: Brian Aker Date: Wed, 31 Oct 2007 07:44:20 +0000 (-0700) Subject: Update to interface (modified to better support lots of hashing functions). X-Git-Tag: 0.8~8 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=d1af612e477eca6e0d26b97ffe341a22390d7efa;p=m6w6%2Flibmemcached Update to interface (modified to better support lots of hashing functions). Yes.... I apologize now for making non-backwards compatible change. Few were probably using MD5 though since it was slow, I don't think this will be an issue. --- diff --git a/include/memcached.h b/include/memcached.h index e2b026df..7a9f3e56 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -66,13 +66,18 @@ typedef enum { typedef enum { MEMCACHED_BEHAVIOR_NO_BLOCK, MEMCACHED_BEHAVIOR_TCP_NODELAY, - MEMCACHED_BEHAVIOR_MD5_HASHING, - MEMCACHED_BEHAVIOR_CRC_HASHING, + MEMCACHED_BEHAVIOR_HASH, MEMCACHED_BEHAVIOR_KETAMA, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, } memcached_behavior; +typedef enum { + MEMCACHED_HASH_DEFAULT= 0, + MEMCACHED_HASH_MD5, + MEMCACHED_HASH_CRC, +} memcached_hash; + typedef enum { MEMCACHED_CONNECTION_UNKNOWN, MEMCACHED_CONNECTION_TCP, @@ -145,6 +150,7 @@ struct memcached_st { unsigned long long flags; int send_size; int recv_size; + memcached_hash hash; memcached_return warning; /* Future Use */ }; diff --git a/lib/memcached_behavior.c b/lib/memcached_behavior.c index 783d411b..fb04d722 100644 --- a/lib/memcached_behavior.c +++ b/lib/memcached_behavior.c @@ -32,11 +32,8 @@ memcached_return memcached_behavior_set(memcached_st *ptr, case MEMCACHED_BEHAVIOR_TCP_NODELAY: set_behavior_flag(ptr, MEM_TCP_NODELAY, data); break; - case MEMCACHED_BEHAVIOR_MD5_HASHING: - set_behavior_flag(ptr, MEM_USE_MD5, data); - break; - case MEMCACHED_BEHAVIOR_CRC_HASHING: - set_behavior_flag(ptr, MEM_USE_CRC, data); + case MEMCACHED_BEHAVIOR_HASH: + ptr->hash= *(memcached_hash *)(data); break; case MEMCACHED_BEHAVIOR_KETAMA: set_behavior_flag(ptr, MEM_USE_KETAMA, data); @@ -73,12 +70,8 @@ unsigned long long memcached_behavior_get(memcached_st *ptr, case MEMCACHED_BEHAVIOR_TCP_NODELAY: temp_flag= MEM_TCP_NODELAY; break; - case MEMCACHED_BEHAVIOR_MD5_HASHING: - temp_flag= MEM_USE_MD5; - break; - case MEMCACHED_BEHAVIOR_CRC_HASHING: - temp_flag= MEM_USE_CRC; - break; + case MEMCACHED_BEHAVIOR_HASH: + return ptr->hash; case MEMCACHED_BEHAVIOR_KETAMA: temp_flag= MEM_USE_KETAMA; break; diff --git a/lib/memcached_hash.c b/lib/memcached_hash.c index 073b0c5d..04319189 100644 --- a/lib/memcached_hash.c +++ b/lib/memcached_hash.c @@ -1,19 +1,33 @@ #include "common.h" +/* Defines */ +static uint64_t FNV_64_INIT= 0xcbf29ce484222325L; +static uint64_t FNV_64_PRIME= 0x100000001b3L; + +static uint32_t FNV_32_INIT= 2166136261L; +static uint32_t FNV_32_PRIME= 16777619; + /* Prototypes */ static unsigned int internal_generate_hash(char *key, size_t key_length); static uint32_t internal_generate_md5(char *key, size_t key_length); +static uint32_t internal_generate_md5(char *key, size_t key_length); unsigned int memcached_generate_hash(memcached_st *ptr, char *key, size_t key_length) { uint32_t hash; - if (ptr->flags & MEM_USE_MD5) + switch (ptr->hash) + { + case MEMCACHED_HASH_DEFAULT: + hash= internal_generate_hash(key, key_length); + break; + case MEMCACHED_HASH_MD5: hash= internal_generate_md5(key, key_length); - else if (ptr->flags & MEM_USE_CRC) + break; + case MEMCACHED_HASH_CRC: hash= hash_crc32(key, key_length); - else - hash= internal_generate_hash(key, key_length); + break; + } if (ptr->flags & MEM_USE_KETAMA) { diff --git a/tests/test.c b/tests/test.c index ab0e9097..ef6afb74 100644 --- a/tests/test.c +++ b/tests/test.c @@ -524,9 +524,10 @@ void behavior_test(memcached_st *memc) value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY); assert(value == 1); - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_MD5_HASHING, &set); - value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_MD5_HASHING); - assert(value == 1); + set= MEMCACHED_HASH_MD5; + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set); + value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH); + assert(value == MEMCACHED_HASH_MD5); set= 0; @@ -538,9 +539,15 @@ void behavior_test(memcached_st *memc) value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY); assert(value == 0); - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_MD5_HASHING, &set); - value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_MD5_HASHING); - assert(value == 0); + set= MEMCACHED_HASH_DEFAULT; + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set); + value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH); + assert(value == MEMCACHED_HASH_DEFAULT); + + set= MEMCACHED_HASH_CRC; + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &set); + value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH); + assert(value == MEMCACHED_HASH_CRC); value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE); assert(value > 0); @@ -812,14 +819,16 @@ memcached_return pre_nonblock(memcached_st *memc) memcached_return pre_md5(memcached_st *memc) { - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_MD5_HASHING, NULL); + memcached_hash value= MEMCACHED_HASH_MD5; + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value); return MEMCACHED_SUCCESS; } memcached_return pre_crc(memcached_st *memc) { - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_CRC_HASHING, NULL); + memcached_hash value= MEMCACHED_HASH_CRC; + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value); return MEMCACHED_SUCCESS; }