4 static uint64_t FNV_64_INIT
= 0xcbf29ce484222325LL
;
5 static uint64_t FNV_64_PRIME
= 0x100000001b3LL
;
7 static uint32_t FNV_32_INIT
= 2166136261UL;
8 static uint32_t FNV_32_PRIME
= 16777619;
11 static uint32_t internal_generate_hash(char *key
, size_t key_length
);
12 static uint32_t internal_generate_md5(char *key
, size_t key_length
);
13 static uint32_t internal_generate_ketama_md5(char *key
, size_t key_length
);
15 unsigned int memcached_generate_hash(memcached_st
*ptr
, char *key
, size_t key_length
)
17 uint32_t hash
= 1; /* Just here to remove compile warning */
20 WATCHPOINT_ASSERT(ptr
->number_of_hosts
);
22 if (ptr
->number_of_hosts
== 1)
27 case MEMCACHED_HASH_DEFAULT
:
28 hash
= internal_generate_hash(key
, key_length
);
30 case MEMCACHED_HASH_MD5
:
31 hash
= internal_generate_md5(key
, key_length
);
33 case MEMCACHED_HASH_CRC
:
34 hash
= ((hash_crc32(key
, key_length
) >> 16) & 0x7fff);
38 /* FNV hash'es lifted from Dustin Sallings work */
39 case MEMCACHED_HASH_FNV1_64
:
41 /* Thanks to pierre@demartines.com for the pointer */
44 temp_hash
= FNV_64_INIT
;
45 for (x
= 0; x
< key_length
; x
++)
47 temp_hash
*= FNV_64_PRIME
;
50 hash
= (uint32_t)temp_hash
;
53 case MEMCACHED_HASH_FNV1A_64
:
56 for (x
= 0; x
< key_length
; x
++)
63 case MEMCACHED_HASH_FNV1_32
:
66 for (x
= 0; x
< key_length
; x
++)
73 case MEMCACHED_HASH_FNV1A_32
:
76 for (x
= 0; x
< key_length
; x
++)
83 case MEMCACHED_HASH_KETAMA
:
85 hash
= internal_generate_ketama_md5(key
, key_length
);
88 case MEMCACHED_HASH_HSIEH
:
90 hash
= hsieh_hash(key
, key_length
);
95 WATCHPOINT_ASSERT(hash
);
97 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_MODULA
)
99 return hash
% ptr
->number_of_hosts
;
103 unsigned int server_key
;
105 server_key
= hash
% MEMCACHED_WHEEL_SIZE
;
107 return ptr
->wheel
[server_key
];
111 static uint32_t internal_generate_hash(char *key
, size_t key_length
)
119 value
+= (value
<< 10);
120 value
^= (value
>> 6);
122 value
+= (value
<< 3);
123 value
^= (value
>> 11);
124 value
+= (value
<< 15);
126 return value
== 0 ? 1 : value
;
129 static uint32_t internal_generate_md5(char *key
, size_t key_length
)
131 unsigned char results
[16];
133 md5_signature((unsigned char*)key
, (unsigned int)key_length
, results
);
135 return (uint32_t)(( results
[3] << 24 )
136 | ( results
[2] << 16 )
137 | ( results
[1] << 8 )
141 static uint32_t internal_generate_ketama_md5(char *key
, size_t key_length
)
143 unsigned char results
[16];
145 md5_signature((unsigned char*)key
, (unsigned int)key_length
, results
);
147 return ((uint32_t) (results
[3] & 0xFF) << 24)
148 | ((uint32_t) (results
[2] & 0xFF) << 16)
149 | ((uint32_t) (results
[1] & 0xFF) << 8)
150 | (results
[0] & 0xFF);