Fix inline methods for strings.
[m6w6/libmemcached] / libmemcached / 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_t 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 case MEMCACHED_HASH_MAX:
99 default:
100 {
101 WATCHPOINT_ASSERT(0);
102 break;
103 }
104 }
105
106 return hash;
107 }
108
109 uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length)
110 {
111 uint32_t hash= 1; /* Just here to remove compile warning */
112
113
114 WATCHPOINT_ASSERT(memcached_server_count(ptr));
115
116 if (memcached_server_count(ptr) == 1)
117 return 0;
118
119 hash= memcached_generate_hash_value(key, key_length, ptr->hash);
120 WATCHPOINT_ASSERT(hash);
121 return hash;
122 }
123
124 static uint32_t dispatch_host(memcached_st *ptr, uint32_t hash)
125 {
126 switch (ptr->distribution)
127 {
128 case MEMCACHED_DISTRIBUTION_CONSISTENT:
129 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
130 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY:
131 {
132 uint32_t num= ptr->continuum_points_counter;
133 WATCHPOINT_ASSERT(ptr->continuum);
134
135 hash= hash;
136 memcached_continuum_item_st *begin, *end, *left, *right, *middle;
137 begin= left= ptr->continuum;
138 end= right= ptr->continuum + num;
139
140 while (left < right)
141 {
142 middle= left + (right - left) / 2;
143 if (middle->value < hash)
144 left= middle + 1;
145 else
146 right= middle;
147 }
148 if (right == end)
149 right= begin;
150 return right->index;
151 }
152 case MEMCACHED_DISTRIBUTION_MODULA:
153 return hash % memcached_server_count(ptr);
154 case MEMCACHED_DISTRIBUTION_RANDOM:
155 return (uint32_t) random() % memcached_server_count(ptr);
156 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX:
157 default:
158 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
159 return hash % memcached_server_count(ptr);
160 }
161 /* NOTREACHED */
162 }
163
164 /*
165 One day make this public, and have it return the actual memcached_server_st
166 to the calling application.
167 */
168 uint32_t memcached_generate_hash(memcached_st *ptr, const char *key, size_t key_length)
169 {
170 uint32_t hash= 1; /* Just here to remove compile warning */
171
172 WATCHPOINT_ASSERT(memcached_server_count(ptr));
173
174 if (memcached_server_count(ptr) == 1)
175 return 0;
176
177 if (ptr->flags.hash_with_prefix_key)
178 {
179 size_t temp_length= ptr->prefix_key_length + key_length;
180 char temp[temp_length];
181
182 if (temp_length > MEMCACHED_MAX_KEY -1)
183 return 0;
184
185 strncpy(temp, ptr->prefix_key, ptr->prefix_key_length);
186 strncpy(temp + ptr->prefix_key_length, key, key_length);
187 hash= generate_hash(ptr, temp, temp_length);
188 }
189 else
190 {
191 hash= generate_hash(ptr, key, key_length);
192 }
193
194 WATCHPOINT_ASSERT(hash);
195
196 if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS) && ptr->next_distribution_rebuild)
197 {
198 struct timeval now;
199
200 if (gettimeofday(&now, NULL) == 0 &&
201 now.tv_sec > ptr->next_distribution_rebuild)
202 {
203 run_distribution(ptr);
204 }
205 }
206
207 return dispatch_host(ptr, hash);
208 }
209
210 static uint32_t internal_generate_hash(const char *key, size_t key_length)
211 {
212 const char *ptr= key;
213 uint32_t value= 0;
214
215 while (key_length--)
216 {
217 uint32_t val= (uint32_t) *ptr++;
218 value += val;
219 value += (value << 10);
220 value ^= (value >> 6);
221 }
222 value += (value << 3);
223 value ^= (value >> 11);
224 value += (value << 15);
225
226 return value == 0 ? 1 : (uint32_t) value;
227 }
228
229 static uint32_t internal_generate_md5(const char *key, size_t key_length)
230 {
231 unsigned char results[16];
232
233 md5_signature((unsigned char*)key, (unsigned int)key_length, results);
234
235 return ((uint32_t) (results[3] & 0xFF) << 24)
236 | ((uint32_t) (results[2] & 0xFF) << 16)
237 | ((uint32_t) (results[1] & 0xFF) << 8)
238 | (results[0] & 0xFF);
239 }