Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 5 additions & 20 deletions redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -1481,27 +1481,12 @@ PHP_METHOD(Redis, sDiffStore) {

/* {{{ proto array Redis::sort(string key, array options) */
PHP_METHOD(Redis, sort) {
char *cmd;
int cmd_len, have_store;
RedisSock *redis_sock;

// Grab socket, handle command construction
if ((redis_sock = redis_sock_get(getThis(), 0)) == NULL ||
redis_sort_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, &have_store,
&cmd, &cmd_len, NULL, NULL) == FAILURE)
{
RETURN_FALSE;
}
REDIS_PROCESS_KW_CMD("SORT", redis_sort_cmd, redis_read_variant_reply);
}

REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
if (IS_ATOMIC(redis_sock)) {
if (redis_read_variant_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU,
redis_sock, NULL, NULL) < 0)
{
RETURN_FALSE;
}
}
REDIS_PROCESS_RESPONSE(redis_read_variant_reply);
/* {{{ proto array Redis::sort(string key, array options) */
PHP_METHOD(Redis, sort_ro) {
REDIS_PROCESS_KW_CMD("SORT_RO", redis_sort_cmd, redis_read_variant_reply);
}

static void
Expand Down
89 changes: 65 additions & 24 deletions redis.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,35 +566,76 @@ public function sismember(string $key, mixed $value): Redis|bool;
public function slaveof(string $host = null, int $port = 6379): bool;

/**
Interact with Redis' slowlog functionality in variousu ways, depending
on the value of 'operations'.

@param string $operation The operation you wish to perform.  This can
be one of the following values:
'get' - Retreive the Redis slowlog as an array.
'len' - Retreive the length of the slowlog.
'reset' - Remove all slowlog entries.
<code>
<?php
$redis->slowllog('get', -1); // Retreive all slowlog entries.
$redis->slowlog('len'); // Retreive slowlog length.
$redis->slowlog('reset'); // Reset the slowlog.
?>
</code>

@param int $length This optional argument can be passed when operation
is 'get' and will specify how many elements to retreive.
If omitted Redis will send up to a default number of
entries, which is configurable.

Note: With Redis >= 7.0.0 you can send -1 to mean "all".

@return mixed
* Interact with Redis' slowlog functionality in variousu ways, depending
* on the value of 'operations'.
*
* @see https://https://redis.io/commands/slowlog/
* @category administration
*
* @param string $operation The operation you wish to perform.  This can
* be one of the following values:
* 'get' - Retreive the Redis slowlog as an array.
* 'len' - Retreive the length of the slowlog.
* 'reset' - Remove all slowlog entries.
* <code>
* <?php
* $redis->slowllog('get', -1); // Retreive all slowlog entries.
* $redis->slowlog('len'); // Retreive slowlog length.
* $redis->slowlog('reset'); // Reset the slowlog.
* ?>
* </code>
*
* @param int $length This optional argument can be passed when operation
* is 'get' and will specify how many elements to retreive.
* If omitted Redis will send up to a default number of
* entries, which is configurable.
*
* Note: With Redis >= 7.0.0 you can send -1 to mean "all".
*
* @return mixed
*/
public function slowlog(string $operation, int $length = 0): mixed;

/**
* Sort the contents of a Redis key in various ways.
*
* @see https://https://redis.io/commands/sort/
*
* @param string $key The key you wish to sort
* @param array $options Various options controlling how you would like the
* data sorted. See blow for a detailed description
* of this options array.
*
* @return mixed This command can either return an array with the sorted data
* or the number of elements placed in a destination set when
* using the STORE option.
*
* <code>
* <?php
* $options = [
* 'SORT' => 'ASC'|| 'DESC' // Sort in descending or descending order.
* 'ALPHA' => true || false // Whether to sort alphanumerically.
* 'LIMIT' => [0, 10] // Return a subset of the data at offset, count
* 'BY' => 'weight_*' // For each element in the key, read data from the
* external key weight_* and sort based on that value.
* 'GET' => 'weight_*' // For each element in the source key, retreive the
* data from key weight_* and return that in the result
* rather than the source keys' element. This can
* be used in combination with 'BY'
* ];
* ?>
* </code>
*
*/
public function sort(string $key, ?array $options = null): mixed;

