Created
September 24, 2017 07:59
-
-
Save jaekwonpark/99baf4cc575a56bd026ec2e923955884 to your computer and use it in GitHub Desktop.
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 <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_t instance = NULL; | |
lcb_create_st crst; | |
memset(&crst, 0, sizeof crst); | |
crst.version = 3; | |
std::string connectstr; | |
connectstr = "couchbase://172.23.123.252/"; | |
connectstr += argv[1]; | |
crst.v.v3.connstr = connectstr.c_str(); | |
crst.v.v3.passwd = "password"; | |
// create lcb_t handle | |
lcb_error_t 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