Search in sources :

Example 11 with DocumentNotFoundException

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

the class KeyValueErrorIntegrationTest method verifyReplaceExceptions.

@Test
void verifyReplaceExceptions() {
    DocumentNotFoundException thrown = assertThrows(DocumentNotFoundException.class, () -> collection.replace(UUID.randomUUID().toString(), "bar"));
    assertNotNull(thrown.context());
    assertThrows(InvalidArgumentException.class, () -> collection.replace("foo", null));
    assertThrows(InvalidArgumentException.class, () -> collection.replace(null, "bar"));
    assertThrows(InvalidArgumentException.class, () -> collection.replace("foo", "bar", null));
    String id = UUID.randomUUID().toString();
    MutationResult result = collection.upsert(id, "bar");
    CasMismatchException mismatch = assertThrows(CasMismatchException.class, () -> collection.replace(id, "bar", replaceOptions().cas(result.cas() + 1)));
    assertNotNull(mismatch.context());
}
Also used : DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) MutationResult(com.couchbase.client.java.kv.MutationResult) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 12 with DocumentNotFoundException

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

the class CouchbaseArrayList method remove.

@Override
public E remove(int index) {
    // fail fast on negative values, as they are interpreted as "starting from the back of the array" otherwise
    if (index < 0) {
        throw new IndexOutOfBoundsException("Index: " + index);
    }
    String idx = "[" + index + "]";
    for (int i = 0; i < arrayListOptions.casMismatchRetries(); i++) {
        try {
            // this loop will allow us to _know_ what element we really did remove.
            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)), arrayListOptions.mutateInOptions().cas(returnCas));
            return result;
        } catch (DocumentNotFoundException e) {
            // ArrayList will throw if underlying list was cleared before a remove.
            throw new IndexOutOfBoundsException("Index:" + index);
        } catch (CasMismatchException ex) {
        // will have to retry get-and-remove
        } catch (PathNotFoundException e) {
            throw new IndexOutOfBoundsException("Index: " + index);
        }
    }
    throw new CouchbaseException("CouchbaseArrayList remove failed", new RetryExhaustedException("Couldn't perform remove in less than " + arrayListOptions.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 13 with DocumentNotFoundException

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

the class CouchbaseArraySet method contains.

@Override
public boolean contains(Object t) {
    // TODO subpar implementation for a Set, use ARRAY_CONTAINS when available
    enforcePrimitive(t);
    try {
        GetResult result = collection.get(id, getOptions);
        JsonArray current = result.contentAs(JsonArray.class);
        for (Object in : current) {
            if (safeEquals(in, t)) {
                return true;
            }
        }
        return false;
    } catch (DocumentNotFoundException e) {
        return false;
    }
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) GetResult(com.couchbase.client.java.kv.GetResult) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 14 with DocumentNotFoundException

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

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

Aggregations

DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)19 Test (org.junit.jupiter.api.Test)10 CasMismatchException (com.couchbase.client.core.error.CasMismatchException)8 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)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 GetResult (com.couchbase.client.java.kv.GetResult)5 LookupInResult (com.couchbase.client.java.kv.LookupInResult)5 JsonObject (com.couchbase.client.java.json.JsonObject)4 MutationResult (com.couchbase.client.java.kv.MutationResult)3 Bucket (com.couchbase.client.java.Bucket)2 Cluster (com.couchbase.client.java.Cluster)2 Collection (com.couchbase.client.java.Collection)2 JsonArray (com.couchbase.client.java.json.JsonArray)2 ClusterAwareIntegrationTest (com.couchbase.client.test.ClusterAwareIntegrationTest)2 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)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