Using PHP API For Server 2.0 Dev Preview, attempting to set valid JSON results in doc with attachment instead
In an attempt to save the following json using the set method results in a doc being created with attachment erroneously:
$cb->set("test1", '{"test":[{"lang":"en", "test":"Hello World"},{"lang":"ger", "test":"Hello World"},{"lang":"ger", "test":"Hello World"}]}');
However, if you remove an element from any of the objects in the array, it works. For example:
$cb->set("test1", '{"test":[{"lang":"en", "test":"Hello World"},{"lang":"ger", "test":"Hello World"},{"lang":"ger"}]}');
I've noted that creating a document with any of the JSON in the couchbase admin UI appears to work just fine.
Anyone? Can anyone reproduce? Is it just me? Am I simply mad?
I have further investigated this, and the problem appears when setting a json string whose length is greater than 99 characters.
For instance, the following 99 character document works fine
{"my-custom-key":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
while the nearly identical 100 character document does not
{"my-custom-key":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
Here is some code to easily reproduce this issue.
<?php require_once(dirname(__FILE__)."/php-couchbase/Couchbase.php"); $cb = new Couchbase; $cb->addCouchbaseServer("localhost"); for ($i = 75; $i < 85; $i += 1) { $data = array("my-custom-key" => str_pad("", $i, "a")); $data = json_encode($data); $key = "length-".strlen($data); echo $key.": ".$data.PHP_EOL; $cb->set($key, $data); }
Which produces output like
[root@cs1017 public_html]# php ctest.php
PHP Warning: ...
length-95: {"my-custom-key":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
length-96: {"my-custom-key":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
length-97: {"my-custom-key":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
length-98: {"my-custom-key":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
length-99: {"my-custom-key":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
length-100: {"my-custom-key":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
length-101: {"my-custom-key":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
length-102: {"my-custom-key":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
length-103: {"my-custom-key":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
length-104: {"my-custom-key":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}Note: This code also makes it very easy to see what different doc lengths produce in the browser, as you can simply query "length-NUMBER" to see how the document of that length actually behaved
Great find. 99 character limit is not nearly enough. What's the word on this? Strange that the couchbase UI allows these values but the API does not.
After further exploration, I was unable to get the memcache api working with longer documents. I was, however, able to get the standard couchDB api working via http://host:8092/
As a result, I'm using a couchDB library to put data (in my application puts are quite rare compared to reads). It would be much more convenient to be able to put data correctly via the memcached api.
Yeah. It looks like using a standard couchdb API (http://www.topdog.za.net/php_couchdb_extension) looks like it might be a better choice.
Couchbase! We'd love a more mature PHP API! Help a brother out!
I had the same problem.
The set() function compresses json data.
You can disable compression.
$cb->setOption(Memcached::OPT_COMPRESSION, FALSE);
http://www.php.net/manual/en/memcached.constants.php
Memcached::OPT_COMPRESSION
Enables or disables payload compression. When enabled, item values longer than a certain threshold (currently 100 bytes) will be compressed during storage and decompressed during retrieval transparently.
Type: boolean, default: TRUE.
I can confirm this behavior. It seems to be related to the length of the JSON document. When I try to set very short documents it succeeds in setting the document, but on longer documents it fails.
I'm using the most recent version of the php-couchbase client from github.