1

I'm trying to implement a very basic C application that queries a Couchbase view using the C SDK. I got the SDK as such working since I can retrieve data from the DB server and even query views with a range query (startkey and endkey). However, if I create a view with a compound key, i.e. an index with two values, then I get nothing back. If I implement the same view in PHP or Node I get the correct data back. My understanding of the C SDK is that it uses the REST API of Couchbase, so the options string (optstr) is essentially the same as if I would use the Couchbase in-built web management console. Using single numeric start and end keys I get it all to work, but as soon as I start using compound keys like

startkey=["test",2]&endkey=["test",4]

I get no results back. The query string that does return data looks like

startkey=2&endkey=4

The bucket has only 5 documents of the following structure:

{number: 1, name: "test"}

with the number simply being between 1 and 5.

the view for the compound key looks like:

function (doc, meta) {
  emit([doc.name, doc.number], doc);
}

and the one for the single key simply has the doc.number as the first value in the emit without being an array.

The code that I'm using for the compound key in C looks like:

lcb_CMDVIEWQUERY vq = {0};
vq.optstr = "startkey=[\"test\",2]&endkey=[\"test\",4]";
vq.noptstr = strlen(vq.optstr);
lcb_view_query_initcmd(&vq, "ddoc", "myView", NULL, viewCallback);
lcb_error_t rc = lcb_view_query(instance, NULL, &vq);

What am I missing for the compound views? The examples on the Couchbase website don't cover views using any keys and the examples in their Git repository don't have any examples either. The Couchbase C SDK seems to be a but light on documentation, even the API documentation doesn't seem to be very detailed.

Any help is appreciated.

FuzzyAmi
  • 7,543
  • 6
  • 45
  • 79
airtruk
  • 321
  • 3
  • 11
  • the returned result is empty by the way compared to the web version and other SDKs, views are all created and published since I am using them though PHP successfully. I just need to integrate with an application written in C/C++ – airtruk Aug 11 '15 at 04:42
  • 1
    The reason there's no documentation on querying complex keys is because your code should just work. What version of the library are you using? – Mark Nunberg Aug 12 '15 at 13:45
  • I thought as well that the code should just work. I use libcouchbase2 v2.5.2-1 on CentOS 7. The Couchbase server itself is version 3.0.3-1716. The code returns an empty dataset, but the web interface returns the expected 2 documents. – airtruk Aug 13 '15 at 01:25
  • And no errors? what if you feed the string directly using HTTP? (i.e. via curl) – Mark Nunberg Aug 14 '15 at 16:35
  • I've tried building the string manually as well as using a string that works with curl with the same result of no rows being returned in the C version, but data is returned in the curl request. the issue only seems to be with compound keys though, using a single field as key works as expected in C and curl – airtruk Aug 15 '15 at 05:38
  • 1
    I've just tried to reproduce a similar use case, using the beer-sample bucket. It works fine without issue. Please be sure you're collecting any error information from the callback. From the description it sounds like the library might be escaping the parameters incorrectly (and possibly applying percent encoding in places where it shouldn't). Finally, if you're running the view immediately after creating the bucket or inserting the items, the view might not have had a chance to update - look into the `stale` query parameter – Mark Nunberg Aug 15 '15 at 17:14

1 Answers1

0

It would help if the view with the compound key would actually being published! The code actually does work as intended. However, it would be interesting to get peoples views on the formatting of the query/option string and if it should be URL encoded or not when created.

Update: After setting up a new Couchbase server and populating it with the same data set and rebuilding the view it actually does work. So I have no idea why it didn't work against the original server. I have gone through the map function with a fine toothed comb and checked the correct spelling (uppercase/lowercase) of all variables and parameters. At least it does work. :)

airtruk
  • 321
  • 3
  • 11