Skip to content

Instantly share code, notes, and snippets.

@mnunberg
Created February 19, 2015 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnunberg/590c48f76f23a0751ade to your computer and use it in GitHub Desktop.
Save mnunberg/590c48f76f23a0751ade to your computer and use it in GitHub Desktop.
lcb-views-beer-sample
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <libcouchbase/couchbase.h>
#include <libcouchbase/views.h>
#define CONNSTR "couchbase://localhost:12000/beer-sample"
static void rowCallback(lcb_t instance, int cbtype, const lcb_RESPVIEWQUERY *resp) {
if (! (resp->rflags & LCB_RESP_F_FINAL)) {
printf("Got key: %.*s\n", (int)resp->nkey, resp->key);
if (resp->docresp && resp->docresp->rc == LCB_SUCCESS) {
// Handle the actual document response
printf("Got document contents: %.*s\n",
(int)resp->docresp->nvalue, resp->docresp->value);
}
} else {
printf("Got metadata: %.*s\n", (int)resp->nvalue, resp->value);
}
}
int main(int argc, char **argv)
{
struct lcb_create_st crst = {
.version = 3,
.v.v3.connstr = CONNSTR
};
lcb_t instance;
lcb_create(&instance, &crst);
lcb_connect(instance);
lcb_wait(instance);
assert(lcb_get_bootstrap_status(instance) == LCB_SUCCESS);
// Initialize the queries:
lcb_CMDVIEWQUERY cmd = { 0 };
lcb_view_query_initcmd(&cmd, "beer", "brewery_beers", "limit=10", rowCallback);
// Enabling this flag gives us 'include docs' functionality:
cmd.cmdflags |= LCB_CMDVIEWQUERY_F_INCLUDE_DOCS;
lcb_error_t rc = lcb_view_query(instance, NULL, &cmd);
assert(rc == LCB_SUCCESS);
lcb_wait(instance);
lcb_destroy(instance);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment