use of com.couchbase.client.core.error.CasMismatchException in project couchbase-jvm-clients by couchbase.
the class SubdocMutateIntegrationTest method reviveDocumentWithCAS.
@IgnoreWhen(missesCapabilities = { Capabilities.SUBDOC_REVIVE_DOCUMENT })
@Test
void reviveDocumentWithCAS() {
String docId = docId();
JsonObject body = JsonObject.create().put("foo", "bar");
MutateInResult mr = coll.mutateIn(docId, Collections.singletonList(upsert("txn", JsonObject.create().put("stgd", body).put("baz", "qux")).xattr().createPath()), MutateInOptions.mutateInOptions().createAsDeleted(true).accessDeleted(true).storeSemantics(StoreSemantics.INSERT));
// Create a CAS mismatch
coll.mutateIn(docId, Collections.singletonList(upsert("txn", JsonObject.create().put("stgd", body).put("baz", "qux")).xattr().createPath()), MutateInOptions.mutateInOptions().accessDeleted(true));
try {
coll.mutateIn(docId, Arrays.asList(new ReplaceBodyWithXattr("txn.stgd"), MutateInSpec.remove("txn").xattr()), MutateInOptions.mutateInOptions().accessDeleted(true).cas(mr.cas()).storeSemantics(StoreSemantics.REVIVE));
fail();
} catch (CasMismatchException ignored) {
}
}
use of com.couchbase.client.core.error.CasMismatchException in project couchbase-jvm-clients by couchbase.
the class SubdocIntegrationTest method subdocCASWithDurability.
// JVMCBC-728
@Test
@IgnoreWhen(missesCapabilities = Capabilities.SYNC_REPLICATION, clusterTypes = ClusterType.CAVES)
void subdocCASWithDurability() {
JsonObject initial = JsonObject.create().put("mutated", 0);
String id = UUID.randomUUID().toString();
collection.upsert(id, initial);
GetResult gr = collection.get(id);
int errorCount = 0;
try {
collection.mutateIn(id, Collections.singletonList(MutateInSpec.upsert("mutated", 1)), MutateInOptions.mutateInOptions().cas(gr.cas()).durability(DurabilityLevel.MAJORITY));
} catch (CasMismatchException err) {
errorCount += 1;
}
try {
collection.mutateIn(id, Collections.singletonList(MutateInSpec.upsert("mutated", 2)), MutateInOptions.mutateInOptions().cas(gr.cas()).durability(DurabilityLevel.MAJORITY));
} catch (CasMismatchException err) {
errorCount += 1;
}
assertEquals(1, errorCount);
}
use of com.couchbase.client.core.error.CasMismatchException 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.CasMismatchException 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.CasMismatchException 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