Search:

Search all manuals
Search this manual
Manual
Couchbase Client Library C (libcouchbase) 1.0
Community Wiki and Resources
Wiki: C and C++ Client Library
Download Client Library (source)
C and C++ Client Library
SDK Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
1 Getting Started
Chapter Sections
Chapters

1.6. Hello C Couchbase

This chapter will get you started with Couchbase Server and the Couchbase C SDK, libcouchbase.

This section assumes you have downloaded and set up a compatible version of Couchbase Server and have at least one instance of Couchbase Server and one data bucket established. If you need to set up these items, you can do with the Couchbase Administrative Console, or Couchbase Command-Line Interface (CLI), or the Couchbase REST-API. For information and instructions, see:

After you have your Couchbase Server set up and you have installed the needed client libraries, you can compile and run the following basic program.

/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2012 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.
 */

/**
 * Example "hello Couchbase" libcouchbase program.
 *
 * @author Matt Ingenthron
 */

#include <getopt.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include <libcouchbase/couchbase.h>

/* the callback invoked by the library when receiving an error */
static void error_callback(libcouchbase_t instance,
                           libcouchbase_error_t error,
                           const char *errinfo)
{
    (void)instance;
    fprintf(stderr, "Error %d", error);
    if (errinfo) {
        fprintf(stderr, ": %s", errinfo);
    }
    fprintf(stderr, "\n");
    exit(EXIT_FAILURE);
}

/* the callback invoked by the library when receiving a get response */
static void get_callback(libcouchbase_t instance,
                         const void *cookie,
                         libcouchbase_error_t error,
                         const void *key, size_t nkey,
                         const void *bytes, size_t nbytes,
                         uint32_t flags, uint64_t cas)
{
    (void)instance; (void)cookie; (void)cas;
    fwrite(bytes, 1, nbytes, stdout);
    fprintf(stdout, "\n");
    fflush(stdout);
    fprintf(stderr, "Get callback received\n");
}


int main(int argc, char **argv)
{
    libcouchbase_t instance;    /* our libcouchbase instance */
    libcouchbase_error_t oprc;  /* for checking various responses */

    const char *host = "localhost:8091";
    const char *username = NULL;
    const char *passwd = NULL;
    const char *bucket = "default";

    instance = libcouchbase_create(host, username,
                                   passwd, bucket, NULL);
    if (instance == NULL) {
        fprintf(stderr, "Failed to create libcouchbase instance\n");
        return 1;
    }

    (void)libcouchbase_set_error_callback(instance, error_callback);
    (void)libcouchbase_set_get_callback(instance, get_callback);

    if (libcouchbase_connect(instance) != LIBCOUCHBASE_SUCCESS) {
        fprintf(stderr, "Failed to connect libcouchbase instance to server\n");
        return 1;
    }

    /* Wait for the connect to compelete */
    libcouchbase_wait(instance);

    /* A simple document we want to create */
    char *key = "hello";
    char *doc = "{ \"message\": \"world\" }";

    /* Store doc to in the system */
    oprc = libcouchbase_store(instance,
                              NULL,
                              LIBCOUCHBASE_SET,
                              key, /* the key or _id of the document */
                              strlen(key), /* the key length */
                              doc,
                              strlen(doc), /* length of */
                              0,  /* flags,  */
                              0,  /* expiration */
                              0); /* and CAS values, see API reference */

    if (oprc != LIBCOUCHBASE_SUCCESS) {
        fprintf(stderr, "Failed to create hello store operation.\n");
        return 1;
    }

    /* Wait for the operation to compelete */
    libcouchbase_wait(instance);

    oprc = libcouchbase_get_last_error(instance);
    if (oprc == LIBCOUCHBASE_SUCCESS) {
        fprintf(stderr, "Successfully set hello.\n");
    } else {
        fprintf(stderr, "Could not set hello.  Error received is %d\n", oprc);
        return 1;
    }

    /* Now go and get "hello" */
    const char* keys[1];
    size_t nkey[1];
    keys[0] = key;
    nkey[0] = strlen(key);
    oprc = libcouchbase_mget(instance,
                             NULL,
                             1,
                             (const void*const*)keys,
                             nkey,
                             NULL);

    if (oprc != LIBCOUCHBASE_SUCCESS) {
        fprintf(stderr, "Failed to create hello mget operation.\n");
        return 1;
    }

    /* Wait for the operation to compelete */
    libcouchbase_wait(instance);

    return 0;
}