Skip to content

Commit 6d19890

Browse files
committed
Merge branch 'master' of github.com:bpospichil/php-memcached
2 parents 5d5960c + f083d9d commit 6d19890

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

memcached-api.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class Memcached {
3434
const DISTRIBUTION_MODULA;
3535

3636
const DISTRIBUTION_CONSISTENT;
37+
38+
const DISTRIBUTION_VIRTUAL_BUCKET;
3739

3840
const LIBKETAMA_COMPATIBLE;
3941

@@ -203,9 +205,9 @@ public function fetchAll( ) {}
203205

204206
public function set( $key, $value, $expiration = 0, $udf_flags = 0 ) {}
205207

206-
public function touch( $key, $expiration = 0 ) {}
208+
public function touch( $key, $expiration = 0 ) {}
207209

208-
public function touchbyKey( $key, $expiration = 0 ) {}
210+
public function touchbyKey( $key, $expiration = 0 ) {}
209211

210212
public function setByKey( $server_key, $key, $value, $expiration = 0, $udf_flags = 0 ) {}
211213

@@ -251,6 +253,8 @@ public function setOption( $option, $value ) {}
251253

252254
public function setOptions( array $options ) {}
253255

256+
public function setBucket( array $host_map, array $forward_map, $buckets, $replicas ) {}
257+
254258
public function addServer( $host, $port, $weight = 0 ) {}
255259

256260
public function addServers( array $servers ) {}

php_memcached.c

+105
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,103 @@ static int php_memc_set_option(php_memc_t *i_obj, long option, zval *value TSRML
25222522
return 1;
25232523
}
25242524

2525+
/* {{{ Memcached::setBucket(array host_map, array forward_map, long buckets, long replicas)
2526+
Sets the memcached virtual buckets */
2527+
2528+
PHP_METHOD(Memcached, setBucket)
2529+
{
2530+
zval *host_map;
2531+
zval *forward_map;
2532+
long buckets;
2533+
long replicas;
2534+
2535+
uint32_t *hm = NULL,*fm = NULL;
2536+
zend_bool ok = 1;
2537+
uint key_len;
2538+
char *key;
2539+
ulong key_index;
2540+
zval **value;
2541+
MEMC_METHOD_INIT_VARS;
2542+
2543+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "aa!ll", &host_map, &forward_map, &buckets, &replicas) == FAILURE) {
2544+
return;
2545+
}
2546+
2547+
2548+
hm = (uint32_t*)malloc(buckets * sizeof(uint32_t));
2549+
2550+
int i = 0;
2551+
for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(host_map));
2552+
zend_hash_get_current_data(Z_ARRVAL_P(host_map), (void *) &value) == SUCCESS;
2553+
zend_hash_move_forward(Z_ARRVAL_P(host_map))) {
2554+
2555+
if (i < buckets) {
2556+
if (zend_hash_get_current_key_ex(Z_ARRVAL_P(host_map), &key, &key_len, &key_index, 0, NULL) == HASH_KEY_IS_LONG) {
2557+
zval copy = **value;
2558+
zval_copy_ctor(&copy);
2559+
INIT_PZVAL(&copy);
2560+
convert_to_long(&copy);
2561+
hm[i] = Z_LVAL_P(&copy);
2562+
++i;
2563+
zval_dtor(&copy);
2564+
} else {
2565+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid configuration option");
2566+
ok = 0;
2567+
}
2568+
} else {
2569+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "array size mismatch");
2570+
ok = 0;
2571+
break;
2572+
}
2573+
}
2574+
2575+
if (i != buckets) {
2576+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "array size mismatch");
2577+
ok = 0;
2578+
}
2579+
2580+
i = 0;
2581+
if (ok != 0 && forward_map != NULL) {
2582+
fm = (uint32_t*)malloc(buckets * sizeof(uint32_t));
2583+
for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(forward_map));
2584+
zend_hash_get_current_data(Z_ARRVAL_P(forward_map), (void *) &value) == SUCCESS;
2585+
zend_hash_move_forward(Z_ARRVAL_P(forward_map))) {
2586+
2587+
if (i < buckets) {
2588+
if (zend_hash_get_current_key_ex(Z_ARRVAL_P(forward_map), &key, &key_len, &key_index, 0, NULL) == HASH_KEY_IS_LONG) {
2589+
zval copy = **value;
2590+
zval_copy_ctor(&copy);
2591+
INIT_PZVAL(&copy);
2592+
2593+
convert_to_long(&copy);
2594+
fm[i] = Z_LVAL_P(&copy);
2595+
++i;
2596+
zval_dtor(&copy);
2597+
} else {
2598+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid configuration option");
2599+
ok = 0;
2600+
}
2601+
} else {
2602+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "array size mismatch");
2603+
ok = 0;
2604+
}
2605+
}
2606+
}
2607+
2608+
MEMC_METHOD_FETCH_OBJECT;
2609+
2610+
if (memcached_bucket_set(m_obj->memc, hm, fm, buckets, replicas) != MEMCACHED_SUCCESS)
2611+
{
2612+
ok = 0;
2613+
php_error_docref(NULL TSRMLS_CC, E_WARNING,"memcached_bucket_set don't returned MEMCACHED_SUCCESS");
2614+
}
2615+
free(hm);
2616+
free(fm);
2617+
RETURN_BOOL(ok);
2618+
2619+
}
2620+
/* }}} */
2621+
25252622
/* {{{ Memcached::setOptions(array options)
25262623
Sets the value for the given option constant */
25272624
static PHP_METHOD(Memcached, setOptions)
@@ -3772,6 +3869,13 @@ ZEND_BEGIN_ARG_INFO(arginfo_setOptions, 0)
37723869
ZEND_ARG_INFO(0, options)
37733870
ZEND_END_ARG_INFO()
37743871

3872+
ZEND_BEGIN_ARG_INFO(arginfo_setBucket, 0)
3873+
ZEND_ARG_INFO(0, host_map)
3874+
ZEND_ARG_INFO(0, forward_map)
3875+
ZEND_ARG_INFO(0, buckets)
3876+
ZEND_ARG_INFO(0, replicas)
3877+
ZEND_END_ARG_INFO()
3878+
37753879
ZEND_BEGIN_ARG_INFO(arginfo_getStats, 0)
37763880
ZEND_END_ARG_INFO()
37773881

@@ -3859,6 +3963,7 @@ static zend_function_entry memcached_class_methods[] = {
38593963
MEMC_ME(getOption, arginfo_getOption)
38603964
MEMC_ME(setOption, arginfo_setOption)
38613965
MEMC_ME(setOptions, arginfo_setOptions)
3966+
MEMC_ME(setBucket, arginfo_setBucket)
38623967
#ifdef HAVE_MEMCACHED_SASL
38633968
MEMC_ME(setSaslAuthData, arginfo_setSaslAuthData)
38643969
#endif

0 commit comments

Comments
 (0)