libcouchbase_arithmetic


#1

I am a trying to use the libcouchbase_arithmetic api to implement a simple counter program. But I am getting a LIBCOUCHBASE_ETMPFAIL error everytime I go to increment the counter.

typedef struct
{
int *cntr;
} CounterT;

/* the callback invoked by the library when receiving an error */
static void error_callback(libcouchbase_t instance,
libcouchbase_error_t error,
const char *errinfo)
{
(void)instance;
fprintf(stderr, “Error %d”, error);
if (errinfo) {
fprintf(stderr, “: %s”, errinfo);
}
fprintf(stderr, “\n”);
exit(EXIT_FAILURE);
}

static void arithmetic_callback(
libcouchbase_t instance, const void cookie,
libcouchbase_error_t err, const void key, size_t nkey,
uint64_t value, uint64_t cas)
{
CounterT
counter = (CounterT
) cookie;
(void)instance; (void)cookie; (void)cas;
printf(“Get callback received\n”);

*(counter->cntr) = value;
printf("Value : %u", value);

}

int incrCounterVal(const char* key, const int keyLen)
{
libcouchbase_error_t oprc; /* for checking various responses */

oprc= libcouchbase_arithmetic(mInstance,
        NULL, // cookie
        key,
        keyLen,
        1,
        0,
        1,
        1);
if (oprc != LIBCOUCHBASE_SUCCESS) {
    printf("\nFailed to do increment operation %d\n", oprc);
    return 0;
}

/* Wait for the operation to compelete */
libcouchbase_wait(mInstance);

oprc = libcouchbase_get_last_error(mInstance);
if (oprc == LIBCOUCHBASE_SUCCESS) {
} else {
    printf("Could not set hello.  Error received is %d", oprc);
    return 0;
}

return 1;

}

int getCounterValue(const char* key, const int keyLen, int counter1)
{
const char
keys[1];
size_t nkey[1];

CounterT counter;
libcouchbase_error_t oprc;  /* for checking various responses */


keys[0] = key;
nkey[0] = keyLen;
counter.cntr = counter1;

oprc = libcouchbase_mget(mInstance,
        &counter,
        1,
        (const void*const*)keys,
        nkey,
        NULL);

if (oprc != LIBCOUCHBASE_SUCCESS) {
    printf("\nFailed to  operation\n");
    return 0;
}

/* Wait for the operation to compelete */
libcouchbase_wait(mInstance);


return 1;

}

int main(int argc, char **argv)
{

const char *host = "localhost:8091";
const char *username = NULL;
const char *passwd = NULL;
const char *bucket = "default";
char key1[] = "Counter" ;
unsigned int counter1, counter2;

mInstance = libcouchbase_create(host, username,
        passwd, bucket, NULL);
if (mInstance == NULL) {
    fprintf(stderr, "Failed to create libcouchbase instance\n");
    return 1;
}

(void)libcouchbase_set_error_callback(mInstance, error_callback);
(void)libcouchbase_set_arithmetic_callback(mInstance, arithmetic_callback);

if (libcouchbase_connect(mInstance) != LIBCOUCHBASE_SUCCESS) {
    fprintf(stderr, "Failed to connect libcouchbase instance to server\n");
    return 1;
}

incrCounterVal(key1, strlen(key1));
incrCounterVal(key1, strlen(key1));
getCounterValue(key1, strlen(key1), &counter1);
printf(“Counter 1 : %d\n”, counter1);
}

I am new to this, it would require some help in this. Thanks in advance.


#2

Try adding a libcouchbase_wait after your libcouchbase_connect()