Skip to content

Commit

Permalink
Replace redis_cmd_format_static with redis_spprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-grunder committed Apr 28, 2017
1 parent 5d35acd commit 0eaeae0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
9 changes: 5 additions & 4 deletions library.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ static int reselect_db(RedisSock *redis_sock TSRMLS_DC) {
char *cmd, *response;
int cmd_len, response_len;

cmd_len = redis_cmd_format_static(&cmd, "SELECT", "d", redis_sock->dbNumber);
cmd_len = redis_spprintf(redis_sock, NULL TSRMLS_CC, &cmd, "SELECT", "d",
redis_sock->dbNumber);

if (redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) < 0) {
efree(cmd);
Expand All @@ -81,8 +82,8 @@ static int resend_auth(RedisSock *redis_sock TSRMLS_DC) {
char *cmd, *response;
int cmd_len, response_len;

cmd_len = redis_cmd_format_static(&cmd, "AUTH", "s", redis_sock->auth,
strlen(redis_sock->auth));
cmd_len = redis_spprintf(redis_sock, NULL TSRMLS_CC, &cmd, "AUTH", "s",
redis_sock->auth, strlen(redis_sock->auth));

if (redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) < 0) {
efree(cmd);
Expand Down Expand Up @@ -1623,7 +1624,7 @@ PHP_REDIS_API void redis_send_discard(INTERNAL_FUNCTION_PARAMETERS,
int response_len, cmd_len;
char * response;

cmd_len = redis_cmd_format_static(&cmd, "DISCARD", "");
cmd_len = redis_spprintf(redis_sock, NULL TSRMLS_CC, &cmd, "DISCARD", "");

SOCKET_WRITE_COMMAND(redis_sock, cmd, cmd_len)
efree(cmd);
Expand Down
2 changes: 1 addition & 1 deletion library.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define REDIS_LIBRARY_H

#define REDIS_SPPRINTF(ret, kw, fmt, ...) \
redis_spprintf(redis_sock, slot TSRMLS_CC, ret, kw, fmt, __VA_ARGS__)
redis_spprintf(redis_sock, slot TSRMLS_CC, ret, kw, fmt, ##__VA_ARGS__)

#define REDIS_CMD_APPEND_SSTR_STATIC(sstr, str) \
redis_cmd_append_sstr(sstr, str, sizeof(str)-1);
Expand Down
25 changes: 12 additions & 13 deletions redis_cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ PHP_METHOD(RedisCluster, keys) {
char *pat, *cmd;
clusterReply *resp;
zval zv, *z_ret = &zv;
int i, pat_free, cmd_len;
int i, cmd_len;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &pat, &pat_len)
==FAILURE)
Expand All @@ -1053,9 +1053,7 @@ PHP_METHOD(RedisCluster, keys) {
}

/* Prefix and then build our command */
pat_free = redis_key_prefix(c->flags, &pat, &pat_len);
cmd_len = redis_cmd_format_static(&cmd, "KEYS", "s", pat, pat_len);
if(pat_free) efree(pat);
cmd_len = redis_spprintf(c->flags, NULL TSRMLS_CC, &cmd, "KEYS", "k", pat, pat_len);

array_init(z_ret);

Expand Down Expand Up @@ -2297,7 +2295,7 @@ cluster_empty_node_cmd(INTERNAL_FUNCTION_PARAMETERS, char *kw,
}

// Construct our command
cmd_len = redis_cmd_format_static(&cmd, kw, "");
cmd_len = redis_spprintf(NULL, NULL TSRMLS_CC, &cmd, kw, "");

// Kick off our command
if(cluster_send_slot(c, slot, cmd, cmd_len, reply_type TSRMLS_CC)<0) {
Expand Down Expand Up @@ -2652,14 +2650,14 @@ PHP_METHOD(RedisCluster, info) {
c->readonly = 0;

slot = cluster_cmd_get_slot(c, z_arg TSRMLS_CC);
if(slot<0) {
if (slot < 0) {
RETURN_FALSE;
}

if(opt != NULL) {
cmd_len = redis_cmd_format_static(&cmd, "INFO", "s", opt, opt_len);
if (opt != NULL) {
cmd_len = redis_spprintf(NULL, NULL TSRMLS_CC, &cmd, "INFO", "s", opt, opt_len);
} else {
cmd_len = redis_cmd_format_static(&cmd, "INFO", "");
cmd_len = redis_spprintf(NULL, NULL TSRMLS_CC, &cmd, "INFO", "");
}

rtype = CLUSTER_IS_ATOMIC(c) ? TYPE_BULK : TYPE_LINE;
Expand Down Expand Up @@ -2726,10 +2724,11 @@ PHP_METHOD(RedisCluster, client) {

/* Construct the command */
if (ZEND_NUM_ARGS() == 3) {
cmd_len = redis_cmd_format_static(&cmd, "CLIENT", "ss", opt, opt_len,
arg, arg_len);
cmd_len = redis_spprintf(NULL, NULL TSRMLS_CC, &cmd, "CLIENT", "ss",
opt, opt_len, arg, arg_len);
} else if(ZEND_NUM_ARGS() == 2) {
cmd_len = redis_cmd_format_static(&cmd, "CLIENT", "s", opt, opt_len);
cmd_len = redis_spprintf(NULL, NULL TSRMLS_CC, &cmd, "CLIENT", "s",
opt, opt_len);
} else {
zend_wrong_param_count(TSRMLS_C);
RETURN_FALSE;
Expand Down Expand Up @@ -2883,7 +2882,7 @@ PHP_METHOD(RedisCluster, echo) {
}

/* Construct our command */
cmd_len = redis_cmd_format_static(&cmd, "ECHO", "s", msg, msg_len);
cmd_len = redis_spprintf(NULL, NULL TSRMLS_CC, &cmd, "ECHO", "s", msg, msg_len);

/* Send it off */
rtype = CLUSTER_IS_ATOMIC(c) ? TYPE_BULK : TYPE_LINE;
Expand Down
26 changes: 11 additions & 15 deletions redis_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int redis_empty_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
*cmd_len = redis_cmd_format_static(cmd, kw, "");
*cmd_len = REDIS_SPPRINTF(cmd, kw, "");
return SUCCESS;
}

Expand Down Expand Up @@ -98,7 +98,7 @@ int redis_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, char *kw,
}

// Build the command without molesting the string
*cmd_len = redis_cmd_format_static(cmd, kw, "s", arg, arg_len);
*cmd_len = REDIS_SPPRINTF(cmd, kw, "s", arg, arg_len);

return SUCCESS;
}
Expand Down Expand Up @@ -1842,7 +1842,7 @@ int redis_auth_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
}

// Construct our AUTH command
*cmd_len = redis_cmd_format_static(cmd, "AUTH", "s", pw, pw_len);
*cmd_len = REDIS_SPPRINTF(cmd, "AUTH", "s", pw, pw_len);

// Free previously allocated password, and update
if(redis_sock->auth) efree(redis_sock->auth);
Expand Down Expand Up @@ -2079,18 +2079,13 @@ int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
// Default that we're not using store
*using_store = 0;

// Handle key prefixing
key_free = redis_key_prefix(redis_sock, &key, &key_len);

// If we don't have an options array, the command is quite simple
if(!z_opts || zend_hash_num_elements(Z_ARRVAL_P(z_opts)) == 0) {
if (!z_opts || zend_hash_num_elements(Z_ARRVAL_P(z_opts)) == 0) {
// Construct command
*cmd_len = redis_cmd_format_static(cmd, "SORT", "s", key, key_len);
*cmd_len = REDIS_SPPRINTF(cmd, "SORT", "k", key, key_len);

// Push out slot, store flag, and clean up
/* Not storing */
*using_store = 0;
CMD_SET_SLOT(slot,key,key_len);
if(key_free) efree(key);

return SUCCESS;
}
Expand All @@ -2099,6 +2094,7 @@ int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
array_init(&z_argv);

// SORT <key>
key_free = redis_key_prefix(redis_sock, &key, &key_len);
add_next_index_stringl(&z_argv, key, key_len);
if (key_free) efree(key);

Expand Down Expand Up @@ -2877,23 +2873,23 @@ int redis_command_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,

/* Construct our command */
if(!kw) {
*cmd_len = redis_cmd_format_static(cmd, "COMMAND", "");
*cmd_len = REDIS_SPPRINTF(cmd, "COMMAND", "");
} else if (!z_arg) {
/* Sanity check */
if (strncasecmp(kw, "count", sizeof("count") - 1)) {
return FAILURE;
}
/* COMMAND COUNT */
*cmd_len = redis_cmd_format_static(cmd, "COMMAND", "s", "COUNT", sizeof("COUNT") - 1);
*cmd_len = REDIS_SPPRINTF(cmd, "COMMAND", "s", "COUNT", sizeof("COUNT") - 1);
} else if (Z_TYPE_P(z_arg) == IS_STRING) {
/* Sanity check */
if (strncasecmp(kw, "info", sizeof("info") - 1)) {
return FAILURE;
}

/* COMMAND INFO <cmd> */
*cmd_len = redis_cmd_format_static(cmd, "COMMAND", "ss", "INFO",
sizeof("INFO")-1, Z_STRVAL_P(z_arg), Z_STRLEN_P(z_arg));
*cmd_len = REDIS_SPPRINTF(cmd, "COMMAND", "ss", "INFO", sizeof("INFO") - 1,
Z_STRVAL_P(z_arg), Z_STRLEN_P(z_arg));
} else {
int arr_len;

Expand Down

0 comments on commit 0eaeae0

Please sign in to comment.