Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e4adc95

Browse files
committed
Added array of patterns to 'get' option in SORT().
1 parent 0288215 commit e4adc95

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

README.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ $redis->flushAll();
11101110
<pre>
11111111
'by' => 'some_pattern_*',
11121112
'limit' => array(0, 1),
1113-
'get' => 'some_other_pattern_*',
1113+
'get' => 'some_other_pattern_*' or an array of patterns,
11141114
'sort' => 'asc' or 'desc',
11151115
'alpha' => TRUE,
11161116
'store' => 'external-key'

redis.c

+45-11
Original file line numberDiff line numberDiff line change
@@ -2060,18 +2060,51 @@ PHP_METHOD(Redis, sort) {
20602060

20612061
if ((zend_hash_find(Z_ARRVAL_P(z_array), "get", sizeof("get"), (void **) &z_cur) == SUCCESS
20622062
|| zend_hash_find(Z_ARRVAL_P(z_array), "GET", sizeof("GET"), (void **) &z_cur) == SUCCESS)
2063-
&& Z_TYPE_PP(z_cur) == IS_STRING) {
2063+
&& (Z_TYPE_PP(z_cur) == IS_STRING || Z_TYPE_PP(z_cur) == IS_ARRAY)) {
20642064

2065-
old_cmd = cmd;
2066-
cmd_len = redis_cmd_format(&cmd, "%s"
2067-
"$3" _NL
2068-
"GET" _NL
2069-
"$%d" _NL
2070-
"%s" _NL
2071-
, cmd, cmd_len
2072-
, Z_STRLEN_PP(z_cur), Z_STRVAL_PP(z_cur), Z_STRLEN_PP(z_cur));
2073-
elements += 2;
2074-
efree(old_cmd);
2065+
if(Z_TYPE_PP(z_cur) == IS_STRING) {
2066+
old_cmd = cmd;
2067+
cmd_len = redis_cmd_format(&cmd, "%s"
2068+
"$3" _NL
2069+
"GET" _NL
2070+
"$%d" _NL
2071+
"%s" _NL
2072+
, cmd, cmd_len
2073+
, Z_STRLEN_PP(z_cur), Z_STRVAL_PP(z_cur), Z_STRLEN_PP(z_cur));
2074+
elements += 2;
2075+
efree(old_cmd);
2076+
} else if(Z_TYPE_PP(z_cur) == IS_ARRAY) { // loop over the strings in that array and add them as patterns
2077+
2078+
HashTable *keytable = Z_ARRVAL_PP(z_cur);
2079+
for(zend_hash_internal_pointer_reset(keytable);
2080+
zend_hash_has_more_elements(keytable) == SUCCESS;
2081+
zend_hash_move_forward(keytable)) {
2082+
2083+
char *key, *val;
2084+
int key_len, val_len;
2085+
unsigned long idx;
2086+
int type;
2087+
zval **z_value_pp;
2088+
2089+
type = zend_hash_get_current_key_ex(keytable, &key, &key_len, &idx, 0, NULL);
2090+
if(zend_hash_get_current_data(keytable, (void**)&z_value_pp) == FAILURE) {
2091+
continue; /* this should never happen, according to the PHP people. */
2092+
}
2093+
2094+
if(Z_TYPE_PP(z_value_pp) == IS_STRING) {
2095+
old_cmd = cmd;
2096+
cmd_len = redis_cmd_format(&cmd, "%s"
2097+
"$3" _NL
2098+
"GET" _NL
2099+
"$%d" _NL
2100+
"%s" _NL
2101+
, cmd, cmd_len
2102+
, Z_STRLEN_PP(z_value_pp), Z_STRVAL_PP(z_value_pp), Z_STRLEN_PP(z_value_pp));
2103+
elements += 2;
2104+
efree(old_cmd);
2105+
}
2106+
}
2107+
}
20752108
}
20762109

20772110
if ((zend_hash_find(Z_ARRVAL_P(z_array), "alpha", sizeof("alpha"), (void **) &z_cur) == SUCCESS
@@ -2122,6 +2155,7 @@ PHP_METHOD(Redis, sort) {
21222155
}
21232156

21242157
/* complete with prefix */
2158+
21252159
old_cmd = cmd;
21262160
cmd_len = redis_cmd_format(&cmd, "*%d" _NL "%s",
21272161
elements, cmd, cmd_len);

tests/TestRedis.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ public function testSortAsc() {
573573

574574
$this->assertEquals(array_slice($byAgeAsc, 0, 2), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', 0, 2));
575575
$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')));
576-
return;
576+
577577
$this->assertEquals(array_slice($byAgeAsc, 1, 2), $this->redis->sortAsc('person:id', 'person:age_*', 'person:name_*', 1, 2));
578578
$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')));
579579
$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 @@ public function testSortAsc() {
586586
$this->assertEquals($agesBySalaryAsc, $this->redis->sortAsc('person:id', 'person:salary_*', 'person:age_*'));
587587
$this->assertEquals($agesBySalaryAsc, $this->redis->sort('person:id', array('by' => 'person:salary_*', 'get' => 'person:age_*', 'sort' => 'asc')));
588588

589+
$agesAndSalaries = $this->redis->sort('person:id', array('by' => 'person:salary_*', 'get' => array('person:age_*', 'person:salary_*'), 'sort' => 'asc'));
590+
$this->assertEquals(array('34', '2000', '27', '2500', '25', '2800', '41', '3100'), $agesAndSalaries);
591+
589592

590593
// sort non-alpha doesn't change all-string lists
591594
// list → [ghi, def, abc]

0 commit comments

Comments
 (0)