Skip to content

Commit d4a0869

Browse files
Allow '-' and '+' arguments and add tests for zLexCount and zRemRangeByLex
1 parent e4c6227 commit d4a0869

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

redis_commands.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,12 @@ int redis_zrangebylex_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
901901
return SUCCESS;
902902
}
903903

904+
/* Validate ZLEX* min/max argument strings */
905+
static int validate_zlex_arg(const char *arg, strlen_t len) {
906+
return (len > 1 && (*arg == '[' || *arg == '(')) ||
907+
(len == 1 && (*arg == '+' || *arg == '-'));
908+
}
909+
904910
/* ZLEXCOUNT/ZREMRANGEBYLEX */
905911
int redis_gen_zlex_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
906912
char *kw, char **cmd, int *cmd_len, short *slot,
@@ -917,11 +923,9 @@ int redis_gen_zlex_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
917923
}
918924

919925
/* Quick sanity check on min/max */
920-
if (min_len<1 || max_len<1 || (min[0]!='(' && min[0]!='[') ||
921-
(max[0]!='(' && max[0]!='['))
922-
{
926+
if (!validate_zlex_arg(min, min_len) || !validate_zlex_arg(max, max_len)) {
923927
php_error_docref(NULL TSRMLS_CC, E_WARNING,
924-
"Min and Max arguments must begin with '(' or '['");
928+
"Min/Max args can be '-' or '+', or start with '[' or '('");
925929
return FAILURE;
926930
}
927931

tests/RedisTest.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2333,11 +2333,60 @@ public function testZRangeByLex() {
23332333
$this->assertEquals($this->redis->zRangeByLex('key', '-', '[c'), Array('a', 'b', 'c'));
23342334
$this->assertEquals($this->redis->zRangeByLex('key', '(e', '+'), Array('f', 'g'));
23352335

2336-
// with limit offset
2336+
// with limit offset
23372337
$this->assertEquals($this->redis->zRangeByLex('key', '-', '[c', 1, 2), Array('b', 'c') );
23382338
$this->assertEquals($this->redis->zRangeByLex('key', '-', '(c', 1, 2), Array('b'));
23392339
}
23402340

2341+
public function testZLexCount() {
2342+
if (version_compare($this->version, "2.8.9", "lt")) {
2343+
$this->MarkTestSkipped();
2344+
return;
2345+
}
2346+
2347+
$this->redis->del('key');
2348+
foreach (range('a', 'g') as $c) {
2349+
$entries[] = $c;
2350+
$this->redis->zAdd('key', 0, $c);
2351+
}
2352+
2353+
/* Special -/+ values */
2354+
$this->assertEquals($this->redis->zLexCount('key', '-', '-'), 0);
2355+
$this->assertEquals($this->redis->zLexCount('key', '-', '+'), count($entries));
2356+
2357+
/* Verify invalid arguments return FALSE */
2358+
$this->assertFalse(@$this->redis->zLexCount('key', '[a', 'bad'));
2359+
$this->assertFalse(@$this->redis->zLexCount('key', 'bad', '[a'));
2360+
2361+
/* Now iterate through */
2362+
$start = $entries[0];
2363+
for ($i = 1; $i < count($entries); $i++) {
2364+
$end = $entries[$i];
2365+
$this->assertEquals($this->redis->zLexCount('key', "[$start", "[$end"), $i + 1);
2366+
$this->assertEquals($this->redis->zLexCount('key', "[$start", "($end"), $i);
2367+
$this->assertEquals($this->redis->zLexCount('key', "($start", "($end"), $i - 1);
2368+
}
2369+
}
2370+
2371+
public function testZRemRangeByLex() {
2372+
if (version_compare($this->version, "2.8.9", "lt")) {
2373+
$this->MarkTestSkipped();
2374+
return;
2375+
}
2376+
2377+
$this->redis->del('key');
2378+
$this->redis->zAdd('key', 0, 'a', 0, 'b', 0, 'c');
2379+
$this->assertEquals($this->redis->zRemRangeByLex('key', '-', '+'), 3);
2380+
2381+
$this->redis->zAdd('key', 0, 'a', 0, 'b', 0, 'c');
2382+
$this->assertEquals($this->redis->zRemRangeByLex('key', '[a', '[c'), 3);
2383+
2384+
$this->redis->zAdd('key', 0, 'a', 0, 'b', 0, 'c');
2385+
$this->assertEquals($this->redis->zRemRangeByLex('key', '[a', '(a'), 0);
2386+
$this->assertEquals($this->redis->zRemRangeByLex('key', '(a', '(c'), 1);
2387+
$this->assertEquals($this->redis->zRemRangeByLex('key', '[a', '[c'), 2);
2388+
}
2389+
23412390
public function testHashes() {
23422391
$this->redis->del('h', 'key');
23432392
$this->assertTrue(0 === $this->redis->hLen('h'));

0 commit comments

Comments
 (0)