Skip to content

Commit 3102ed6

Browse files
committed
INTERNAL: Add get_shard_key for hash_ketama
1 parent 154d41a commit 3102ed6

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-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)

0 commit comments

Comments
 (0)