Skip to content
This repository was archived by the owner on Apr 20, 2022. It is now read-only.

Commit a27c938

Browse files
committed
Updated support for getReplica
The parsing of the arguments should probably be refactored into a helper function. Change-Id: I3e2dafeb7a6d10901d828eb9c8890c18796b815b Reviewed-on: http://review.couchbase.org/27364 Reviewed-by: Matt Ingenthron <matt@couchbase.com> Tested-by: Trond Norbye <trond.norbye@gmail.com>
1 parent 90de63d commit a27c938

17 files changed

+1001
-86
lines changed

apidecl.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ ZEND_ARG_ARRAY_INFO(1, cas, 1)
375375
ZEND_ARG_INFO(0, flags)
376376
ZEND_END_ARG_INFO()
377377

378+
ZEND_BEGIN_ARG_INFO_EX(arginfo_m_getreplica, 0, 0, 1)
379+
ZEND_ARG_INFO(0, ids)
380+
ZEND_ARG_INFO(0, strategy)
381+
ZEND_END_ARG_INFO()
382+
378383
ZEND_BEGIN_ARG_INFO_EX(arginfo_m_getdelayed, 0, 0, 1)
379384
ZEND_ARG_ARRAY_INFO(0, ids, 0)
380385
ZEND_ARG_INFO(0, with_cas)
@@ -597,8 +602,7 @@ static zend_function_entry couchbase_methods[] = {
597602
PHP_ME(couchbase, cas, arginfo_m_cas, ZEND_ACC_PUBLIC)
598603
PHP_ME(couchbase, get, arginfo_m_get, ZEND_ACC_PUBLIC)
599604
PHP_ME(couchbase, getMulti, arginfo_m_getmulti, ZEND_ACC_PUBLIC)
600-
PHP_ME(couchbase, getReplica, arginfo_m_get, ZEND_ACC_PUBLIC)
601-
PHP_ME(couchbase, getReplicaMulti, arginfo_m_getmulti, ZEND_ACC_PUBLIC)
605+
PHP_ME(couchbase, getReplica, arginfo_m_getreplica, ZEND_ACC_PUBLIC)
602606
PHP_ME(couchbase, getDelayed, arginfo_m_getdelayed, ZEND_ACC_PUBLIC)
603607
PHP_ME(couchbase, getAndLock, arginfo_m_get_and_lock, ZEND_ACC_PUBLIC)
604608
PHP_ME(couchbase, getAndLockMulti, arginfo_m_get_and_lock_multi, ZEND_ACC_PUBLIC)
@@ -655,12 +659,7 @@ PHP_METHOD(couchbase, getMulti)
655659

656660
PHP_METHOD(couchbase, getReplica)
657661
{
658-
php_couchbase_get_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1, 0, 0, 1);
659-
}
660-
661-
PHP_METHOD(couchbase, getReplicaMulti)
662-
{
663-
php_couchbase_get_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 1, 0, 0, 1);
662+
php_couchbase_get_replica_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU);
664663
}
665664

666665
PHP_METHOD(couchbase, getAndLock)
@@ -1290,7 +1289,11 @@ PHP_GINIT_FUNCTION(couchbase)
12901289
XX("COMPRESSION_FASTLZ", COUCHBASE_COMPRESSION_FASTLZ) \
12911290
XX("COMPRESSION_ZLIB", COUCHBASE_COMPRESSION_ZLIB) \
12921291
XX("GET_PRESERVE_ORDER", COUCHBASE_GET_PRESERVE_ORDER) \
1293-
XX("OPT_VOPTS_PASSTHROUGH", COUCHBASE_OPT_VOPTS_PASSTHROUGH)
1292+
XX("OPT_VOPTS_PASSTHROUGH", COUCHBASE_OPT_VOPTS_PASSTHROUGH) \
1293+
/* REPLICA */ \
1294+
XX("REPLICA_FIRST", LCB_REPLICA_FIRST) \
1295+
XX("REPLICA_ALL", LCB_REPLICA_ALL) \
1296+
XX("REPLICA_SELECT", LCB_REPLICA_SELECT)
12941297

