Updating the exceptions header file with some slight modifications.
[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 ^= 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 hash ^= key[x];
54 hash *= (uint32_t) FNV_64_PRIME;
55 }
56 }
57 break;
58 case MEMCACHED_HASH_FNV1_32:
59 {
60 hash= FNV_32_INIT;
61 for (x= 0; x < key_length; x++)
62 {
63 hash *= FNV_32_PRIME;
64 hash ^= key[x];
65 }
66 }
67 break;
68 case MEMCACHED_HASH_FNV1A_32:
69 {
70 hash= FNV_32_INIT;
71 for (x= 0; x < key_length; x++)
72 {
73 hash ^= key[x];
74 hash *= FNV_32_PRIME;
75 }
76 }
77 break;
78 case MEMCACHED_HASH_HSIEH:
79 {
80 #ifdef HAVE_HSIEH_HASH
81 hash= hsieh_hash(key, key_length);
82 #endif
83 break;
84 }
85 case MEMCACHED_HASH_MURMUR:
86 {
87 hash= murmur_hash(key, key_length);
88 break;
89 }
90 case MEMCACHED_HASH_JENKINS:
91 {
92 hash=jenkins_hash(key, key_length, 13);
93 break;
94 }
95 default:
96 {
97 WATCHPOINT_ASSERT(hash_algorithm);
98 break;
99 }
100 }
101 return hash;
102 }
103
104 uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length)
105 {
106 uint32_t hash= 1; /* Just here to remove compile warning */
107
108
109 WATCHPOINT_ASSERT(ptr->number_of_hosts);
110
111 if (ptr->number_of_hosts == 1)
112 return 0;
113
114 hash= memcached_generate_hash_value(key, key_length, ptr->hash);
115 WATCHPOINT_ASSERT(hash);
116 return hash;
117 }
118
119 static uint32_t dispatch_host(memcached_st *ptr, uint32_t hash)
120 {
121 switch (ptr->distribution)
122 {
123 case MEMCACHED_DISTRIBUTION_CONSISTENT:
124 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
125 {
126 uint32_t num= ptr->continuum_points_counter;
127 WATCHPOINT_ASSERT(ptr->continuum);
128
129 hash= hash;
130 memcached_continuum_item_st *begin, *end, *left, *right, *middle;
131 begin= left= ptr->continuum;
132 end= right= ptr->continuum + num;
133
134 while (left < right)
135 {
136 middle= left + (right - left) / 2;
137 if (middle->value < hash)
138 left= middle + 1;
139 else
140 right= middle;
141 }
142 if (right == end)
143 right= begin;
144 return right->index;
145 }
146 case MEMCACHED_DISTRIBUTION_MODULA:
147 return hash % ptr->number_of_hosts;
148 case MEMCACHED_DISTRIBUTION_RANDOM:
149 return (uint32_t) random() % ptr->number_of_hosts;
150 default:
151 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
152 return hash % ptr->number_of_hosts;
153 }
154
155 /* NOTREACHED */
156 }
157
158 /*
159 One day make this public, and have it return the actual memcached_server_st
160 to the calling application.
161 */
162 uint32_t memcached_generate_hash(memcached_st *ptr, const char *key, size_t key_length)
163 {
164 uint32_t hash= 1; /* Just here to remove compile warning */
165
166 WATCHPOINT_ASSERT(ptr->number_of_hosts);
167
168 if (ptr->number_of_hosts == 1)
169 return 0;
170
171 if (ptr->flags & MEM_HASH_WITH_PREFIX_KEY)
172 {
173 size_t temp_length= ptr->prefix_key_length + key_length;
174 char temp[temp_length];
175
176 if (temp_length > MEMCACHED_MAX_KEY -1)
177 return 0;
178
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);
182 }
183 else
184 {
185 hash= generate_hash(ptr, key, key_length);
186 }
187
188 WATCHPOINT_ASSERT(hash);
189
190 if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS) && ptr->next_distribution_rebuild) {
191 struct timeval now;
192
193 if (gettimeofday(&now, NULL) == 0 &&
194 now.tv_sec > ptr->next_distribution_rebuild)
195 run_distribution(ptr);
196 }
197
198 return dispatch_host(ptr, hash);
199 }
200
201 static uint32_t internal_generate_hash(const char *key, size_t key_length)
202 {
203 const char *ptr= key;
204 uint32_t value= 0;
205
206 while (key_length--)
207 {
208 value += (uint32_t) *ptr++;
209 value += (value << 10);
210 value ^= (value >> 6);
211 }
212 value += (value << 3);
213 value ^= (value >> 11);
214 value += (value << 15);
215
216 return value == 0 ? 1 : (uint32_t) value;
217 }
218
219 static uint32_t internal_generate_md5(const char *key, size_t key_length)
220 {
221 unsigned char results[16];
222
223 md5_signature((unsigned char*)key, (unsigned int)key_length, results);
224
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);
229 }