Skip to content

Commit

Permalink
PCBC-493: Implement subdoc GET_COUNT
Browse files Browse the repository at this point in the history
Change-Id: I64b1434cfc620ffe17a2ed1b1080d1f76cb9de59
Reviewed-on: http://review.couchbase.org/83575
Reviewed-by: Mike Goldsmith <[email protected]>
Tested-by: Build Bot <[email protected]>
  • Loading branch information
avsej committed Sep 21, 2017
1 parent da280a8 commit c560fd3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api/couchbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2068,6 +2068,19 @@ final private function __construct() {}
*/
final public function get($path, $options = []) {}

/**
* Get a count of values inside the JSON document.
*
* This method is only available with Couchbase Server 5.0 and later.
*
* @param string $path the path inside the document where to get the count from.
* @param array $options the array with command modificators. Supported values are
* * "xattr" (default: false) if true, the path refers to a location
* within the document's extended attributes, not the document body.
* @return LookupInBuilder
*/
final public function getCount($path, $options = []) {}

/**
* Check if a value exists inside the document.
*
Expand Down
35 changes: 35 additions & 0 deletions src/couchbase/lookup_in_builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,40 @@ PHP_METHOD(LookupInBuilder, exists)
RETURN_ZVAL(getThis(), 1, 0);
} /* }}} */

/* {{{ proto \Couchbase\LookupInBuilder LookupInBuilder::getCount(string $path, array $options = []) */
PHP_METHOD(LookupInBuilder, getCount)
{
pcbc_lookup_in_builder_t *obj;
const char *path = NULL;
pcbc_str_arg_size path_len = 0;
int rv;
zval *options = NULL;
pcbc_sd_spec_t *spec;

obj = Z_LOOKUP_IN_BUILDER_OBJ_P(getThis());

rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z", &path, &path_len, &options);
if (rv == FAILURE) {
RETURN_NULL();
}

spec = ecalloc(1, sizeof(pcbc_sd_spec_t));
spec->next = NULL;
spec->s.sdcmd = LCB_SDCMD_GET_COUNT;
spec->s.options = pcbc_subdoc_options_to_flags(1, 1, options TSRMLS_CC);
PCBC_SDSPEC_COPY_PATH(spec, path, path_len);
if (obj->tail) {
obj->tail->next = spec;
}
obj->tail = spec;
if (obj->head == NULL) {
obj->head = obj->tail;
}
obj->nspecs++;

RETURN_ZVAL(getThis(), 1, 0);
} /* }}} */

/* {{{ proto \Couchbase\LookupInBuilder LookupInBuilder::execute() */
PHP_METHOD(LookupInBuilder, execute)
{
Expand Down Expand Up @@ -130,6 +164,7 @@ ZEND_END_ARG_INFO()
zend_function_entry lookup_in_builder_methods[] = {
PHP_ME(LookupInBuilder, __construct, ai_LookupInBuilder_none, ZEND_ACC_PRIVATE | ZEND_ACC_FINAL | ZEND_ACC_CTOR)
PHP_ME(LookupInBuilder, get, ai_LookupInBuilder_get, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
PHP_ME(LookupInBuilder, getCount, ai_LookupInBuilder_get, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
PHP_ME(LookupInBuilder, exists, ai_LookupInBuilder_get, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
PHP_ME(LookupInBuilder, execute, ai_LookupInBuilder_none, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
PHP_FE_END
Expand Down
24 changes: 24 additions & 0 deletions tests/BucketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,30 @@ function testLookupIn($b) {
$this->assertEquals(COUCHBASE_KEY_ENOENT, $result->error->getCode());
}

/**
* @depends testConnect
*/
function testGetCount($b) {
if (!getenv('CB_SPOCK')) {
$this->markTestSkipped("Subdoc GET_COUNT not available for {$this->serverVersion}");
return;
}

$key = $this->makeKey('get_count');
$b->upsert($key, ['list' => [1, 2, 42], 'object' => ['foo' => 42]]);

# Try existence
$result = $b->lookupIn($key)
->getCount('list')
->getCount('object')
->execute();
$this->assertEquals(2, count($result->value));
$this->assertEquals(COUCHBASE_SUCCESS, $result->value[0]['code']);
$this->assertEquals(3, $result->value[0]['value']);
$this->assertEquals(COUCHBASE_SUCCESS, $result->value[1]['code']);
$this->assertEquals(1, $result->value[1]['value']);
}

/**
* @expectedException \Couchbase\Exception
* @expectedExceptionMessageRegExp /EMPTY_PATH/
Expand Down

0 comments on commit c560fd3

Please sign in to comment.