Merge from Trond
[m6w6/libmemcached] / libmemcached / memcached_hash.c
1 #include "common.h"
2
3
4 /* Defines */
5 static uint64_t FNV_64_INIT= UINT64_C(0xcbf29ce484222325);
6 static uint64_t FNV_64_PRIME= UINT64_C(0x100000001b3);
7
8 static uint32_t FNV_32_INIT= 2166136261UL;
9 static uint32_t FNV_32_PRIME= 16777619;
10
11 /* Prototypes */
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);
14
15 uint32_t memcached_generate_hash_value(const char *key, size_t key_length, memcached_hash hash_algorithm)
16 {
17 uint32_t hash= 1; /* Just here to remove compile warning */
18 uint32_t x= 0;
19
20 switch (hash_algorithm)
21 {
22 case MEMCACHED_HASH_DEFAULT:
23 hash= internal_generate_hash(key, key_length);
24 break;
25 case MEMCACHED_HASH_MD5:
26 hash= internal_generate_md5(key, key_length);
27 break;
28 case MEMCACHED_HASH_CRC:
29 hash= ((hash_crc32(key, key_length) >> 16) & 0x7fff);
30 if (hash == 0)
31 hash= 1;
32 break;
33 /* FNV hash'es lifted from Dustin Sallings work */
34 case MEMCACHED_HASH_FNV1_64:
35 {
36 /* Thanks to pierre@demartines.com for the pointer */
37 uint64_t temp_hash;
38
39 temp_hash= FNV_64_INIT;
40 for (x= 0; x < key_length; x++)
41 {
42 temp_hash *= FNV_64_PRIME;
43 temp_hash ^= (uint64_t)key[x];
44 }
45 hash= (uint32_t)temp_hash;
46 }
47 break;
48 case MEMCACHED_HASH_FNV1A_64:
49 {
50 hash= (uint32_t) FNV_64_INIT;
51 for (x= 0; x < key_length; x++)
52 {
53 uint32_t val= (uint32_t)key[x];
54 hash ^= val;
55 hash *= (uint32_t) FNV_64_PRIME;
56 }
57 }
58 break;
59 case MEMCACHED_HASH_FNV1_32:
60 {
61 hash= FNV_32_INIT;
62 for (x= 0; x < key_length; x++)
63 {
64 uint32_t val= (uint32_t)key[x];
65 hash *= FNV_32_PRIME;
66 hash ^= val;
67 }
68 }
69 break;
70 case MEMCACHED_HASH_FNV1A_32:
71 {
72 hash= FNV_32_INIT;
73 for (x= 0; x < key_length; x++)
74 {
75 uint32_t val= (uint32_t)key[x];
76 hash ^= val;
77 hash *= FNV_32_PRIME;
78 }
79 }
80 break;
81 case MEMCACHED_HASH_HSIEH:
82 {
83 #ifdef HAVE_HSIEH_HASH
84 hash= hsieh_hash(key, key_length);
85 #endif
86 break;
87 }
88 case MEMCACHED_HASH_MURMUR:
89 {
90 hash= murmur_hash(key, key_length);
91 break;
92 }
93 case MEMCACHED_HASH_JENKINS:
94 {
95 hash=jenkins_hash(key, key_length, 13);
96 break;
97 }
98 default:
99 {
100 WATCHPOINT_ASSERT(hash_algorithm);
101 break;
102 }
103 }
104 return hash;
105 }
106
107 uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length)
108 {
109 uint32_t hash= 1; /* Just here to remove compile warning */
110
111
112 WATCHPOINT_ASSERT(ptr->number_of_hosts);
113
114 if (ptr->number_of_hosts == 1)
115 return 0;
116
117 hash= memcached_generate_hash_value(key, key_length, ptr->hash);
118 WATCHPOINT_ASSERT(hash);
119 return hash;
120 }
121
122 static uint32_t dispatch_host(memcached_st *ptr, uint32_t hash)
123 {
124 switch (ptr->distribution)
125 {
126 case MEMCACHED_DISTRIBUTION_CONSISTENT:
127 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
128 {
129 uint32_t num= ptr->continuum_points_counter;
130 WATCHPOINT_ASSERT(ptr->continuum);
131
132 hash= hash;
133 memcached_continuum_item_st *begin, *end, *left, *right, *middle;
134 begin= left= ptr->continuum;
135 end= right= ptr->continuum + num;
136
137 while (left < right)
138 {
139 middle= left + (right - left) / 2;
140 if (middle->value < hash)
141 left= middle + 1;
142 else
143 right= middle;
144 }
145 if (right == end)
146 right= begin;
147 return right->index;
148 }
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;
153 default:
154 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
155 return hash % ptr->number_of_hosts;
156 }
157
158 /* NOTREACHED */
159 }
160
161 /*
162 One day make this public, and have it return the actual memcached_server_st
163 to the calling application.
164 */
165 uint32_t memcached_generate_hash(memcached_st *ptr, const char *key, size_t key_length)
166 {
167 uint32_t hash= 1; /* Just here to remove compile warning */
168
169 WATCHPOINT_ASSERT(ptr->number_of_hosts);
170
171 if (ptr->number_of_hosts == 1)
172 return 0;
173
174 if (ptr->flags & MEM_HASH_WITH_PREFIX_KEY)
175 {
176 size_t temp_length= ptr->prefix_key_length + key_length;
177 char temp[temp_length];
178
179 if (temp_length > MEMCACHED_MAX_KEY -1)
180 return 0;
181
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);
185 }
186 else
187 {
188 hash= generate_hash(ptr, key, key_length);
189 }
190
191 WATCHPOINT_ASSERT(hash);
192
193 if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS) && ptr->next_distribution_rebuild) {
194 struct timeval now;
195
196 if (gettimeofday(&now, NULL) == 0 &&
197 now.tv_sec > ptr->next_distribution_rebuild)
198 run_distribution(ptr);
199 }
200
201 return dispatch_host(ptr, hash);
202 }
203
204 static uint32_t internal_generate_hash(const char *key, size_t key_length)
205 {
206 const char *ptr= key;
207 uint32_t value= 0;
208
209 while (key_length--)
210 {
211 uint32_t val= (uint32_t) *ptr++;
212 value += val;
213 value += (value << 10);
214 value ^= (value >> 6);
215 }
216 value += (value << 3);
217 value ^= (value >> 11);
218 value += (value << 15);
219
220 return value == 0 ? 1 : (uint32_t) value;
221 }
222
223 static uint32_t internal_generate_md5(const char *key, size_t key_length)
224 {
225 unsigned char results[16];
226
227 md5_signature((unsigned char*)key, (unsigned int)key_length, results);
228
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);
233 }