Search in sources :

Example 41 with CouchbaseException

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

the class CouchbaseArraySet method remove.

@Override
public boolean remove(Object t) {
    enforcePrimitive(t);
    for (int i = 0; i < arraySetOptions.casMismatchRetries(); i++) {
        try {
            GetResult result = collection.get(id);
            JsonArray current = result.contentAsArray();
            long cas = result.cas();
            int index = 0;
            boolean found = false;
            for (Object next : current) {
                if (safeEquals(next, t)) {
                    found = true;
                    break;
                }
                index++;
            }
            String path = "[" + index + "]";
            if (!found) {
                return false;
            } else {
                collection.mutateIn(id, Collections.singletonList(MutateInSpec.remove(path)), arraySetOptions.mutateInOptions().cas(cas));
                return true;
            }
        } catch (CasMismatchException e) {
        // retry
        } catch (DocumentNotFoundException ex) {
            return false;
        }
    }
    throw new CouchbaseException("CouchbaseArraySet remove failed", new RetryExhaustedException("Couldn't perform remove in less than " + arraySetOptions.casMismatchRetries() + " iterations. It is likely concurrent modifications of this document are the reason"));
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) GetResult(com.couchbase.client.java.kv.GetResult) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 42 with CouchbaseException

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

the class CouchbaseMap method remove.

@Override
public E remove(Object key) {
    String idx = checkKey(key);
    for (int i = 0; i < mapOptions.casMismatchRetries(); i++) {
        try {
            LookupInResult current = collection.lookupIn(id, Collections.singletonList(LookupInSpec.get(idx)), lookupInOptions);
            long returnCas = current.cas();
            E result = current.contentAs(0, entityTypeClass);
            collection.mutateIn(id, Collections.singletonList(MutateInSpec.remove(idx)), mapOptions.mutateInOptions().cas(returnCas));
            return result;
        } catch (DocumentNotFoundException | PathNotFoundException e) {
            return null;
        } catch (CasMismatchException ex) {
        // will have to retry get-and-remove
        }
    }
    throw new CouchbaseException("CouchbaseMap remove failed", new RetryExhaustedException("Couldn't perform remove 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 43 with CouchbaseException

use of com.couchbase.client.core.error.CouchbaseException 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 44 with CouchbaseException

use of com.couchbase.client.core.error.CouchbaseException 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)

Example 45 with CouchbaseException

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

the class EnhancedPreparedStatementStrategy method prepareAndExecute.

private Mono<QueryResponse> prepareAndExecute(QueryRequest request) {
    return // auto-execute!
    executeAdhoc(request.toPrepareRequest(true, requestTracer())).flatMap(queryResponse -> {
        // intercept the response and pluck out the prepared statement name
        String preparedName = queryResponse.header().prepared().orElse(null);
        if (preparedName == null) {
            return Mono.error(new CouchbaseException("Failed to locate prepared statement name in query response; this is a query bug!"));
        }
        cache.put(request.statement(), PreparedStatement.enhanced(preparedName));
        return Mono.just(queryResponse);
    });
}
Also used : CouchbaseException(com.couchbase.client.core.error.CouchbaseException)

Aggregations

CouchbaseException (com.couchbase.client.core.error.CouchbaseException)46 DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)8 Test (org.junit.jupiter.api.Test)8 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)7 CasMismatchException (com.couchbase.client.core.error.CasMismatchException)7 RetryExhaustedException (com.couchbase.client.core.retry.reactor.RetryExhaustedException)7 LookupInResult (com.couchbase.client.java.kv.LookupInResult)7 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)7 Duration (java.time.Duration)7 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)6 ArrayList (java.util.ArrayList)6 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)5 RateLimitedException (com.couchbase.client.core.error.RateLimitedException)5 IndexNotFoundException (com.couchbase.client.core.error.IndexNotFoundException)4 UnambiguousTimeoutException (com.couchbase.client.core.error.UnambiguousTimeoutException)4 PathNotFoundException (com.couchbase.client.core.error.subdoc.PathNotFoundException)4 JsonObject (com.couchbase.client.java.json.JsonObject)4 SearchIndex (com.couchbase.client.java.manager.search.SearchIndex)4 List (java.util.List)4 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)3