Skip to content

DATACASS-825 - Implements deleteAllById(Iterable<ID> ids). #181

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 3 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 @@ -11,7 +11,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-DATACASS-825-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data for Apache Cassandra</name>
Expand Down Expand Up @@ -98,7 +98,7 @@
<hppc.version>0.5.4</hppc.version>
<multithreadedtc.version>1.01</multithreadedtc.version>
<project.type>multi</project.type>
<springdata.commons>2.5.0-SNAPSHOT</springdata.commons>
<springdata.commons>2.4.0-DATACMNS-800-SNAPSHOT</springdata.commons>
</properties>

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Optional;

import org.jetbrains.annotations.NotNull;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.core.InsertOptions;
Expand All @@ -44,6 +45,7 @@
* @author Matthew T. Adams
* @author Mark Paluch
* @author John Blum
* @author Jens Schauder
* @see org.springframework.data.cassandra.repository.CassandraRepository
*/
public class SimpleCassandraRepository<T, ID> implements CassandraRepository<T, ID> {
Expand Down Expand Up @@ -188,19 +190,11 @@ public List<T> findAllById(Iterable<ID> ids) {

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

FindByIdQuery mapIdQuery = FindByIdQuery.forIds(ids);
List<Object> idCollection = mapIdQuery.getIdCollection();
String idField = mapIdQuery.getIdProperty();

if (idCollection.isEmpty()) {
if (!ids.iterator().hasNext()) {
return Collections.emptyList();
}

if (idField == null) {
idField = this.entityInformation.getIdAttribute();
}

return this.operations.select(Query.query(where(idField).in(idCollection)), this.entityInformation.getJavaType());
return this.operations.select(createIdsInQuery(ids), this.entityInformation.getJavaType());
}

/* (non-Javadoc)
Expand Down Expand Up @@ -247,11 +241,36 @@ public void deleteAll(Iterable<? extends T> entities) {
entities.forEach(this.operations::delete);
}

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

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

if (!ids.iterator().hasNext()) {
return;
}

this.operations.delete(createIdsInQuery(ids), this.entityInformation.getJavaType());
}

/* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#deleteAll()
*/
@Override
public void deleteAll() {
this.operations.truncate(this.entityInformation.getJavaType());
}

private Query createIdsInQuery(Iterable<? extends ID> ids) {
FindByIdQuery mapIdQuery = FindByIdQuery.forIds(ids);
List<Object> idCollection = mapIdQuery.getIdCollection();
String idField = mapIdQuery.getIdProperty();

if (idField == null) {
idField = this.entityInformation.getIdAttribute();
}

return Query.query(where(idField).in(idCollection));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand All @@ -40,6 +41,7 @@
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Jens Schauder
* @since 2.0
*/
public class SimpleReactiveCassandraRepository<T, ID> implements ReactiveCassandraRepository<T, ID> {
Expand Down Expand Up @@ -224,19 +226,11 @@ public Flux<T> findAllById(Iterable<ID> ids) {
return findAllById(Flux.fromIterable(ids));
}

FindByIdQuery query = FindByIdQuery.forIds(ids);
List<Object> idCollection = query.getIdCollection();
String idField = query.getIdProperty();

if (idCollection.isEmpty()) {
if (!ids.iterator().hasNext()) {
return Flux.empty();
}

if (idField == null) {
idField = this.entityInformation.getIdAttribute();
}

return this.operations.select(Query.query(where(idField).in(idCollection)), this.entityInformation.getJavaType());
return this.operations.select(createIdsInCollectionQuery(ids), this.entityInformation.getJavaType());
}

/*
Expand Down Expand Up @@ -304,6 +298,22 @@ public Mono<Void> deleteAll(Iterable<? extends T> entities) {
return Flux.fromIterable(entities).flatMap(this.operations::delete).then();
}

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

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

if (FindByIdQuery.hasCompositeKeys(ids)) {
return deleteById(Flux.fromIterable(ids));
}

if (!ids.iterator().hasNext()) {
return Mono.empty();
}

return this.operations.delete(createIdsInCollectionQuery(ids), this.entityInformation.getJavaType()).then();
}

/* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll(org.reactivestreams.Publisher)
*/
Expand All @@ -314,4 +324,17 @@ public Mono<Void> deleteAll(Publisher<? extends T> entityStream) {

return Flux.from(entityStream).flatMap(this.operations::delete).then();
}

private Query createIdsInCollectionQuery(Iterable<? extends ID> ids) {

FindByIdQuery query = FindByIdQuery.forIds(ids);
List<Object> idCollection = query.getIdCollection();
String idField = query.getIdProperty();

if (idField == null) {
idField = this.entityInformation.getIdAttribute();
}

return Query.query(where(idField).in(idCollection));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
* Integration tests for {@link SimpleCassandraRepository}.
*
* @author Mark Paluch
* @author Jens Schauder
*/
@SpringJUnitConfig
public class SimpleCassandraRepositoryIntegrationTests extends IntegrationTestsSupport
Expand Down Expand Up @@ -355,6 +356,16 @@ void deleteByIdShouldRemoveEntity() {
assertThat(loaded).isEmpty();
}

@Test // DATACASS-825
void deleteAllByIdShouldRemoveEntity() {

repository.deleteAllById(Arrays.asList(dave.getId()));

Optional<User> loaded = repository.findById(dave.getId());

assertThat(loaded).isEmpty();
}

@Test // DATACASS-396
void deleteShouldRemoveEntity() {

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

import static org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils.*;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;

Expand Down Expand Up @@ -59,6 +60,7 @@
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Jens Schauder
*/
@SpringJUnitConfig
public class SimpleReactiveCassandraRepositoryIntegrationTests extends IntegrationTestsSupport
Expand Down Expand Up @@ -391,6 +393,17 @@ void deleteByIdUsingMonoShouldRemoveEntity() {
repository.existsById(dave.getId()).as(StepVerifier::create).expectNext(false).verifyComplete();
}

@Test // DATACASS-825
void deleteAllByIdRemovesEntities() {

insertTestData();

repository.deleteAllById(Arrays.asList(dave.getId(), carter.getId())).as(StepVerifier::create).verifyComplete();

repository.existsById(dave.getId()).as(StepVerifier::create).expectNext(false).verifyComplete();
repository.existsById(carter.getId()).as(StepVerifier::create).expectNext(false).verifyComplete();
}

@Test // DATACASS-462
void deleteByIdUsingFluxShouldRemoveFirstEntity() {

Expand Down