question regarding C client library

I'm using libcouchbase, have couple questions here:
1. lcb_datatype_t is ENUM ? what is the possible value of lcb_datatype_t? i tried to use LCB_JSON, my code cannot be compile, cannot find this type. can anyone point out the definition of lcb_datatype_t?
typedef lcb_uint8_t lcb_datatype_t doesn't tell what kind of ENUM could be assigned to it.
2. I try to run lcb_store code to store new record in couchbase server get error:
Key exists (with a different CAS value)
if i remove store->v.v0.cas = 0x1234; it works.
if i change the cas value, still get error.
LCB_JSON issue as described above, i changed it to 0 or 1, but still store data in binary instead of json. please advice. thanks a lot!
* sample code:
* lcb_store_cmd_st *store = calloc(1, sizeof(*store));
* store->version = 0;
* store->v.v0.key = "my-key";
* store->v.v0.nkey = strlen(store->v.v0.key);
* store->v.v0.bytes = "{ value:666 }"
* store->v.v0.nbytes = strlen(store->v.v0.bytes);
* store->v.v0.flags = 0xdeadcafe;
* store->v.v0.cas = 0x1234;
* store->v.v0.exptime = 0x666;
* store->v.v0.datatype = LCB_JSON;
* store->v.v0.operation = LCB_REPLACE;
* lcb_store_cmd_st* commands[] = { store };
* lcb_store(instance, NULL, 1, commands);

1 Answer

« Back to question.

lcb_datatype_t is type tag, it doesn't force any data conversion.
The datatypes defined in this file https://github.com/couchbase/libcouchbase/blob/master/include/memcached/... they aren't exported, because final decision about server-side implementation hasn't been made.
The CAS value is changing after each mutation, so you should track it and pass to further mutations to implement optimistic locking. You can pick CAS either in any operation callback
And by the way "{ value:666 }" isn't valid JSON. It should be "{ \"value\":666 }". Your value is valid javascript, but javascript isn't JSON.

thanks, avsej. what value I should assign to store->v.v0.datatype if I want to store JSON data?
how to track CAS ? any API to call or I have to store them in data structure like map and track them by myself?
thanks a lot!

jygan wrote:
what value I should assign to store->v.v0.datatype if I want to store JSON data?

right now this field ignored, so let it be zero
jygan wrote:
how to track CAS ? any API to call or I have to store them in data structure like map and track them by myself?

depends on your algorithm, if some of your operations require it, just store it in whatever struct you want in your application, if not, you can ignore it. little examples:
1. cas-insensitive case: keeping "the most recent event", in this case it is ok, if the key will be rewritten, most recent wins anyway
2. cas-sensitive case: tracking list of product ratings, here you aren't allowed to rewrite the object.