From 3ee187f89a88980ef6e3ea4ba89d22bdde441aa6 Mon Sep 17 00:00:00 2001 From: John Blum Date: Fri, 7 Jul 2017 10:54:42 -0700 Subject: [PATCH] DATACASS-474 - Polish. Resolves gh-110. --- .../core/ReactiveCassandraOperations.java | 40 +++--- .../core/ReactiveCassandraTemplate.java | 87 ++++++------- .../SimpleReactiveCassandraRepository.java | 121 ++++++++++-------- 3 files changed, 133 insertions(+), 115 deletions(-) diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraOperations.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraOperations.java index 05b6175c7..a010d8216 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraOperations.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraOperations.java @@ -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. */ - Mono selectOneById(Object id, Class entityClass) throws DataAccessException; + Mono 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 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 count(Class entityClass) throws DataAccessException; + Mono selectOneById(Object id, Class 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 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 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 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 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 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(); + } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraTemplate.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraTemplate.java index 68fdd6008..60d228b36 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraTemplate.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/ReactiveCassandraTemplate.java @@ -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 Mono selectOneById(Object id, Class entityClass) { + public Mono 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 count(Class entityClass) { + public Mono selectOneById(Object id, Class 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 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 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)); } } - } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleReactiveCassandraRepository.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleReactiveCassandraRepository.java index c9a4bab0e..415fc1071 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleReactiveCassandraRepository.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/support/SimpleReactiveCassandraRepository.java @@ -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 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 findById(ID id) { - - Assert.notNull(id, "The given id must not be null"); - - return operations.selectOneById(id, entityInformation.getJavaType()); + public Mono count() { + return operations.count(entityInformation.getJavaType()); } - /* (non-Javadoc) - * @see org.springframework.data.repository.reactive.ReactiveCrudRepository#findById(org.reactivestreams.Publisher) - */ - @Override - public Mono findById(Publisher 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 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 existsById(Publisher 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 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 findById(Publisher 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 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 findAllById(Iterable 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 findAllById(Publisher 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 count() { - return operations.count(entityInformation.getJavaType()); + public Mono 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 implements ReactiveCassand @Override public Mono deleteById(Publisher 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 delete(T entity) { - - Assert.notNull(entity, "The given entity must not be null"); - - return operations.delete(entity).then(); + public Mono deleteAll() { + return operations.truncate(entityInformation.getJavaType()); } /* (non-Javadoc) @@ -271,19 +290,13 @@ public class SimpleReactiveCassandraRepository implements ReactiveCassand return Flux.from(entityStream).flatMap(operations::delete).then(); } - /* (non-Javadoc) - * @see org.springframework.data.repository.reactive.ReactiveCrudRepository#deleteAll() - */ - @Override - public Mono deleteAll() { - return operations.truncate(entityInformation.getJavaType()); - } - private Insert createFullInsert(S entity) { CassandraConverter converter = operations.getConverter(); - CassandraPersistentEntity persistentEntity = converter.getMappingContext() - .getRequiredPersistentEntity(entity.getClass()); + + CassandraPersistentEntity persistentEntity = + converter.getMappingContext().getRequiredPersistentEntity(entity.getClass()); + Map toInsert = new LinkedHashMap<>(); converter.write(entity, toInsert, persistentEntity);