Couchbase C Client  3.3.10
Asynchronous C Client for Couchbase
example/minimal/query.c

Shows N1QL query API. Also because queries executed in a loop, the sample might be used as simple benchmark (more sofisticated shipped with cbc tools, as cbc-n1qlback)

/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2018-2020 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <libcouchbase/ixmgmt.h>
static void check(lcb_STATUS err, const char *msg)
{
if (err != LCB_SUCCESS) {
fprintf(stderr, "[\x1b[31mERROR\x1b[0m] %s: %s\n", msg, lcb_strerror_short(err));
exit(EXIT_FAILURE);
}
}
static int err2color(lcb_STATUS err)
{
switch (err) {
case LCB_SUCCESS:
return 32;
case LCB_ERR_DOCUMENT_EXISTS:
return 33;
default:
return 31;
}
}
static void ln2space(const void *buf, size_t nbuf)
{
size_t ii;
char *str = (char *)buf;
for (ii = 0; ii < nbuf; ii++) {
if (str[ii] == '\n') {
str[ii] = ' ';
}
}
}
static void row_callback(lcb_INSTANCE *instance, int type, const lcb_RESPQUERY *resp)
{
const char *row;
size_t nrow;
lcb_STATUS rc = lcb_respquery_status(resp);
lcb_respquery_row(resp, &row, &nrow);
ln2space(row, nrow);
fprintf(stderr, "[\x1b[%dmQUERY\x1b[0m] %s, (size=%d) %.*s\n", err2color(rc), lcb_strerror_short(rc), (int)nrow,
(int)nrow, row);
if (lcb_respquery_is_final(resp)) {
fprintf(stderr, "\n");
}
}
static void idx_callback(lcb_INSTANCE *instance, int type, const lcb_RESPN1XMGMT *resp)
{
const lcb_RESPQUERY *inner = resp->inner;
const char *row;
size_t nrow;
lcb_respquery_row(inner, &row, &nrow);
ln2space(row, nrow);
fprintf(stderr, "[\x1b[%dmINDEX\x1b[0m] %s, (%d) %.*s\n", err2color(resp->rc), lcb_strerror_short(resp->rc),
(int)nrow, (int)nrow, row);
}
static void store_callback(lcb_INSTANCE *instance, int type, const lcb_RESPSTORE *resp)
{
lcb_STATUS rc = lcb_respstore_status(resp);
const char *key;
size_t nkey;
lcb_respstore_key(resp, &key, &nkey);
fprintf(stderr, "[\x1b[%dm%-5s\x1b[0m] %s, key=%.*s\n", err2color(rc), lcb_strcbtype(type), lcb_strerror_short(rc),
(int)nkey, key);
}
static void get_callback(lcb_INSTANCE *instance, int type, const lcb_RESPGET *resp)
{
const char *key;
size_t nkey;
rc = lcb_respget_status(resp);
lcb_respget_key(resp, &key, &nkey);
fprintf(stderr, "[\x1b[%dm%-5s\x1b[0m] %s, key=%.*s\n", err2color(rc), lcb_strcbtype(type), lcb_strerror_short(rc),
(int)nkey, key);
}
static int running = 1;
static void sigint_handler(int unused)
{
running = 0;
}
int main(int argc, char *argv[])
{
lcb_INSTANCE *instance;
char *bucket = NULL;
const char *key = "user:king_arthur";
const char *val = "{"
" \"email\": \"kingarthur@couchbase.com\","
" \"interests\": [\"Holy Grail\", \"African Swallows\"]"
"}";
if (argc < 2) {
fprintf(stderr, "Usage: %s couchbase://host/bucket [ password [ username ] ]\n", argv[0]);
exit(EXIT_FAILURE);
}
{
lcb_CREATEOPTS *options = NULL;
lcb_createopts_create(&options, LCB_TYPE_BUCKET);
lcb_createopts_connstr(options, argv[1], strlen(argv[1]));
if (argc > 3) {
lcb_createopts_credentials(options, argv[3], strlen(argv[3]), argv[2], strlen(argv[2]));
}
check(lcb_create(&instance, options), "create couchbase handle");
lcb_createopts_destroy(options);
check(lcb_connect(instance), "schedule connection");
check(lcb_get_bootstrap_status(instance), "bootstrap from cluster");
check(lcb_cntl(instance, LCB_CNTL_GET, LCB_CNTL_BUCKETNAME, &bucket), "get bucket name");
}
{
lcb_CMDSTORE *cmd;
lcb_cmdstore_create(&cmd, LCB_STORE_UPSERT);
lcb_cmdstore_key(cmd, key, strlen(key));
lcb_cmdstore_value(cmd, val, strlen(val));
check(lcb_store(instance, NULL, cmd), "schedule STORE operation");
lcb_cmdstore_destroy(cmd);
}
{
lcb_CMDGET *cmd;
lcb_cmdget_create(&cmd);
lcb_cmdget_key(cmd, key, strlen(key));
check(lcb_get(instance, NULL, cmd), "schedule GET operation");
lcb_cmdget_destroy(cmd);
}
{
char query[1024] = {0};
snprintf(query, sizeof(query), "CREATE PRIMARY INDEX ON `%s` USING gsi", bucket);
check(lcb_cmdquery_statement(cmd, query, strlen(query)), "set QUERY statement");
lcb_cmdquery_callback(cmd, row_callback);
check(lcb_query(instance, NULL, cmd), "schedule QUERY INDEX create operation");
}
/* setup CTRL-C handler */
struct sigaction action;
sigemptyset(&action.sa_mask);
action.sa_handler = sigint_handler;
action.sa_flags = 0;
sigaction(SIGINT, &action, NULL);
while (running) {
char query[1024] = {0};
const char *params = "[\"African Swallows\"]";
snprintf(query, sizeof(query), "SELECT * FROM `%s` WHERE $1 in interests LIMIT 1", bucket);
check(lcb_cmdquery_statement(cmd, query, strlen(query)), "set QUERY statement");
check(lcb_cmdquery_positional_params(cmd, params, strlen(params)), "set QUERY positional parameters");
check(lcb_cmdquery_option(cmd, "pretty", strlen("pretty"), "false", strlen("false")),
"set QUERY 'pretty' option");
lcb_cmdquery_callback(cmd, row_callback);
check(lcb_query(instance, NULL, cmd), "schedule QUERY operation");
}
/* Now that we're all done, close down the connection handle */
lcb_destroy(instance);
return 0;
}
Main header file for Couchbase.
void lcb_destroy(lcb_INSTANCE *instance)
Destroy (and release all allocated resources) an instance of lcb.
#define LCB_CNTL_BUCKETNAME
Get the name of the bucket This returns the name of the bucket this instance is connected to,...
Definition: cntl.h:175
#define LCB_CNTL_GET
Retrieve a setting.
Definition: cntl.h:123
lcb_STATUS lcb_cntl(lcb_INSTANCE *instance, int mode, int cmd, void *arg)
This function exposes an ioctl/fcntl-like interface to read and write various configuration propertie...
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:212
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)
const char * lcb_strcbtype(int cbtype)
Returns the type of the callback as a string.
void(* lcb_RESPCALLBACK)(lcb_INSTANCE *instance, int cbtype, const lcb_RESPBASE *resp)
Callback invoked for responses.
Definition: couchbase.h:554
@ LCB_CALLBACK_GET
lcb_get()
Definition: couchbase.h:471
@ LCB_CALLBACK_STORE
lcb_store()
Definition: couchbase.h:472
lcb_STATUS lcb_query(lcb_INSTANCE *instance, void *cookie, const lcb_CMDQUERY *cmd)
Execute a N1QL query.
lcb_STATUS lcb_cmdquery_statement(lcb_CMDQUERY *cmd, const char *statement, size_t statement_len)
Sets the actual statement to be executed.
lcb_STATUS lcb_cmdquery_option(lcb_CMDQUERY *cmd, const char *name, size_t name_len, const char *value, size_t value_len)
Set a query option.
struct lcb_RESPQUERY_ lcb_RESPQUERY
Opaque query response structure.
Definition: couchbase.h:2838
lcb_STATUS lcb_cmdquery_create(lcb_CMDQUERY **cmd)
Create a new lcb_CMDQUERY object.
struct lcb_CMDQUERY_ lcb_CMDQUERY
Opaque query command structure.
Definition: couchbase.h:2843
lcb_STATUS lcb_cmdquery_destroy(lcb_CMDQUERY *cmd)
Free the command structure.
lcb_STATUS lcb_cmdquery_positional_params(lcb_CMDQUERY *cmd, const char *value, size_t value_len)
Sets the positional arguments for the query.
struct lcb_RESPSTORE_ lcb_RESPSTORE
Schedule a single storage request.
Definition: couchbase.h:971
@ LCB_STORE_UPSERT
The default storage mode.
Definition: couchbase.h:887
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:1854
Definition: ixmgmt.h:157
const lcb_RESPQUERY * inner
Inner N1QL response.
Definition: ixmgmt.h:171