Permalink
Browse files

Added array of patterns to 'get' option in SORT().

  • Loading branch information...
1 parent 0288215 commit e4adc956ad3aae8f67d49bb44f911f1cea567f9c @nicolasff nicolasff committed Sep 17, 2010
Showing with 50 additions and 13 deletions.
  1. +1 −1 README.markdown
  2. +45 −11 redis.c
  3. +4 −1 tests/TestRedis.php
View
@@ -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'
View
56 redis.c
@@ -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);
View
@@ -573,7 +573,7 @@ public function testSortAsc() {
$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 @@ public function testSortAsc() {
$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]

0 comments on commit e4adc95

Please sign in to comment.