2 * Copyright (C) 2006-2010 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
15 uint32_t memcached_generate_hash_value(const char *key
, size_t key_length
, memcached_hash_t hash_algorithm
)
17 return libhashkit_digest(key
, key_length
, (hashkit_hash_algorithm_t
)hash_algorithm
);
20 static inline uint32_t generate_hash(const memcached_st
*ptr
, const char *key
, size_t key_length
)
22 return hashkit_digest(&ptr
->hashkit
, key
, key_length
);
25 static uint32_t dispatch_host(const memcached_st
*ptr
, uint32_t hash
)
27 switch (ptr
->distribution
)
29 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
30 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
31 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
:
33 uint32_t num
= ptr
->continuum_points_counter
;
34 WATCHPOINT_ASSERT(ptr
->continuum
);
37 memcached_continuum_item_st
*begin
, *end
, *left
, *right
, *middle
;
38 begin
= left
= ptr
->continuum
;
39 end
= right
= ptr
->continuum
+ num
;
43 middle
= left
+ (right
- left
) / 2;
44 if (middle
->value
< hash
)
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
:
59 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
60 return hash
% memcached_server_count(ptr
);
66 One version is public and will not modify the distribution hash, the other will.
68 static inline uint32_t _generate_hash_wrapper(const memcached_st
*ptr
, const char *key
, size_t key_length
)
70 WATCHPOINT_ASSERT(memcached_server_count(ptr
));
72 if (memcached_server_count(ptr
) == 1)
75 if (ptr
->flags
.hash_with_prefix_key
)
77 size_t temp_length
= ptr
->prefix_key_length
+ key_length
;
78 char temp
[temp_length
];
80 if (temp_length
> MEMCACHED_MAX_KEY
-1)
83 strncpy(temp
, ptr
->prefix_key
, ptr
->prefix_key_length
);
84 strncpy(temp
+ ptr
->prefix_key_length
, key
, key_length
);
86 return generate_hash(ptr
, temp
, temp_length
);
90 return generate_hash(ptr
, key
, key_length
);
94 static inline void _regen_for_auto_eject(memcached_st
*ptr
)
96 if (_is_auto_eject_host(ptr
) && ptr
->next_distribution_rebuild
)
100 if (gettimeofday(&now
, NULL
) == 0 &&
101 now
.tv_sec
> ptr
->next_distribution_rebuild
)
103 run_distribution(ptr
);
108 void memcached_autoeject(memcached_st
*ptr
)
110 _regen_for_auto_eject(ptr
);
113 uint32_t memcached_generate_hash_with_redistribution(memcached_st
*ptr
, const char *key
, size_t key_length
)
115 uint32_t hash
= _generate_hash_wrapper(ptr
, key
, key_length
);
117 _regen_for_auto_eject(ptr
);
119 return dispatch_host(ptr
, hash
);
122 uint32_t memcached_generate_hash(const memcached_st
*ptr
, const char *key
, size_t key_length
)
124 return dispatch_host(ptr
, _generate_hash_wrapper(ptr
, key
, key_length
));
127 hashkit_st
*memcached_get_hashkit(memcached_st
*ptr
)
129 return &ptr
->hashkit
;