Couchbase C++ callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment