Skip to content

Commit 7ae4cee

Browse files
committed
INTERNAL: Add get_shard_key for hash_ketama
1 parent 77e7950 commit 7ae4cee

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

cluster_config.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ struct cluster_config {
8484
};
8585

8686

87+
#ifdef SHARD_KEY
88+
static const char *get_shard_key(const char *key, uint32_t nkey, uint32_t *nshardkey)
89+
{
90+
const char *left = memchr(key, '{', nkey);
91+
if (left == NULL) {
92+
return NULL;
93+
}
94+
95+
const char *right = memchr(left + 1, '}', nkey - (left - key) - 1);
96+
if (right == NULL) {
97+
return NULL;
98+
}
99+
100+
*nshardkey = right - left - 1;
101+
if (*nshardkey == 0) {
102+
return NULL;
103+
}
104+
105+
return left + 1;
106+
}
107+
#endif
108+
87109
static void hash_md5(const char *key, uint32_t nkey, unsigned char *result)
88110
{
89111
MD5_CTX ctx;
@@ -97,6 +119,15 @@ static uint32_t hash_ketama(const char *key, uint32_t nkey)
97119
{
98120
unsigned char digest[16];
99121

122+
#ifdef SHARD_KEY
123+
uint32_t nshardkey;
124+
const char *shardkey = get_shard_key(key, nkey, &nshardkey);
125+
if (shardkey) {
126+
key = shardkey;
127+
nkey = nshardkey;
128+
}
129+
#endif
130+
100131
hash_md5(key, nkey, digest);
101132
return (uint32_t)((digest[3] << 24)
102133
|(digest[2] << 16)

util.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,12 @@ void safe_hexatostr(const unsigned char *bin, const int size, char *str) {
164164
static inline bool mc_isnamechar(int c) {
165165
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
166166
(c >= '0' && c <= '9') ||
167+
#ifdef SHARD_KEY
168+
(c == '_') || (c == '-') || (c == '+') || (c == '.') ||
169+
(c == '{') || (c == '}'));
170+
#else
167171
(c == '_') || (c == '-') || (c == '+') || (c == '.'));
172+
#endif
168173
}
169174

170175
static inline bool mc_ishyphon(int c) {

0 commit comments

Comments
 (0)