public
Created

Couchbase C++ callbacks

  • Download Gist
couchbase.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include "couchbase.hpp"
 
namespace glim {
 
//! Invoke \code cookie \endcode as \code std::function \endcode and then \code delete \endcode it.
void couchbase_get_callback (libcouchbase_t instance, const void *cookie, libcouchbase_error_t error,
const void *key, libcouchbase_size_t nkey, const void *bytes, libcouchbase_size_t nbytes,
libcouchbase_uint32_t flags, libcouchbase_cas_t cas) {
if (!cookie) return;
couchbase_get_function* fun = (couchbase_get_function*) cookie;
fun->operator()(error, (const char*) bytes, (size_t) nbytes);
delete fun;
}
 
}; // namespace glim
couchbase.hpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#ifndef GLIM_COUCHBASE_HPP_
#define GLIM_COUCHBASE_HPP_
 
#include <sys/types.h>
#include <libcouchbase/couchbase.h> // http://www.couchbase.com/develop/c/current
#include <functional> // http://en.cppreference.com/w/cpp/utility/functional/function
 
namespace glim {
 
//! Function receiving Couchbase error code, value bytes and value length.
typedef std::function<void(libcouchbase_error_t,const char*,size_t)> couchbase_get_function;
 
inline libcouchbase_error_t couchbase_get (libcouchbase_t instance, couchbase_get_function callback,
const void *key, libcouchbase_size_t nkey, const libcouchbase_time_t *exp) {
const void* keys[] = {key}; size_t keysLen[] = {nkey};
return libcouchbase_mget (instance, new couchbase_get_function (callback), 1, (const void* const*) keys, keysLen, exp);
}
 
//! Invoke \code cookie \endcode as \code std::function \endcode and then \code delete \endcode it.
void couchbase_get_callback (libcouchbase_t instance, const void *cookie, libcouchbase_error_t error,
const void *key, libcouchbase_size_t nkey, const void *bytes, libcouchbase_size_t nbytes,
libcouchbase_uint32_t flags, libcouchbase_cas_t cas);
 
}; // namespace glim
 
#endif // GLIM_COUCHBASE_HPP_
test_couchbase.cc
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include "couchbase.cpp"
#include <assert.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <stdexcept>
 
int main () {
std::cout << "Testing couchbase.hpp ... " << std::flush;
 
const char* host = getenv ("TEST_COUCHBASE_HOST");
if (!host) host = "localhost";
libcouchbase_t couchbase = libcouchbase_create (host, NULL, NULL, "default", NULL);
libcouchbase_connect (couchbase);
libcouchbase_wait (couchbase);
libcouchbase_error_t err = libcouchbase_get_last_error (couchbase);
if (err != LIBCOUCHBASE_SUCCESS) throw std::runtime_error (libcouchbase_strerror (couchbase, err));
 
libcouchbase_set_get_callback (couchbase, glim::couchbase_get_callback);
std::string key = "libglim_test_couchbase", value = "{\"foo\": \"bar\"}";
err = libcouchbase_store (couchbase, NULL,
LIBCOUCHBASE_SET, key.c_str(), key.length(), value.c_str(), value.length(), 0, 0, 0);
if (err != LIBCOUCHBASE_SUCCESS) throw std::runtime_error (libcouchbase_strerror (couchbase, err));
 
const char* keys[1] = {key.c_str()}; size_t keysLen[1] = {key.length()};
bool callback1 = false; bool callback2 = false;
err = libcouchbase_mget (couchbase, new glim::couchbase_get_function (
[&callback1](libcouchbase_error_t err, const char* vb, size_t vl){
assert (err == LIBCOUCHBASE_SUCCESS);
callback1 = true;
std::cout << "callback " << std::string (vb, vl) << " ... " << std::flush;
}), 1, (const void* const*) keys, keysLen, NULL);
if (err != LIBCOUCHBASE_SUCCESS) throw std::runtime_error (libcouchbase_strerror (couchbase, err));
 
err = glim::couchbase_get (couchbase,
[&callback2](libcouchbase_error_t err, const char* vb, size_t vl){
assert (err == LIBCOUCHBASE_SUCCESS);
callback2 = true;
}, key.c_str(), key.length(), NULL);
if (err != LIBCOUCHBASE_SUCCESS) throw std::runtime_error (libcouchbase_strerror (couchbase, err));
 
assert (!callback1); assert (!callback2);
 
err = libcouchbase_remove (couchbase, NULL, key.c_str(), key.length(), 0);
if (err != LIBCOUCHBASE_SUCCESS) throw std::runtime_error (libcouchbase_strerror (couchbase, err));
 
libcouchbase_wait (couchbase);
err = libcouchbase_get_last_error (couchbase);
if (err != LIBCOUCHBASE_SUCCESS) throw std::runtime_error (libcouchbase_strerror (couchbase, err));
 
assert (callback1); assert (callback2);
 
std::cout << "pass." << std::endl;
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.