DATACOUCH-595 - Add cas to replace options if present on entity.

This commit is contained in:
mikereiche
2020-07-28 16:17:50 -07:00
parent 5fff02849c
commit 2b62059550
4 changed files with 31 additions and 4 deletions

View File

@@ -96,6 +96,21 @@ class CouchbaseTemplateSupport implements ApplicationContextAware {
}
}
public long getCas(final Object entity) {
final ConvertingPropertyAccessor<Object> accessor = getPropertyAccessor(entity);
final CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entity.getClass());
final CouchbasePersistentProperty versionProperty = persistentEntity.getVersionProperty();
long cas = 0;
if (versionProperty != null) {
Object casObject = (Number)accessor.getProperty(versionProperty);
if (casObject instanceof Number){
cas = ((Number)casObject).longValue();
}
}
return cas;
}
public String getJavaNameForEntity(final Class<?> clazz) {
final CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(clazz);
MappingCouchbaseEntityInformation<?, Object> info = new MappingCouchbaseEntityInformation<>(persistentEntity);

View File

@@ -68,8 +68,8 @@ public class ReactiveReplaceByIdOperationSupport implements ReactiveReplaceByIdO
return Mono.just(object).flatMap(o -> {
CouchbaseDocument converted = template.support().encodeEntity(o);
return template.getCollection(collection).reactive()
.replace(converted.getId(), converted.export(), buildReplaceOptions()).map(result -> {
template.support().applyUpdatedCas(object, result.cas());
.replace(converted.getId(), converted.export(), buildReplaceOptions(o)).map(result -> {
template.support().applyUpdatedCas(o, result.cas());
return o;
});
}).onErrorMap(throwable -> {
@@ -86,13 +86,15 @@ public class ReactiveReplaceByIdOperationSupport implements ReactiveReplaceByIdO
return Flux.fromIterable(objects).flatMap(this::one);
}
private ReplaceOptions buildReplaceOptions() {
private ReplaceOptions buildReplaceOptions(T object) {
final ReplaceOptions options = ReplaceOptions.replaceOptions();
if (persistTo != PersistTo.NONE || replicateTo != ReplicateTo.NONE) {
options.durability(persistTo, replicateTo);
} else if (durabilityLevel != DurabilityLevel.NONE) {
options.durability(durabilityLevel);
}
long cas = template.support().getCas(object);
options.cas(cas);
return options;
}

View File

@@ -32,7 +32,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.DataIntegrityViolationException;;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.couchbase.CouchbaseClientFactory;
import org.springframework.data.couchbase.SimpleCouchbaseClientFactory;
@@ -78,6 +78,12 @@ class CouchbaseTemplateKeyValueIntegrationTests extends ClusterAwareIntegrationT
User modified = couchbaseTemplate.upsertById(User.class).one(user);
assertEquals(user, modified);
modified = couchbaseTemplate.replaceById(User.class).one(user);
assertEquals(user, modified);
user.setVersion(12345678);
assertThrows(DataIntegrityViolationException.class, () -> couchbaseTemplate.replaceById(User.class).one(user));
User found = couchbaseTemplate.findById(User.class).one(user.getId());
assertEquals(user, found);

View File

@@ -85,6 +85,10 @@ public class User {
return version;
}
public void setVersion(long version) {
this.version = version;
}
@Override
public boolean equals(Object o) {
if (this == o)