Created
February 19, 2015 21:45
-
-
Save mnunberg/590c48f76f23a0751ade to your computer and use it in GitHub Desktop.
lcb-views-beer-sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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