5 static uint64_t FNV_64_INIT
= UINT64_C(0xcbf29ce484222325);
6 static uint64_t FNV_64_PRIME
= UINT64_C(0x100000001b3);
8 static uint32_t FNV_32_INIT
= 2166136261UL;
9 static uint32_t FNV_32_PRIME
= 16777619;
12 static uint32_t internal_generate_hash(const char *key
, size_t key_length
);
13 static uint32_t internal_generate_md5(const char *key
, size_t key_length
);
15 uint32_t memcached_generate_hash_value(const char *key
, size_t key_length
, memcached_hash hash_algorithm
)
17 uint32_t hash
= 1; /* Just here to remove compile warning */
20 switch (hash_algorithm
)
22 case MEMCACHED_HASH_DEFAULT
:
23 hash
= internal_generate_hash(key
, key_length
);
25 case MEMCACHED_HASH_MD5
:
26 hash
= internal_generate_md5(key
, key_length
);
28 case MEMCACHED_HASH_CRC
:
29 hash
= ((hash_crc32(key
, key_length
) >> 16) & 0x7fff);
33 /* FNV hash'es lifted from Dustin Sallings work */
34 case MEMCACHED_HASH_FNV1_64
:
36 /* Thanks to pierre@demartines.com for the pointer */
39 temp_hash
= FNV_64_INIT
;
40 for (x
= 0; x
< key_length
; x
++)
42 temp_hash
*= FNV_64_PRIME
;
45 hash
= (uint32_t)temp_hash
;
48 case MEMCACHED_HASH_FNV1A_64
:
51 for (x
= 0; x
< key_length
; x
++)
58 case MEMCACHED_HASH_FNV1_32
:
61 for (x
= 0; x
< key_length
; x
++)
68 case MEMCACHED_HASH_FNV1A_32
:
71 for (x
= 0; x
< key_length
; x
++)
78 case MEMCACHED_HASH_HSIEH
:
80 #ifdef HAVE_HSIEH_HASH
81 hash
= hsieh_hash(key
, key_length
);
85 case MEMCACHED_HASH_MURMUR
:
87 hash
= murmur_hash(key
, key_length
);
90 case MEMCACHED_HASH_JENKINS
:
92 hash
=jenkins_hash(key
, key_length
, 13);
97 WATCHPOINT_ASSERT(hash_algorithm
);
104 uint32_t generate_hash(memcached_st
*ptr
, const char *key
, size_t key_length
)
106 uint32_t hash
= 1; /* Just here to remove compile warning */
109 WATCHPOINT_ASSERT(ptr
->number_of_hosts
);
111 if (ptr
->number_of_hosts
== 1)
114 hash
= memcached_generate_hash_value(key
, key_length
, ptr
->hash
);
115 WATCHPOINT_ASSERT(hash
);
119 static uint32_t dispatch_host(memcached_st
*ptr
, uint32_t hash
)
121 switch (ptr
->distribution
)
123 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
124 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
126 uint32_t num
= ptr
->continuum_points_counter
;
127 WATCHPOINT_ASSERT(ptr
->continuum
);
130 memcached_continuum_item_st
*begin
, *end
, *left
, *right
, *middle
;
131 begin
= left
= ptr
->continuum
;
132 end
= right
= ptr
->continuum
+ num
;
136 middle
= left
+ (right
- left
) / 2;
137 if (middle
->value
< hash
)
146 case MEMCACHED_DISTRIBUTION_MODULA
:
147 return hash
% ptr
->number_of_hosts
;
148 case MEMCACHED_DISTRIBUTION_RANDOM
:
149 return random() % ptr
->number_of_hosts
;
151 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
152 return hash
% ptr
->number_of_hosts
;
159 One day make this public, and have it return the actual memcached_server_st
160 to the calling application.
162 uint32_t memcached_generate_hash(memcached_st
*ptr
, const char *key
, size_t key_length
)
164 uint32_t hash
= 1; /* Just here to remove compile warning */
166 WATCHPOINT_ASSERT(ptr
->number_of_hosts
);
168 if (ptr
->number_of_hosts
== 1)
171 if (ptr
->flags
& MEM_HASH_WITH_PREFIX_KEY
)
173 size_t temp_length
= ptr
->prefix_key_length
+ key_length
;
174 char temp
[temp_length
];
176 if (temp_length
> MEMCACHED_MAX_KEY
-1)
179 strncpy(temp
, ptr
->prefix_key
, ptr
->prefix_key_length
);
180 strncpy(temp
+ ptr
->prefix_key_length
, key
, key_length
);
181 hash
= generate_hash(ptr
, temp
, temp_length
);
185 hash
= generate_hash(ptr
, key
, key_length
);
188 WATCHPOINT_ASSERT(hash
);
190 if (memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
) && ptr
->next_distribution_rebuild
) {
193 if (gettimeofday(&now
, NULL
) == 0 &&
194 now
.tv_sec
> ptr
->next_distribution_rebuild
)
195 run_distribution(ptr
);
198 return dispatch_host(ptr
, hash
);
201 static uint32_t internal_generate_hash(const char *key
, size_t key_length
)
203 const char *ptr
= key
;
209 value
+= (value
<< 10);
210 value
^= (value
>> 6);
212 value
+= (value
<< 3);
213 value
^= (value
>> 11);
214 value
+= (value
<< 15);
216 return value
== 0 ? 1 : value
;
219 static uint32_t internal_generate_md5(const char *key
, size_t key_length
)
221 unsigned char results
[16];
223 md5_signature((unsigned char*)key
, (unsigned int)key_length
, results
);
225 return ((uint32_t) (results
[3] & 0xFF) << 24)
226 | ((uint32_t) (results
[2] & 0xFF) << 16)
227 | ((uint32_t) (results
[1] & 0xFF) << 8)
228 | (results
[0] & 0xFF);