Skip to content

Commit

Permalink
redisxClusterGetShard() to allow NULL and empty keys also.
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Jan 8, 2025
1 parent 23c77b8 commit d2485af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/redisx-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ static void process(const char **cmdargs, int nargs) {
RESP *reply, *attr = NULL;

if(cluster) {
const char *key = cmdargs[1];
const char *key = NULL;

if(nargs > 3) if(strcasecmp("EVAL", cmdargs[0]) == 0 || strcasecmp("EVALSHA", cmdargs[0]) || strcasecmp("FCALL", cmdargs[0]))
key = cmdargs[3];
if(nargs > 1) {
key = cmdargs[1];
if(nargs > 3) if(strcasecmp("EVAL", cmdargs[0]) == 0 || strcasecmp("EVALSHA", cmdargs[0]) || strcasecmp("FCALL", cmdargs[0]))
key = cmdargs[3];
}

redis = redisxClusterGetShard(cluster, key);
if(!redis) {
Expand Down
17 changes: 6 additions & 11 deletions src/redisx-cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ static uint16_t crc16(const uint8_t *buf, size_t len) {
* @return
*/
uint16_t rCalcHash(const char *key) {
const char *from = strchr(key, '{');
const char *from;

if(!key) return 0;

from = strchr(key, '{');

if(from) {
const char *to = strchr(++from, '}');
Expand Down Expand Up @@ -350,6 +354,7 @@ int rClusterRefresh(RedisCluster *cluster) {
* contains a segment enclosed in {} brackets, then the hash will be
* calculated on the bracketed segment only. E.g. `{user:1000}.name` and
* `{user:1000}.address` will both return the same hash for `user:1000` only.
* NULL and empty keys are allowed and will return the shard for slot 0.
* @return A connected Redis server (cluster shard), which can be used for
* queries on the given keyword, or NULL if either input pointer is NULL
* (errno = EINVAL), or the cluster has not been initialized (errno = ENXIO),
Expand All @@ -371,16 +376,6 @@ Redis *redisxClusterGetShard(RedisCluster *cluster, const char *key) {
return NULL;
}

if(!key) {
x_error(X_NAME_INVALID, EINVAL, fn, "key is NULL");
return NULL;
}

if(!key[0]) {
x_error(X_NAME_INVALID, EINVAL, fn, "key is empty");
return NULL;
}

p = (ClusterPrivate *) cluster->priv;
if(!p) {
x_error(X_NO_INIT, ENXIO, fn, "cluster is not initialized");
Expand Down

0 comments on commit d2485af

Please sign in to comment.