DATACASS-825 - Polishing.
Reoder methods. Remove unused code and imports. Original pull request: #181.
This commit is contained in:
@@ -96,7 +96,6 @@ class FindByIdQuery {
|
||||
if (id instanceof MapId) {
|
||||
|
||||
MapId mapId = (MapId) id;
|
||||
Iterator<String> iterator = mapId.keySet().iterator();
|
||||
|
||||
if (mapId.size() > 1) {
|
||||
return true;
|
||||
|
||||
@@ -22,7 +22,6 @@ import java.util.Collections;
|
||||
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;
|
||||
@@ -36,8 +35,6 @@ import org.springframework.data.domain.Slice;
|
||||
import org.springframework.data.mapping.context.AbstractMappingContext;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.datastax.oss.driver.api.querybuilder.insert.Insert;
|
||||
|
||||
/**
|
||||
* Repository base implementation for Cassandra.
|
||||
*
|
||||
@@ -75,6 +72,10 @@ public class SimpleCassandraRepository<T, ID> implements CassandraRepository<T,
|
||||
this.mappingContext = operations.getConverter().getMappingContext();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Methods from CrudRepository
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#save(S)
|
||||
*/
|
||||
@@ -112,6 +113,134 @@ public class SimpleCassandraRepository<T, ID> implements CassandraRepository<T,
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#findById(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public Optional<T> findById(ID id) {
|
||||
|
||||
Assert.notNull(id, "The given id must not be null");
|
||||
|
||||
return Optional.ofNullable(doFindOne(id));
|
||||
}
|
||||
|
||||
private T doFindOne(ID id) {
|
||||
return this.operations.selectOneById(id, this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#existsById(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean existsById(ID id) {
|
||||
|
||||
Assert.notNull(id, "The given id must not be null");
|
||||
|
||||
return this.operations.exists(id, this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#findAll()
|
||||
*/
|
||||
@Override
|
||||
public List<T> findAll() {
|
||||
return this.operations.select(Query.empty(), this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
|
||||
*/
|
||||
@Override
|
||||
public List<T> findAllById(Iterable<ID> ids) {
|
||||
|
||||
Assert.notNull(ids, "The given Iterable of id's must not be null");
|
||||
|
||||
if (!ids.iterator().hasNext()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return this.operations.select(createIdsInQuery(ids), this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#count()
|
||||
*/
|
||||
@Override
|
||||
public long count() {
|
||||
return this.operations.count(this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#deleteById(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public void deleteById(ID id) {
|
||||
|
||||
Assert.notNull(id, "The given id must not be null");
|
||||
|
||||
this.operations.deleteById(id, this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public void delete(T entity) {
|
||||
|
||||
Assert.notNull(entity, "The given entity must not be null");
|
||||
|
||||
deleteById(this.entityInformation.getRequiredId(entity));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#deleteAllById(java.lang.Iterable)
|
||||
*/
|
||||
@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(java.lang.Iterable)
|
||||
*/
|
||||
@Override
|
||||
public void deleteAll(Iterable<? extends T> entities) {
|
||||
|
||||
Assert.notNull(entities, "The given Iterable of entities must not be null");
|
||||
|
||||
entities.forEach(this.operations::delete);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#deleteAll()
|
||||
*/
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
this.operations.truncate(this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Methods from CassandraRepository
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.repository.CassandraRepository#findAll(org.springframework.data.domain.Pageable)
|
||||
*/
|
||||
@Override
|
||||
public Slice<T> findAll(Pageable pageable) {
|
||||
|
||||
Assert.notNull(pageable, "Pageable must not be null");
|
||||
|
||||
return this.operations.slice(Query.empty().pageRequest(pageable), this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.repository.TypedIdCassandraRepository#insert(java.lang.Object)
|
||||
*/
|
||||
@@ -140,128 +269,8 @@ public class SimpleCassandraRepository<T, ID> implements CassandraRepository<T,
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#findById(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public Optional<T> findById(ID id) {
|
||||
|
||||
Assert.notNull(id, "The given id must not be null");
|
||||
|
||||
return Optional.ofNullable(doFindOne(id));
|
||||
}
|
||||
|
||||
private T doFindOne(ID id) {
|
||||
return this.operations.selectOneById(id, this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#existsById(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean existsById(ID id) {
|
||||
|
||||
Assert.notNull(id, "The given id must not be null");
|
||||
|
||||
return this.operations.exists(id, this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#count()
|
||||
*/
|
||||
@Override
|
||||
public long count() {
|
||||
return this.operations.count(this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#findAll()
|
||||
*/
|
||||
@Override
|
||||
public List<T> findAll() {
|
||||
return this.operations.select(Query.empty(), this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
|
||||
*/
|
||||
@Override
|
||||
public List<T> findAllById(Iterable<ID> ids) {
|
||||
|
||||
Assert.notNull(ids, "The given Iterable of id's must not be null");
|
||||
|
||||
if (!ids.iterator().hasNext()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return this.operations.select(createIdsInQuery(ids), this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.repository.CassandraRepository#findAll(org.springframework.data.domain.Pageable)
|
||||
*/
|
||||
@Override
|
||||
public Slice<T> findAll(Pageable pageable) {
|
||||
|
||||
Assert.notNull(pageable, "Pageable must not be null");
|
||||
|
||||
return this.operations.slice(Query.empty().pageRequest(pageable), this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#deleteById(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public void deleteById(ID id) {
|
||||
|
||||
Assert.notNull(id, "The given id must not be null");
|
||||
|
||||
this.operations.deleteById(id, this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public void delete(T entity) {
|
||||
|
||||
Assert.notNull(entity, "The given entity must not be null");
|
||||
|
||||
deleteById(this.entityInformation.getRequiredId(entity));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#deleteAll(java.lang.Iterable)
|
||||
*/
|
||||
@Override
|
||||
public void deleteAll(Iterable<? extends T> entities) {
|
||||
|
||||
Assert.notNull(entities, "The given Iterable of entities must not be null");
|
||||
|
||||
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();
|
||||
|
||||
@@ -17,7 +17,6 @@ package org.springframework.data.cassandra.repository.support;
|
||||
|
||||
import static org.springframework.data.cassandra.core.query.Criteria.*;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@@ -72,6 +71,10 @@ public class SimpleReactiveCassandraRepository<T, ID> implements ReactiveCassand
|
||||
this.mappingContext = operations.getConverter().getMappingContext();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Methods from ReactiveCrudRepository
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#save(S)
|
||||
*/
|
||||
@@ -114,46 +117,28 @@ public class SimpleReactiveCassandraRepository<T, ID> implements ReactiveCassand
|
||||
return Flux.from(entityStream).flatMap(this::save);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.repository.ReactiveCassandraRepository#insert(java.lang.Object)
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#findById(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public <S extends T> Mono<S> insert(S entity) {
|
||||
public Mono<T> findById(ID id) {
|
||||
|
||||
Assert.notNull(entity, "Entity must not be null");
|
||||
Assert.notNull(id, "The given id must not be null");
|
||||
|
||||
return this.operations.insert(entity);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.repository.ReactiveCassandraRepository#insert(java.lang.Iterable)
|
||||
*/
|
||||
@Override
|
||||
public <S extends T> Flux<S> insert(Iterable<S> entities) {
|
||||
|
||||
Assert.notNull(entities, "The given Iterable of entities must not be null");
|
||||
|
||||
return Flux.fromIterable(entities).flatMap(this.operations::insert);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.repository.ReactiveCassandraRepository#insert(org.reactivestreams.Publisher)
|
||||
*/
|
||||
@Override
|
||||
public <S extends T> Flux<S> insert(Publisher<S> entityStream) {
|
||||
|
||||
Assert.notNull(entityStream, "The given Publisher of entities must not be null");
|
||||
|
||||
return Flux.from(entityStream).flatMap(this.operations::insert);
|
||||
return this.operations.selectOneById(id, this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#count()
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#findById(org.reactivestreams.Publisher)
|
||||
*/
|
||||
@Override
|
||||
public Mono<Long> count() {
|
||||
return this.operations.count(this.entityInformation.getJavaType());
|
||||
public Mono<T> findById(Publisher<ID> publisher) {
|
||||
|
||||
Assert.notNull(publisher, "The Publisher of ids must not be null");
|
||||
|
||||
return Mono.from(publisher).flatMap(this::findById);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -180,30 +165,6 @@ public class SimpleReactiveCassandraRepository<T, ID> implements ReactiveCassand
|
||||
return Mono.from(publisher).flatMap(this::existsById);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#findById(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public Mono<T> findById(ID id) {
|
||||
|
||||
Assert.notNull(id, "The given id must not be null");
|
||||
|
||||
return this.operations.selectOneById(id, this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#findById(org.reactivestreams.Publisher)
|
||||
*/
|
||||
@Override
|
||||
public Mono<T> findById(Publisher<ID> publisher) {
|
||||
|
||||
Assert.notNull(publisher, "The Publisher of ids must not be null");
|
||||
|
||||
return Mono.from(publisher).flatMap(this::findById);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#findAll()
|
||||
@@ -247,14 +208,11 @@ public class SimpleReactiveCassandraRepository<T, ID> implements ReactiveCassand
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#delete(java.lang.Object)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#count()
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> delete(T entity) {
|
||||
|
||||
Assert.notNull(entity, "The given entity must not be null");
|
||||
|
||||
return this.operations.delete(entity).then();
|
||||
public Mono<Long> count() {
|
||||
return this.operations.count(this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -279,29 +237,25 @@ public class SimpleReactiveCassandraRepository<T, ID> implements ReactiveCassand
|
||||
return Mono.from(publisher).flatMap(this::deleteById).then();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll()
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#delete(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> deleteAll() {
|
||||
return this.operations.truncate(this.entityInformation.getJavaType());
|
||||
public Mono<Void> delete(T entity) {
|
||||
|
||||
Assert.notNull(entity, "The given entity must not be null");
|
||||
|
||||
return this.operations.delete(entity).then();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll(java.lang.Iterable)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAllById(java.lang.Iterable)
|
||||
*/
|
||||
@Override
|
||||
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.operations::delete).then();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> deleteAllById(Iterable<? extends ID> ids) {
|
||||
|
||||
Assert.notNull(ids, "The given Iterable of entities must not be null");
|
||||
Assert.notNull(ids, "The given Iterable of ids must not be null");
|
||||
|
||||
if (FindByIdQuery.hasCompositeKeys(ids)) {
|
||||
return deleteById(Flux.fromIterable(ids));
|
||||
@@ -314,6 +268,17 @@ public class SimpleReactiveCassandraRepository<T, ID> implements ReactiveCassand
|
||||
return this.operations.delete(createIdsInCollectionQuery(ids), this.entityInformation.getJavaType()).then();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll(java.lang.Iterable)
|
||||
*/
|
||||
@Override
|
||||
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.operations::delete).then();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll(org.reactivestreams.Publisher)
|
||||
*/
|
||||
@@ -325,6 +290,51 @@ public class SimpleReactiveCassandraRepository<T, ID> implements ReactiveCassand
|
||||
return Flux.from(entityStream).flatMap(this.operations::delete).then();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll()
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> deleteAll() {
|
||||
return this.operations.truncate(this.entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Methods from ReactiveCrudRepository
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.repository.ReactiveCassandraRepository#insert(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public <S extends T> Mono<S> insert(S entity) {
|
||||
|
||||
Assert.notNull(entity, "Entity must not be null");
|
||||
|
||||
return this.operations.insert(entity);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.repository.ReactiveCassandraRepository#insert(java.lang.Iterable)
|
||||
*/
|
||||
@Override
|
||||
public <S extends T> Flux<S> insert(Iterable<S> entities) {
|
||||
|
||||
Assert.notNull(entities, "The given Iterable of entities must not be null");
|
||||
|
||||
return Flux.fromIterable(entities).flatMap(this.operations::insert);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.repository.ReactiveCassandraRepository#insert(org.reactivestreams.Publisher)
|
||||
*/
|
||||
@Override
|
||||
public <S extends T> Flux<S> insert(Publisher<S> entityStream) {
|
||||
|
||||
Assert.notNull(entityStream, "The given Publisher of entities must not be null");
|
||||
|
||||
return Flux.from(entityStream).flatMap(this.operations::insert);
|
||||
}
|
||||
|
||||
private Query createIdsInCollectionQuery(Iterable<? extends ID> ids) {
|
||||
|
||||
FindByIdQuery query = FindByIdQuery.forIds(ids);
|
||||
|
||||
@@ -20,6 +20,7 @@ import static org.junit.Assume.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
@@ -359,7 +360,7 @@ public class SimpleCassandraRepositoryIntegrationTests extends IntegrationTestsS
|
||||
@Test // DATACASS-825
|
||||
void deleteAllByIdShouldRemoveEntity() {
|
||||
|
||||
repository.deleteAllById(Arrays.asList(dave.getId()));
|
||||
repository.deleteAllById(Collections.singletonList(dave.getId()));
|
||||
|
||||
Optional<User> loaded = repository.findById(dave.getId());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user