Couchbase C Client  3.3.12
Asynchronous C Client for Couchbase
example/fts/fts.c

Shows Full Text search queries.

Shows Full Text search queries.

/* -*- 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 "queries.h"
static void fail(const char *msg)
{
printf("[\x1b[31mERROR\x1b[0m] %s\n", msg);
exit(EXIT_FAILURE);
}
static void check(lcb_STATUS err, const char *msg)
{
if (err != LCB_SUCCESS) {
char buf[1024] = {0};
snprintf(buf, sizeof(buf), "%s: %s\n", msg, lcb_strerror_short(err));
fail(buf);
}
}
static int err2color(lcb_STATUS err)
{
switch (err) {
case LCB_SUCCESS:
return 49;
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_RESPSEARCH *resp)
{
const char *row;
size_t nrow;
lcb_respsearch_row(resp, &row, &nrow);
ln2space(row, nrow);
lcb_STATUS rc = lcb_respsearch_status(resp);
if (rc != LCB_SUCCESS) {
printf("\x1b[31m%s\x1b[0m: ", lcb_strerror_short(rc));
}
printf("%.*s\n", (int)nrow, row);
if (lcb_respsearch_is_final(resp)) {
printf("\n");
}
}
int main(int argc, char *argv[])
{
lcb_INSTANCE *instance;
char *bucket = NULL;
size_t ii;
if (argc < 2) {
printf("Usage: %s couchbase://host/bucket [ password [ username ] ]\n", argv[0]);
exit(EXIT_FAILURE);
}
{
lcb_CREATEOPTS *create_options = NULL;
lcb_createopts_create(&create_options, LCB_TYPE_BUCKET);
lcb_createopts_connstr(create_options, argv[1], strlen(argv[1]));
if (argc > 3) {
lcb_createopts_credentials(create_options, argv[3], strlen(argv[3]), argv[2], strlen(argv[2]));
}
check(lcb_create(&instance, create_options), "create couchbase handle");
lcb_createopts_destroy(create_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");
if (strcmp(bucket, "travel-sample") != 0) {
fail("expected bucket to be \"travel-sample\"");
}
}
for (ii = 0; ii < num_queries; ii++) {
lcb_CMDSEARCH *cmd;
lcb_cmdsearch_create(&cmd);
lcb_cmdsearch_callback(cmd, row_callback);
lcb_cmdsearch_payload(cmd, queries[ii].query, queries[ii].query_len);
check(lcb_search(instance, NULL, cmd), "schedule FTS index creation operation");
lcb_cmdsearch_destroy(cmd);
printf("----> \x1b[1m%s\x1b[0m\n", queries[ii].comment);
printf("----> \x1b[32m%.*s\x1b[0m\n", (int)queries[ii].query_len, queries[ii].query);
}
/* 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.
lcb_STATUS lcb_search(lcb_INSTANCE *instance, void *cookie, const lcb_CMDSEARCH *cmd)
Issue a full-text query.
#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
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_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