Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/phpredis/phpredis.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormichael-grunder <michael.grunder@gmail.com>2022-10-23 21:38:29 +0300
committerMichael Grunder <michael.grunder@gmail.com>2022-10-23 23:18:13 +0300
commit8c7c5a3aa2c6b693839a391bffa18ee161cbe73a (patch)
tree5da4bc9ca3f46d7ef1ee54a7ff643ef7d22c50be
parent1343f5008301a256fcbfa0161931e2a39791055f (diff)
Refactor SORT and add SORT_RO command
See #2068
-rw-r--r--redis.c25
-rw-r--r--redis.stub.php89
-rw-r--r--redis_arginfo.h6
-rw-r--r--redis_cluster.c26
-rw-r--r--redis_cluster.stub.php8
-rw-r--r--redis_cluster_arginfo.h6
-rw-r--r--redis_cluster_legacy_arginfo.h6
-rw-r--r--redis_commands.c16
-rw-r--r--redis_commands.h6
-rw-r--r--redis_legacy_arginfo.h6
-rw-r--r--tests/RedisTest.php59
11 files changed, 142 insertions, 111 deletions
diff --git a/redis.c b/redis.c
index adfb6827..7360cdb0 100644
--- a/redis.c
+++ b/redis.c
@@ -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
diff --git a/redis.stub.php b/redis.stub.php
index 3c3c78e5..ddf6a26d 100644
--- a/redis.stub.php
+++ b/redis.stub.php
@@ -566,36 +566,77 @@ class Redis {
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
*/
public function sortAsc(string $key, ?string $pattern = null, mixed $get = null, int $offset = -1, int $count = -1, ?string $store = null): array;
diff --git a/redis_arginfo.h b/redis_arginfo.h
index 7d350910..216f5154 100644
--- a/redis_arginfo.h
+++ b/redis_arginfo.h
@@ -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")
@@ -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")
@@ -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);
@@ -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)
diff --git a/redis_cluster.c b/redis_cluster.c
index c5078521..8d38989e 100644
--- a/redis_cluster.c
+++ b/redis_cluster.c
@@ -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) */
diff --git a/redis_cluster.stub.php b/redis_cluster.stub.php
index 165dfd3b..88ca6c0e 100644
--- a/redis_cluster.stub.php
+++ b/redis_cluster.stub.php
@@ -336,8 +336,16 @@ class RedisCluster {
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;
diff --git a/redis_cluster_arginfo.h b/redis_cluster_arginfo.h
index 0b4619fb..9357e62a 100644
--- a/redis_cluster_arginfo.h
+++ b/redis_cluster_arginfo.h
@@ -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)
@@ -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")
@@ -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);
@@ -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)
diff --git a/redis_cluster_legacy_arginfo.h b/redis_cluster_legacy_arginfo.h
index cc73a262..e3f41e25 100644
--- a/redis_cluster_legacy_arginfo.h
+++ b/redis_cluster_legacy_arginfo.h
@@ -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)
@@ -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
@@ -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);
@@ -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)
diff --git a/redis_commands.c b/redis_commands.c
index 416972d6..18dfeb61 100644
--- a/redis_commands.c
+++ b/redis_commands.c
@@ -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;
@@ -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;
}
@@ -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
@@ -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) {
diff --git a/redis_commands.h b/redis_commands.h
index 4d7a13e0..2de64347 100644
--- a/redis_commands.h
+++ b/redis_commands.h
@@ -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);
@@ -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
diff --git a/redis_legacy_arginfo.h b/redis_legacy_arginfo.h
index 0b6fbd23..4eedda7a 100644
--- a/redis_legacy_arginfo.h
+++ b/redis_legacy_arginfo.h
@@ -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)
@@ -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)
@@ -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);
@@ -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)
diff --git a/tests/RedisTest.php b/tests/RedisTest.php
index ea036ebc..2a5085ca 100644
--- a/tests/RedisTest.php
+++ b/tests/RedisTest.php
@@ -1364,41 +1364,46 @@ class Redis_Test extends TestSuite
public function testSortDesc() {
- $this->setupSort();
+ $this->setupSort();
- // sort by age and get IDs
- $byAgeDesc = ['4','2','1','3'];
- $this->assertEquals($byAgeDesc, $this->redis->sort('person:id', ['by' => 'person:age_*', 'sort' => 'desc']));
+ // sort by age and get IDs
+ $byAgeDesc = ['4','2','1','3'];
+ $this->assertEquals($byAgeDesc, $this->redis->sort('person:id', ['by' => 'person:age_*', 'sort' => 'desc']));
- // sort by age and get names
- $byAgeDesc = ['Dave', 'Bob', 'Alice', 'Carol'];
- $this->assertEquals($byAgeDesc, $this->redis->sort('person:id', ['by' => 'person:age_*', 'get' => 'person:name_*', 'sort' => 'desc']));
+ // sort by age and get names
+ $byAgeDesc = ['Dave', 'Bob', 'Alice', 'Carol'];
+ $this->assertEquals($byAgeDesc, $this->redis->sort('person:id', ['by' => 'person:age_*', 'get' => 'person:name_*', 'sort' => 'desc']));
- $this->assertEquals(array_slice($byAgeDesc, 0, 2), $this->redis->sort('person:id', ['by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => [0, 2], 'sort' => 'desc']));
- $this->assertEquals(array_slice($byAgeDesc, 1, 2), $this->redis->sort('person:id', ['by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => [1, 2], 'sort' => 'desc']));
+ $this->assertEquals(array_slice($byAgeDesc, 0, 2), $this->redis->sort('person:id', ['by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => [0, 2], 'sort' => 'desc']));
+ $this->assertEquals(array_slice($byAgeDesc, 1, 2), $this->redis->sort('person:id', ['by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => [1, 2], 'sort' => 'desc']));
- // sort by salary and get ages
- $agesBySalaryDesc = ['41', '25', '27', '34'];
- $this->assertEquals($agesBySalaryDesc, $this->redis->sort('person:id', ['by' => 'person:salary_*', 'get' => 'person:age_*', 'sort' => 'desc']));
+ // sort by salary and get ages
+ $agesBySalaryDesc = ['41', '25', '27', '34'];
+ $this->assertEquals($agesBySalaryDesc, $this->redis->sort('person:id', ['by' => 'person:salary_*', 'get' => 'person:age_*', 'sort' => 'desc']));
- // sort non-alpha doesn't change all-string lists
- $list = ['def', 'abc', 'ghi'];
- $this->redis->del('list');
- foreach($list as $i) {
- $this->redis->lPush('list', $i);
- }
+ // sort non-alpha doesn't change all-string lists
+ $list = ['def', 'abc', 'ghi'];
+ $this->redis->del('list');
+ foreach($list as $i) {
+ $this->redis->lPush('list', $i);
+ }
- // SORT list → [ghi, abc, def]
- if (version_compare($this->version, "2.5.0") < 0) {
- $this->assertEquals(array_reverse($list), $this->redis->sort('list', ['sort' => 'desc']));
- } else {
- // TODO rewrite, from 2.6.0 release notes:
- // SORT now will refuse to sort in numerical mode elements that can't be parsed
- // as numbers
+ // SORT list ALPHA → [abc, def, ghi]
+ $this->assertEquals(['ghi', 'def', 'abc'], $this->redis->sort('list', ['sort' => 'desc', 'alpha' => TRUE]));
}
- // SORT list ALPHA → [abc, def, ghi]
- $this->assertEquals(['ghi', 'def', 'abc'], $this->redis->sort('list', ['sort' => 'desc', 'alpha' => TRUE]));
+ /* This test is just to make sure SORT and SORT_RO are both callable */
+ public function testSortHandler() {
+ $this->redis->del('list');
+
+ $this->redis->rpush('list', 'c', 'b', 'a');
+
+ $methods = ['sort'];
+ if ($this->minVersionCheck('7.0.0')) $methods[] = 'sort_ro';
+
+ foreach ($methods as $method) {
+ $this->assertEquals(['a', 'b', 'c'], $this->redis->$method('list', ['sort' => 'asc', 'alpha' => true]));
+ }
}
// LINDEX