5 static uint64_t FNV_64_INIT
= 0xcbf29ce484222325LL
;
6 static uint64_t FNV_64_PRIME
= 0x100000001b3LL
;
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);
99 uint32_t generate_hash(memcached_st
*ptr
, const char *key
, size_t key_length
)
101 uint32_t hash
= 1; /* Just here to remove compile warning */
104 WATCHPOINT_ASSERT(ptr
->number_of_hosts
);
106 if (ptr
->number_of_hosts
== 1)
109 hash
= memcached_generate_hash_value(key
, key_length
, ptr
->hash
);
110 WATCHPOINT_ASSERT(hash
);
114 static uint32_t dispatch_host(memcached_st
*ptr
, uint32_t hash
)
116 switch (ptr
->distribution
)
118 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
119 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
121 uint32_t num
= ptr
->continuum_points_counter
;
122 WATCHPOINT_ASSERT(ptr
->continuum
);
125 memcached_continuum_item_st
*begin
, *end
, *left
, *right
, *middle
;
126 begin
= left
= ptr
->continuum
;
127 end
= right
= ptr
->continuum
+ num
;
131 middle
= left
+ (right
- left
) / 2;
132 if (middle
->value
< hash
)
142 case MEMCACHED_DISTRIBUTION_MODULA
:
143 return hash
% ptr
->number_of_hosts
;
144 case MEMCACHED_DISTRIBUTION_RANDOM
:
145 return random() % ptr
->number_of_hosts
;
147 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
148 return hash
% ptr
->number_of_hosts
;
155 One day make this public, and have it return the actual memcached_server_st
156 to the calling application.
158 uint32_t memcached_generate_hash(memcached_st
*ptr
, const char *key
, size_t key_length
)
160 uint32_t hash
= 1; /* Just here to remove compile warning */
162 WATCHPOINT_ASSERT(ptr
->number_of_hosts
);
164 if (ptr
->number_of_hosts
== 1)
167 if (ptr
->flags
& MEM_HASH_WITH_PREFIX_KEY
)
169 int temp_len
= ptr
->prefix_key_length
+ key_length
;
170 char *temp
= (char *)malloc(temp_len
);
171 strncpy(temp
, ptr
->prefix_key
, ptr
->prefix_key_length
);
172 strncpy(temp
+ ptr
->prefix_key_length
, key
, key_length
);
173 hash
= generate_hash(ptr
, temp
, temp_len
);
178 hash
= generate_hash(ptr
, key
, key_length
);
181 WATCHPOINT_ASSERT(hash
);
183 if (memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
) && ptr
->next_distribution_rebuild
) {
186 if (gettimeofday(&now
, NULL
) == 0 &&
187 now
.tv_sec
> ptr
->next_distribution_rebuild
)
188 run_distribution(ptr
);
191 return dispatch_host(ptr
, hash
);
194 static uint32_t internal_generate_hash(const char *key
, size_t key_length
)
196 const char *ptr
= key
;
202 value
+= (value
<< 10);
203 value
^= (value
>> 6);
205 value
+= (value
<< 3);
206 value
^= (value
>> 11);
207 value
+= (value
<< 15);
209 return value
== 0 ? 1 : value
;
212 static uint32_t internal_generate_md5(const char *key
, size_t key_length
)
214 unsigned char results
[16];
216 md5_signature((unsigned char*)key
, (unsigned int)key_length
, results
);
218 return ((uint32_t) (results
[3] & 0xFF) << 24)
219 | ((uint32_t) (results
[2] & 0xFF) << 16)
220 | ((uint32_t) (results
[1] & 0xFF) << 8)
221 | (results
[0] & 0xFF);