Search in sources :

Example 11 with CasMismatchException

use of com.couchbase.client.core.error.CasMismatchException in project couchbase-jvm-clients by couchbase.

the class CouchbaseMap method put.

@Override
public E put(String key, E value) {
    checkKey(key);
    for (int i = 0; i < mapOptions.casMismatchRetries(); i++) {
        try {
            long returnCas = 0;
            E result = null;
            try {
                LookupInResult current = collection.lookupIn(id, Collections.singletonList(LookupInSpec.get(key)), lookupInOptions);
                returnCas = current.cas();
                if (current.exists(0)) {
                    result = current.contentAs(0, entityTypeClass);
                }
            } catch (PathNotFoundException e) {
            // that's ok, we will just upsert anyways, and return null
            } catch (DocumentNotFoundException e) {
                // we will create an empty doc and remember the cas
                returnCas = createEmpty();
            }
            collection.mutateIn(id, Collections.singletonList(MutateInSpec.upsert(key, value)), mapOptions.mutateInOptions().cas(returnCas));
            return result;
        } catch (CasMismatchException ex) {
        // will need to retry get-and-set
        }
    }
    throw new CouchbaseException("CouchbaseMap put failed", new RetryExhaustedException("Couldn't perform put in less than " + mapOptions.casMismatchRetries() + " iterations. It is likely concurrent modifications of this document are the reason"));
}
Also used : LookupInResult(com.couchbase.client.java.kv.LookupInResult) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) PathNotFoundException(com.couchbase.client.core.error.subdoc.PathNotFoundException)

Example 12 with CasMismatchException

use of com.couchbase.client.core.error.CasMismatchException in project couchbase-jvm-clients by couchbase.

the class CouchbaseQueue method poll.

@Override
public E poll() {
    // FIFO queue as offer uses ARRAY_PREPEND
    String idx = "[-1]";
    for (int i = 0; i < queueOptions.casMismatchRetries(); i++) {
        try {
            LookupInResult result = collection.lookupIn(id, Collections.singletonList(LookupInSpec.get(idx)), lookupInOptions);
            E current = result.contentAs(0, entityTypeClass);
            long returnCas = result.cas();
            collection.mutateIn(id, Collections.singletonList(MutateInSpec.remove(idx)), queueOptions.mutateInOptions().cas(returnCas));
            return current;
        } catch (DocumentNotFoundException | PathNotFoundException ex) {
            return null;
        } catch (CasMismatchException ex) {
        // will have to retry get-and-remove
        }
    }
    throw new CouchbaseException("CouchbaseQueue poll failed", new RetryExhaustedException("Couldn't perform poll in less than " + queueOptions.casMismatchRetries() + " iterations. It is likely concurrent modifications of this document are the reason"));
}
Also used : LookupInResult(com.couchbase.client.java.kv.LookupInResult) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) PathNotFoundException(com.couchbase.client.core.error.subdoc.PathNotFoundException)

Aggregations

CasMismatchException (com.couchbase.client.core.error.CasMismatchException)12 DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)8 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)7 RetryExhaustedException (com.couchbase.client.core.retry.reactor.RetryExhaustedException)6 PathNotFoundException (com.couchbase.client.core.error.subdoc.PathNotFoundException)5 LookupInResult (com.couchbase.client.java.kv.LookupInResult)5 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)5 Test (org.junit.jupiter.api.Test)5 JsonObject (com.couchbase.client.java.json.JsonObject)4 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)4 GetResult (com.couchbase.client.java.kv.GetResult)2 MutateInResult (com.couchbase.client.java.kv.MutateInResult)2 ReplaceBodyWithXattr (com.couchbase.client.java.kv.ReplaceBodyWithXattr)2 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)1 DmlFailureException (com.couchbase.client.core.error.DmlFailureException)1 DocumentExistsException (com.couchbase.client.core.error.DocumentExistsException)1 ErrorCodeAndMessage (com.couchbase.client.core.error.ErrorCodeAndMessage)1 IndexExistsException (com.couchbase.client.core.error.IndexExistsException)1 IndexFailureException (com.couchbase.client.core.error.IndexFailureException)1 IndexNotFoundException (com.couchbase.client.core.error.IndexNotFoundException)1