Skip to content

Commit de2db4b

Browse files
authored
Handle list argument for findById, deletedById for DynamicInvocationHandler. (#1940)
Closes #1939.
1 parent 33bf8c8 commit de2db4b

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
110110
.map(o -> o == null ? null : (o.getClass() == entityInformation.getJavaType() ? Object.class : o.getClass()))
111111
.toArray(Class<?>[]::new);
112112
// the CouchbaseRepository methods - findById(id) etc - will have a parameter type of Object instead of ID
113-
if (method.getName().endsWith("ById") && args.length == 1) {
113+
// but deleteByAllId first parameter will be an iterable.
114+
if (method.getName().endsWith("ById") && args.length == 1 && ! Iterable.class.isAssignableFrom(paramTypes[0]) ) {
114115
paramTypes[0] = Object.class;
115116
}
116117
}

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public <S extends T> S save(S entity) {
7373
String scopeName = getScope();
7474
String collectionName = getCollection();
7575
// clear out the PseudoArgs here as whatever is called by operations.save() could be in a different thread.
76-
// not that this will also clear out Options, but that's ok as any options would not work
76+
// note that this will also clear out Options, but that's ok as any options would not work
7777
// with all of insert/upsert/replace. If Options are needed, use template.insertById/upsertById/replaceById
7878
getReactiveTemplate().setPseudoArgs(null);
7979
return operations.save(entity, scopeName, collectionName);
@@ -82,7 +82,13 @@ public <S extends T> S save(S entity) {
8282
@Override
8383
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
8484
Assert.notNull(entities, "The given Iterable of entities must not be null!");
85-
return Streamable.of(entities).stream().map((e) -> save(e)).collect(StreamUtils.toUnmodifiableList());
85+
String scopeName = getScope();
86+
String collectionName = getCollection();
87+
// clear out the PseudoArgs here as whatever is called by operations.save() could be in a different thread.
88+
// note that this will also clear out Options, but that's ok as any options would not work
89+
// with all of insert/upsert/replace. If Options are needed, use template.insertById/upsertById/replaceById
90+
getReactiveTemplate().setPseudoArgs(null);
91+
return Streamable.of(entities).stream().map((e) -> operations.save(e,scopeName, collectionName)).collect(StreamUtils.toUnmodifiableList());
8692
}
8793

8894
@Override

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public <S extends T> Mono<S> save(S entity) {
7777
String scopeName = getScope();
7878
String collectionName = getCollection();
7979
// clear out the PseudoArgs here as whatever is called by operations.save() could be in a different thread.
80-
// not that this will also clear out Options, but that's ok as any options would not work
80+
// note that this will also clear out Options, but that's ok as any options would not work
8181
// with all of insert/upsert/replace. If Options are needed, use template.insertById/upsertById/replaceById
8282
getReactiveTemplate().setPseudoArgs(null);
8383
return operations.save(entity, scopeName, collectionName);
@@ -88,6 +88,10 @@ public <S extends T> Flux<S> saveAll(Iterable<S> entities) {
8888
Assert.notNull(entities, "The given Iterable of entities must not be null!");
8989
String scope = getScope();
9090
String collection = getCollection();
91+
// clear out the PseudoArgs here as whatever is called by operations.save() could be in a different thread.
92+
// note that this will also clear out Options, but that's ok as any options would not work
93+
// with all of insert/upsert/replace. If Options are needed, use template.insertById/upsertById/replaceById
94+
getReactiveTemplate().setPseudoArgs(null);
9195
return Flux.fromIterable(entities).flatMap(e -> save(e, scope, collection));
9296
}
9397

@@ -96,6 +100,10 @@ public <S extends T> Flux<S> saveAll(Publisher<S> entityStream) {
96100
Assert.notNull(entityStream, "The given Iterable of entities must not be null!");
97101
String scope = getScope();
98102
String collection = getCollection();
103+
// clear out the PseudoArgs here as whatever is called by operations.save() could be in a different thread.
104+
// note that this will also clear out Options, but that's ok as any options would not work
105+
// with all of insert/upsert/replace. If Options are needed, use template.insertById/upsertById/replaceById
106+
getReactiveTemplate().setPseudoArgs(null);
99107
return Flux.from(entityStream).flatMap(e -> save(e, scope, collection));
100108
}
101109

src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQueryCollectionIntegrationTests.java

+21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import static com.couchbase.client.core.io.CollectionIdentifier.DEFAULT_SCOPE;
1919
import static com.couchbase.client.java.query.QueryScanConsistency.REQUEST_PLUS;
20+
import static java.util.Arrays.asList;
21+
import static org.assertj.core.api.Assertions.assertThat;
2022
import static org.junit.jupiter.api.Assertions.assertEquals;
2123
import static org.junit.jupiter.api.Assertions.assertThrows;
2224

@@ -443,4 +445,23 @@ void stringDeleteWithMethodAnnotationTest() {
443445
}
444446
}
445447

448+
449+
@Test // DATACOUCH-650, SDC-1939
450+
void deleteAllById() {
451+
452+
Airport vienna = new Airport("airports::vie", "vie", "LOWW");
453+
Airport frankfurt = new Airport("airports::fra", "fra", "EDDZ");
454+
Airport losAngeles = new Airport("airports::lax", "lax", "KLAX");
455+
AirportRepository ar = airportRepository.withScope(scopeName).withCollection(collectionName);
456+
try {
457+
ar.saveAll(asList(vienna, frankfurt, losAngeles));
458+
List<Airport> airports = ar.findAllById(asList(vienna.getId(), losAngeles.getId()));
459+
assertEquals(2, airports.size());
460+
ar.deleteAllById(asList(vienna.getId(), losAngeles.getId()));
461+
assertThat(ar.findAll()).containsExactly(frankfurt);
462+
ar.deleteAll(asList(frankfurt));
463+
} finally {
464+
ar.deleteAll();
465+
}
466+
}
446467
}

src/test/java/org/springframework/data/couchbase/repository/query/ReactiveCouchbaseRepositoryQueryCollectionIntegrationTests.java

+22
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
*/
1616
package org.springframework.data.couchbase.repository.query;
1717

18+
import static java.util.Arrays.asList;
19+
import static org.assertj.core.api.Assertions.assertThat;
1820
import static org.junit.jupiter.api.Assertions.assertEquals;
1921
import static org.junit.jupiter.api.Assertions.assertThrows;
2022

23+
import org.springframework.data.couchbase.domain.AirportRepository;
2124
import reactor.core.Disposable;
2225

2326
import java.util.List;
@@ -298,4 +301,23 @@ void stringDeleteWithMethodAnnotationTest() {
298301
}
299302
}
300303

304+
@Test // DATACOUCH-650, SDC-1939
305+
void deleteAllById() {
306+
307+
Airport vienna = new Airport("airports::vie", "vie", "LOWW");
308+
Airport frankfurt = new Airport("airports::fra", "fra", "EDDZ");
309+
Airport losAngeles = new Airport("airports::lax", "lax", "KLAX");
310+
ReactiveAirportRepository ar = reactiveAirportRepository.withScope(scopeName).withCollection(collectionName);
311+
try {
312+
ar.saveAll(asList(vienna, frankfurt, losAngeles)).blockLast();
313+
List<Airport> airports = ar.findAllById(asList(vienna.getId(), losAngeles.getId())).collectList().block();
314+
assertEquals(2, airports.size());
315+
ar.deleteAllById(asList(vienna.getId(), losAngeles.getId())).block();
316+
assertThat(ar.findAll().collectList().block()).containsExactly(frankfurt);
317+
ar.deleteAll(asList(frankfurt)).block();
318+
} finally {
319+
ar.deleteAll().block();
320+
}
321+
}
322+
301323
}

0 commit comments

Comments
 (0)