Skip to content

Commit

Permalink
Handle list argument for findById, deletedById for DynamicInvocationH…
Browse files Browse the repository at this point in the history
…andler. (#1940)

Closes #1939.
  • Loading branch information
mikereiche committed May 6, 2024
1 parent 33bf8c8 commit de2db4b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
.map(o -> o == null ? null : (o.getClass() == entityInformation.getJavaType() ? Object.class : o.getClass()))
.toArray(Class<?>[]::new);
// the CouchbaseRepository methods - findById(id) etc - will have a parameter type of Object instead of ID
if (method.getName().endsWith("ById") && args.length == 1) {
// but deleteByAllId first parameter will be an iterable.
if (method.getName().endsWith("ById") && args.length == 1 && ! Iterable.class.isAssignableFrom(paramTypes[0]) ) {
paramTypes[0] = Object.class;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public <S extends T> S save(S entity) {
String scopeName = getScope();
String collectionName = getCollection();
// clear out the PseudoArgs here as whatever is called by operations.save() could be in a different thread.
// not that this will also clear out Options, but that's ok as any options would not work
// note that this will also clear out Options, but that's ok as any options would not work
// with all of insert/upsert/replace. If Options are needed, use template.insertById/upsertById/replaceById
getReactiveTemplate().setPseudoArgs(null);
return operations.save(entity, scopeName, collectionName);
Expand All @@ -82,7 +82,13 @@ public <S extends T> S save(S entity) {
@Override
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
Assert.notNull(entities, "The given Iterable of entities must not be null!");
return Streamable.of(entities).stream().map((e) -> save(e)).collect(StreamUtils.toUnmodifiableList());
String scopeName = getScope();
String collectionName = getCollection();
// clear out the PseudoArgs here as whatever is called by operations.save() could be in a different thread.
// note that this will also clear out Options, but that's ok as any options would not work
// with all of insert/upsert/replace. If Options are needed, use template.insertById/upsertById/replaceById
getReactiveTemplate().setPseudoArgs(null);
return Streamable.of(entities).stream().map((e) -> operations.save(e,scopeName, collectionName)).collect(StreamUtils.toUnmodifiableList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public <S extends T> Mono<S> save(S entity) {
String scopeName = getScope();
String collectionName = getCollection();
// clear out the PseudoArgs here as whatever is called by operations.save() could be in a different thread.
// not that this will also clear out Options, but that's ok as any options would not work
// note that this will also clear out Options, but that's ok as any options would not work
// with all of insert/upsert/replace. If Options are needed, use template.insertById/upsertById/replaceById
getReactiveTemplate().setPseudoArgs(null);
return operations.save(entity, scopeName, collectionName);
Expand All @@ -88,6 +88,10 @@ public <S extends T> Flux<S> saveAll(Iterable<S> entities) {
Assert.notNull(entities, "The given Iterable of entities must not be null!");
String scope = getScope();
String collection = getCollection();
// clear out the PseudoArgs here as whatever is called by operations.save() could be in a different thread.
// note that this will also clear out Options, but that's ok as any options would not work
// with all of insert/upsert/replace. If Options are needed, use template.insertById/upsertById/replaceById
getReactiveTemplate().setPseudoArgs(null);
return Flux.fromIterable(entities).flatMap(e -> save(e, scope, collection));
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import static com.couchbase.client.core.io.CollectionIdentifier.DEFAULT_SCOPE;
import static com.couchbase.client.java.query.QueryScanConsistency.REQUEST_PLUS;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down Expand Up @@ -443,4 +445,23 @@ void stringDeleteWithMethodAnnotationTest() {
}
}


@Test // DATACOUCH-650, SDC-1939
void deleteAllById() {

Airport vienna = new Airport("airports::vie", "vie", "LOWW");
Airport frankfurt = new Airport("airports::fra", "fra", "EDDZ");
Airport losAngeles = new Airport("airports::lax", "lax", "KLAX");
AirportRepository ar = airportRepository.withScope(scopeName).withCollection(collectionName);
try {
ar.saveAll(asList(vienna, frankfurt, losAngeles));
List<Airport> airports = ar.findAllById(asList(vienna.getId(), losAngeles.getId()));
assertEquals(2, airports.size());
ar.deleteAllById(asList(vienna.getId(), losAngeles.getId()));
assertThat(ar.findAll()).containsExactly(frankfurt);
ar.deleteAll(asList(frankfurt));
} finally {
ar.deleteAll();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
*/
package org.springframework.data.couchbase.repository.query;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.springframework.data.couchbase.domain.AirportRepository;
import reactor.core.Disposable;

import java.util.List;
Expand Down Expand Up @@ -298,4 +301,23 @@ void stringDeleteWithMethodAnnotationTest() {
}
}

@Test // DATACOUCH-650, SDC-1939
void deleteAllById() {

Airport vienna = new Airport("airports::vie", "vie", "LOWW");
Airport frankfurt = new Airport("airports::fra", "fra", "EDDZ");
Airport losAngeles = new Airport("airports::lax", "lax", "KLAX");
ReactiveAirportRepository ar = reactiveAirportRepository.withScope(scopeName).withCollection(collectionName);
try {
ar.saveAll(asList(vienna, frankfurt, losAngeles)).blockLast();
List<Airport> airports = ar.findAllById(asList(vienna.getId(), losAngeles.getId())).collectList().block();
assertEquals(2, airports.size());
ar.deleteAllById(asList(vienna.getId(), losAngeles.getId())).block();
assertThat(ar.findAll().collectList().block()).containsExactly(frankfurt);
ar.deleteAll(asList(frankfurt)).block();
} finally {
ar.deleteAll().block();
}
}

}

0 comments on commit de2db4b

Please sign in to comment.