Sample about how to retrieve Multiple Keys using the C client

Hello

We have been testing the C example in the couchbase documentation and so far so good.

We would like to be able to retrieve more than one Key in one call to lcb_get(...)

I was unable to find any example for the C client so we have wrote something which is working but I would like to know if this is the right approach. Below some snippets of the code with the relevant parts

//==== Conforming keys ======
lcb_get_cmd_t cmdReq;
lcb_get_cmd_t cmdResp;

const lcb_get_cmd_t * const commands[2] = { &cmdReq, &cmdResp };

memset(&cmdReq, 0, sizeof(cmdReq));
memset(&cmdResp, 0, sizeof(cmdResp));

counter = 1; //

cmd.v.v0.key = "foo";
cmd.v.v0.nkey = 3;

cmd.v.v0.key = "foo1";
cmd.v.v0.nkey = 4;

err = lcb_get(instance, NULL, 2, commands);
...............

//==== Callback function for get ========
static void get_callback(lcb_t instance,
const void *cookie,
lcb_error_t error,
const lcb_get_resp_t *resp)
{
if (error != LCB_SUCCESS) {
ShowMessage("Failed to retrieve Key.\nError:" + String(lcb_strerror(instance, error)));

} else {
if (counter == 1) {
keyValueReq = keyValueReq.assign((const char *)resp->v.v0.bytes, resp->v.v0.nbytes);
counter++;
} else {
keyValueResp = keyValueResp.assign((const char *)resp->v.v0.bytes, resp->v.v0.nbytes);
}
}

This code is working ok so far and as you can see, we are using this "counter" variable to differentiate between the values associated to each of the keys BUT

1- Is ok to assume that the calls to get_callback(...) function will be executed in the same order as the keys in the "commands[]" variable?

2- Is this the best solution or would you suggest a different approach?

3- How much faster is the retrieving of multiple Keys at a time when compared to individual retrieves?

Thanks a lot
Javier

1 Answer

« Back to question.

1. no, callbacks are executing in order the packets received back and parsed. It might seem like they are in order while you are using single server, but you will quickly notice difference when your keys will be hashed to different nodes

2. probably better solution will be to allocate the structure, which could be filled out of the order. For example, use some of hash map implementation, when you will leverage key in the callback to get position where to insert result. Or just do lookup the key among known and ordered array of keys (commands array in your case).

3. obviously they are faster, because they will be likely transferred with the single syscall (like sendmsg(2)), more likely will fit single TCP transfer unit. but of course you should benchmark it in your sandbox.

Hi avsej,

Thanks a lot. I will be using a map solution. I just that I read in the SDK documentation about multi-gets/bulk-gets options. I saw an example for ruby in the documentation and I was wondering is there was anything similar to it for the C API.

Thx
Javier