Couchbase
  • Why NoSQL?
  • Couchbase Server
  • Download
  • Resources
  • Careers
Home | Forums | Couchbase | Couchbase Server 2.0

Using PHP API For Server 2.0 Dev Preview, attempting to set valid JSON results in doc with attachment instead

7 replies [Last post]
  • Login or register to post comments
Wed, 02/01/2012 - 21:44
tdondich
Offline
Joined: 02/01/2012
Groups: None

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?

Top
  • Login or register to post comments
Thu, 02/02/2012 - 18:21
cjo
Offline
Joined: 02/02/2012
Groups: None

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.

Top
  • Login or register to post comments
Thu, 02/02/2012 - 18:37
cjo
Offline
Joined: 02/02/2012
Groups: None

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"}

Top
  • Login or register to post comments
Thu, 02/02/2012 - 18:42
cjo
Offline
Joined: 02/02/2012
Groups: None

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

Top
  • Login or register to post comments
Thu, 02/02/2012 - 18:44
tdondich
Offline
Joined: 02/01/2012
Groups: None

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.

Top
  • Login or register to post comments
Thu, 02/02/2012 - 19:43
cjo
Offline
Joined: 02/02/2012
Groups: None

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.

Top
  • Login or register to post comments
Thu, 02/02/2012 - 20:02
tdondich
Offline
Joined: 02/01/2012
Groups: None

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!

Top
  • Login or register to post comments
Mon, 02/06/2012 - 20:16
jaeyoi
Offline
Joined: 02/06/2012
Groups: None

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.

Top
  • Login or register to post comments
  • Login or register to post comments
  • Login
  • Register

Company

  • About Us
  • Leadership
  • Customers
  • Partners
  • Contact Us

Product

  • Couchbase Server
  • Couchbase SDKs
  • Use Cases
  • Documentation
  • Forums

Open Source

  • Couchbase Project
  • Couchbase vs. CouchDB

Commercial

  • Subscriptions & Support
  • Training & Services

News

  • Blog
  • Newsletter
  • Press Releases
  • Buzz

Follow Us

    
  • Customer Login
  • Terms of Service
  • Privacy Policy
  • Trademark Policy
  • Site Map

© 2013 COUCHBASE All rights reserved.

Sign in to Couchbase Community

close
  • Create new account
  • Request new password
You are logging into the Forums, Wiki and Issue Tracker