How to decrement using Atomic operation?

In my couchbase, I have bucket name called log_info. Using atomic operation I am able to insert data inside log_info as follow :-

public void sendDataBucket(JioTURequest jioTURequest) {
        try {
            String jioTuIdGenerator = userBucket.counter("log_info", 1, 0).toString();
            EntityDocument<JioTURequest> doc = EntityDocument.create(jioTuIdGenerator,jioTURequest);
        } catch (Exception e){
            LOGGER.error("Unable insert data in couchbase log bucket :"+e.getMessage());
            LOGGER.fatal("Log details : Keyword:"+jioTURequest.getShortUrl()+" | Click Time:"+jioTURequest.getClickTime()+" | IP addresss:"+jioTURequest.getIpAddress()+" | Referer:"+jioTURequest.getReferrer()+" | User Agent:"+jioTURequest.getUserAgent());

But now I need to select document from log_info and then remove it from bucket one by one.

There is no primary key defined so I need to query something like this for select operation
select * from log_info limit 1
and then remove the selected object from bucket and decrement the auto generated value by 1
How to achieve this in java using atomic operation?

If I understand what you’re doing correctly, you want to decrease the counter when you remove one document. That’s not correct from a design perspective.

If you create documents A and B, they will respectively get id 0 and 1 from the counter. Now imagine you remove A. You then decrement the counter, which goes from 1 down to 0.
Now you create document C, and it attempts to get the next number in the counter as id and gets 1

:boom: boom! two documents with the same key, so you’ll get an error (when inserting) :frowning:

Unless you want to atomically delete ALL documents in the bucket and reset the counter to 0?
Unfortunately, Couchbase doesn’t support transactions, which would be required for this use case…

Your answer is quite justified. Actually my main objective was to implement trigger at server side. So that if an entry come it will immediately update number of entries of another bucket’s document but unfortunately trigger functionality is not there.

Ah I see. This is actually one possible usecase for an upcoming feature, for now called “micro DCP”, that will allow users to easily listen to events like document creation.

It is being designed and implemented over the much more complex DCP protocol by @daschl :thumbsup:.

But that is not readily available yet :sweat:

Okay…May be I need to change my application design… :disappointed: