mirror of
https://github.com/phpredis/phpredis.git
synced 2026-06-19 07:35:31 +00:00
Added array of patterns to 'get' option in SORT().
This commit is contained in:
+1
-1
@@ -1110,7 +1110,7 @@ $redis->flushAll();
|
||||
<pre>
|
||||
'by' => 'some_pattern_*',
|
||||
'limit' => array(0, 1),
|
||||
'get' => 'some_other_pattern_*',
|
||||
'get' => 'some_other_pattern_*' or an array of patterns,
|
||||
'sort' => 'asc' or 'desc',
|
||||
'alpha' => TRUE,
|
||||
'store' => 'external-key'
|
||||
|
||||
@@ -2060,18 +2060,51 @@ PHP_METHOD(Redis, sort) {
|
||||
|
||||
if ((zend_hash_find(Z_ARRVAL_P(z_array), "get", sizeof("get"), (void **) &z_cur) == SUCCESS
|
||||
|| zend_hash_find(Z_ARRVAL_P(z_array), "GET", sizeof("GET"), (void **) &z_cur) == SUCCESS)
|
||||
&& Z_TYPE_PP(z_cur) == IS_STRING) {
|
||||
&& (Z_TYPE_PP(z_cur) == IS_STRING || Z_TYPE_PP(z_cur) == IS_ARRAY)) {
|
||||
|
||||
old_cmd = cmd;
|
||||
cmd_len = redis_cmd_format(&cmd, "%s"
|
||||
"$3" _NL
|
||||
"GET" _NL
|
||||
"$%d" _NL
|
||||
"%s" _NL
|
||||
, cmd, cmd_len
|
||||
, Z_STRLEN_PP(z_cur), Z_STRVAL_PP(z_cur), Z_STRLEN_PP(z_cur));
|
||||
elements += 2;
|
||||
efree(old_cmd);
|
||||
if(Z_TYPE_PP(z_cur) == IS_STRING) {
|
||||
old_cmd = cmd;
|
||||
cmd_len = redis_cmd_format(&cmd, "%s"
|
||||
"$3" _NL
|
||||
"GET" _NL
|
||||
"$%d" _NL
|
||||
"%s" _NL
|
||||
, cmd, cmd_len
|
||||
, Z_STRLEN_PP(z_cur), Z_STRVAL_PP(z_cur), Z_STRLEN_PP(z_cur));
|
||||
elements += 2;
|
||||
efree(old_cmd);
|
||||
} else if(Z_TYPE_PP(z_cur) == IS_ARRAY) { // loop over the strings in that array and add them as patterns
|
||||
|
||||
HashTable *keytable = Z_ARRVAL_PP(z_cur);
|
||||
for(zend_hash_internal_pointer_reset(keytable);
|
||||
zend_hash_has_more_elements(keytable) == SUCCESS;
|
||||
zend_hash_move_forward(keytable)) {
|
||||
|
||||
char *key, *val;
|
||||
int key_len, val_len;
|
||||
unsigned long idx;
|
||||
int type;
|
||||
zval **z_value_pp;
|
||||
|
||||
type = zend_hash_get_current_key_ex(keytable, &key, &key_len, &idx, 0, NULL);
|
||||
if(zend_hash_get_current_data(keytable, (void**)&z_value_pp) == FAILURE) {
|
||||
continue; /* this should never happen, according to the PHP people. */
|
||||
}
|
||||
|
||||
if(Z_TYPE_PP(z_value_pp) == IS_STRING) {
|
||||
old_cmd = cmd;
|
||||
cmd_len = redis_cmd_format(&cmd, "%s"
|
||||
"$3" _NL
|
||||
"GET" _NL
|
||||
"$%d" _NL
|
||||
"%s" _NL
|
||||
, cmd, cmd_len
|
||||
, Z_STRLEN_PP(z_value_pp), Z_STRVAL_PP(z_value_pp), Z_STRLEN_PP(z_value_pp));
|
||||
elements += 2;
|
||||
efree(old_cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((zend_hash_find(Z_ARRVAL_P(z_array), "alpha", sizeof("alpha"), (void **) &z_cur) == SUCCESS
|
||||
@@ -2122,6 +2155,7 @@ PHP_METHOD(Redis, sort) {
|
||||
}
|
||||
|
||||
/* complete with prefix */
|
||||
|
||||
old_cmd = cmd;
|
||||
cmd_len = redis_cmd_format(&cmd, "*%d" _NL "%s",
|
||||
elements, cmd, cmd_len);
|
||||
|
||||
+4
-1
@@ -573,7 +573,7 @@ class Redis_Test extends PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertEquals(array_slice($byAgeAsc, 0, 2), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', 0, 2));
|
||||
$this->assertEquals(array_slice($byAgeAsc, 0, 2), $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => array(0, 2), 'sort' => 'asc')));
|
||||
return;
|
||||
|
||||
$this->assertEquals(array_slice($byAgeAsc, 1, 2), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', 1, 2));
|
||||
$this->assertEquals(array_slice($byAgeAsc, 1, 2), $this->redis->sort('person:id', array('by' => 'person:age_*', 'get' => 'person:name_*', 'limit' => array(1, 2), 'sort' => 'asc')));
|
||||
$this->assertEquals(array_slice($byAgeAsc, 0, 3), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', NULL, 3)); // NULL is transformed to 0 if there is something after it.
|
||||
@@ -586,6 +586,9 @@ class Redis_Test extends PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($agesBySalaryAsc, $this->redis->sortAsc('person:id', 'person:salary_*', 'person:age_*'));
|
||||
$this->assertEquals($agesBySalaryAsc, $this->redis->sort('person:id', array('by' => 'person:salary_*', 'get' => 'person:age_*', 'sort' => 'asc')));
|
||||
|
||||
$agesAndSalaries = $this->redis->sort('person:id', array('by' => 'person:salary_*', 'get' => array('person:age_*', 'person:salary_*'), 'sort' => 'asc'));
|
||||
$this->assertEquals(array('34', '2000', '27', '2500', '25', '2800', '41', '3100'), $agesAndSalaries);
|
||||
|
||||
|
||||
// sort non-alpha doesn't change all-string lists
|
||||
// list → [ghi, def, abc]
|
||||
|
||||
Reference in New Issue
Block a user