DATACASS-474 - Polish.

Resolves gh-110.
This commit is contained in:
John Blum
2017-07-07 10:54:42 -07:00
parent 0ccb64a686
commit 3ee187f89a
3 changed files with 133 additions and 115 deletions

View File

@@ -141,14 +141,13 @@ public interface ReactiveCassandraOperations {
// -------------------------------------------------------------------------
/**
* Execute the Select by {@code id} for the given {@code entityClass}.
* Returns the number of rows for the given entity class.
*
* @param id must not be {@literal null}.
* @param entityClass The entity type must not be {@literal null}.
* @return the result object returned by the action or {@link Mono#empty()}
* @param entityClass must not be {@literal null}.
* @return the number of existing entities.
* @throws DataAccessException if there is any problem issuing the execution.
*/
<T> Mono<T> selectOneById(Object id, Class<T> entityClass) throws DataAccessException;
Mono<Long> count(Class<?> entityClass) throws DataAccessException;
/**
* Determine whether the row {@code entityClass} with the given {@code id} exists.
@@ -161,13 +160,14 @@ public interface ReactiveCassandraOperations {
Mono<Boolean> exists(Object id, Class<?> entityClass) throws DataAccessException;
/**
* Returns the number of rows for the given entity class.
* Execute the Select by {@code id} for the given {@code entityClass}.
*
* @param entityClass must not be {@literal null}.
* @return the number of existing entities.
* @param id must not be {@literal null}.
* @param entityClass The entity type must not be {@literal null}.
* @return the result object returned by the action or {@link Mono#empty()}
* @throws DataAccessException if there is any problem issuing the execution.
*/
Mono<Long> count(Class<?> entityClass) throws DataAccessException;
<T> Mono<T> selectOneById(Object id, Class<T> entityClass) throws DataAccessException;
/**
* Insert the given entity and emit the entity if the insert was applied.
@@ -187,6 +187,7 @@ public interface ReactiveCassandraOperations {
* @throws DataAccessException if there is any problem issuing the execution.
*/
Mono<WriteResult> insert(Object entity, InsertOptions options) throws DataAccessException;
/**
* Update the given entity and emit the entity if the update was applied.
*
@@ -205,15 +206,6 @@ public interface ReactiveCassandraOperations {
* @throws DataAccessException if there is any problem issuing the execution.
*/
Mono<WriteResult> update(Object entity, UpdateOptions options) throws DataAccessException;
/**
* Remove the given object from the table by id.
*
* @param id must not be {@literal null}.
* @param entityClass The entity type must not be {@literal null}.
* @return {@literal true} if the deletion was applied.
* @throws DataAccessException if there is any problem issuing the execution.
*/
Mono<Boolean> deleteById(Object id, Class<?> entityClass) throws DataAccessException;
/**
* Delete the given entity and emit the entity if the delete was applied.
@@ -233,6 +225,17 @@ public interface ReactiveCassandraOperations {
* @throws DataAccessException if there is any problem issuing the execution.
*/
Mono<WriteResult> delete(Object entity, QueryOptions options) throws DataAccessException;
/**
* Remove the given object from the table by id.
*
* @param id must not be {@literal null}.
* @param entityClass The entity type must not be {@literal null}.
* @return {@literal true} if the deletion was applied.
* @throws DataAccessException if there is any problem issuing the execution.
*/
Mono<Boolean> deleteById(Object id, Class<?> entityClass) throws DataAccessException;
/**
* Execute a {@code TRUNCATE} query to remove all entities of a given class.
*
@@ -255,4 +258,5 @@ public interface ReactiveCassandraOperations {
* @see ReactiveCqlOperations
*/
ReactiveCqlOperations getReactiveCqlOperations();
}

View File

