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 d9eefc22cb
commit 23d2f61d45
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;
}