LPOP, RPOP, LRANGE. + tests, of course.

This commit is contained in:
Nicolas Favre-Felix
2009-11-18 14:46:49 +01:00
parent 405616d87c
commit a2d436df7a
3 changed files with 104 additions and 22 deletions
+1
View File
@@ -40,6 +40,7 @@ PHP_METHOD(Redis, getSort);
PHP_METHOD(Redis, lPush);
PHP_METHOD(Redis, rPush);
PHP_METHOD(Redis, lPop);
PHP_METHOD(Redis, rPop);
PHP_METHOD(Redis, lSize);
PHP_METHOD(Redis, lRemove);
PHP_METHOD(Redis, listTrim);
+39 -14
View File
@@ -54,6 +54,7 @@ zend_function_entry redis_functions[] = {
PHP_ME(Redis, lPush, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, rPush, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, lPop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, rPop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, lSize, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, lRemove, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, listTrim, NULL, ZEND_ACC_PUBLIC)
@@ -1215,7 +1216,7 @@ PHP_METHOD(Redis, rPush)
}
/* }}} */
/* {{{ proto string Redis::lPOP(string key , [, int type=0])
/* {{{ proto string Redis::lPOP(string key)
*/
PHP_METHOD(Redis, lPop)
{
@@ -1225,9 +1226,9 @@ PHP_METHOD(Redis, lPop)
int key_len, cmd_len, response_len;
long type = 0;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|l",
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
&object, redis_ce,
&key, &key_len, &type) == FAILURE) {
&key, &key_len) == FAILURE) {
RETURN_FALSE;
}
@@ -1235,13 +1236,7 @@ PHP_METHOD(Redis, lPop)
RETURN_FALSE;
}
if (type == 0) {
cmd_len = spprintf(&cmd, 0, "RPOP %s\r\n", key);
} else if (type == 1) {
cmd_len = spprintf(&cmd, 0, "LPOP %s\r\n", key);
} else {
RETURN_FALSE;
}
cmd_len = spprintf(&cmd, 0, "LPOP %s\r\n", key);
if (redis_sock_write(redis_sock, cmd, cmd_len) < 0) {
efree(cmd);
@@ -1253,12 +1248,42 @@ PHP_METHOD(Redis, lPop)
RETURN_FALSE;
}
if (strncmp(response, "nil", 4) != 0) {
RETURN_STRINGL(response, response_len, 0);
} else {
efree(response);
RETURN_STRINGL(response, response_len, 0);
}
/* {{{ proto string Redis::rPOP(string key)
*/
PHP_METHOD(Redis, rPop)
{
zval *object;
RedisSock *redis_sock;
char *key = NULL, *cmd, *response;
int key_len, cmd_len, response_len;
long type = 0;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
&object, redis_ce,
&key, &key_len) == FAILURE) {
RETURN_FALSE;
}
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
RETURN_FALSE;
}
cmd_len = spprintf(&cmd, 0, "RPOP %s\r\n", key);
if (redis_sock_write(redis_sock, cmd, cmd_len) < 0) {
efree(cmd);
RETURN_FALSE;
}
efree(cmd);
if ((response = redis_sock_read(redis_sock, &response_len TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
RETURN_STRINGL(response, response_len, 0);
}
/* }}} */
+64 -8
View File
@@ -279,8 +279,8 @@ class Redis_Test extends PHPUnit_Framework_TestCase
/* None */
$this->assertEquals(0, $this->redis->type('keyNotExists'));
}
/* PUSH, POP : LPUSH, RPUSH, LPOP, RPOP */
/* PUSH, POP : LPUSH, LPOP */
public function testlPop()
{
/*
@@ -296,9 +296,9 @@ class Redis_Test extends PHPUnit_Framework_TestCase
/* 'list' = [ 'val2', 'val', 'val3'] */
$this->assertEquals('val2', $this->redis->lPop('list', 1));
$this->assertEquals('val3', $this->redis->lPop('list'));
$this->assertEquals('val2', $this->redis->lPop('list'));
$this->assertEquals('val', $this->redis->lPop('list'));
$this->assertEquals('val3', $this->redis->lPop('list'));
$this->assertEquals(FALSE, $this->redis->lPop('list'));
/* testing binary data */
@@ -308,9 +308,43 @@ class Redis_Test extends PHPUnit_Framework_TestCase
$this->assertEquals(TRUE, $this->redis->lPush('list', gzcompress('val2')));
$this->assertEquals(TRUE, $this->redis->lPush('list', gzcompress('val3')));
$this->assertEquals('val1', gzuncompress($this->redis->lPop('list')));
$this->assertEquals('val2', gzuncompress($this->redis->lPop('list')));
$this->assertEquals('val3', gzuncompress($this->redis->lPop('list')));
$this->assertEquals('val2', gzuncompress($this->redis->lPop('list')));
$this->assertEquals('val1', gzuncompress($this->redis->lPop('list')));
}
/* PUSH, POP : RPUSH, RPOP */
public function testrPop()
{
/*
rpush => tail
lpush => head
*/
$this->redis->delete('list');
$this->redis->rPush('list', 'val');
$this->redis->rPush('list', 'val2');
$this->redis->lPush('list', 'val3');
/* 'list' = [ 'val3', 'val', 'val2'] */
$this->assertEquals('val2', $this->redis->rPop('list'));
$this->assertEquals('val', $this->redis->rPop('list'));
$this->assertEquals('val3', $this->redis->rPop('list'));
$this->assertEquals(FALSE, $this->redis->rPop('list'));
/* testing binary data */
$this->redis->delete('list');
$this->assertEquals(TRUE, $this->redis->rPush('list', gzcompress('val1')));
$this->assertEquals(TRUE, $this->redis->rPush('list', gzcompress('val2')));
$this->assertEquals(TRUE, $this->redis->rPush('list', gzcompress('val3')));
$this->assertEquals('val3', gzuncompress($this->redis->rPop('list')));
$this->assertEquals('val2', gzuncompress($this->redis->rPop('list')));
$this->assertEquals('val1', gzuncompress($this->redis->rPop('list')));
}
@@ -325,10 +359,10 @@ class Redis_Test extends PHPUnit_Framework_TestCase
$this->redis->lPush('list', 'val2');
$this->assertEquals(2, $this->redis->lSize('list'));
$this->assertEquals('val', $this->redis->lPop('list'));
$this->assertEquals('val2', $this->redis->lPop('list'));
$this->assertEquals(1, $this->redis->lSize('list'));
$this->assertEquals('val2', $this->redis->lPop('list'));
$this->assertEquals('val', $this->redis->lPop('list'));
$this->assertEquals(0, $this->redis->lSize('list'));
$this->assertEquals(FALSE, $this->redis->lPop('list'));
@@ -522,6 +556,28 @@ class Redis_Test extends PHPUnit_Framework_TestCase
$this->assertEquals($this->redis->lGet('list', 2), 'val');
}
public function testlGetRange() {
$this->redis->delete('list');
$this->redis->lPush('list', 'val');
$this->redis->lPush('list', 'val2');
$this->redis->lPush('list', 'val3');
// pos : 0 1 2
// pos : -3 -2 -1
// list: [val3, val2, val]
$this->assertEquals($this->redis->lGetRange('list', 0, 0), array('val3'));
$this->assertEquals($this->redis->lGetRange('list', 0, 1), array('val3', 'val2'));
$this->assertEquals($this->redis->lGetRange('list', 0, 2), array('val3', 'val2', 'val'));
$this->assertEquals($this->redis->lGetRange('list', 0, 3), array('val3', 'val2', 'val'));
$this->assertEquals($this->redis->lGetRange('list', 0, -1), array('val3', 'val2', 'val'));
$this->assertEquals($this->redis->lGetRange('list', 0, -2), array('val3', 'val2'));
$this->assertEquals($this->redis->lGetRange('list', -2, -1), array('val2', 'val'));
}
}
?>