Fix a few spots where when I did the update for EXIT_ I made more
[awesomized/libmemcached] / libmemcached / hash.c
1 /* LibMemcached
2 * Copyright (C) 2006-2010 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 #include "common.h"
13
14
15 uint32_t memcached_generate_hash_value(const char *key, size_t key_length, memcached_hash_t hash_algorithm)
16 {
17 return libhashkit_digest(key, key_length, (hashkit_hash_algorithm_t)hash_algorithm);
18 }
19
20 static inline uint32_t generate_hash(const memcached_st *ptr, const char *key, size_t key_length)
21 {
22 return hashkit_digest(&ptr->hashkit, key, key_length);
23 }
24
25 static uint32_t dispatch_host(const memcached_st *ptr, uint32_t hash)
26 {
27 switch (ptr->distribution)
28 {
29 case MEMCACHED_DISTRIBUTION_CONSISTENT:
30 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
31 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY:
32 {
33 uint32_t num= ptr->continuum_points_counter;
34 WATCHPOINT_ASSERT(ptr->continuum);
35
36 hash= hash;
37 memcached_continuum_item_st *begin, *end, *left, *right, *middle;
38 begin= left= ptr->continuum;
39 end= right= ptr->continuum + num;
40
41 while (left < right)
42 {
43 middle= left + (right - left) / 2;
44 if (middle->value < hash)
45 left= middle + 1;
46 else
47 right= middle;
48 }
49 if (right == end)
50 right= begin;
51 return right->index;
52 }
53 case MEMCACHED_DISTRIBUTION_MODULA:
54 return hash % memcached_server_count(ptr);
55 case MEMCACHED_DISTRIBUTION_RANDOM:
56 return (uint32_t) random() % memcached_server_count(ptr);
57 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX:
58 default:
59 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
60 return hash % memcached_server_count(ptr);
61 }
62 /* NOTREACHED */
63 }
64
65 /*
66 One version is public and will not modify the distribution hash, the other will.
67 */
68 static inline uint32_t _generate_hash_wrapper(const memcached_st *ptr, const char *key, size_t key_length)
69 {
70 WATCHPOINT_ASSERT(memcached_server_count(ptr));
71
72 if (memcached_server_count(ptr) == 1)
73 return 0;
74
75 if (ptr->flags.hash_with_prefix_key)
76 {
77 size_t temp_length= ptr->prefix_key_length + key_length;
78 char temp[temp_length];
79
80 if (temp_length > MEMCACHED_MAX_KEY -1)
81 return 0;
82
83 strncpy(temp, ptr->prefix_key, ptr->prefix_key_length);
84 strncpy(temp + ptr->prefix_key_length, key, key_length);
85
86 return generate_hash(ptr, temp, temp_length);
87 }
88 else
89 {
90 return generate_hash(ptr, key, key_length);
91 }
92 }
93
94 static inline void _regen_for_auto_eject(memcached_st *ptr)
95 {
96 if (_is_auto_eject_host(ptr) && ptr->next_distribution_rebuild)
97 {
98 struct timeval now;
99
100 if (gettimeofday(&now, NULL) == 0 &&
101 now.tv_sec > ptr->next_distribution_rebuild)
102 {
103 run_distribution(ptr);
104 }
105 }
106 }
107
108 void memcached_autoeject(memcached_st *ptr)
109 {
110 _regen_for_auto_eject(ptr);
111 }
112
113 uint32_t memcached_generate_hash_with_redistribution(memcached_st *ptr, const char *key, size_t key_length)
114 {
115 uint32_t hash= _generate_hash_wrapper(ptr, key, key_length);
116
117 _regen_for_auto_eject(ptr);
118
119 return dispatch_host(ptr, hash);
120 }
121
122 uint32_t memcached_generate_hash(const memcached_st *ptr, const char *key, size_t key_length)
123 {
124 return dispatch_host(ptr, _generate_hash_wrapper(ptr, key, key_length));
125 }
126
127 const hashkit_st *memcached_get_hashkit(const memcached_st *ptr)
128 {
129 return &ptr->hashkit;
130 }
131
132 memcached_return_t memcached_set_hashkit(memcached_st *self, hashkit_st *hashk)
133 {
134 hashkit_free(&self->hashkit);
135 hashkit_clone(&self->hashkit, hashk);
136
137 return MEMCACHED_SUCCESS;
138 }