Hi, folks:
after i upgrade libcouchbase to latest one, following code doesn’t work.
I try to get data from view via number key. lcb_connect success but lcb_make_http_request failed. Any clue why?
You must call lcb_wait() after lcb_connect() (this should be the case for older versions as well)
You would use the optstr/noptstr to add any additional view parameter information (as you are doing with path in the HTTP subsystem, except that you wouldn’t need to use _view/name/_design/name?)
thanks a lot.
I didn’t know we need to call lcb_get_bootstrap_status after lcb_connect(instance);lcb_wait(instance);
in older version, we call lcb_set_error_callback(instance, error_callback); before lcb_connect then i got compile error, so i switch to lcb_set_bootstrap_callback(instance, error_callback);
here is my CouchbaseHandler looks like, any comments would be appreciated.
bool CouchbaseHandler::init(const string _host,const string _port,const string _user,const string _password,const string _bucket)
{
host = _host;
port = _port;
user = _user;
password = _password;
bucket = _bucket;
outKey = "";
outValue = "";
string complete_host = host +":"+port;
std::cout<<"Init libcouchbase instance,hots: "<<complete_host<<",user: "<<user<<",pass: "<<password<<" ,bucket: "<<bucket<<std::endl;
lcb_error_t err;
memset(&create_options, 0, sizeof(create_options));
create_options.version = 3;
std::string connectStr = "couchbase://"+host+"/"+bucket;
create_options.v.v3.connstr = connectStr.c_str();
create_options.v.v3.passwd = password.c_str();
err = lcb_create(&instance, &create_options);
if (err != LCB_SUCCESS)
{
// fprintf(stderr, "Failed to create libcouchbase instance: %s\n",lcb_strerror(NULL, err));
std::cout<<"Failed to create libcouchbase instance,hots: "<<host<<",user: "<<user<<",pass: "<<password<<" ,bucket: "<<bucket<<std::endl;
return false;
}
/* Set up the handler to catch all errors! */
//lcb_set_error_callback(instance, error_callback);
lcb_set_bootstrap_callback(instance, error_callback);
/*
* Initiate the connect sequence in libcouchbase
*/
if ((err = lcb_connect(instance)) != LCB_SUCCESS)
{
// fprintf(stderr, "Failed to initiate connect: %s\n",lcb_strerror(NULL, err));
std::cout<<"Failed to connect to libcouchbase instance,hots: "<<host<<",user: "<<user<<",pass: "<<password<<" ,bucket: "<<bucket<<std::endl;
return false;
}
/* Run the event loop and wait until we've connected */
lcb_wait(instance);
//set up callback for store requests
lcb_set_store_callback(instance, store_callback);
//Set up a callback for our get requests
lcb_set_get_callback(instance, get_callback);
lcb_set_http_complete_callback(instance, complete_callback);
//Set up data call back for view request
lcb_set_http_data_callback(instance, data_callback);
//std::cout<<"init return true"<<std::endl;
lcb_set_remove_callback (instance, remove_callback);
return true;
}
It looks mainly OK. I will note that lcb_set_bootstrap_callback is really only useful if you are dealing with an event loop and you want to be notified of connection errors asynchronously.
If your application is blocking, the following would be much simpler to read:
if ((err = lcb_connect(instance)) != LCB_SUCCESS) {
// print error and exit
}
lcb_wait(instance);
if ((err = lcb_get_bootstrap_status(instance)) != LCB_SUCCESS) {
// print error and exit
}
// ....
thanks, i can get data in row callback. One more question, what if the key map to multiple rows?
so the row_callback would be called multiple times? if it’s called multiple times, how can I aggregate data from multiple row callnback? thanks in advance.
You can use the cookie argument to lcb_view_query. The response will also have a cookie field which is the same pointer passed to the view_query function. This cookie is an opaque pointer if your choosing which can be a list, for example, to help you aggregate.