Skip to content

Commit c1a8ffe

Browse files
schaudermp911de
authored andcommitted
DATAMONGO-2652 - Implements CrudRepository and ReactiveCrudRepository.delete(Iterable<ID> ids).
See also: DATACMNS-800. Original pull request: #892.
1 parent 5c3bb00 commit c1a8ffe

File tree

4 files changed

+78
-20
lines changed

4 files changed

+78
-20
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* @author Thomas Darimont
5252
* @author Mark Paluch
5353
* @author Mehran Behnam
54+
* @author Jens Schauder
5455
*/
5556
public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
5657

@@ -182,11 +183,19 @@ public void delete(T entity) {
182183
@Override
183184
public void deleteAll(Iterable<? extends T> entities) {
184185

185-
Assert.notNull(entities, "The given Iterable of entities not be null!");
186+
Assert.notNull(entities, "The given Iterable of entities must not be null!");
186187

187188
entities.forEach(this::delete);
188189
}
189190

191+
@Override
192+
public void deleteAllById(Iterable<? extends ID> ids) {
193+
194+
Assert.notNull(ids, "The given Iterable of ids must not be null!");
195+
196+
ids.forEach(this::deleteById);
197+
}
198+
190199
/*
191200
* (non-Javadoc)
192201
* @see org.springframework.data.repository.CrudRepository#deleteAll()

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717

1818
import static org.springframework.data.mongodb.core.query.Criteria.*;
1919

20-
import reactor.core.publisher.Flux;
21-
import reactor.core.publisher.Mono;
22-
2320
import java.io.Serializable;
21+
import java.util.Collection;
2422
import java.util.List;
2523
import java.util.stream.Collectors;
2624

@@ -40,13 +38,17 @@
4038

4139
import com.mongodb.client.result.DeleteResult;
4240

41+
import reactor.core.publisher.Flux;
42+
import reactor.core.publisher.Mono;
43+
4344
/**
4445
* Reactive repository base implementation for Mongo.
4546
*
4647
* @author Mark Paluch
4748
* @author Oliver Gierke
4849
* @author Christoph Strobl
4950
* @author Ruben J Garcia
51+
* @author Jens Schauder
5052
* @since 2.0
5153
*/
5254
public class SimpleReactiveMongoRepository<T, ID extends Serializable> implements ReactiveMongoRepository<T, ID> {
@@ -401,7 +403,28 @@ public Mono<Void> deleteAll(Iterable<? extends T> entities) {
401403

402404
Assert.notNull(entities, "The given Iterable of entities must not be null!");
403405

404-
return Flux.fromIterable(entities).flatMap(this::delete).then();
406+
Collection<?> idCollection = StreamUtils.createStreamFromIterator(entities.iterator())
407+
.map(entityInformation::getId)
408+
.collect(Collectors.toList());
409+
410+
Criteria idsInCriteria = where(entityInformation.getIdAttribute()).in(idCollection);
411+
412+
return mongoOperations
413+
.remove(new Query(idsInCriteria), entityInformation.getJavaType(), entityInformation.getCollectionName())
414+
.then();
415+
}
416+
417+
@Override
418+
public Mono<Void> deleteAllById(Iterable<? extends ID> ids) {
419+
420+
Assert.notNull(ids, "The given Iterable of ids must not be null!");
421+
422+
Collection<?> idCollection = StreamUtils.createStreamFromIterator(ids.iterator()).collect(Collectors.toList());
423+
Criteria idsInCriteria = where(entityInformation.getIdAttribute()).in(idCollection);
424+
425+
return mongoOperations
426+
.remove(new Query(idsInCriteria), entityInformation.getJavaType(), entityInformation.getCollectionName())
427+
.then();
405428
}
406429

407430
/*

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ReactiveMongoRepositoryTests.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.repository;
1717

18+
import static java.util.Arrays.*;
1819
import static org.assertj.core.api.Assertions.*;
1920
import static org.springframework.data.domain.Sort.Direction.*;
2021
import static org.springframework.data.mongodb.core.query.Criteria.*;
@@ -66,7 +67,6 @@
6667
import org.springframework.data.mongodb.test.util.MongoTestUtils;
6768
import org.springframework.data.querydsl.ReactiveQuerydslPredicateExecutor;
6869
import org.springframework.data.repository.Repository;
69-
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
7070
import org.springframework.data.repository.query.ReactiveQueryMethodEvaluationContextProvider;
7171
import org.springframework.test.context.junit.jupiter.SpringExtension;
7272

@@ -77,10 +77,12 @@
7777
*
7878
* @author Mark Paluch
7979
* @author Christoph Strobl
80+
* @author Jens Schauder
8081
*/
8182
@ExtendWith({ MongoClientExtension.class, SpringExtension.class })
8283
public class ReactiveMongoRepositoryTests {
8384

85+
public static final int PERSON_COUNT = 7;
8486
static @Client MongoClient mongoClient;
8587

8688
@Autowired ReactiveMongoTemplate template;
@@ -154,17 +156,17 @@ public void setUp() throws Exception {
154156
dave = new Person("Dave", "Matthews", 42);
155157
oliver = new Person("Oliver August", "Matthews", 4);
156158
carter = new Person("Carter", "Beauford", 49);
157-
carter.setSkills(Arrays.asList("Drums", "percussion", "vocals"));
159+
carter.setSkills(asList("Drums", "percussion", "vocals"));
158160
Thread.sleep(10);
159161
boyd = new Person("Boyd", "Tinsley", 45);
160-
boyd.setSkills(Arrays.asList("Violin", "Electric Violin", "Viola", "Mandolin", "Vocals", "Guitar"));
162+
boyd.setSkills(asList("Violin", "Electric Violin", "Viola", "Mandolin", "Vocals", "Guitar"));
161163
stefan = new Person("Stefan", "Lessard", 34);
162164
leroi = new Person("Leroi", "Moore", 41);
163165

164166
alicia = new Person("Alicia", "Keys", 30, Sex.FEMALE);
165167

166-
repository.saveAll(Arrays.asList(oliver, carter, boyd, stefan, leroi, alicia, dave)).as(StepVerifier::create) //
167-
.expectNextCount(7) //
168+
repository.saveAll(asList(oliver, carter, boyd, stefan, leroi, alicia, dave)).as(StepVerifier::create) //
169+
.expectNextCount(PERSON_COUNT) //
168170
.verifyComplete();
169171
}
170172

@@ -411,7 +413,7 @@ public void considersRepositoryCollectionName() {
411413

412414
leroi.id = null;
413415
boyd.id = null;
414-
contactRepository.saveAll(Arrays.asList(leroi, boyd)) //
416+
contactRepository.saveAll(asList(leroi, boyd)) //
415417
.as(StepVerifier::create) //
416418
.expectNextCount(2) //
417419
.verifyComplete();
@@ -430,7 +432,7 @@ public void considersRepositoryCollectionName() {
430432
@Test // DATAMONGO-2182
431433
public void shouldFindPersonsWhenUsingQueryDslPerdicatedOnIdProperty() {
432434

433-
repository.findAll(person.id.in(Arrays.asList(dave.id, carter.id))) //
435+
repository.findAll(person.id.in(asList(dave.id, carter.id))) //
434436
.collectList() //
435437
.as(StepVerifier::create) //
436438
.assertNext(actual -> {
@@ -468,7 +470,7 @@ public void annotatedAggregationWithPlaceholderValue() {
468470
.contains(new PersonAggregate("Tinsley", "Boyd")) //
469471
.contains(new PersonAggregate("Beauford", "Carter")) //
470472
.contains(new PersonAggregate("Moore", "Leroi")) //
471-
.contains(new PersonAggregate("Matthews", Arrays.asList("Dave", "Oliver August")));
473+
.contains(new PersonAggregate("Matthews", asList("Dave", "Oliver August")));
472474
}).verifyComplete();
473475
}
474476

@@ -484,7 +486,7 @@ public void annotatedAggregationWithSort() {
484486
new PersonAggregate("Beauford", "Carter"), //
485487
new PersonAggregate("Keys", "Alicia"), //
486488
new PersonAggregate("Lessard", "Stefan"), //
487-
new PersonAggregate("Matthews", Arrays.asList("Dave", "Oliver August")), //
489+
new PersonAggregate("Matthews", asList("Dave", "Oliver August")), //
488490
new PersonAggregate("Moore", "Leroi"), //
489491
new PersonAggregate("Tinsley", "Boyd"));
490492
}) //
@@ -501,7 +503,7 @@ public void annotatedAggregationWithPageable() {
501503
assertThat(actual) //
502504
.containsExactly( //
503505
new PersonAggregate("Lessard", "Stefan"), //
504-
new PersonAggregate("Matthews", Arrays.asList("Dave", "Oliver August")));
506+
new PersonAggregate("Matthews", asList("Dave", "Oliver August")));
505507
}) //
506508
.verifyComplete();
507509
}
@@ -576,7 +578,7 @@ public void annotatedAggregationSkipsEmptyDocumentsWhenExtractingSimpleValue() {
576578
Person p3 = new Person(firstname, null);
577579
p3.setEmail("[email protected]");
578580

579-
repository.saveAll(Arrays.asList(p1, p2, p3)).then().as(StepVerifier::create).verifyComplete();
581+
repository.saveAll(asList(p1, p2, p3)).then().as(StepVerifier::create).verifyComplete();
580582

581583
repository.projectToLastnameAndRemoveId(firstname) //
582584
.as(StepVerifier::create) //
@@ -617,6 +619,18 @@ public void deleteByShouldAllowSingleDocumentRemovalCorrectly() {
617619
.verifyComplete();
618620
}
619621

622+
@Test // DATAMONGO-2652
623+
public void deleteAllById() {
624+
625+
repository.deleteAllById(asList(carter.id, dave.id)) //
626+
.as(StepVerifier::create) //
627+
.verifyComplete();
628+
629+
repository.count().as(StepVerifier::create) //
630+
.expectNext(PERSON_COUNT - 2L) //
631+
.verifyComplete();
632+
}
633+
620634
interface ReactivePersonRepository
621635
extends ReactiveMongoRepository<Person, String>, ReactiveQuerydslPredicateExecutor<Person> {
622636

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.support;
1717

18+
import static java.util.Arrays.*;
1819
import static org.assertj.core.api.Assertions.*;
1920
import static org.springframework.data.domain.ExampleMatcher.*;
2021

2122
import java.util.ArrayList;
22-
import java.util.Arrays;
2323
import java.util.HashMap;
2424
import java.util.HashSet;
2525
import java.util.List;
@@ -58,6 +58,7 @@
5858
* @author Thomas Darimont
5959
* @author Christoph Strobl
6060
* @author Mark Paluch
61+
* @author Jens Schauder
6162
*/
6263
@ExtendWith({ MongoTemplateExtension.class, MongoServerCondition.class })
6364
public class SimpleMongoRepositoryTests {
@@ -85,11 +86,11 @@ public void setUp() {
8586
leroi = new Person("Leroi", "Moore", 41);
8687
alicia = new Person("Alicia", "Keys", 30, Sex.FEMALE);
8788

88-
all = repository.saveAll(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia));
89+
all = repository.saveAll(asList(oliver, dave, carter, boyd, stefan, leroi, alicia));
8990
}
9091

9192
@Test
92-
public void findALlFromCustomCollectionName() {
93+
public void findAllFromCustomCollectionName() {
9394
assertThat(repository.findAll()).hasSize(all.size());
9495
}
9596

@@ -384,7 +385,7 @@ public void saveAllUsesEntityCollection() {
384385

385386
repository.deleteAll();
386387

387-
repository.saveAll(Arrays.asList(first, second));
388+
repository.saveAll(asList(first, second));
388389

389390
assertThat(repository.findAll()).containsExactlyInAnyOrder(first, second);
390391
}
@@ -435,6 +436,17 @@ public void existsShouldBePossibleInTransaction() {
435436
assertThat(exists).isTrue();
436437
}
437438

439+
@Test // DATAMONGO-2652
440+
public void deleteAllByIds() {
441+
442+
repository.deleteAllById(asList(dave.getId(), carter.getId()));
443+
444+
assertThat(repository.findAll()) //
445+
.hasSize(all.size() - 2) //
446+
.doesNotContain(dave) //
447+
.doesNotContain(carter);
448+
}
449+
438450
private void assertThatAllReferencePersonsWereStoredCorrectly(Map<String, Person> references, List<Person> saved) {
439451

440452
for (Person person : saved) {

0 commit comments

Comments
 (0)