Skip to content

Instantly share code, notes, and snippets.

@mnunberg
Created April 26, 2012 19:03
Show Gist options
  • Save mnunberg/2502023 to your computer and use it in GitHub Desktop.
Save mnunberg/2502023 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <assert.h>
#include <libcouchbase/couchbase.h>
static void
error_callback(
libcouchbase_t instance,
libcouchbase_error_t err,
const char *errinfo)
{
fprintf(stderr, "Got error %d: %s\n",
err, errinfo);
abort();
}
static void
version_callback(
libcouchbase_t instance,
const void *cookie,
const char *endpoint,
libcouchbase_error_t err,
const char *version,
size_t nversion)
{
if (! (endpoint && version)) {
return;
}
char vbuf[nversion+1];
memset(vbuf, 0, sizeof(vbuf));
printf("Server %s: %s\n", endpoint, version);
}
static void
store_callback(
libcouchbase_t instance,
const void *cookie,
libcouchbase_storage_t oper,
libcouchbase_error_t err,
const void *key, size_t nkey,
libcouchbase_cas_t cas)
{
char kbuf[nkey+1];
kbuf[sizeof(kbuf)] = 0;
memcpy(kbuf, key, nkey);
assert(err == LIBCOUCHBASE_SUCCESS);
printf("Got key %s\n", kbuf);
}
static void
get_callback(libcouchbase_t instance,
const void *cookie,
libcouchbase_error_t err,
const void *key, size_t nkey,
const void *bytes, size_t nbytes,
uint32_t flags, uint64_t cas)
{
char kbuf[nkey+1], vbuf[nbytes+1];
memset(kbuf, 0, sizeof(kbuf));
memset(vbuf, 0, sizeof(vbuf));
assert(err == LIBCOUCHBASE_SUCCESS);
memcpy(kbuf, key, nkey);
memcpy(vbuf, bytes, nbytes);
printf("Got %s => %s\n", kbuf, vbuf);
}
#define MY_KEY "THE_KEY"
#define INITIAL_JSON "\"foo\":1}"
int main(void)
{
char *kbuf = MY_KEY;
char *vbuf = INITIAL_JSON;
size_t nk = strlen(kbuf);
size_t nv = strlen(vbuf);
libcouchbase_error_t err;
libcouchbase_t instance = libcouchbase_create(
"10.0.0.99",
"Administrator",
"123456",
"membase0",
NULL);
assert(instance);
libcouchbase_set_storage_callback(instance, store_callback);
libcouchbase_set_get_callback(instance, get_callback);
libcouchbase_set_error_callback(instance, error_callback);
libcouchbase_set_version_callback(instance, version_callback);
assert(libcouchbase_connect(instance) == LIBCOUCHBASE_SUCCESS);
libcouchbase_wait(instance);
err = libcouchbase_server_versions(instance, NULL);
assert(err == LIBCOUCHBASE_SUCCESS);
libcouchbase_wait(instance);
err =libcouchbase_store(
instance,
NULL,
LIBCOUCHBASE_SET,
kbuf, nk,
vbuf, nv,
0, 0, 0);
assert(err == LIBCOUCHBASE_SUCCESS);
libcouchbase_wait(instance);
err = libcouchbase_mget(
instance,
NULL,
1,
&kbuf,
&nk,
NULL);
assert(err == LIBCOUCHBASE_SUCCESS);
libcouchbase_wait(instance);
err = libcouchbase_store(
instance,
NULL,
LIBCOUCHBASE_APPEND,
kbuf, nk,
"TRAILING GARBAGE", sizeof("TRAILING GARBAGE")-1,
0,0,0);
assert(err == LIBCOUCHBASE_SUCCESS);
libcouchbase_wait(instance);
err = libcouchbase_mget(
instance,
NULL,
1,
&kbuf,
&nk,
NULL);
assert(err == LIBCOUCHBASE_SUCCESS);
libcouchbase_wait(instance);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment