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());
}
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"));
}
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;
}
}
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"));
}
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"));
}
Aggregations