Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save avsej/3006da6863f97810d31aa1d1c5137e51 to your computer and use it in GitHub Desktop.
Save avsej/3006da6863f97810d31aa1d1c5137e51 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <thread>
#include "libcouchbase/couchbase.h"
static void opCallback(lcb_t instance, int cbtype, const lcb_RESPBASE *rb) {
if (rb->rc != LCB_SUCCESS) {
fprintf(stderr, "%s\n", lcb_strerror(NULL, rb->rc));
} else {
if (cbtype == LCB_CALLBACK_GET) {
const lcb_RESPGET *rg = (const lcb_RESPGET *)rb;
fprintf(stderr, "Get: %.*s\n", (int)rg->nvalue, rg->value);
}
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "need bucket name\n");
exit(1);
}
fprintf(stdout, "version:%X\n", lcb_version_g);
lcb_error_t error;
struct lcb_create_io_ops_st cio = {0};
lcb_io_opt_t io = NULL;
cio.v.v0.type = LCB_IO_OPS_LIBEVENT;
error = lcb_create_io_ops(&io, &cio);
if (error != LCB_SUCCESS) {
fprintf(stderr, "Failed to create a libevent IO: %s\n", lcb_strerror(NULL, error));
exit(1);
}
lcb_t instance = NULL;
lcb_create_st crst;
memset(&crst, 0, sizeof crst);
crst.version = 3;
std::string connectstr;
connectstr = "couchbase://192.168.1.101/";
connectstr += argv[1];
crst.v.v3.connstr = connectstr.c_str();
crst.v.v3.passwd = "password";
crst.v.v3.io = io;
// create lcb_t handle
error = lcb_create(&instance, &crst);
if (error != LCB_SUCCESS) {
fprintf(stderr, "Failed to create a libcouchbase instance: %s\n", lcb_strerror(NULL, error));
exit(1);
}
// connect using the handle
if ((error = lcb_connect(instance)) != LCB_SUCCESS) {
fprintf(stderr, "Failed to connect libcouchbase instance: %s\n", lcb_strerror(NULL, error));
exit(1);
}
lcb_wait(instance);
// bootstrap to assure connection is successful
if ((error = lcb_get_bootstrap_status(instance)) != LCB_SUCCESS) {
fprintf(stderr, "Failed to connect: %s", lcb_strerror(NULL, error));
exit(1);
}
// install operation callback
lcb_install_callback3(instance, LCB_CALLBACK_GET, opCallback);
lcb_install_callback3(instance, LCB_CALLBACK_STORE, opCallback);
std::string keybase = "key";
// upsert workload
for (int i = 0; i < 1000; i++) {
lcb_CMDSTORE scmd = {0};
std::string key;
key = keybase;
key += std::to_string(i);
std::string value;
value = "value";
value += std::to_string(i);
LCB_CMD_SET_KEY(&scmd, key.c_str(), key.size());
LCB_CMD_SET_VALUE(&scmd, value.c_str(), value.size());
scmd.operation = LCB_SET;
error = lcb_store3(instance, NULL, &scmd);
if (error != LCB_SUCCESS) {
fprintf(stderr, "Failed on Set:0x%X (%s)\n", error, lcb_strerror(instance, error));
}
lcb_wait(instance);
}
// get forever
int i = 0;
while(true) {
lcb_CMDGET gcmd = {0};
std::string key;
key = keybase;
key += std::to_string(i);
LCB_CMD_SET_KEY(&gcmd, key.c_str(), key.size());
error = lcb_get3(instance, NULL, &gcmd);
if (error != LCB_SUCCESS) {
fprintf(stderr, "Failed on Get:0x%X (%s)\n", error, lcb_strerror(instance, error));
}
lcb_wait(instance);
i++;
if (i > 999) {
i = 0;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment