Skip to content

DATAMONGO-2652 - Implements deleteAllById. #892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-DATAMONGO-2652-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand All @@ -26,7 +26,7 @@
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>2.5.0-SNAPSHOT</springdata.commons>
<springdata.commons>2.4.0-DATACMNS-800-SNAPSHOT</springdata.commons>
<mongo>4.1.1</mongo>
<mongo.reactivestreams>${mongo}</mongo.reactivestreams>
<jmh.version>1.19</jmh.version>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-DATAMONGO-2652-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-DATAMONGO-2652-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-DATAMONGO-2652-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* @author Thomas Darimont
* @author Mark Paluch
* @author Mehran Behnam
* @author Jens Schauder
*/
public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {

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

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

entities.forEach(this::delete);
}

@Override
public void deleteAllById(Iterable<? extends ID> ids) {

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

ids.forEach(this::deleteById);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use a single query here. Going to apply that change during the merge.

}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#deleteAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

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

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -40,13 +38,17 @@

import com.mongodb.client.result.DeleteResult;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
* Reactive repository base implementation for Mongo.
*
* @author Mark Paluch
* @author Oliver Gierke
* @author Christoph Strobl
* @author Ruben J Garcia
* @author Jens Schauder
* @since 2.0
*/
public class SimpleReactiveMongoRepository<T, ID extends Serializable> implements ReactiveMongoRepository<T, ID> {
Expand Down Expand Up @@ -401,7 +403,28 @@ public Mono<Void> deleteAll(Iterable<? extends T> entities) {

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

return Flux.fromIterable(entities).flatMap(this::delete).then();
Collection<?> idCollection = StreamUtils.createStreamFromIterator(entities.iterator())
.map(entityInformation::getId)
.collect(Collectors.toList());

Criteria idsInCriteria = where(entityInformation.getIdAttribute()).in(idCollection);

return mongoOperations
.remove(new Query(idsInCriteria), entityInformation.getJavaType(), entityInformation.getCollectionName())
.then();
}

@Override
public Mono<Void> deleteAllById(Iterable<? extends ID> ids) {

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

Collection<?> idCollection = StreamUtils.createStreamFromIterator(ids.iterator()).collect(Collectors.toList());
Criteria idsInCriteria = where(entityInformation.getIdAttribute()).in(idCollection);

return mongoOperations
.remove(new Query(idsInCriteria), entityInformation.getJavaType(), entityInformation.getCollectionName())
.then();
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.mongodb.repository;

import static java.util.Arrays.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.domain.Sort.Direction.*;
import static org.springframework.data.mongodb.core.query.Criteria.*;
Expand Down Expand Up @@ -66,7 +67,6 @@
import org.springframework.data.mongodb.test.util.MongoTestUtils;
import org.springframework.data.querydsl.ReactiveQuerydslPredicateExecutor;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
import org.springframework.data.repository.query.ReactiveQueryMethodEvaluationContextProvider;
import org.springframework.test.context.junit.jupiter.SpringExtension;

Expand All @@ -77,10 +77,12 @@
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Jens Schauder
*/
@ExtendWith({ MongoClientExtension.class, SpringExtension.class })
public class ReactiveMongoRepositoryTests {

public static final int PERSON_COUNT = 7;
static @Client MongoClient mongoClient;

@Autowired ReactiveMongoTemplate template;
Expand Down Expand Up @@ -154,17 +156,17 @@ public void setUp() throws Exception {
dave = new Person("Dave", "Matthews", 42);
oliver = new Person("Oliver August", "Matthews", 4);
carter = new Person("Carter", "Beauford", 49);
carter.setSkills(Arrays.asList("Drums", "percussion", "vocals"));
carter.setSkills(asList("Drums", "percussion", "vocals"));
Thread.sleep(10);
boyd = new Person("Boyd", "Tinsley", 45);
boyd.setSkills(Arrays.asList("Violin", "Electric Violin", "Viola", "Mandolin", "Vocals", "Guitar"));
boyd.setSkills(asList("Violin", "Electric Violin", "Viola", "Mandolin", "Vocals", "Guitar"));
stefan = new Person("Stefan", "Lessard", 34);
leroi = new Person("Leroi", "Moore", 41);

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

repository.saveAll(Arrays.asList(oliver, carter, boyd, stefan, leroi, alicia, dave)).as(StepVerifier::create) //
.expectNextCount(7) //
repository.saveAll(asList(oliver, carter, boyd, stefan, leroi, alicia, dave)).as(StepVerifier::create) //
.expectNextCount(PERSON_COUNT) //
.verifyComplete();
}

Expand Down Expand Up @@ -411,7 +413,7 @@ public void considersRepositoryCollectionName() {

leroi.id = null;
boyd.id = null;
contactRepository.saveAll(Arrays.asList(leroi, boyd)) //
contactRepository.saveAll(asList(leroi, boyd)) //
.as(StepVerifier::create) //
.expectNextCount(2) //
.verifyComplete();
Expand All @@ -430,7 +432,7 @@ public void considersRepositoryCollectionName() {
@Test // DATAMONGO-2182
public void shouldFindPersonsWhenUsingQueryDslPerdicatedOnIdProperty() {

repository.findAll(person.id.in(Arrays.asList(dave.id, carter.id))) //
repository.findAll(person.id.in(asList(dave.id, carter.id))) //
.collectList() //
.as(StepVerifier::create) //
.assertNext(actual -> {
Expand Down Expand Up @@ -468,7 +470,7 @@ public void annotatedAggregationWithPlaceholderValue() {
.contains(new PersonAggregate("Tinsley", "Boyd")) //
.contains(new PersonAggregate("Beauford", "Carter")) //
.contains(new PersonAggregate("Moore", "Leroi")) //
.contains(new PersonAggregate("Matthews", Arrays.asList("Dave", "Oliver August")));
.contains(new PersonAggregate("Matthews", asList("Dave", "Oliver August")));
}).verifyComplete();
}

Expand All @@ -484,7 +486,7 @@ public void annotatedAggregationWithSort() {
new PersonAggregate("Beauford", "Carter"), //
new PersonAggregate("Keys", "Alicia"), //
new PersonAggregate("Lessard", "Stefan"), //
new PersonAggregate("Matthews", Arrays.asList("Dave", "Oliver August")), //
new PersonAggregate("Matthews", asList("Dave", "Oliver August")), //
new PersonAggregate("Moore", "Leroi"), //
new PersonAggregate("Tinsley", "Boyd"));
}) //
Expand All @@ -501,7 +503,7 @@ public void annotatedAggregationWithPageable() {
assertThat(actual) //
.containsExactly( //
new PersonAggregate("Lessard", "Stefan"), //
new PersonAggregate("Matthews", Arrays.asList("Dave", "Oliver August")));
new PersonAggregate("Matthews", asList("Dave", "Oliver August")));
}) //
.verifyComplete();
}
Expand Down Expand Up @@ -576,7 +578,7 @@ public void annotatedAggregationSkipsEmptyDocumentsWhenExtractingSimpleValue() {
Person p3 = new Person(firstname, null);
p3.setEmail("p3@example.com");

repository.saveAll(Arrays.asList(p1, p2, p3)).then().as(StepVerifier::create).verifyComplete();
repository.saveAll(asList(p1, p2, p3)).then().as(StepVerifier::create).verifyComplete();

repository.projectToLastnameAndRemoveId(firstname) //
.as(StepVerifier::create) //
Expand Down Expand Up @@ -617,6 +619,18 @@ public void deleteByShouldAllowSingleDocumentRemovalCorrectly() {
.verifyComplete();
}

@Test // DATAMONGO-2652
public void deleteAllById() {

repository.deleteAllById(asList(carter.id, dave.id)) //
.as(StepVerifier::create) //
.verifyComplete();

repository.count().as(StepVerifier::create) //
.expectNext(PERSON_COUNT - 2L) //
.verifyComplete();
}

interface ReactivePersonRepository
extends ReactiveMongoRepository<Person, String>, ReactiveQuerydslPredicateExecutor<Person> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package org.springframework.data.mongodb.repository.support;

import static java.util.Arrays.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.domain.ExampleMatcher.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -58,6 +58,7 @@
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
* @author Jens Schauder
*/
@ExtendWith({ MongoTemplateExtension.class, MongoServerCondition.class })
public class SimpleMongoRepositoryTests {
Expand Down Expand Up @@ -85,11 +86,11 @@ public void setUp() {
leroi = new Person("Leroi", "Moore", 41);
alicia = new Person("Alicia", "Keys", 30, Sex.FEMALE);

all = repository.saveAll(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia));
all = repository.saveAll(asList(oliver, dave, carter, boyd, stefan, leroi, alicia));
}

@Test
public void findALlFromCustomCollectionName() {
public void findAllFromCustomCollectionName() {
assertThat(repository.findAll()).hasSize(all.size());
}

Expand Down Expand Up @@ -384,7 +385,7 @@ public void saveAllUsesEntityCollection() {

repository.deleteAll();

repository.saveAll(Arrays.asList(first, second));
repository.saveAll(asList(first, second));

assertThat(repository.findAll()).containsExactlyInAnyOrder(first, second);
}
Expand Down Expand Up @@ -435,6 +436,17 @@ public void existsShouldBePossibleInTransaction() {
assertThat(exists).isTrue();
}

@Test // DATAMONGO-2652
public void deleteAllByIds() {

repository.deleteAllById(asList(dave.getId(), carter.getId()));

assertThat(repository.findAll()) //
.hasSize(all.size() - 2) //
.doesNotContain(dave) //
.doesNotContain(carter);
}

private void assertThatAllReferencePersonsWereStoredCorrectly(Map<String, Person> references, List<Person> saved) {

for (Person person : saved) {
Expand Down