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
);
93 case MEMCACHED_HASH_MURMUR
:
95 hash
= murmur_hash(key
, key_length
);
100 WATCHPOINT_ASSERT(hash
);
102 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_MODULA
)
104 return hash
% ptr
->number_of_hosts
;
108 unsigned int server_key
;
110 server_key
= hash
% MEMCACHED_WHEEL_SIZE
;
112 return ptr
->wheel
[server_key
];
116 static uint32_t internal_generate_hash(char *key
, size_t key_length
)
124 value
+= (value
<< 10);
125 value
^= (value
>> 6);
127 value
+= (value
<< 3);
128 value
^= (value
>> 11);
129 value
+= (value
<< 15);
131 return value
== 0 ? 1 : value
;
134 static uint32_t internal_generate_md5(char *key
, size_t key_length
)
136 unsigned char results
[16];
138 md5_signature((unsigned char*)key
, (unsigned int)key_length
, results
);
140 return (uint32_t)(( results
[3] << 24 )
141 | ( results
[2] << 16 )
142 | ( results
[1] << 8 )
146 static uint32_t internal_generate_ketama_md5(char *key
, size_t key_length
)
148 unsigned char results
[16];
150 md5_signature((unsigned char*)key
, (unsigned int)key_length
, results
);
152 return ((uint32_t) (results
[3] & 0xFF) << 24)
153 | ((uint32_t) (results
[2] & 0xFF) << 16)
154 | ((uint32_t) (results
[1] & 0xFF) << 8)
155 | (results
[0] & 0xFF);