@@ -17,10 +17,10 @@ package org.springframework.data.cassandra.core;
import lombok.NonNull;
import lombok.Value;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.reactivestreams.Publisher;
import org.springframework.dao.DataAccessException;
import org.springframework.data.cassandra.core.convert.CassandraConverter;
import org.springframework.data.cassandra.core.convert.MappingCassandraConverter;
@@ -44,6 +44,8 @@ import org.springframework.data.mapping.context.MappingContext;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.reactivestreams.Publisher;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.Statement;
@@ -268,8 +270,8 @@ public class ReactiveCassandraTemplate implements ReactiveCassandraOperations {
Assert.notNull(query, "Query must not be null");
Assert.notNull(entityClass, "Entity type must not be null");
return select(getStatementFactory().select(query, getMappingContext().getRequiredPersistentEntity(entityClass)),
entityClass);
return select(getStatementFactory().select(query,
getMappingContext().getRequiredPersistentEntity(entityClass)), entityClass);
}
/* (non-Javadoc)
@@ -281,8 +283,8 @@ public class ReactiveCassandraTemplate implements ReactiveCassandraOperations {
Assert.notNull(query, "Query must not be null");
Assert.notNull(entityClass, "Entity type must not be null");
return selectOne(getStatementFactory().select(query, getMappingContext().getRequiredPersistentEntity(entityClass)),
entityClass);
return selectOne(getStatementFactory().select(query,
getMappingContext().getRequiredPersistentEntity(entityClass)), entityClass);
}
/* (non-Javadoc)
@@ -296,8 +298,8 @@ public class ReactiveCassandraTemplate implements ReactiveCassandraOperations {
Assert.notNull(update, "Update must not be null");
Assert.notNull(entityClass, "Entity type must not be null");
return getReactiveCqlOperations().execute(
getStatementFactory().update(query, update, getMappingContext().getRequiredPersistentEntity(entityClass)));
return getReactiveCqlOperations().execute(getStatementFactory().update(query, update,
getMappingContext().getRequiredPersistentEntity(entityClass)));
}
/* (non-Javadoc)
@@ -309,8 +311,8 @@ public class ReactiveCassandraTemplate implements ReactiveCassandraOperations {
Assert.notNull(query, "Query must not be null");
Assert.notNull(entityClass, "Entity type must not be null");
return getReactiveCqlOperations()
.execute(getStatementFactory().delete(query, getMappingContext().getRequiredPersistentEntity(entityClass)));
return getReactiveCqlOperations().execute(getStatementFactory().delete(query,
getMappingContext().getRequiredPersistentEntity(entityClass)));
}
// -------------------------------------------------------------------------
@@ -319,21 +321,17 @@ public class ReactiveCassandraTemplate implements ReactiveCassandraOperations {
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.ReactiveCassandraOperations#selectOneById(java.lang.Object, java.lang.Class)
* @see org.springframework.data.cassandra.core.ReactiveCassandraOperations#count(java.lang.Class)
*/
@Override
public <T> Mono<T> selectOneById(Object id, Class<T> entityClass) {
public Mono<Long> count(Class<?> entityClass) {
Assert.notNull(id, "Id must not be null");
Assert.notNull(entityClass, "Entity type must not be null");
CassandraPersistentEntity<?> entity = getMappingContext().getRequiredPersistentEntity(entityClass);
Select select = QueryBuilder.select().countAll()
.from(getMappingContext().getRequiredPersistentEntity(entityClass).getTableName().toCql());
Select select = QueryBuilder.select().all().from(entity.getTableName().toCql());
getConverter().write(id, select.where(), entity);
return selectOne(select, entityClass);
return getReactiveCqlOperations().queryForObject(select, Long.class);
}
/*
@@ -357,17 +355,21 @@ public class ReactiveCassandraTemplate implements ReactiveCassandraOperations {
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.ReactiveCassandraOperations#count(java.lang.Class)
* @see org.springframework.data.cassandra.core.ReactiveCassandraOperations#selectOneById(java.lang.Object, java.lang.Class)
*/
@Override
public Mono<Long> count(Class<?> entityClass) {
public <T> Mono<T> selectOneById(Object id, Class<T> entityClass) {
Assert.notNull(id, "Id must not be null");
Assert.notNull(entityClass, "Entity type must not be null");
Select select = QueryBuilder.select().countAll()
.from(getMappingContext().getRequiredPersistentEntity(entityClass).getTableName().toCql());
CassandraPersistentEntity<?> entity = getMappingContext().getRequiredPersistentEntity(entityClass);
return getReactiveCqlOperations().queryForObject(select, Long.class);
Select select = QueryBuilder.select().all().from(entity.getTableName().toCql());
getConverter().write(id, select.where(), entity);
return selectOne(select, entityClass);
}
/*
@@ -416,25 +418,6 @@ public class ReactiveCassandraTemplate implements ReactiveCassandraOperations {
return getReactiveCqlOperations().execute(new StatementCallback(update)).next();
}
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.ReactiveCassandraOperations#deleteById(java.lang.Object, java.lang.Class)
*/
@Override
public Mono<Boolean> deleteById(Object id, Class<?> entityClass) {
Assert.notNull(id, "Id must not be null");
Assert.notNull(entityClass, "Entity type must not be null");
CassandraPersistentEntity<?> entity = getMappingContext().getRequiredPersistentEntity(entityClass);
Delete delete = QueryBuilder.delete().from(entity.getTableName().toCql());
getConverter().write(id, delete.where(), entity);
return getReactiveCqlOperations().execute(delete);
}
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.ReactiveCassandraOperations#delete(java.lang.Object)
@@ -458,6 +441,25 @@ public class ReactiveCassandraTemplate implements ReactiveCassandraOperations {
return getReactiveCqlOperations().execute(new StatementCallback(delete)).next();
}
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.ReactiveCassandraOperations#deleteById(java.lang.Object, java.lang.Class)
*/
@Override
public Mono<Boolean> deleteById(Object id, Class<?> entityClass) {
Assert.notNull(id, "Id must not be null");
Assert.notNull(entityClass, "Entity type must not be null");
CassandraPersistentEntity<?> entity = getMappingContext().getRequiredPersistentEntity(entityClass);
Delete delete = QueryBuilder.delete().from(entity.getTableName().toCql());
getConverter().write(id, delete.where(), entity);
return getReactiveCqlOperations().execute(delete);
}
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.ReactiveCassandraOperations#truncate(java.lang.Class)
@@ -493,5 +495,4 @@ public class ReactiveCassandraTemplate implements ReactiveCassandraOperations {
.map(rows -> new WriteResult(resultSet.getAllExecutionInfo(), resultSet.wasApplied(), rows));
}
}
}

