Skip to content

Commit 23d2f61

Browse files
committed
DATACOUCH-595 - Add cas to replace options if present on entity.
1 parent d9eefc2 commit 23d2f61

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplateSupport.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ public void applyUpdatedCas(final Object entity, final long cas) {
9696
}
9797
}
9898

99+
public long getCas(final Object entity) {
100+
final ConvertingPropertyAccessor<Object> accessor = getPropertyAccessor(entity);
101+
final CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entity.getClass());
102+
final CouchbasePersistentProperty versionProperty = persistentEntity.getVersionProperty();
103+
104+
long cas = 0;
105+
if (versionProperty != null) {
106+
Object casObject = (Number)accessor.getProperty(versionProperty);
107+
if (casObject instanceof Number){
108+
cas = ((Number)casObject).longValue();
109+
}
110+
}
111+
return cas;
112+
}
113+
99114
public String getJavaNameForEntity(final Class<?> clazz) {
100115
final CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(clazz);
101116
MappingCouchbaseEntityInformation<?, Object> info = new MappingCouchbaseEntityInformation<>(persistentEntity);

src/main/java/org/springframework/data/couchbase/core/ReactiveReplaceByIdOperationSupport.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public Mono<T> one(T object) {
6868
return Mono.just(object).flatMap(o -> {
6969
CouchbaseDocument converted = template.support().encodeEntity(o);
7070
return template.getCollection(collection).reactive()
71-
.replace(converted.getId(), converted.export(), buildReplaceOptions()).map(result -> {
72-
template.support().applyUpdatedCas(object, result.cas());
71+
.replace(converted.getId(), converted.export(), buildReplaceOptions(o)).map(result -> {
72+
template.support().applyUpdatedCas(o, result.cas());
7373
return o;
7474
});
7575
}).onErrorMap(throwable -> {
@@ -86,13 +86,15 @@ public Flux<? extends T> all(Collection<? extends T> objects) {
8686
return Flux.fromIterable(objects).flatMap(this::one);
8787
}
8888

89-
private ReplaceOptions buildReplaceOptions() {
89+
private ReplaceOptions buildReplaceOptions(T object) {
9090
final ReplaceOptions options = ReplaceOptions.replaceOptions();
9191
if (persistTo != PersistTo.NONE || replicateTo != ReplicateTo.NONE) {
9292
options.durability(persistTo, replicateTo);
9393
} else if (durabilityLevel != DurabilityLevel.NONE) {
9494
options.durability(durabilityLevel);
9595
}
96+
long cas = template.support().getCas(object);
97+
options.cas(cas);
9698
return options;
9799
}
98100

src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.junit.jupiter.api.Test;
3030
import org.springframework.context.ApplicationContext;
3131
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
32-
import org.springframework.dao.DataRetrievalFailureException;
32+
import org.springframework.dao.DataIntegrityViolationException;;
3333
import org.springframework.dao.DuplicateKeyException;
3434
import org.springframework.data.couchbase.CouchbaseClientFactory;
3535
import org.springframework.data.couchbase.SimpleCouchbaseClientFactory;
@@ -74,6 +74,12 @@ void upsertAndFindById() {
7474
User modified = couchbaseTemplate.upsertById(User.class).one(user);
7575
assertEquals(user, modified);
7676

77+
modified = couchbaseTemplate.replaceById(User.class).one(user);
78+
assertEquals(user, modified);
79+
80+
user.setVersion(12345678);
81+
assertThrows(DataIntegrityViolationException.class, () -> couchbaseTemplate.replaceById(User.class).one(user));
82+
7783
User found = couchbaseTemplate.findById(User.class).one(user.getId());
7884
assertEquals(user, found);
7985

src/test/java/org/springframework/data/couchbase/domain/User.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ public long getVersion() {
8585
return version;
8686
}
8787

88+
public void setVersion(long version) {
89+
this.version = version;
90+
}
91+
8892
@Override
8993
public boolean equals(Object o) {
9094
if (this == o)

0 commit comments

Comments
 (0)