Encapsulate more of the cleanup logic.
[awesomized/libmemcached] / libmemcached / hash.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38
39 #include <libmemcached/common.h>
40
41 #include <sys/time.h>
42
43 #include <libmemcached/virtual_bucket.h>
44
45 uint32_t memcached_generate_hash_value(const char *key, size_t key_length, memcached_hash_t hash_algorithm)
46 {
47 return libhashkit_digest(key, key_length, (hashkit_hash_algorithm_t)hash_algorithm);
48 }
49
50 static inline uint32_t generate_hash(const Memcached *ptr, const char *key, size_t key_length)
51 {
52 return hashkit_digest(&ptr->hashkit, key, key_length);
53 }
54
55 static uint32_t dispatch_host(const Memcached *ptr, uint32_t hash)
56 {
57 switch (ptr->distribution)
58 {
59 case MEMCACHED_DISTRIBUTION_CONSISTENT:
60 case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED:
61 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
62 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY:
63 {
64 uint32_t num= ptr->ketama.continuum_points_counter;
65 WATCHPOINT_ASSERT(ptr->ketama.continuum);
66
67 memcached_continuum_item_st *begin, *end, *left, *right, *middle;
68 begin= left= ptr->ketama.continuum;
69 end= right= ptr->ketama.continuum + num;
70
71 while (left < right)
72 {
73 middle= left + (right - left) / 2;
74 if (middle->value < hash)
75 left= middle + 1;
76 else
77 right= middle;
78 }
79 if (right == end)
80 right= begin;
81 return right->index;
82 }
83 case MEMCACHED_DISTRIBUTION_MODULA:
84 return hash % memcached_server_count(ptr);
85 case MEMCACHED_DISTRIBUTION_RANDOM:
86 return (uint32_t) random() % memcached_server_count(ptr);
87 case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET:
88 {
89 return memcached_virtual_bucket_get(ptr, hash);
90 }
91 default:
92 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX:
93 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
94 return hash % memcached_server_count(ptr);
95 }
96 /* NOTREACHED */
97 }
98
99 /*
100 One version is public and will not modify the distribution hash, the other will.
101 */
102 static inline uint32_t _generate_hash_wrapper(const Memcached *ptr, const char *key, size_t key_length)
103 {
104 WATCHPOINT_ASSERT(memcached_server_count(ptr));
105
106 if (memcached_server_count(ptr) == 1)
107 return 0;
108
109 if (ptr->flags.hash_with_namespace)
110 {
111 size_t temp_length= memcached_array_size(ptr->_namespace) + key_length;
112 char temp[MEMCACHED_MAX_KEY];
113
114 if (temp_length > MEMCACHED_MAX_KEY -1)
115 return 0;
116
117 strncpy(temp, memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace));
118 strncpy(temp + memcached_array_size(ptr->_namespace), key, key_length);
119
120 return generate_hash(ptr, temp, temp_length);
121 }
122 else
123 {
124 return generate_hash(ptr, key, key_length);
125 }
126 }
127
128 static inline void _regen_for_auto_eject(Memcached *ptr)
129 {
130 if (_is_auto_eject_host(ptr) && ptr->ketama.next_distribution_rebuild)
131 {
132 struct timeval now;
133
134 if (gettimeofday(&now, NULL) == 0 and
135 now.tv_sec > ptr->ketama.next_distribution_rebuild)
136 {
137 run_distribution(ptr);
138 }
139 }
140 }
141
142 void memcached_autoeject(memcached_st *ptr)
143 {
144 _regen_for_auto_eject(ptr);
145 }
146
147 uint32_t memcached_generate_hash_with_redistribution(memcached_st *ptr, const char *key, size_t key_length)
148 {
149 uint32_t hash= _generate_hash_wrapper(ptr, key, key_length);
150
151 _regen_for_auto_eject(ptr);
152
153 return dispatch_host(ptr, hash);
154 }
155
156 uint32_t memcached_generate_hash(const memcached_st *shell, const char *key, size_t key_length)
157 {
158 const Memcached* ptr= memcached2Memcached(shell);
159 if (ptr)
160 {
161 return dispatch_host(ptr, _generate_hash_wrapper(ptr, key, key_length));
162 }
163
164 return UINT32_MAX;
165 }
166
167 const hashkit_st *memcached_get_hashkit(const memcached_st *shell)
168 {
169 const Memcached* ptr= memcached2Memcached(shell);
170 if (ptr)
171 {
172 return &ptr->hashkit;
173 }
174
175 return NULL;
176 }
177
178 memcached_return_t memcached_set_hashkit(memcached_st *shell, hashkit_st *hashk)
179 {
180 Memcached* self= memcached2Memcached(shell);
181 if (self)
182 {
183 hashkit_free(&self->hashkit);
184 hashkit_clone(&self->hashkit, hashk);
185
186 return MEMCACHED_SUCCESS;
187 }
188
189 return MEMCACHED_INVALID_ARGUMENTS;
190 }
191
192 const char * libmemcached_string_hash(memcached_hash_t type)
193 {
194 return libhashkit_string_hash((hashkit_hash_algorithm_t)type);
195 }