DATACOUCH-623 - Add replace() method to CouchbaseRepository for CAS usage. (#268)

Co-authored-by: mikereiche <michael.reiche@couchbase.com>
This commit is contained in:
Michael Reiche
2020-10-12 17:16:01 -07:00
committed by GitHub
parent 7c431d640f
commit 0ce0f36b49
6 changed files with 74 additions and 8 deletions

View File

@@ -26,6 +26,7 @@ import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.data.couchbase.repository.CouchbaseRepository;
import org.springframework.data.couchbase.repository.query.CouchbaseEntityInformation;
import static org.springframework.data.couchbase.repository.support.Util.hasNonZeroVersionProperty;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
@@ -75,15 +76,19 @@ public class SimpleCouchbaseRepository<T, ID> implements CouchbaseRepository<T,
@SuppressWarnings("unchecked")
public <S extends T> S save(final S entity) {
Assert.notNull(entity, "Entity must not be null!");
return (S) couchbaseOperations.upsertById(entityInformation.getJavaType()).one(entity);
// if entity has non-null, non-zero version property, then replace()
if (hasNonZeroVersionProperty(entity, couchbaseOperations.getConverter())) {
return (S) couchbaseOperations.replaceById(entityInformation.getJavaType()).one(entity);
} else {
return (S) couchbaseOperations.upsertById(entityInformation.getJavaType()).one(entity);
}
}
@Override
@SuppressWarnings("unchecked")
public <S extends T> Iterable<S> saveAll(final Iterable<S> entities) {
Assert.notNull(entities, "The given Iterable of entities must not be null!");
return (Iterable<S>) couchbaseOperations.upsertById(entityInformation.getJavaType())
.all(Streamable.of(entities).toList());
return Streamable.of(entities).stream().map((e) -> save(e)).collect(StreamUtils.toUnmodifiableList());
}
@Override

View File

@@ -29,6 +29,7 @@ import org.springframework.data.couchbase.core.ReactiveCouchbaseOperations;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository;
import org.springframework.data.couchbase.repository.query.CouchbaseEntityInformation;
import static org.springframework.data.couchbase.repository.support.Util.hasNonZeroVersionProperty;
import org.springframework.data.domain.Sort;
import org.springframework.data.util.Streamable;
import org.springframework.util.Assert;
@@ -75,9 +76,15 @@ public class SimpleReactiveCouchbaseRepository<T, ID> implements ReactiveCouchba
}
@SuppressWarnings("unchecked")
@Override
public <S extends T> Mono<S> save(final S entity) {
Assert.notNull(entity, "Entity must not be null!");
return (Mono<S>) operations.upsertById(entityInformation.getJavaType()).one(entity);
// if entity has non-null version property, then replace()
if (hasNonZeroVersionProperty(entity, operations.getConverter())) {
return (Mono<S>) operations.replaceById(entityInformation.getJavaType()).one(entity);
} else {
return (Mono<S>) operations.upsertById(entityInformation.getJavaType()).one(entity);
}
}
@Override
@@ -89,7 +96,7 @@ public class SimpleReactiveCouchbaseRepository<T, ID> implements ReactiveCouchba
@Override
public <S extends T> Flux<S> saveAll(final Iterable<S> entities) {
Assert.notNull(entities, "The given Iterable of entities must not be null!");
return (Flux<S>) operations.upsertById(entityInformation.getJavaType()).all(Streamable.of(entities).toList());
return Flux.fromIterable(entities).flatMap(this::save);
}
@SuppressWarnings("unchecked")

View File

@@ -0,0 +1,21 @@
package org.springframework.data.couchbase.repository.support;
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
public class Util {
public static boolean hasNonZeroVersionProperty(Object entity, CouchbaseConverter converter) {
CouchbasePersistentEntity<?> mapperEntity = converter.getMappingContext().getPersistentEntity(entity.getClass());
final CouchbasePersistentProperty versionProperty = mapperEntity.getVersionProperty();
boolean hasVersionProperty = false;
try {
if (versionProperty != null && versionProperty.getField() != null) {
Object versionValue = versionProperty.getField().get(entity);
hasVersionProperty = versionValue != null && !versionValue.equals(Long.valueOf(0));
}
} catch (IllegalAccessException iae) {}
return hasVersionProperty;
}
}