SINTERSTORE + refactored SINTER. + tests.

This commit is contained in:
Nicolas Favre-Felix
2009-11-18 17:46:41 +01:00
parent b01bfba357
commit 00c7471dd8
3 changed files with 116 additions and 17 deletions
+1
View File
@@ -59,6 +59,7 @@ PHP_METHOD(Redis, sPop);
PHP_METHOD(Redis, sContains);
PHP_METHOD(Redis, sGetMembers);
PHP_METHOD(Redis, sInter);
PHP_METHOD(Redis, sInterStore);
PHP_METHOD(Redis, setTimeout);
#ifdef PHP_WIN32
+55 -15
View File
@@ -73,6 +73,7 @@ zend_function_entry redis_functions[] = {
PHP_ME(Redis, sContains, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, sGetMembers, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, sInter, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, sInterStore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, setTimeout, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(Redis, open, connect, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
@@ -1961,17 +1962,63 @@ PHP_METHOD(Redis, sGetMembers)
*/
PHP_METHOD(Redis, sInter) {
sinter_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SINTER", sizeof("SINTER") - 1);
}
char *response;
int response_len;
RedisSock *redis_sock;
PHPAPI int sinter_common(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int keyword_len TSRMLS_DC)
sinter_common(INTERNAL_FUNCTION_PARAM_PASSTHRU,
"SINTER", sizeof("SINTER") - 1,
0, &redis_sock);
/* read multibulk reply */
if (redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU,
redis_sock, &response_len TSRMLS_CC) < 0) {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto array Redis::sInter(string destination, string key0, ... string keyN)
*/
PHP_METHOD(Redis, sInterStore) {
char *response;
int response_len;
RedisSock *redis_sock;
sinter_common(INTERNAL_FUNCTION_PARAM_PASSTHRU,
"SINTERSTORE", sizeof("SINTERSTORE") - 1,
1, &redis_sock);
if ((response = redis_sock_read(redis_sock, &response_len TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
if(response[0] == '+') {
efree(response);
RETURN_FALSE;
} else {
long ret = atol(response + 1);
efree(response);
RETURN_LONG(ret);
}
}
/* }}} */
PHPAPI int sinter_common(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int keyword_len,
int min_argc, RedisSock **redis_sock TSRMLS_DC)
{
zval *object, **z_args;
RedisSock *redis_sock;
char **keys, *cmd, *response;
int cmd_len, count, response_len, *keys_len;
char **keys, *cmd;
int cmd_len, count, *keys_len;
int i, argc = ZEND_NUM_ARGS();
if(argc < min_argc) {
WRONG_PARAM_COUNT;
RETURN_FALSE;
}
z_args = emalloc(argc * sizeof(zval*));
if(zend_get_parameters_array(ht, argc, z_args) == FAILURE) {
efree(z_args);
@@ -1991,7 +2038,7 @@ PHPAPI int sinter_common(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int keywor
}
/* get redis socket */
if (redis_sock_get(getThis(), &redis_sock TSRMLS_CC) < 0) {
if (redis_sock_get(getThis(), redis_sock TSRMLS_CC) < 0) {
efree(keys);
efree(keys_len);
efree(z_args);
@@ -2016,19 +2063,12 @@ PHPAPI int sinter_common(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int keywor
efree(z_args);
if (redis_sock_write(redis_sock, cmd, cmd_len) < 0) {
if (redis_sock_write(*redis_sock, cmd, cmd_len) < 0) {
efree(cmd);
RETURN_FALSE;
}
efree(cmd);
/* read multibulk reply */
if (redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU,
redis_sock, &response_len TSRMLS_CC) < 0) {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto array Redis::getSort(string key [,order = 0, pattern = "*", start=0,
* end = 0])
+60 -2
View File
@@ -693,8 +693,8 @@ class Redis_Test extends PHPUnit_Framework_TestCase
$this->assertTrue(in_array($i, array_intersect($y, $z)));
}
$zt = $this->redis->sInter('y', 'z'); // prime squares
$this->assertTrue($zt === array('1'));
$zt = $this->redis->sInter('z', 't'); // prime squares
$this->assertTrue($zt === array());
$xyz = $this->redis->sInter('x', 'y', 'z');// odd prime squares
$this->assertTrue($xyz === array('1'));
@@ -703,6 +703,64 @@ class Redis_Test extends PHPUnit_Framework_TestCase
$this->assertTrue($nil === FALSE);
}
public function testsInterStore() {
$this->redis->delete('x'); // set of odd numbers
$this->redis->delete('y'); // set of prime numbers
$this->redis->delete('z'); // set of squares
$this->redis->delete('t'); // set of numbers of the form n^2 - 1
$x = array(1,3,5,7,9,11,13,15,17,19,21,23,25);
foreach($x as $i) {
$this->redis->sAdd('x', $i);
}
$y = array(1,2,3,5,7,11,13,17,19,23);
foreach($y as $i) {
$this->redis->sAdd('y', $i);
}
$z = array(1,4,9,16,25);
foreach($z as $i) {
$this->redis->sAdd('z', $i);
}
$t = array(2,5,10,17,26);
foreach($t as $i) {
$this->redis->sAdd('t', $i);
}
$count = $this->redis->sInterStore('k', 'x', 'y'); // odd prime numbers
$this->assertEquals($count, $this->redis->sSize('k'));
foreach(array_intersect($x, $y) as $i) {
$this->assertTrue($this->redis->sContains('k', $i));
}
$count = $this->redis->sInterStore('k', 'y', 'z'); // set of odd squares
$this->assertEquals($count, $this->redis->sSize('k'));
foreach(array_intersect($y, $z) as $i) {
$this->assertTrue($this->redis->sContains('k', $i));
}
$count = $this->redis->sInterStore('k', 'z', 't'); // squares of the form n^2 + 1
$this->assertEquals($count, 0);
$this->assertEquals($count, $this->redis->sSize('k'));
$this->redis->delete('z');
$xyz = $this->redis->sInterStore('k', 'x', 'y', 'z'); // only z missing, expect FALSE.
$this->assertTrue($xyz === FALSE);
$this->redis->delete('y');
$xyz = $this->redis->sInterStore('k', 'x', 'y', 'z'); // y and z missing, expect FALSE.
$this->assertTrue($xyz === FALSE);
$this->redis->delete('x');
$xyz = $this->redis->sInterStore('k', 'x', 'y', 'z'); // x y and z ALL missing, expect FALSE.
$this->assertTrue($xyz === FALSE);
$o = $this->redis->sInterStore('k');
$this->assertTrue($o === 0);
}
public function testlGetRange() {
$this->redis->delete('list');