3

I put binary data in Couchbase. But for my new use case, I need to set a field called created_date along with my document. I was thinking of using xattr for this.

The purpose of this field "created_date" will be to use it for comparison between two versions of the same document (let's say doc_1)

For e.g.

doc_1 - created_date1 - In couchbase currently

doc_1 - created_date2 - Received from upstream

And I need to compare created_date2 vs created_date1 and replace the upstream document in Couchbase only if created_date2 > created_date1 .

I am using Java SDK for Couchbase. I was using upsert for inserting/updating in Couchbase which supports transcoder for non-JSON documents in UpsertOptions.

UpsertOptions upsertOptions = UpsertOptions.upsertOptions()
                .transcoder(transcoder)

But for setting Xattr I think I must use

collection.mutateIn

as MutateInSpec supports xattr

MutateInSpec.replace("created_date", created_date).xattr())

But I am not able to find an option to specify transcoder in

MutateInOptions.mutateInOptions()

Any help will be appreciated.

Yash Agarwal
  • 955
  • 1
  • 13
  • 28
  • I don't know of a way to upsert/insert a binary document and set its xattrs at the same time. It might be necessary to upsert first and then set the xattrs with a separate call to `mutateIn`. You'll probably want to use CAS from the upsert result when making the `mutateIn` request to make sure nobody else is trying to do the same thing at the same time. – dnault Feb 26 '21 at 21:34
  • Yeah, that's what I figured. But this means making an un-necessary network call. Not sure why this is not supported though – Yash Agarwal Mar 01 '21 at 05:09

1 Answers1

0

So you need to write the binary document body at the same time as writing an xattr? This is possible, you can do:

    collection.mutateIn("doc-id", Arrays.asList(

            // Make sure xattrs go first in the specs list
            MutateInSpec.upsert("xattr-path", "xattr-field").xattr(),

            // You can replace the document's body with binary data like this
            MutateInSpec.replace("", new byte[]{1,2,3,4})));
Graham Pople
  • 416
  • 4
  • 8