Transcoder decoder not working

I am using couchbase server 6.6 and php sdk version 3.0.4. When i test setTranscoder, encoder worked but decoder didn’t work

 18 function encoder($value) {
 19   var_dump($value);
 20   return array($value, SPECIAL_BYTEARRAY, 0);
 21 }
 22 function decoder($bytes, $flags, $datatype) {
 23   var_dump($bytes);
 24   switch ($flags) {
 25     case SPECIAL_INT:
 26       return $bytes;
 27     case SPECIAL_BYTEARRAY;
 28       return $bytes;
 29 
 30   }
 31   return NULL;
 32 }
....
 36 $connectionString = "couchbase://localhost";
 37 $options = new \Couchbase\ClusterOptions();
 38 $options->credentials("Administrator", "112233");
 39 $cluster = new \Couchbase\Cluster($connectionString, $options);
 40 
 41 $bucket = $cluster->bucket("test");
 42 
 43 $bucket->setTranscoder('encoder', 'decoder');

Hello @gio can you please let us know what it would mean when you say didnt work ? Do you see an error ? or decoder is not decoding the data ? What data were you passing ?

It calls function encoder but it doesn’t run into function decoder

Interesting, I am looking at the GitHub code example and it matches to what you have not sure if it has to do with the name of the functions (most unlikely) .

@avsej anything you can think of ?

$bucket->setTranscoder('encoder', 'decoder');
$collection = $bucket->defaultCollection();
$getResult = $collection->get("a");

var_dump($getResult);

echo "result: " . $getResult->content();

It doesn’t invoke function decoder when i call function $getResult-> content()

with recent release 3.1.0 it works as expected:

class MyFlags {
    const SPECIAL_INT = 1;
    const SPECIAL_BYTEARRAY = 2;
}

function encoder($value) {
  if (gettype($value) == "integer") {
    printf("encode value as integer\n");
    return [pack("L", $value), MyFlags::SPECIAL_INT, 0];
  } else {
    printf("encode value as bytearray\n");
    return [base64_encode($value), MyFlags::SPECIAL_BYTEARRAY, 0];
  }
}
function decoder($bytes, $flags, $datatype) {
  if ($flags & MyFlags::SPECIAL_INT) {
      printf("decode value as integer\n");
      return unpack("L", $bytes);
  } else if ($flags & MyFlags::SPECIAL_BYTEARRAY) {
      printf("decode value as bytearray\n");
      return base64_decode($bytes);
  } else {
      printf("unexpected encoding\n");
      return $bytes;
  }
}

$options = new \Couchbase\ClusterOptions();
$options->credentials('Administrator', 'password');
$cluster = new \Couchbase\Cluster('couchbase://localhost/default', $options);
$bucket = $cluster->bucket("default");

$bucket->setTranscoder('encoder', 'decoder');

$collection = $bucket->defaultCollection();
$collection->upsert("foo", 42);
var_dump($collection->get("foo")->content());

Also it is possible to specify encoder/decoder in options to operations (see example below):

Ohh, thank you. I will test with this version

Does the issue still exist? Or You already fixed it and solution is above

@MirandaJO89 has been fixed with the latest release 3.1

I tried to install phpsdk 3.1.0 but got an error

/private/tmp/pear/temp/couchbase/src/couchbase/cluster.c:66:22: error: member reference base type 'char' is not a structure or union
            bucket = ZSTR_VAL(url->path);
                     ^~~~~~~~~~~~~~~~~~~
/usr/local/Cellar/php@7.2/7.2.34_1/include/php/Zend/zend_string.h:51:31: note: expanded from macro 'ZSTR_VAL'
#define ZSTR_VAL(zstr)  (zstr)->val
                        ~~~~~~^ ~~~
/private/tmp/pear/temp/couchbase/src/couchbase/cluster.c:193:74: warning: \U used with no following hex digits; treating as '\' followed by identifier [-Wunicode]
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(ai_Cluster_users, 0, 0, Couchbase\\UserManager, 0)
                                                                         ^
1 warning and 1 error generated.
make: *** [src/couchbase/cluster.lo] Error 1
ERROR: `make' failed

It works very well with php7.3 +
Thank you!

1 Like