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
;
43 temp_hash
^= (uint64_t)key
[x
];
45 hash
= (uint32_t)temp_hash
;
48 case MEMCACHED_HASH_FNV1A_64
:
50 hash
= (uint32_t) FNV_64_INIT
;
51 for (x
= 0; x
< key_length
; x
++)
53 uint32_t val
= (uint32_t)key
[x
];
55 hash
*= (uint32_t) FNV_64_PRIME
;
59 case MEMCACHED_HASH_FNV1_32
:
62 for (x
= 0; x
< key_length
; x
++)
64 uint32_t val
= (uint32_t)key
[x
];
70 case MEMCACHED_HASH_FNV1A_32
:
73 for (x
= 0; x
< key_length
; x
++)
75 uint32_t val
= (uint32_t)key
[x
];
81 case MEMCACHED_HASH_HSIEH
:
83 #ifdef HAVE_HSIEH_HASH
84 hash
= hsieh_hash(key
, key_length
);
88 case MEMCACHED_HASH_MURMUR
:
90 hash
= murmur_hash(key
, key_length
);
93 case MEMCACHED_HASH_JENKINS
:
95 hash
=jenkins_hash(key
, key_length
, 13);
100 WATCHPOINT_ASSERT(hash_algorithm
);
107 uint32_t generate_hash(memcached_st
*ptr
, const char *key
, size_t key_length
)
109 uint32_t hash
= 1; /* Just here to remove compile warning */
112 WATCHPOINT_ASSERT(ptr
->number_of_hosts
);
114 if (ptr
->number_of_hosts
== 1)
117 hash
= memcached_generate_hash_value(key
, key_length
, ptr
->hash
);
118 WATCHPOINT_ASSERT(hash
);
122 static uint32_t dispatch_host(memcached_st
*ptr
, uint32_t hash
)
124 switch (ptr
->distribution
)
126 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
127 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
129 uint32_t num
= ptr
->continuum_points_counter
;
130 WATCHPOINT_ASSERT(ptr
->continuum
);
133 memcached_continuum_item_st
*begin
, *end
, *left
, *right
, *middle
;
134 begin
= left
= ptr
->continuum
;
135 end
= right
= ptr
->continuum
+ num
;
139 middle
= left
+ (right
- left
) / 2;
140 if (middle
->value
< hash
)
149 case MEMCACHED_DISTRIBUTION_MODULA
:
150 return hash
% ptr
->number_of_hosts
;
151 case MEMCACHED_DISTRIBUTION_RANDOM
:
152 return (uint32_t) random() % ptr
->number_of_hosts
;
154 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
155 return hash
% ptr
->number_of_hosts
;
162 One day make this public, and have it return the actual memcached_server_st
163 to the calling application.
165 uint32_t memcached_generate_hash(memcached_st
*ptr
, const char *key
, size_t key_length
)
167 uint32_t hash
= 1; /* Just here to remove compile warning */
169 WATCHPOINT_ASSERT(ptr
->number_of_hosts
);
171 if (ptr
->number_of_hosts
== 1)
174 if (ptr
->flags
& MEM_HASH_WITH_PREFIX_KEY
)
176 size_t temp_length
= ptr
->prefix_key_length
+ key_length
;
177 char temp
[temp_length
];
179 if (temp_length
> MEMCACHED_MAX_KEY
-1)
182 strncpy(temp
, ptr
->prefix_key
, ptr
->prefix_key_length
);
183 strncpy(temp
+ ptr
->prefix_key_length
, key
, key_length
);
184 hash
= generate_hash(ptr
, temp
, temp_length
);
188 hash
= generate_hash(ptr
, key
, key_length
);
191 WATCHPOINT_ASSERT(hash
);
193 if (memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
) && ptr
->next_distribution_rebuild
) {
196 if (gettimeofday(&now
, NULL
) == 0 &&
197 now
.tv_sec
> ptr
->next_distribution_rebuild
)
198 run_distribution(ptr
);
201 return dispatch_host(ptr
, hash
);
204 static uint32_t internal_generate_hash(const char *key
, size_t key_length
)
206 const char *ptr
= key
;
211 uint32_t val
= (uint32_t) *ptr
++;
213 value
+= (value
<< 10);
214 value
^= (value
>> 6);
216 value
+= (value
<< 3);
217 value
^= (value
>> 11);
218 value
+= (value
<< 15);
220 return value
== 0 ? 1 : (uint32_t) value
;
223 static uint32_t internal_generate_md5(const char *key
, size_t key_length
)
225 unsigned char results
[16];
227 md5_signature((unsigned char*)key
, (unsigned int)key_length
, results
);
229 return ((uint32_t) (results
[3] & 0xFF) << 24)
230 | ((uint32_t) (results
[2] & 0xFF) << 16)
231 | ((uint32_t) (results
[1] & 0xFF) << 8)
232 | (results
[0] & 0xFF);