Skip to content

Commit

Permalink
Add new APIs for variadic cluster commands
Browse files Browse the repository at this point in the history
- valkeyClustervCommandToNode()
- valkeyClustervAppendCommandToNode()

From Nordix/hiredis-cluster#231
Co-authored-by: Alex Unigovsky <[email protected]>

Signed-off-by: Björn Svensson <[email protected]>
  • Loading branch information
bjosv committed Aug 19, 2024
1 parent 6d8471e commit afe0fc5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
6 changes: 6 additions & 0 deletions include/valkey/valkeycluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ void *valkeyClusterCommandToNode(valkeyClusterContext *cc,
/* Variadic using va_list */
void *valkeyClustervCommand(valkeyClusterContext *cc, const char *format,
va_list ap);
void *valkeyClustervCommandToNode(valkeyClusterContext *cc,
valkeyClusterNode *node, const char *format,
va_list ap);
/* Using argc and argv */
void *valkeyClusterCommandArgv(valkeyClusterContext *cc, int argc,
const char **argv, const size_t *argvlen);
Expand All @@ -248,6 +251,9 @@ int valkeyClusterAppendCommandToNode(valkeyClusterContext *cc,
/* Variadic using va_list */
int valkeyClustervAppendCommand(valkeyClusterContext *cc, const char *format,
va_list ap);
int valkeyClustervAppendCommandToNode(valkeyClusterContext *cc,
valkeyClusterNode *node,
const char *format, va_list ap);
/* Using argc and argv */
int valkeyClusterAppendCommandArgv(valkeyClusterContext *cc, int argc,
const char **argv, const size_t *argvlen);
Expand Down
49 changes: 36 additions & 13 deletions src/valkeycluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -2383,11 +2383,10 @@ void *valkeyClusterCommand(valkeyClusterContext *cc, const char *format, ...) {
return reply;
}

void *valkeyClusterCommandToNode(valkeyClusterContext *cc,
valkeyClusterNode *node, const char *format,
...) {
void *valkeyClustervCommandToNode(valkeyClusterContext *cc,
valkeyClusterNode *node, const char *format,
va_list ap) {
valkeyContext *c;
va_list ap;
int ret;
void *reply;
int updating_slotmap = 0;
Expand All @@ -2405,9 +2404,7 @@ void *valkeyClusterCommandToNode(valkeyClusterContext *cc,
memset(cc->errstr, '\0', sizeof(cc->errstr));
}

va_start(ap, format);
ret = valkeyvAppendCommand(c, format, ap);
va_end(ap);

if (ret != VALKEY_OK) {
valkeyClusterSetError(cc, c->err, c->errstr);
Expand Down Expand Up @@ -2440,6 +2437,19 @@ void *valkeyClusterCommandToNode(valkeyClusterContext *cc,
return reply;
}

void *valkeyClusterCommandToNode(valkeyClusterContext *cc,
valkeyClusterNode *node, const char *format,
...) {
va_list ap;
valkeyReply *reply = NULL;

va_start(ap, format);
reply = valkeyClustervCommandToNode(cc, node, format, ap);
va_end(ap);

return reply;
}

void *valkeyClusterCommandArgv(valkeyClusterContext *cc, int argc,
const char **argv, const size_t *argvlen) {
valkeyReply *reply = NULL;
Expand Down Expand Up @@ -2544,11 +2554,10 @@ int valkeyClusterAppendCommand(valkeyClusterContext *cc, const char *format,
return ret;
}

int valkeyClusterAppendCommandToNode(valkeyClusterContext *cc,
valkeyClusterNode *node,
const char *format, ...) {
int valkeyClustervAppendCommandToNode(valkeyClusterContext *cc,
valkeyClusterNode *node,
const char *format, va_list ap) {
valkeyContext *c;
va_list ap;
struct cmd *command = NULL;
char *cmd = NULL;
int len;
Expand All @@ -2569,10 +2578,7 @@ int valkeyClusterAppendCommandToNode(valkeyClusterContext *cc,
return VALKEY_ERR;
}

/* Allocate cmd and encode the variadic command */
va_start(ap, format);
len = valkeyvFormatCommand(&cmd, format, ap);
va_end(ap);

if (len == -1) {
goto oom;
Expand Down Expand Up @@ -2611,6 +2617,23 @@ int valkeyClusterAppendCommandToNode(valkeyClusterContext *cc,
return VALKEY_ERR;
}

int valkeyClusterAppendCommandToNode(valkeyClusterContext *cc,
valkeyClusterNode *node,
const char *format, ...) {
int ret;
va_list ap;

if (cc == NULL || node == NULL || format == NULL) {
return VALKEY_ERR;
}

va_start(ap, format);
ret = valkeyClustervAppendCommandToNode(cc, node, format, ap);
va_end(ap);

return ret;
}

int valkeyClusterAppendCommandArgv(valkeyClusterContext *cc, int argc,
const char **argv, const size_t *argvlen) {
int ret;
Expand Down

0 comments on commit afe0fc5

Please sign in to comment.