Couchbase C Client
2.10.7
Asynchronous C Client for Couchbase
|
Shows how to work with XATTRs using subdocument API.
#include <unistd.h>
#include <stdlib.h>
#include <libcouchbase/n1ql.h>
#include <assert.h>
#include <string.h>
{
return;
}
switch (type) {
size_t iter = 0;
size_t index = 0;
return;
}
if (index == 1) {
printf(
" * %.*s: %.*s%%\n", (
int)resp->nkey, resp->key, (
int)ent.
nvalue, ent.
value);
}
index++;
}
break;
}
case LCB_CALLBACK_SDMUTATE: {
size_t iter = 0;
size_t oix = 0;
size_t index = oix++;
if (type == LCB_CALLBACK_SDMUTATE) {
}
}
break;
}
}
}
{
printf(
"Failure: 0x%x, %s\n", resp->rc,
lcb_strerror(instance, resp->rc));
{
for (; hdr && *hdr; hdr++) {
printf("%s", *hdr);
if (hdr + 1) {
printf(" = %s", *(++hdr));
}
printf("\n");
}
}
printf(
"%.*s\n", (
int)resp->
nrow, resp->
row);
return;
}
char *start = "{\"docID\":\"";
char *stop = "\"";
char *key = strstr(resp->
row, start);
if (key == NULL || key != resp->
row) {
return;
}
key += strlen(start);
char *z = strstr(key, stop);
if (z == NULL) {
return;
}
*z = '\0';
{
lcb_CMDSUBDOC cmd = {};
char *path = "discounts.jsmith123";
cmd.specs = specs;
cmd.nspecs = 2;
}
}
#define DEFAULT_CONNSTR "couchbase://localhost/travel-sample"
static lcb_t connect_as(
char *username,
char *password)
{
return instance;
}
int main()
{
instance = connect_as("Administrator", "password");
{
lcb_CMDSUBDOC cmd = {};
char *key = "hotel_10138";
{
char *path = "discounts.jsmith123";
char *val = "20";
}
{
char *path = "discounts.pjones356";
char *val = "30";
}
{
char *path = "discounts.jbrown789";
char *val = "25";
}
{
char *path = "discounts.jbrown789";
}
cmd.specs = specs;
cmd.nspecs = 4;
}
{
lcb_CMDSUBDOC cmd = {};
char *key = "hotel_10142";
{
char *path = "discounts.jsmith123";
char *val = "15";
}
{
char *path = "discounts.pjones356";
char *val = "10";
}
cmd.specs = specs;
cmd.nspecs = 2;
}
{
char *path = "/settings/rbac/users/local/jsmith123";
char *payload = "password=jsmith123pwd&name=John+Smith"
"&roles=data_reader[travel-sample],query_select[travel-sample],data_writer[travel-sample]";
cmd.
method = LCB_HTTP_METHOD_PUT;
cmd.
nbody = strlen(payload);
}
instance = connect_as("jsmith123", "jsmith123pwd");
{
char *query = "SELECT id, meta(`travel-sample`).id AS docID FROM `travel-sample`";
printf("User \"jsmith123\" has discounts in the hotels below:\n");
}
}
@ LCB_CALLBACK_DEFAULT
Default callback invoked as a fallback.
Definition: couchbase.h:696
lcb_U8 index
Request index which this result pertains to.
Definition: subdoc.h:318
#define LCB_SDSPEC_F_MKINTERMEDIATES
Create intermediate paths.
Definition: subdoc.h:189
lcb_error_t lcb_wait(lcb_t instance)
Wait for the execution of all batched requests.
const char * lcb_strerror(lcb_t instance, lcb_error_t error)
Get a textual descrtiption for the given error code.
const char * row
Current result row.
Definition: n1ql.h:438
lcb_error_t lcb_http3(lcb_t instance, const void *cookie, const lcb_CMDHTTP *cmd)
@ LCB_SDCMD_DICT_UPSERT
Unconditionally set the value at the path.
Definition: subdoc.h:73
const char * passwd
Password for bucket.
Definition: couchbase.h:293
lcb_U32 options
Set of option flags for the command.
Definition: subdoc.h:171
const lcb_RESPHTTP * htresp
Raw HTTP response, if applicable.
Definition: n1ql.h:442
lcb_error_t lcb_get_bootstrap_status(lcb_t instance)
Gets the initial bootstrap status.
const char *const * headers
List of key-value headers.
Definition: couchbase.h:2795
short htstatus
HTTP status code.
Definition: couchbase.h:2791
const char * connstr
Connection string.
Definition: couchbase.h:282
lcb_http_type_t type
Type of request to issue.
Definition: couchbase.h:2748
int lcb_sdresult_next(const lcb_RESPSUBDOC *resp, lcb_SDENTRY *out, size_t *iter)
Iterate over the results for a subdocument response.
lcb_error_t lcb_connect(lcb_t instance)
Schedule the initial connection This function will schedule the initial connection for the handle.
const char * username
Username to use for authentication.
Definition: couchbase.h:288
Response for a N1QL query.
Definition: n1ql.h:426
lcb_error_t lcb_n1ql_query(lcb_t instance, const void *cookie, const lcb_CMDN1QL *cmd)
lcb_U32 sdcmd
The command code, lcb_SUBDOCOP.
Definition: subdoc.h:165
@ LCB_CALLBACK_SDLOOKUP
<for lcb_storedur3()
Definition: couchbase.h:714
lcb_N1QLPARAMS * lcb_n1p_new(void)
Create a new N1QL Parameters object.
lcb_error_t
Error codes returned by the library.
Definition: error.h:476
@ LCB_SDCMD_EXISTS
Check if the value for a path exists.
Definition: subdoc.h:51
void lcb_sched_enter(lcb_t instance)
Enter a scheduling context.
struct lcb_N1QLPARAMS_st lcb_N1QLPARAMS
Opaque object representing N1QL parameters.
Definition: n1ql.h:109
size_t nvalue
Length of the value.
Definition: subdoc.h:309
@ LCB_HTTP_TYPE_MANAGEMENT
Execute a management API request.
Definition: couchbase.h:2674
lcb_error_t rc
Status code.
Definition: couchbase.h:626
void lcb_sched_leave(lcb_t instance)
Leave the current scheduling context, scheduling the commands within the context to be flushed to the...
#define LCB_SDSPEC_SET_PATH(s, p, n)
Set the path for an lcb_SDSPEC structure.
Definition: subdoc.h:206
lcb_http_method_t method
HTTP Method to use.
Definition: couchbase.h:2749
struct lcb_create_st3 v3
Use this field.
Definition: couchbase.h:338
@ LCB_SUBDOC_MULTI_FAILURE
Could not execute one or more multi lookups or mutations.
Definition: error.h:478
lcb_SIZE nbody
Length of the body for the request.
Definition: couchbase.h:2756
Wrapper structure for lcb_create()
Definition: couchbase.h:328
lcb_error_t lcb_n1p_mkcmd(lcb_N1QLPARAMS *params, lcb_CMDN1QL *cmd)
Populates the given low-level lcb_CMDN1QL structure with the relevant fields from the params structur...
#define LCB_CMD_SET_KEY(cmd, keybuf, keylen)
Set the key for the command.
Definition: couchbase.h:556
struct lcb_st * lcb_t
Definition: couchbase.h:41
lcb_error_t lcb_subdoc3(lcb_t instance, const void *cookie, const lcb_CMDSUBDOC *cmd)
Perform one or more subdocument operations.
const void * value
Value for the mutation (only applicable for LCB_SDCMD_COUNTER, currently)
Definition: subdoc.h:307
lcb_error_t status
Status code.
Definition: subdoc.h:311
lcb_error_t lcb_create(lcb_t *instance, const struct lcb_create_st *options)
Create an instance of lcb.
@ LCB_SUCCESS
Success.
Definition: error.h:478
size_t nrow
Length of the row.
Definition: n1ql.h:440
Base response structure for callbacks.
Definition: couchbase.h:625
void lcb_destroy(lcb_t instance)
Destroy (and release all allocated resources) an instance of lcb.
lcb_N1QLCALLBACK callback
Callback to be invoked for each row.
Definition: n1ql.h:414
@ LCB_SDCMD_GET
Retrieve the value for a path.
Definition: subdoc.h:45
Structure for a single sub-document mutation or lookup result.
Definition: subdoc.h:305
Structure for performing an HTTP request.
Definition: couchbase.h:2742
Command structure for N1QL queries.
Definition: n1ql.h:385
Response structure for multi lookups.
Definition: subdoc.h:294
#define LCB_CMD_SET_VALUE(scmd, valbuf, vallen)
Set the value buffer for the command. This may be used when the new value is a single contiguous buff...
Definition: couchbase.h:1139
int version
Indicates which field in the lcb_CRST_u union should be used.
Definition: couchbase.h:330
const char * body
If the request requires a body (e.g.
Definition: couchbase.h:2753
#define lcb_n1p_setstmtz(params, qstr)
Shortcut to set NUL-terminated string as statement via lcb_n1p_setquery.
Definition: n1ql.h:159
@ LCB_SDCMD_REMOVE
Remove an existing path in the document.
Definition: subdoc.h:130
#define LCB_SDSPEC_F_XATTRPATH
Access document XATTR path.
Definition: subdoc.h:192
lcb_RESPCALLBACK lcb_install_callback3(lcb_t instance, int cbtype, lcb_RESPCALLBACK cb)
const char * content_type
For views, set this to application/json
Definition: couchbase.h:2763
@ LCB_SDCMD_DICT_ADD
Add the value at the given path, if the given path does not exist.
Definition: subdoc.h:66
Subdoc command specification.
Definition: subdoc.h:160