Shows how to work with XATTRs using subdocument API.
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
static void subdoc_callback(
lcb_INSTANCE *instance,
int type,
const lcb_RESPSUBDOC *resp)
{
size_t idx, total;
if (rc != LCB_SUCCESS) {
return;
}
total = lcb_respsubdoc_result_size(resp);
for (idx = 0; idx < total; idx++) {
const char *value;
size_t nvalue;
rc = lcb_respsubdoc_result_status(resp, idx);
lcb_respsubdoc_result_value(resp, idx, &value, &nvalue);
printf("[%lu]: 0x%x. %.*s\n", idx, rc, (int)nvalue, value);
}
}
{
const char *row;
size_t nrow;
lcb_respquery_row(resp, &row, &nrow);
if (rc != LCB_SUCCESS) {
uint16_t status;
lcb_respquery_http_response(resp, &http);
lcb_resphttp_http_status(http, &status);
printf("HTTP status: %d\n", (int)status);
{
const char *const *hdr;
for (; hdr && *hdr; hdr++) {
printf("%s", *hdr);
if (hdr + 1) {
printf(" = %s", *(++hdr));
}
printf("\n");
}
}
printf("%.*s\n", (int)nrow, row);
return;
}
char *start = "{\"docID\":\"";
char *stop = "\"";
char *key = strstr(row, start);
if (key == NULL || key != row) {
return;
}
key += strlen(start);
char *z = strstr(key, stop);
if (z == NULL) {
return;
}
*z = '\0';
{
char *path = "discounts.jsmith123";
lcb_SUBDOCSPECS *specs;
lcb_subdocspecs_create(&specs, 2);
lcb_CMDSUBDOC *cmd;
lcb_cmdsubdoc_create(&cmd);
lcb_cmdsubdoc_key(cmd, key, strlen(key));
lcb_cmdsubdoc_specs(cmd, specs);
rc = lcb_subdoc(instance, NULL, cmd);
lcb_subdocspecs_destroy(specs);
lcb_cmdsubdoc_destroy(cmd);
assert(rc == LCB_SUCCESS);
}
}
#define DEFAULT_CONNSTR "couchbase://localhost/travel-sample"
static lcb_INSTANCE *connect_as(
char *username,
char *password)
{
lcb_CREATEOPTS *crst = NULL;
lcb_createopts_connstr(crst, DEFAULT_CONNSTR, strlen(DEFAULT_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);
return instance;
}
int main()
{
instance = connect_as("Administrator", "password");
{
lcb_SUBDOCSPECS *specs;
lcb_subdocspecs_create(&specs, 4);
{
char *path = "discounts.jsmith123";
char *val = "20";
strlen(path), val, strlen(val));
}
{
char *path = "discounts.pjones356";
char *val = "30";
strlen(path), val, strlen(val));
}
{
char *path = "discounts.jbrown789";
char *val = "25";
strlen(path), val, strlen(val));
}
{
char *path = "discounts.jbrown789";
}
char *key = "hotel_10138";
lcb_CMDSUBDOC *cmd;
lcb_cmdsubdoc_create(&cmd);
lcb_cmdsubdoc_key(cmd, key, strlen(key));
lcb_cmdsubdoc_specs(cmd, specs);
rc = lcb_subdoc(instance, NULL, cmd);
lcb_subdocspecs_destroy(specs);
lcb_cmdsubdoc_destroy(cmd);
assert(rc == LCB_SUCCESS);
}
{
lcb_SUBDOCSPECS *specs;
lcb_subdocspecs_create(&specs, 2);
{
char *path = "discounts.jsmith123";
char *val = "15";
strlen(path), val, strlen(val));
}
{
char *path = "discounts.pjones356";
char *val = "10";
strlen(path), val, strlen(val));
}
char *key = "hotel_10142";
lcb_CMDSUBDOC *cmd;
lcb_cmdsubdoc_create(&cmd);
lcb_cmdsubdoc_key(cmd, key, strlen(key));
lcb_cmdsubdoc_specs(cmd, specs);
rc = lcb_subdoc(instance, NULL, cmd);
lcb_subdocspecs_destroy(specs);
lcb_cmdsubdoc_destroy(cmd);
assert(rc == LCB_SUCCESS);
}
{
lcb_CMDHTTP *cmd;
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]";
char *content_type = "application/x-www-form-urlencoded";
lcb_cmdhttp_method(cmd, LCB_HTTP_METHOD_PUT);
lcb_cmdhttp_path(cmd, path, strlen(path));
lcb_cmdhttp_body(cmd, payload, strlen(payload));
lcb_cmdhttp_content_type(cmd, content_type, strlen(content_type));
lcb_http(instance, NULL, cmd);
lcb_cmdhttp_destroy(cmd);
}
instance = connect_as("jsmith123", "jsmith123pwd");
{
char *query = "SELECT id, meta(`travel-sample`).id AS docID FROM `travel-sample`";
lcb_cmdquery_callback(cmd, n1qlrow_callback);
printf("User \"jsmith123\" has discounts in the hotels below:\n");
}
}