/**
* This is simply a read-only variant of the sort command
*
* @see Redis::sort()
*/
public function sort_ro(string $key, ?array $options = null): mixed;

/**
* @deprecated
*/
Expand Down
6 changes: 5 additions & 1 deletion redis_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: c04531e86379ab5c0de12e8e82868b7c7f024068 */
* Stub hash: 0ff60ed233053935cfc7c5a5ecacd6adaf06a458 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null")
Expand Down Expand Up @@ -804,6 +804,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_sort, 0, 1, IS_MIXED
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 1, "null")
ZEND_END_ARG_INFO()

#define arginfo_class_Redis_sort_ro arginfo_class_Redis_sort

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_sortAsc, 0, 1, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, pattern, IS_STRING, 1, "null")
Expand Down Expand Up @@ -1286,6 +1288,7 @@ ZEND_METHOD(Redis, sismember);
ZEND_METHOD(Redis, slaveof);
ZEND_METHOD(Redis, slowlog);
ZEND_METHOD(Redis, sort);
ZEND_METHOD(Redis, sort_ro);
ZEND_METHOD(Redis, sortAsc);
ZEND_METHOD(Redis, sortAscAlpha);
ZEND_METHOD(Redis, sortDesc);
Expand Down Expand Up @@ -1532,6 +1535,7 @@ static const zend_function_entry class_Redis_methods[] = {
ZEND_ME(Redis, slaveof, arginfo_class_Redis_slaveof, ZEND_ACC_PUBLIC)
ZEND_ME(Redis, slowlog, arginfo_class_Redis_slowlog, ZEND_ACC_PUBLIC)
ZEND_ME(Redis, sort, arginfo_class_Redis_sort, ZEND_ACC_PUBLIC)
ZEND_ME(Redis, sort_ro, arginfo_class_Redis_sort_ro, ZEND_ACC_PUBLIC)
ZEND_ME(Redis, sortAsc, arginfo_class_Redis_sortAsc, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
ZEND_ME(Redis, sortAscAlpha, arginfo_class_Redis_sortAscAlpha, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
ZEND_ME(Redis, sortDesc, arginfo_class_Redis_sortDesc, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
Expand Down
26 changes: 5 additions & 21 deletions redis_cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -1555,28 +1555,12 @@ PHP_METHOD(RedisCluster, bzpopmin) {

/* {{{ proto RedisCluster::sort(string key, array options) */
PHP_METHOD(RedisCluster, sort) {
redisCluster *c = GET_CONTEXT();
char *cmd; int cmd_len, have_store; short slot;

if (redis_sort_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, c->flags, &have_store,
&cmd, &cmd_len, &slot, NULL) == FAILURE)
{
RETURN_FALSE;
}

if (cluster_send_command(c,slot,cmd,cmd_len) < 0 || c->err != NULL) {
efree(cmd);
RETURN_FALSE;
}

efree(cmd);
CLUSTER_PROCESS_KW_CMD("SORT", redis_sort_cmd, cluster_variant_resp, 0);
}

// Response type differs based on presence of STORE argument
if (!have_store) {
cluster_mbulk_resp(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, NULL);
} else {
cluster_long_resp(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, NULL);
}
/* {{{ proto RedisCluster::sort_ro(string key, array options) */
PHP_METHOD(RedisCluster, sort_ro) {
CLUSTER_PROCESS_KW_CMD("SORT_RO", redis_sort_cmd, cluster_variant_resp, 1);
}

/* {{{ proto RedisCluster::object(string subcmd, string key) */
Expand Down
8 changes: 8 additions & 0 deletions redis_cluster.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,16 @@ public function smembers(string $key): RedisCluster|array|false;

public function smove(string $src, string $dst, string $member): RedisCluster|bool;

/**
* @see Redis::sort()
*/
public function sort(string $key, ?array $options = NULL): RedisCluster|array|bool|int|string;

/**
* @see Redis::sort_ro()
*/
public function sort_ro(string $key, ?array $options = NULL): RedisCluster|array|bool|int|string;

public function spop(string $key, int $count = 0): RedisCluster|string|array|false;

public function srandmember(string $key, int $count = 0): RedisCluster|string|array|false;
Expand Down
6 changes: 5 additions & 1 deletion redis_cluster_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: e761b2a65f9f57254e0201f9643b823e79e2a0a8 */
* Stub hash: 3d10a4c161f9a4bcf65ac9acfebbb86d11f9cf0d */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 1)
Expand Down Expand Up @@ -684,6 +684,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_RedisCluster_sort, 0,
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 1, "NULL")
ZEND_END_ARG_INFO()

#define arginfo_class_RedisCluster_sort_ro arginfo_class_RedisCluster_sort

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_RedisCluster_spop, 0, 1, RedisCluster, MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, count, IS_LONG, 0, "0")
Expand Down Expand Up @@ -1092,6 +1094,7 @@ ZEND_METHOD(RedisCluster, slowlog);
ZEND_METHOD(RedisCluster, smembers);
ZEND_METHOD(RedisCluster, smove);
ZEND_METHOD(RedisCluster, sort);
ZEND_METHOD(RedisCluster, sort_ro);
ZEND_METHOD(RedisCluster, spop);
ZEND_METHOD(RedisCluster, srandmember);
ZEND_METHOD(RedisCluster, srem);
Expand Down Expand Up @@ -1298,6 +1301,7 @@ static const zend_function_entry class_RedisCluster_methods[] = {
ZEND_ME(RedisCluster, smembers, arginfo_class_RedisCluster_smembers, ZEND_ACC_PUBLIC)
ZEND_ME(RedisCluster, smove, arginfo_class_RedisCluster_smove, ZEND_ACC_PUBLIC)
ZEND_ME(RedisCluster, sort, arginfo_class_RedisCluster_sort, ZEND_ACC_PUBLIC)
ZEND_ME(RedisCluster, sort_ro, arginfo_class_RedisCluster_sort_ro, ZEND_ACC_PUBLIC)
ZEND_ME(RedisCluster, spop, arginfo_class_RedisCluster_spop, ZEND_ACC_PUBLIC)
ZEND_ME(RedisCluster, srandmember, arginfo_class_RedisCluster_srandmember, ZEND_ACC_PUBLIC)
ZEND_ME(RedisCluster, srem, arginfo_class_RedisCluster_srem, ZEND_ACC_PUBLIC)
Expand Down
6 changes: 5 additions & 1 deletion redis_cluster_legacy_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: e761b2a65f9f57254e0201f9643b823e79e2a0a8 */
* Stub hash: 3d10a4c161f9a4bcf65ac9acfebbb86d11f9cf0d */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
ZEND_ARG_INFO(0, name)
Expand Down Expand Up @@ -581,6 +581,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster_sort, 0, 0, 1)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()

#define arginfo_class_RedisCluster_sort_ro arginfo_class_RedisCluster_sort

#define arginfo_class_RedisCluster_spop arginfo_class_RedisCluster_lpop

#define arginfo_class_RedisCluster_srandmember arginfo_class_RedisCluster_lpop
Expand Down Expand Up @@ -944,6 +946,7 @@ ZEND_METHOD(RedisCluster, slowlog);
ZEND_METHOD(RedisCluster, smembers);
ZEND_METHOD(RedisCluster, smove);
ZEND_METHOD(RedisCluster, sort);
ZEND_METHOD(RedisCluster, sort_ro);
ZEND_METHOD(RedisCluster, spop);
ZEND_METHOD(RedisCluster, srandmember);
ZEND_METHOD(RedisCluster, srem);
Expand Down Expand Up @@ -1150,6 +1153,7 @@ static const zend_function_entry class_RedisCluster_methods[] = {
ZEND_ME(RedisCluster, smembers, arginfo_class_RedisCluster_smembers, ZEND_ACC_PUBLIC)
ZEND_ME(RedisCluster, smove, arginfo_class_RedisCluster_smove, ZEND_ACC_PUBLIC)
ZEND_ME(RedisCluster, sort, arginfo_class_RedisCluster_sort, ZEND_ACC_PUBLIC)
ZEND_ME(RedisCluster, sort_ro, arginfo_class_RedisCluster_sort_ro, ZEND_ACC_PUBLIC)
ZEND_ME(RedisCluster, spop, arginfo_class_RedisCluster_spop, ZEND_ACC_PUBLIC)
ZEND_ME(RedisCluster, srandmember, arginfo_class_RedisCluster_srandmember, ZEND_ACC_PUBLIC)
ZEND_ME(RedisCluster, srem, arginfo_class_RedisCluster_srem, ZEND_ACC_PUBLIC)
Expand Down
16 changes: 4 additions & 12 deletions redis_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -3535,8 +3535,7 @@ int redis_zincrby_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,

/* SORT */
int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
int *using_store, char **cmd, int *cmd_len, short *slot,
void **ctx)
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx)
{
zval *z_opts=NULL, *z_ele, z_argv;
char *key;
Expand All @@ -3551,16 +3550,10 @@ int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
return FAILURE;
}

