Added srcdir includes so that the include files could be found in vpath builds.
[m6w6/libmemcached] / libmemcached / memcached_hash.c
1 #include "common.h"
2
3
4 /* Defines */
5 static uint64_t FNV_64_INIT= 0xcbf29ce484222325LL;
6 static uint64_t FNV_64_PRIME= 0x100000001b3LL;
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= FNV_64_INIT;
51 for (x= 0; x < key_length; x++)
52 {
53 hash ^= key[x];
54 hash *= 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 }
96 return hash;
97 }
98
99 uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length)
100 {
101 uint32_t hash= 1; /* Just here to remove compile warning */
102
103
104 WATCHPOINT_ASSERT(ptr->number_of_hosts);
105
106 if (ptr->number_of_hosts == 1)
107 return 0;
108
109 hash= memcached_generate_hash_value(key, key_length, ptr->hash);
110 WATCHPOINT_ASSERT(hash);
111 return hash;
112 }
113
114 static uint32_t dispatch_host(memcached_st *ptr, uint32_t hash)
115 {
116 switch (ptr->distribution)
117 {
118 case MEMCACHED_DISTRIBUTION_CONSISTENT:
119 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
120 {
121 uint32_t num= ptr->continuum_points_counter;
122 WATCHPOINT_ASSERT(ptr->continuum);
123
124 hash= hash;
125 memcached_continuum_item_st *begin, *end, *left, *right, *middle;
126 begin= left= ptr->continuum;
127 end= right= ptr->continuum + num;
128
129 while (left < right)
130 {
131 middle= left + (right - left) / 2;
132 if (middle->value < hash)
133 left= middle + 1;
134 else
135 right= middle;
136 }
137 if (right == end)
138 right= begin;
139 return right->index;
140 }
141 break;
142 case MEMCACHED_DISTRIBUTION_MODULA:
143 return hash % ptr->number_of_hosts;
144 case MEMCACHED_DISTRIBUTION_RANDOM:
145 return random() % ptr->number_of_hosts;
146 default:
147 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
148 return hash % ptr->number_of_hosts;
149 }
150
151 /* NOTREACHED */
152 }
153
154 /*
155 One day make this public, and have it return the actual memcached_server_st
156 to the calling application.
157 */
158 uint32_t memcached_generate_hash(memcached_st *ptr, const char *key, size_t key_length)
159 {
160 uint32_t hash= 1; /* Just here to remove compile warning */
161
162 WATCHPOINT_ASSERT(ptr->number_of_hosts);
163
164 if (ptr->number_of_hosts == 1)
165 return 0;
166
167 if (ptr->flags & MEM_HASH_WITH_PREFIX_KEY)
168 {
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);
174 free(temp);
175 }
176 else
177 {
178 hash= generate_hash(ptr, key, key_length);
179 }
180
181 WATCHPOINT_ASSERT(hash);
182
183 if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS) && ptr->next_distribution_rebuild) {
184 struct timeval now;
185
186 if (gettimeofday(&now, NULL) == 0 &&
187 now.tv_sec > ptr->next_distribution_rebuild)
188 run_distribution(ptr);
189 }
190
191 return dispatch_host(ptr, hash);
192 }
193
194 static uint32_t internal_generate_hash(const char *key, size_t key_length)
195 {
196 const char *ptr= key;
197 uint32_t value= 0;
198
199 while (key_length--)
200 {
201 value += *ptr++;
202 value += (value << 10);
203 value ^= (value >> 6);
204 }
205 value += (value << 3);
206 value ^= (value >> 11);
207 value += (value << 15);
208
209 return value == 0 ? 1 : value;
210 }
211
212 static uint32_t internal_generate_md5(const char *key, size_t key_length)
213 {
214 unsigned char results[16];
215
216 md5_signature((unsigned char*)key, (unsigned int)key_length, results);
217
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);
222 }