View File

@@ -15,14 +15,13 @@
*/
package org.springframework.data.cassandra.repository.support;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.data.cassandra.core.ReactiveCassandraOperations;
import org.springframework.data.cassandra.core.convert.CassandraConverter;
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
@@ -30,6 +29,8 @@ import org.springframework.data.cassandra.repository.ReactiveCassandraRepository
import org.springframework.data.cassandra.repository.query.CassandraEntityInformation;
import org.springframework.util.Assert;
import org.reactivestreams.Publisher;
import com.datastax.driver.core.querybuilder.Insert;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
@@ -132,29 +133,17 @@ public class SimpleReactiveCassandraRepository<T, ID> implements ReactiveCassand
return Flux.from(entityStream).flatMap(operations::insert);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#findById(java.lang.Object)
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#count()
*/
@Override
public Mono<T> findById(ID id) {
Assert.notNull(id, "The given id must not be null");
return operations.selectOneById(id, entityInformation.getJavaType());
public Mono<Long> count() {
return operations.count(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 given id must not be null");
return Mono.from(publisher).flatMap(id -> operations.selectOneById(id, entityInformation.getJavaType()));
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#existsById(java.lang.Object)
*/
@Override
@@ -165,55 +154,88 @@ public class SimpleReactiveCassandraRepository<T, ID> implements ReactiveCassand
return operations.exists(id, entityInformation.getJavaType());
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#existsById(org.reactivestreams.Publisher)
*/
@Override
public Mono<Boolean> existsById(Publisher<ID> publisher) {
Assert.notNull(publisher, "The given id must not be null");
Assert.notNull(publisher, "The Publisher of ids must not be null");
return Mono.from(publisher).flatMap(id -> operations.exists(id, entityInformation.getJavaType()));
return Mono.from(publisher).flatMap(this::existsById);
}
/* (non-Javadoc)
/*
* (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 operations.selectOneById(id, 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()
*/
@Override
public Flux<T> findAll() {
Select select = QueryBuilder.select().from(entityInformation.getTableName().toCql());
return operations.select(select, entityInformation.getJavaType());
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#findAllById(java.lang.Iterable)
*/
@Override
public Flux<T> findAllById(Iterable<ID> iterable) {
Assert.notNull(iterable, "The given Iterable of id's must not be null");
Assert.notNull(iterable, "The given Iterable of ids must not be null");
return findAllById(Flux.fromIterable(iterable));
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#findAllById(org.reactivestreams.Publisher)
*/
@Override
public Flux<T> findAllById(Publisher<ID> idStream) {
Assert.notNull(idStream, "The given Publisher of id's must not be null");
Assert.notNull(idStream, "The given Publisher of ids must not be null");
return Flux.from(idStream).flatMap(id -> operations.selectOneById(id, entityInformation.getJavaType()));
return Flux.from(idStream).flatMap(this::findById);
}
/* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#count()
/*
* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#delete(java.lang.Object)
*/
@Override
public Mono<Long> count() {
return operations.count(entityInformation.getJavaType());
public Mono<Void> delete(T entity) {
Assert.notNull(entity, "The given entity must not be null");
return operations.delete(entity).then();
}
/* (non-Javadoc)
@@ -233,20 +255,17 @@ public class SimpleReactiveCassandraRepository<T, ID> implements ReactiveCassand
@Override
public Mono<Void> deleteById(Publisher<ID> publisher) {
Assert.notNull(publisher, "The given id must not be null");
Assert.notNull(publisher, "The Publisher of ids must not be null");
return Mono.from(publisher).flatMap(id -> operations.deleteById(id, entityInformation.getJavaType())).then();
return Mono.from(publisher).flatMap(this::deleteById).then();
}
/* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#delete(java.lang.Object)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll()
*/
@Override
public Mono<Void> delete(T entity) {
Assert.notNull(entity, "The given entity must not be null");
return operations.delete(entity).then();
public Mono<Void> deleteAll() {
return operations.truncate(entityInformation.getJavaType());
}
/* (non-Javadoc)
@@ -271,19 +290,13 @@ public class SimpleReactiveCassandraRepository<T, ID> implements ReactiveCassand
return Flux.from(entityStream).flatMap(operations::delete).then();
}
/* (non-Javadoc)
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll()
*/
@Override
public Mono<Void> deleteAll() {
return operations.truncate(entityInformation.getJavaType());
}
private <S extends T> Insert createFullInsert(S entity) {
CassandraConverter converter = operations.getConverter();
CassandraPersistentEntity<?> persistentEntity = converter.getMappingContext()
.getRequiredPersistentEntity(entity.getClass());
CassandraPersistentEntity<?> persistentEntity =
converter.getMappingContext().getRequiredPersistentEntity(entity.getClass());
Map<String, Object> toInsert = new LinkedHashMap<>();
converter.write(entity, toInsert, persistentEntity);