Question regarding lcb_wait and lcb_store


#1

You don’t have to call it every time before those operations, you only need to really call it after you connect and/or before issuing other commands. You probably only need it after the initial connect anyway.


#2

why we need call lcb_wait(instance); every time before operation like Add, get data, destroy instance?
how to implement timeout mechanism with lcb_wait(instance), any example?
according to http://www.couchbase.com/docs/couchbase-sdk-c-2.0/api-reference-summary
lcb_store is NOT Asynchronous, so the call back has been called when we get error code ?
why signature of lcb_store defined in the link above is different from the one declared in couchbase.h?
thanks a lot!
sample code from libcouchbase :
/* Run the event loop and wait until we’ve connected */
lcb_wait(instance);
{
lcb_store_cmd_t cmd;
const lcb_store_cmd_t *commands[1];
commands[0] = &cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.v.v0.operation = LCB_SET;
cmd.v.v0.key = “foo”;
cmd.v.v0.nkey = 3;
cmd.v.v0.bytes = “bar”;
cmd.v.v0.nbytes = 3;
err = lcb_store(instance, NULL, 1, commands);
if (err != LCB_SUCCESS) {
fprintf(stderr, “Failed to store: %s\n”, lcb_strerror(NULL, err));
return 1;
}
}
lcb_wait(instance);
{
lcb_get_cmd_t cmd;
const lcb_get_cmd_t *commands[1];
commands[0] = &cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.v.v0.key = “foo”;
cmd.v.v0.nkey = 3;
err = lcb_get(instance, NULL, 1, commands);
if (err != LCB_SUCCESS) {
fprintf(stderr, “Failed to get: %s\n”, lcb_strerror(NULL, err));
return 1;
}
}
lcb_wait(instance);
lcb_destroy(instance);


#3

call back is actually Async, how to check if get_call_back has got value and assign value to the pointer specified by cookie
refer to code below, after lcb_get return, outValue has not been updated by callback yet. adding a timer is not a desired solution. Any better solution? thanks a lot!
bool CouchbaseHandler::getData(string _key,string& _outValue)
{
lcb_wait(instance);
lcb_get_cmd_t cmd;
const lcb_get_cmd_t * const commands[1] = { &cmd };
memset(&cmd, 0, sizeof(cmd));
cmd.v.v0.key = _key.c_str();
cmd.v.v0.nkey = strlen((const char*)(cmd.v.v0.key));
lcb_error_t err;
err = lcb_get(instance, this, 1, commands);
if (err != LCB_SUCCESS) {
fprintf(stderr, “Failed to get: %s\n”,
lcb_strerror(NULL, err));
return false;
}
//put a timer and wait call back return?
if(outKey.compare(_key) == 0)
{
cout<<"value: “<v.v0.key, sizeof(char), resp->v.v0.nkey, stderr);
fprintf(stderr, “”: %s\n”, lcb_strerror(instance, error));
}
else
{
fprintf(stderr, "get_callback Data for key: “”);
fwrite(resp->v.v0.key, sizeof(char), resp->v.v0.nkey, stderr);
fprintf(stderr, “” is : “);
fwrite(resp->v.v0.bytes, sizeof(char), resp->v.v0.nbytes, stderr);
fprintf(stderr, “” CAS: %llu FLAGS:0x%x SIZE:%lu\n”,
resp->v.v0.cas, resp->v.v0.flags, (unsigned long)resp->v.v0.nbytes);
CouchbaseHandler couchbaseHandler;
couchbaseHandler
= const_cast(reinterpret_cast(cookie));
string key((const char
)resp->v.v0.key);
couchbaseHandler->setOutKey(key);
string value((const char*)resp->v.v0.bytes);
couchbaseHandler->setOutValue(value);
}
}


#4

I find the solution, we need to call lcb_wait(instance); to sync with callback. see code beblow for more details:
bool CouchbaseHandler::getData(string _key,string& _outValue)
{
lcb_wait(instance);
lcb_get_cmd_t cmd;
const lcb_get_cmd_t * const commands[1] = { &cmd };
memset(&cmd, 0, sizeof(cmd));
cmd.v.v0.key = _key.c_str();
cmd.v.v0.nkey = strlen((const char*)(cmd.v.v0.key));
lcb_error_t err;
err = lcb_get(instance, this, 1, commands);
if (err != LCB_SUCCESS) {
fprintf(stderr, “Failed to get: %s\n”,
lcb_strerror(NULL, err));
return false;
}
//wait call back return
lcb_wait(instance);
cout<<"current key: "<