12951298
PHP_MINIT_FUNCTION(couchbase)
12961299
{

apidecl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ PHP_COUCHBASE_LOCAL
4545
void php_couchbase_get_impl(INTERNAL_FUNCTION_PARAMETERS, int multi,
4646
int oo, int lock, int touch, int replica);
4747

48+
PHP_COUCHBASE_LOCAL
49+
void php_couchbase_get_replica_impl(INTERNAL_FUNCTION_PARAMETERS);
50+
4851
PHP_COUCHBASE_LOCAL
4952
void php_couchbase_get_delayed_impl(INTERNAL_FUNCTION_PARAMETERS, int oo);
5053

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ if test "$PHP_COUCHBASE" != "no"; then
4949
observe.c \
5050
options.c \
5151
remove.c \
52+
replica.c \
5253
resmgr.c \
5354
simple_observe.c \
5455
stat.c \

config.w32

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ if (PHP_COUCHBASE != "no") {
2323
ADD_SOURCES(configure_module_dirname, "observe.c", "couchbase");
2424
ADD_SOURCES(configure_module_dirname, "options.c", "couchbase");
2525
ADD_SOURCES(configure_module_dirname, "remove.c", "couchbase");
26+
ADD_SOURCES(configure_module_dirname, "replica.c", "couchbase");
2627
ADD_SOURCES(configure_module_dirname, "resmgr.c", "couchbase");
2728
ADD_SOURCES(configure_module_dirname, "stat.c", "couchbase");
2829
ADD_SOURCES(configure_module_dirname, "simple_observe.c", "couchbase");

example/couchbase-api.php

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
*/
8787
const COUCHBASE_UNKNOWN_HOST = 0x15; //LCB_UNKNOWN_HOST
8888

89-
9089
////////////////////////////////////////////////////////
9190
// //
9291
// The following option constants exist //
@@ -151,6 +150,34 @@
151150
const COUCHBASE_PRESERVE_ORDER = 1;
152151

153152

153+
////////////////////////////////////////////////////////
154+
// //
155+
// The following replica strategies exist //
156+
// //
157+
////////////////////////////////////////////////////////
158+
159+
/**
160+
* The caller will get a reply from the first replica to successfully
161+
* reply within the timeout for the operation or will receive an
162+
* error.
163+
*/
164+
const COUCHBASE_REPLICA_FIRST = LCB_REPLICA_FIRST;
165+
166+
/**
167+
* Ask all replicas to send documents/items back. The order of the
168+
* replicas does not imply anything and is just the order they are
169+
* received from the servers (one server may be more busy than
170+
* the other etc).
171+
*/
172+
const COUCHBASE_REPLICA_ALL = LCB_REPLICA_ALL;
173+
174+
/**
175+
* Select one replica by the index in the configuration starting from
176+
* zero. This approach can more quickly receive all possible replies
177+
* for a given topology, but it can also generate false negatives
178+
*/
179+
const COUCHBASE_REPLICA_SELECT = LCB_REPLICA_SELECT;
180+
154181
class Couchbase {
155182

156183
/**
@@ -349,21 +376,44 @@ function getMulti($ids, &$cas = array(), $flags = 0) {
349376
/**
350377
* Retrieve a replica of a document from the cluster.
351378
*
352-
* Please note that this method is currently experimental and its
353-
* signature may change in a future release.
379+
* Examples:
380+
* <code>
381+
* $obj = $cb->getReplica("key",
382+
* array("strategy" => COUCHBASE_REPLICA_SELECT,
383+
* "index" => 0));
354384
*
355-
* @param string $id identifies the object to retrieve
356-
* @param callable $callback a callback function to call for missing
357-
* objects. The function signature looks like:
358-
* <code>bool function($res, $id, &$val)</code>
359-
* if the function returns <code>true</code> the value
360-
* returned through $val is returned. Please note that
361-
* the cas field is not updated in these cases.
362-
* @param string $cas where to store the cas identifier of the object
385+
* returns
386+
* [ "foo" => [ "value" => "bar", "cas" => 0] ]
387+
*
388+
*
389+
* $obj = $cb->getReplica("foo", COUCHBASE_REPLICA_ALL);
390+
*
391+
* returns
392+
* [ "foo" => [ [ "value" => "bar", "cas" => 0],
393+
* [ "value" => "bar", "cas" => 0] ]
394+
* ]
395+
*
396+
*
397+
* $obj = $cb->getReplica(array("foo", "bar"), COUCHBASE_REPLICA_ALL);
398+
*
399+
* returns
400+
* [ "foo" => [ [ "value" => "bar", "cas" => 0],
401+
* [ "value" => "bar", "cas" => 0] ],
402+
* "bar" => [ [ "value" => "val", "cas" => 0],
403+
* [ "value" => "val", "cas" => 0] ]
404+
* ]
405+
*
406+
* </code>
407+
*
408+
* @param string $ids The document id(s) to get. Pass an array to
409+
* retrieve multiple documets
410+
* @param string $strategy Which strategy to use to pick the replica.
411+
* Pass in an array to specify extra options
412+
* to the strategy.
363413
* @return object The document from the cluster
364414
* @throws CouchbaseException if an error occurs
365415
*/
366-
function getReplica($id, $callback = NULL, &$cas = "") {
416+
function getReplica($id, $strategy = COUCHBASE_REPLICA_FIRST) {
367417

368418
}
369419

@@ -375,10 +425,12 @@ function getReplica($id, $callback = NULL, &$cas = "") {
375425
*
376426
* @param array $ids an array containing all of the document identifiers
377427
* @param array $cas an array to store the cas identifiers of the documents
428+
* @param string $strategy Which strategy to use to pick the replica
429+
* @param int $replicaidx Index (for COUCHBASE_REPLICA_SELECT)
378430
* @return array an array containing the documents
379431
* @throws CouchbaseException if an error occurs
380432
*/
381-
function getReplicaMulti($ids, &$cas = array()) {
433+
function getReplicaMulti($ids, &$cas = array(), $strategy, $replicaidx) {
382434

383435
}
384436

internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ PHP_METHOD(couchbase, cas);
194194
PHP_METHOD(couchbase, get);
195195
PHP_METHOD(couchbase, getMulti);
196196
PHP_METHOD(couchbase, getReplica);
197-
PHP_METHOD(couchbase, getReplicaMulti);
198197
PHP_METHOD(couchbase, getDelayed);
199198
PHP_METHOD(couchbase, getAndLock);
200199
PHP_METHOD(couchbase, getAndLockMulti);

0 commit comments

Comments
 (0)