Shows how to use subdocument API.
Shows how to use subdocument API.
#undef NDEBUG
#include <assert.h>
#include <string.h>
{
fprintf(stderr,
"Got callback for %s.. ",
lcb_strcbtype(cbtype));
if (rc != LCB_SUCCESS) {
return;
}
const char *value;
size_t nvalue;
lcb_respget_value(resp, &value, &nvalue);
fprintf(stderr, "Value %.*s\n", (int)nvalue, value);
}
{
fprintf(stderr,
"Got callback for %s.. ",
lcb_strcbtype(cbtype));
if (rc != LCB_SUCCESS) {
return;
}
fprintf(stderr, "OK\n");
}
static void subdoc_callback(
lcb_INSTANCE *,
int cbtype,
const lcb_RESPSUBDOC *resp)
{
fprintf(stderr,
"Got callback for %s.. ",
lcb_strcbtype(cbtype));
if (rc != LCB_SUCCESS) {
return;
}
if (lcb_respsubdoc_result_size(resp) > 0) {
const char *value;
size_t nvalue;
lcb_respsubdoc_result_value(resp, 0, &value, &nvalue);
rc = lcb_respsubdoc_result_status(resp, 0);
} else {
fprintf(stderr, "No result!\n");
}
}
static void demoKey(
lcb_INSTANCE *instance,
const char *key)
{
printf("Retrieving '%s'\n", key);
printf("====\n");
lcb_CMDGET *gcmd;
lcb_cmdget_create(&gcmd);
lcb_cmdget_key(gcmd, key, strlen(key));
assert(rc == LCB_SUCCESS);
lcb_cmdget_destroy(gcmd);
printf("====\n\n");
}
#define DEFAULT_CONNSTR "couchbase://localhost"
int main(int argc, char **argv)
{
lcb_CREATEOPTS *crst = NULL;
const char *connstr, *username, *password;
if (argc > 1) {
connstr = argv[1];
} else {
connstr = DEFAULT_CONNSTR;
}
if (argc > 2) {
username = argv[2];
} else {
username = "Administrator";
}
if (argc > 3) {
password = argv[3];
} else {
password = "password";
}
lcb_createopts_connstr(crst, connstr, strlen(connstr));
lcb_createopts_credentials(crst, username, strlen(username), password, strlen(password));
lcb_createopts_destroy(crst);
assert(rc == LCB_SUCCESS);
assert(rc == LCB_SUCCESS);
assert(rc == LCB_SUCCESS);
printf("Storing the initial item..\n");
lcb_CMDSTORE *scmd;
lcb_cmdstore_key(scmd, "key", 3);
const char *initval = "{\"hello\":\"world\"}";
lcb_cmdstore_value(scmd, initval, strlen(initval));
rc = lcb_store(instance, NULL, scmd);
lcb_cmdstore_destroy(scmd);
assert(rc == LCB_SUCCESS);
lcb_CMDSUBDOC *cmd;
lcb_SUBDOCSPECS *ops;
lcb_cmdsubdoc_create(&cmd);
lcb_cmdsubdoc_key(cmd, "key", 3);
printf("Getting the 'hello' path from the document\n");
lcb_subdocspecs_create(&ops, 1);
lcb_subdocspecs_get(ops, 0, 0, "hello", 5);
lcb_cmdsubdoc_specs(cmd, ops);
rc = lcb_subdoc(instance, NULL, cmd);
lcb_subdocspecs_destroy(ops);
assert(rc == LCB_SUCCESS);
printf("Adding new 'goodbye' path to document\n");
lcb_subdocspecs_create(&ops, 1);
lcb_subdocspecs_dict_upsert(ops, 0, 0, "goodbye", 7, "\"hello\"", 7);
lcb_cmdsubdoc_specs(cmd, ops);
rc = lcb_subdoc(instance, NULL, cmd);
lcb_subdocspecs_destroy(ops);
assert(rc == LCB_SUCCESS);
demoKey(instance, "key");
printf("Appending element to array (array might be missing)\n");
lcb_subdocspecs_create(&ops, 1);
lcb_cmdsubdoc_specs(cmd, ops);
rc = lcb_subdoc(instance, NULL, cmd);
lcb_subdocspecs_destroy(ops);
assert(rc == LCB_SUCCESS);
demoKey(instance, "key");
printf("Prepending element to array (array must exist)\n");
lcb_subdocspecs_create(&ops, 1);
lcb_subdocspecs_array_add_first(ops, 0, 0, "array", 5, "1", 1);
lcb_cmdsubdoc_specs(cmd, ops);
rc = lcb_subdoc(instance, NULL, cmd);
lcb_subdocspecs_destroy(ops);
assert(rc == LCB_SUCCESS);
demoKey(instance, "key");
printf("Getting first array element...\n");
lcb_subdocspecs_create(&ops, 1);
lcb_subdocspecs_get(ops, 0, 0, "array[0]", 8);
lcb_cmdsubdoc_specs(cmd, ops);
rc = lcb_subdoc(instance, NULL, cmd);
lcb_subdocspecs_destroy(ops);
assert(rc == LCB_SUCCESS);
return 0;
}
Main header file for Couchbase.
#define LCB_SUBDOCSPECS_F_MKINTERMEDIATES
Create intermediate paths.
Definition couchbase.h:3274
void lcb_destroy(lcb_INSTANCE *instance)
Destroy (and release all allocated resources) an instance of lcb.
LCB_INTERNAL_API const char * lcb_strerror_short(lcb_STATUS error)
Get a shorter textual description of an error message.
lcb_STATUS
Error codes returned by the library.
Definition error.h:213
struct lcb_RESPGET_ lcb_RESPGET
Command for retrieving a single item.
Definition couchbase.h:687
lcb_STATUS lcb_get_bootstrap_status(lcb_INSTANCE *instance)
Gets the initial bootstrap status.
lcb_STATUS lcb_create(lcb_INSTANCE **instance, const lcb_CREATEOPTS *options)
Create an instance of lcb.
struct lcb_st lcb_INSTANCE
Library handle representing a connection to a cluster and its data buckets.
Definition couchbase.h:35
lcb_STATUS lcb_connect(lcb_INSTANCE *instance)
Schedule the initial connection This function will schedule the initial connection for the handle.
@ LCB_TYPE_BUCKET
Handle for data access (default)
Definition couchbase.h:256
lcb_RESPCALLBACK lcb_install_callback(lcb_INSTANCE *instance, int cbtype, lcb_RESPCALLBACK cb)
void(* lcb_RESPCALLBACK)(lcb_INSTANCE *instance, int cbtype, const lcb_RESPBASE *resp)
Callback invoked for responses.
Definition couchbase.h:554
const char * lcb_strcbtype(int cbtype)
Returns the type of the callback as a string.
@ LCB_CALLBACK_GET
lcb_get()
Definition couchbase.h:471
@ LCB_CALLBACK_STORE
lcb_store()
Definition couchbase.h:472
@ LCB_CALLBACK_SDLOOKUP
<for lcb_storedur3()
Definition couchbase.h:487
struct lcb_RESPSTORE_ lcb_RESPSTORE
Schedule a single storage request.
Definition couchbase.h:978
@ LCB_STORE_UPSERT
The default storage mode.
Definition couchbase.h:894
lcb_STATUS lcb_wait(lcb_INSTANCE *instance, lcb_WAITFLAGS flags)
Wait for completion of scheduled operations.
@ LCB_WAIT_DEFAULT
Behave like the old lcb_wait()
Definition couchbase.h:1861