Permalink
Browse files

Merge branch 'master' of github.com:nicolasff/phpredis

  • Loading branch information...
michael-grunder committed Sep 10, 2012
2 parents 4c72cbc + 6dc3ad6 commit b4735e578b7881c1c72805fd1127e5051386ce83
Showing with 111 additions and 23 deletions.
  1. +19 −1 README.markdown
  2. +1 −1 arrays.markdown
  3. +3 −0 library.c
  4. +1 −0 php_redis.h
  5. +27 −0 redis.c
  6. +10 −4 redis_array.c
  7. +27 −14 redis_array_impl.c
  8. +1 −1 redis_array_impl.h
  9. +14 −2 tests/TestRedis.php
  10. +8 −0 tests/array-tests.php
View
@@ -2486,7 +2486,7 @@ $redis->script('exists', $script1, [$script2, $script3, ...]);
## getLastError
##### Description
-The last error message (if any) returned from a SCRIPT call
+The last error message (if any)
##### Parameters
*none*
##### Return Value
@@ -2498,6 +2498,24 @@ $err = $redis->getLastError();
// "ERR Error compiling script (new function): user_script:1: '=' expected near '-'"
</pre>
+## clearLastError
+##### Description
+Clear the last error message
+##### Parameters
+*none*
+##### Return Value
+*BOOL* TRUE
+##### Examples
+<pre>
+$redis->set('x', 'a');
+$redis->incr('x');
+$err = $redis->getLastError();
+// "ERR value is not an integer or out of range"
+$redis->clearLastError();
+$err = $redis->getLastError();
+// NULL
+</pre>
+
## _prefix
##### Description
A utility method to prefix the value with the prefix setting for phpredis.
View
@@ -73,7 +73,7 @@ For instance, the keys “{user:1}:name” and “{user:1}:email” will be stor
## Migrating keys
-When a node is added or removed from a ring, RedisArray instances must be instanciated with a “previous” list of nodes. A single call to `$ra->_rehash()` causes all the keys to be redistributed according to the new list of nodes. Passing a callback function to `_rehash()` makes it possible to track the progress of that operation: the function is called with a node name and a number of keys that will be examined.
+When a node is added or removed from a ring, RedisArray instances must be instanciated with a “previous” list of nodes. A single call to `$ra->_rehash()` causes all the keys to be redistributed according to the new list of nodes. Passing a callback function to `_rehash()` makes it possible to track the progress of that operation: the function is called with a node name and a number of keys that will be examined, e.g. `_rehash(function ($host, $count){ ... });`.
It is possible to automate this process, by setting `'autorehash' => TRUE` in the constructor options. This will cause keys to be migrated when they need to be read from the previous array.
View
@@ -172,6 +172,7 @@ PHPAPI char *redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC)
{
char inbuf[1024];
char *resp = NULL;
+ size_t err_len;
if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
return NULL;
@@ -189,6 +190,8 @@ PHPAPI char *redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC)
switch(inbuf[0]) {
case '-':
+ err_len = strlen(inbuf+1) - 2;
+ redis_sock_set_err(redis_sock, inbuf+1, err_len);
/* stale data */
if(memcmp(inbuf + 1, "-ERR SYNC ", 10) == 0) {
zend_throw_exception(redis_exception_ce, "SYNC with master in progress", 0 TSRMLS_CC);
View
@@ -139,6 +139,7 @@ PHP_METHOD(Redis, migrate);
PHP_METHOD(Redis, time);
PHP_METHOD(Redis, getLastError);
+PHP_METHOD(Redis, clearLastError);
PHP_METHOD(Redis, _prefix);
PHP_METHOD(Redis, _unserialize);
View
27 redis.c
@@ -222,6 +222,7 @@ static zend_function_entry redis_functions[] = {
PHP_ME(Redis, migrate, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, getLastError, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(Redis, clearLastError, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, _prefix, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Redis, _unserialize, NULL, ZEND_ACC_PUBLIC)
@@ -6181,6 +6182,32 @@ PHP_METHOD(Redis, getLastError) {
}
}
+/*
+ * {{{ proto Redis::clearLastError()
+ */
+PHP_METHOD(Redis, clearLastError) {
+ zval *object;
+ RedisSock *redis_sock;
+
+ // Grab our object
+ if(zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, redis_ce) == FAILURE) {
+ RETURN_FALSE;
+ }
+ // Grab socket
+ if(redis_sock_get(object, &redis_sock TSRMLS_CC, 0) < 0) {
+ RETURN_FALSE;
+ }
+
+ // Clear error message
+ if(redis_sock->err) {
+ efree(redis_sock->err);
+ }
+ redis_sock->err = NULL;
+
+ RETURN_TRUE;
+}
+
+
/*
* {{{ proto Redis::time()
*/
View
@@ -499,19 +499,25 @@ PHP_METHOD(RedisArray, _distributor)
PHP_METHOD(RedisArray, _rehash)
{
- zval *object, *z_cb = NULL;
+ zval *object;
RedisArray *ra;
+ zend_fcall_info z_cb;
+ zend_fcall_info_cache z_cb_cache;
- if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|z",
- &object, redis_array_ce, &z_cb) == FAILURE) {
+ if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|f",
+ &object, redis_array_ce, &z_cb, &z_cb_cache) == FAILURE) {
RETURN_FALSE;
}
if (redis_array_get(object, &ra TSRMLS_CC) < 0) {
RETURN_FALSE;
}
- ra_rehash(ra, z_cb TSRMLS_CC);
+ if (ZEND_NUM_ARGS() == 0) {
+ ra_rehash(ra, NULL, NULL TSRMLS_CC);
+ } else {
+ ra_rehash(ra, &z_cb, &z_cb_cache TSRMLS_CC);
+ }
}
static void multihost_distribute(INTERNAL_FUNCTION_PARAMETERS, const char *method_name)
View
@@ -1105,24 +1105,37 @@ ra_move_key(const char *key, int key_len, zval *z_from, zval *z_to TSRMLS_DC) {
}
/* callback with the current progress, with hostname and count */
-static void zval_rehash_callback(zval *z_cb, const char *hostname, long count TSRMLS_DC) {
+static void zval_rehash_callback(zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache,
+ const char *hostname, long count TSRMLS_DC) {
- zval z_ret, *z_args[2];
+ zval *z_ret = NULL, **z_args[2];
+ zval *z_host, *z_count;
+
+ z_cb->retval_ptr_ptr = &z_ret;
+ z_cb->params = &z_args;
+ z_cb->param_count = 2;
+ z_cb->no_separation = 0;
/* run cb(hostname, count) */
- MAKE_STD_ZVAL(z_args[0]);
- ZVAL_STRING(z_args[0], hostname, 0);
- MAKE_STD_ZVAL(z_args[1]);
- ZVAL_LONG(z_args[1], count);
- call_user_function(EG(function_table), NULL, z_cb, &z_ret, 2, z_args TSRMLS_CC);
+ MAKE_STD_ZVAL(z_host);
+ ZVAL_STRING(z_host, hostname, 0);
+ z_args[0] = &z_host;
+ MAKE_STD_ZVAL(z_count);
+ ZVAL_LONG(z_count, count);
+ z_args[1] = &z_count;
+
+ zend_call_function(z_cb, z_cb_cache TSRMLS_CC);
/* cleanup */
- efree(z_args[0]);
- efree(z_args[1]);
+ efree(z_host);
+ efree(z_count);
+ if(z_ret)
+ efree(z_ret);
}
static void
-ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool b_index, zval *z_cb TSRMLS_DC) {
+ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool b_index,
+ zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache TSRMLS_DC) {
char **keys;
int *key_lens;
@@ -1138,8 +1151,8 @@ ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool
}
/* callback */
- if(z_cb) {
- zval_rehash_callback(z_cb, hostname, count TSRMLS_CC);
+ if(z_cb && z_cb_cache) {
+ zval_rehash_callback(z_cb, z_cb_cache, hostname, count TSRMLS_CC);
}
/* for each key, redistribute */
@@ -1163,7 +1176,7 @@ ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool
}
void
-ra_rehash(RedisArray *ra, zval *z_cb TSRMLS_DC) {
+ra_rehash(RedisArray *ra, zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache TSRMLS_DC) {
int i;
@@ -1172,7 +1185,7 @@ ra_rehash(RedisArray *ra, zval *z_cb TSRMLS_DC) {
return; /* TODO: compare the two rings for equality */
for(i = 0; i < ra->prev->count; ++i) {
- ra_rehash_server(ra, ra->prev->redis[i], ra->prev->hosts[i], ra->index, z_cb TSRMLS_CC);
+ ra_rehash_server(ra, ra->prev->redis[i], ra->prev->hosts[i], ra->index, z_cb, z_cb_cache TSRMLS_CC);
}
}
View
@@ -24,6 +24,6 @@ void ra_index_discard(zval *z_redis, zval *return_value TSRMLS_DC);
void ra_index_unwatch(zval *z_redis, zval *return_value TSRMLS_DC);
zend_bool ra_is_write_cmd(RedisArray *ra, const char *cmd, int cmd_len);
-void ra_rehash(RedisArray *ra, zval *z_cb TSRMLS_DC);
+void ra_rehash(RedisArray *ra, zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache TSRMLS_DC);
#endif
View
@@ -3023,8 +3023,20 @@ public function testGetLastError() {
$this->redis->eval("not-a-lua-script");
// Now we should have an error
- $this->assertTrue(strlen($this->redis->getLastError()) > 0);
- }
+ $evalError = $this->redis->getLastError();
+ $this->assertTrue(strlen($evalError) > 0);
+
+ // test getLastError with a regular command
+ $this->redis->set('x', 'a');
+ $this->assertFalse($this->redis->incr('x'));
+ $incrError = $this->redis->getLastError();
+ $this->assertTrue($incrError !== $evalError); // error has changed
+ $this->assertTrue(strlen($incrError) > 0);
+
+ // clear error
+ $this->redis->clearLastError();
+ $this->assertTrue($this->redis->getLastError() === NULL);
+ }
// Helper function to compare nested results -- from the php.net array_diff page, I believe
private function array_diff_recursive($aArray1, $aArray2) {
View
@@ -289,6 +289,14 @@ public function testRehash() {
$this->ra->_rehash(); // this will redistribute the keys
}
+ public function testRehashWithCallback() {
+ $total = 0;
+ $this->ra->_rehash(function ($host, $count) use (&$total) {
+ $total += $count;
+ });
+ $this->assertTrue($total > 0);
+ }
+
public function testReadRedistributedKeys() {
$this->readAllvalues(); // we shouldn't have any missed reads now.
}

0 comments on commit b4735e5

Please sign in to comment.