Skip to content

Instantly share code, notes, and snippets.

@couchtim
Created October 2, 2012 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save couchtim/3822134 to your computer and use it in GitHub Desktop.
Save couchtim/3822134 to your computer and use it in GitHub Desktop.
simple libcouchbase example
#include <sys/types.h>
#include <libcouchbase/couchbase.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DIE(msg) do { fprintf(stderr, "%s:%d: Fatal error: %s\n", __FILE__, __LINE__, (msg)); exit(1); } while (0)
libcouchbase_error_t error;
static void storageCallback(libcouchbase_t instance, const void *cookie,
libcouchbase_storage_t store_type, libcouchbase_error_t e,
const void *key, libcouchbase_size_t keysize, libcouchbase_cas_t cas)
{
error = e;
if (error != LIBCOUCHBASE_SUCCESS) {
fprintf(stderr, "Failed during store '%s': %s\n", (const char *)key, libcouchbase_strerror(instance, error));
}
else {
printf("Stored: %s (%lu)\n", (const char *)key, keysize);
}
(void)cookie;
(void)store_type;
(void)cas;
}
static void getCallback(libcouchbase_t instance, const void *cookie, libcouchbase_error_t e,
const void *key, libcouchbase_size_t keysize,
const void *value, libcouchbase_size_t valuesize,
libcouchbase_uint32_t flags, libcouchbase_cas_t cas)
{
error = e;
if (error != LIBCOUCHBASE_SUCCESS) {
fprintf(stderr, "Failed during get '%s': %s\n", (const char *)key, libcouchbase_strerror(instance, error));
}
else {
printf("Got: %s (%lu) = %s (%lu)\n", (const char *)key, keysize, (const char *)value, valuesize);
}
(void)cookie;
(void)flags;
(void)cas;
}
int main(int argc, char **argv) {
libcouchbase_t instance;
char *key;
char *value;
if (argc > 1) {
--argc;
++argv;
key = *argv;
}
else {
key = "hello";
}
if (argc > 1) {
--argc;
++argv;
value = *argv;
}
else {
value = "world!";
}
instance = libcouchbase_create("10.4.2.14:8091", "Administrator", "password", "default", NULL);
if (instance == NULL) {
/* XXX: Logs error message to stdout; any way to fix that? */
fprintf(stderr, "Failed to create couchbase instance\n");
exit(1);
}
(void)libcouchbase_set_storage_callback(instance, storageCallback);
(void)libcouchbase_set_get_callback(instance, getCallback);
if ((error = libcouchbase_connect(instance)) != LIBCOUCHBASE_SUCCESS) {
fprintf(stderr, "Failed to connect: %s\n", libcouchbase_strerror(instance, error));
exit(1);
}
libcouchbase_wait(instance);
error = libcouchbase_get_last_error(instance);
if (error != LIBCOUCHBASE_SUCCESS) {
fprintf(stderr, "Failed to connect: %s\n", libcouchbase_strerror(instance, error));
exit(1);
}
error = libcouchbase_store(instance, NULL, LIBCOUCHBASE_SET,
key, strlen(key) + 1,
value, strlen(value) + 1,
0, 0, 0);
if (error != LIBCOUCHBASE_SUCCESS) {
fprintf(stderr, "Failed to store: %s\n", libcouchbase_strerror(instance, error));
}
const char *keys[1];
libcouchbase_size_t nkey[1];
keys[0] = "hello";
nkey[0] = sizeof("hello");
error = libcouchbase_mget(instance, NULL, 1, (const void * const *)keys, nkey, NULL);
if (error != LIBCOUCHBASE_SUCCESS) {
fprintf(stderr, "Failed to get: %s\n", libcouchbase_strerror(instance, error));
}
libcouchbase_wait(instance);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment