Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add setBucket function #19

Merged
merged 3 commits into from

2 participants

@bpospichil

Hi,

I added the support to libmemcached vbuckets function, making the match to memcached_bucket_set function:

LIBMEMCACHED_API
memcached_return_t memcached_bucket_set(memcached_st *self,
const uint32_t *host_map,
const uint32_t *forward_map,
const uint32_t buckets,
const uint32_t replicas);

'host_map' needs to have 'buckets' elements;
'forward_map' needs to have 'buckets' elements or NULL;

I made tests writing keys using libmemcached and reading using php-memcached and vice-versa.

@mkoppanen mkoppanen commented on the diff
php_memcached.c
((46 lines not shown))
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "array size mismatch");
+ ok = 0;
+ break;
+ }
+ }
+
+ if (i != buckets) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "array size mismatch");
+ ok = 0;
+ }
+
+ i = 0;
+ if (ok != 0 && forward_map != NULL) {
+ fm = (uint32_t*)malloc(buckets * sizeof(uint32_t));
@mkoppanen Owner

This should probably be emalloc and efree later. same with hm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@mkoppanen
Owner

DISTRIBUTION_VIRTUAL_BUCKET constants seems to be missing. Can you add that and a test case and i'll merge.

@mkoppanen mkoppanen was assigned
@mkoppanen mkoppanen merged commit f083d9d into from
@bpospichil

Hi Mikko, weirdly I did not received any message about your comment in my pull request. I just saw it because I received an e-mail notifying the merge. Sorry for not keeping on touch and thanks for the follow up of my fork.

@mkoppanen
Owner

No worries! The most important thing is that it's in now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 23, 2012
Commits on May 4, 2012
  1. Threating return of memcached_bucket_set.

    Bruno Pospichil authored
Commits on May 21, 2012
  1. Merge branch 'master' of git://github.com/php-memcached-dev/php-memca…

    Bruno Pospichil authored
    …ched
    
    Conflicts:
    	php_memcached.c
This page is out of date. Refresh to see the latest.
Showing with 111 additions and 2 deletions.
  1. +6 −2 memcached-api.php
  2. +105 −0 php_memcached.c
View
8 memcached-api.php
@@ -34,6 +34,8 @@ class Memcached {
const DISTRIBUTION_MODULA;
const DISTRIBUTION_CONSISTENT;
+
+ const DISTRIBUTION_VIRTUAL_BUCKET;
const LIBKETAMA_COMPATIBLE;
@@ -201,9 +203,9 @@ public function fetchAll( ) {}
public function set( $key, $value, $expiration = 0 ) {}
- public function touch( $key, $expiration = 0 ) {}
+ public function touch( $key, $expiration = 0 ) {}
- public function touchbyKey( $key, $expiration = 0 ) {}
+ public function touchbyKey( $key, $expiration = 0 ) {}
public function setByKey( $server_key, $key, $value, $expiration = 0 ) {}
@@ -249,6 +251,8 @@ public function setOption( $option, $value ) {}
public function setOptions( array $options ) {}
+ public function setBucket( array $host_map, array $forward_map, $buckets, $replicas ) {}
+
public function addServer( $host, $port, $weight = 0 ) {}
public function addServers( array $servers ) {}
View
105 php_memcached.c
@@ -2333,6 +2333,103 @@ static int php_memc_set_option(php_memc_t *i_obj, long option, zval *value TSRML
return 1;
}
+/* {{{ Memcached::setBucket(array host_map, array forward_map, long buckets, long replicas)
+ Sets the memcached virtual buckets */
+
+PHP_METHOD(Memcached, setBucket)
+{
+ zval *host_map;
+ zval *forward_map;
+ long buckets;
+ long replicas;
+
+ uint32_t *hm = NULL,*fm = NULL;
+ zend_bool ok = 1;
+ uint key_len;
+ char *key;
+ ulong key_index;
+ zval **value;
+ MEMC_METHOD_INIT_VARS;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "aa!ll", &host_map, &forward_map, &buckets, &replicas) == FAILURE) {
+ return;
+ }
+
+
+ hm = (uint32_t*)malloc(buckets * sizeof(uint32_t));
+
+ int i = 0;
+ for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(host_map));
+ zend_hash_get_current_data(Z_ARRVAL_P(host_map), (void *) &value) == SUCCESS;
+ zend_hash_move_forward(Z_ARRVAL_P(host_map))) {
+
+ if (i < buckets) {
+ if (zend_hash_get_current_key_ex(Z_ARRVAL_P(host_map), &key, &key_len, &key_index, 0, NULL) == HASH_KEY_IS_LONG) {
+ zval copy = **value;
+ zval_copy_ctor(&copy);
+ INIT_PZVAL(&copy);
+ convert_to_long(&copy);
+ hm[i] = Z_LVAL_P(&copy);
+ ++i;
+ zval_dtor(&copy);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid configuration option");
+ ok = 0;
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "array size mismatch");
+ ok = 0;
+ break;
+ }
+ }
+
+ if (i != buckets) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "array size mismatch");
+ ok = 0;
+ }
+
+ i = 0;
+ if (ok != 0 && forward_map != NULL) {
+ fm = (uint32_t*)malloc(buckets * sizeof(uint32_t));
@mkoppanen Owner

This should probably be emalloc and efree later. same with hm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(forward_map));
+ zend_hash_get_current_data(Z_ARRVAL_P(forward_map), (void *) &value) == SUCCESS;
+ zend_hash_move_forward(Z_ARRVAL_P(forward_map))) {
+
+ if (i < buckets) {
+ if (zend_hash_get_current_key_ex(Z_ARRVAL_P(forward_map), &key, &key_len, &key_index, 0, NULL) == HASH_KEY_IS_LONG) {
+ zval copy = **value;
+ zval_copy_ctor(&copy);
+ INIT_PZVAL(&copy);
+
+ convert_to_long(&copy);
+ fm[i] = Z_LVAL_P(&copy);
+ ++i;
+ zval_dtor(&copy);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid configuration option");
+ ok = 0;
+ }
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "array size mismatch");
+ ok = 0;
+ }
+ }
+ }
+
+ MEMC_METHOD_FETCH_OBJECT;
+
+ if (memcached_bucket_set(m_obj->memc, hm, fm, buckets, replicas) != MEMCACHED_SUCCESS)
+ {
+ ok = 0;
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,"memcached_bucket_set don't returned MEMCACHED_SUCCESS");
+ }
+ free(hm);
+ free(fm);
+ RETURN_BOOL(ok);
+
+}
+/* }}} */
+
/* {{{ Memcached::setOptions(array options)
Sets the value for the given option constant */
static PHP_METHOD(Memcached, setOptions)
@@ -3482,6 +3579,13 @@ ZEND_BEGIN_ARG_INFO(arginfo_setOptions, 0)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()
+ZEND_BEGIN_ARG_INFO(arginfo_setBucket, 0)
+ ZEND_ARG_INFO(0, host_map)
+ ZEND_ARG_INFO(0, forward_map)
+ ZEND_ARG_INFO(0, buckets)
+ ZEND_ARG_INFO(0, replicas)
+ZEND_END_ARG_INFO()
+
ZEND_BEGIN_ARG_INFO(arginfo_getStats, 0)
ZEND_END_ARG_INFO()
@@ -3560,6 +3664,7 @@ static zend_function_entry memcached_class_methods[] = {
MEMC_ME(getOption, arginfo_getOption)
MEMC_ME(setOption, arginfo_setOption)
MEMC_ME(setOptions, arginfo_setOptions)
+ MEMC_ME(setBucket, arginfo_setBucket)
#ifdef HAVE_MEMCACHED_SASL
MEMC_ME(setSaslAuthData, arginfo_setSaslAuthData)
#endif
Something went wrong with that request. Please try again.