Skip to content

Commit c205141

Browse files
committed
DATACOUCH-623 - Add replace() method to CouchbaseRepository for CAS usage.
1 parent e72cdba commit c205141

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

src/main/java/org/springframework/data/couchbase/repository/CouchbaseRepository.java

+4
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,8 @@ public interface CouchbaseRepository<T, ID> extends PagingAndSortingRepository<T
4343
@Override
4444
List<T> findAllById(Iterable<ID> iterable);
4545

46+
<S extends T> S replace(S var1);
47+
48+
<S extends T> Iterable<S> replaceAll(Iterable<S> var1);
49+
4650
}

src/main/java/org/springframework/data/couchbase/repository/support/SimpleCouchbaseRepository.java

+15
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ public <S extends T> Iterable<S> saveAll(final Iterable<S> entities) {
8686
.all(Streamable.of(entities).toList());
8787
}
8888

89+
@Override
90+
@SuppressWarnings("unchecked")
91+
public <S extends T> S replace(final S entity) {
92+
Assert.notNull(entity, "Entity must not be null!");
93+
return (S) couchbaseOperations.replaceById(entityInformation.getJavaType()).one(entity);
94+
}
95+
96+
@Override
97+
@SuppressWarnings("unchecked")
98+
public <S extends T> Iterable<S> replaceAll(final Iterable<S> entities) {
99+
Assert.notNull(entities, "The given Iterable of entities must not be null!");
100+
return (Iterable<S>) couchbaseOperations.replaceById(entityInformation.getJavaType())
101+
.all(Streamable.of(entities).toList());
102+
}
103+
89104
@Override
90105
public Optional<T> findById(final ID id) {
91106
Assert.notNull(id, "The given id must not be null!");

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.List;
2020

21+
import com.couchbase.client.java.json.JsonArray;
22+
import org.springframework.data.couchbase.repository.CouchbaseRepository;
2123
import org.springframework.data.couchbase.repository.Query;
2224
import org.springframework.data.repository.PagingAndSortingRepository;
2325
import org.springframework.data.repository.query.Param;
@@ -30,10 +32,14 @@
3032
* @author Michael Reiche
3133
*/
3234
@Repository
33-
public interface UserRepository extends PagingAndSortingRepository<User, String> {
35+
public interface UserRepository extends CouchbaseRepository<User, String> {
3436

3537
List<User> findByFirstname(String firstname);
3638

39+
List<User> findByFirstnameIn(String... firstnames);
40+
41+
List<User> findByFirstnameIn(JsonArray firstnames);
42+
3743
List<User> findByFirstnameAndLastname(String firstname, String lastname);
3844

3945
@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and firstname = $1 and lastname = $2")

src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
*/
1616

1717
package org.springframework.data.couchbase.repository;
18-
18+
import java.util.HashSet;
19+
import java.util.UUID;
1920
import static org.junit.jupiter.api.Assertions.*;
2021

2122
import java.util.ArrayList;
2223
import java.util.Arrays;
2324
import java.util.List;
2425
import java.util.Locale;
26+
import java.util.Set;
2527
import java.util.concurrent.Callable;
2628
import java.util.concurrent.ExecutorService;
2729
import java.util.concurrent.Executors;
@@ -31,10 +33,13 @@
3133
import org.junit.jupiter.api.Test;
3234
import org.springframework.beans.factory.annotation.Autowired;
3335
import org.springframework.context.annotation.Configuration;
36+
import org.springframework.dao.DataIntegrityViolationException;
3437
import org.springframework.data.couchbase.CouchbaseClientFactory;
3538
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
3639
import org.springframework.data.couchbase.domain.Airport;
3740
import org.springframework.data.couchbase.domain.AirportRepository;
41+
import org.springframework.data.couchbase.domain.User;
42+
import org.springframework.data.couchbase.domain.UserRepository;
3843
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
3944
import org.springframework.data.couchbase.util.Capabilities;
4045
import org.springframework.data.couchbase.util.ClusterAwareIntegrationTests;
@@ -58,6 +63,9 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr
5863

5964
@Autowired AirportRepository airportRepository;
6065

66+
@Autowired UserRepository userRepository;
67+
68+
6169
@BeforeEach
6270
void beforeEach() {
6371
try {
@@ -118,6 +126,17 @@ void findBySimpleProperty() {
118126

119127
}
120128

129+
@Test
130+
public void testCas() {
131+
132+
User user = new User("1","Dave","Wilson");
133+
userRepository.save(user);
134+
user.setVersion(user.getVersion() - 1);
135+
assertThrows(DataIntegrityViolationException.class, () -> userRepository.replace(user));
136+
userRepository.delete(user);
137+
138+
}
139+
121140
@Test
122141
void count() {
123142
String[] iatas = { "JFK", "IAD", "SFO", "SJC", "SEA", "LAX", "PHX" };

0 commit comments

Comments
 (0)