// Default that we're not using store
*using_store = 0;

// 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) {
// Construct command
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "SORT", "k", key, key_len);

/* Not storing */
*using_store = 0;
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "k", key, key_len);

return SUCCESS;
}
Expand Down Expand Up @@ -3627,7 +3620,7 @@ int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
add_next_index_stringl(&z_argv, Z_STRVAL_P(z_ele), Z_STRLEN_P(z_ele));

// We are using STORE
*using_store = 1;
*ctx = PHPREDIS_CTX_PTR;
}

// GET option
Expand Down Expand Up @@ -3725,8 +3718,7 @@ int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,

// Start constructing our command
HashTable *ht_argv = Z_ARRVAL_P(&z_argv);
redis_cmd_init_sstr(&cmdstr, zend_hash_num_elements(ht_argv), "SORT",
sizeof("SORT")-1);
redis_cmd_init_sstr(&cmdstr, zend_hash_num_elements(ht_argv), kw, strlen(kw));

// Iterate through our arguments
ZEND_HASH_FOREACH_VAL(ht_argv, z_ele) {
Expand Down
6 changes: 3 additions & 3 deletions redis_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,6 @@ int redis_srandmember_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
int redis_zincrby_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx);

int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
int *using_store, char **cmd, int *cmd_len, short *slot, void **ctx);

int redis_hdel_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx);

Expand Down Expand Up @@ -365,6 +362,9 @@ int redis_sentinel_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
int redis_sentinel_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx);

int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx);

/* Commands that don't communicate with Redis at all (such as getOption,
* setOption, _prefix, _serialize, etc). These can be handled in one place
* with the method of grabbing our RedisSock* object in different ways
Expand Down
6 changes: 5 additions & 1 deletion redis_legacy_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: c04531e86379ab5c0de12e8e82868b7c7f024068 */
* Stub hash: 0ff60ed233053935cfc7c5a5ecacd6adaf06a458 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
ZEND_ARG_INFO(0, options)
Expand Down Expand Up @@ -678,6 +678,8 @@ ZEND_END_ARG_INFO()

#define arginfo_class_Redis_sort arginfo_class_Redis_getEx

#define arginfo_class_Redis_sort_ro arginfo_class_Redis_getEx

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_sortAsc, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, pattern)
Expand Down Expand Up @@ -1118,6 +1120,7 @@ ZEND_METHOD(Redis, sismember);
ZEND_METHOD(Redis, slaveof);
ZEND_METHOD(Redis, slowlog);
ZEND_METHOD(Redis, sort);
ZEND_METHOD(Redis, sort_ro);
ZEND_METHOD(Redis, sortAsc);
ZEND_METHOD(Redis, sortAscAlpha);
ZEND_METHOD(Redis, sortDesc);
Expand Down Expand Up @@ -1364,6 +1367,7 @@ static const zend_function_entry class_Redis_methods[] = {
ZEND_ME(Redis, slaveof, arginfo_class_Redis_slaveof, ZEND_ACC_PUBLIC)
ZEND_ME(Redis, slowlog, arginfo_class_Redis_slowlog, ZEND_ACC_PUBLIC)
ZEND_ME(Redis, sort, arginfo_class_Redis_sort, ZEND_ACC_PUBLIC)
ZEND_ME(Redis, sort_ro, arginfo_class_Redis_sort_ro, ZEND_ACC_PUBLIC)
ZEND_ME(Redis, sortAsc, arginfo_class_Redis_sortAsc, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
ZEND_ME(Redis, sortAscAlpha, arginfo_class_Redis_sortAscAlpha, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
ZEND_ME(Redis, sortDesc, arginfo_class_Redis_sortDesc, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
Expand Down
Loading