DATACASS-617 - Accept WriteOptions in imperative and reactive delete(…) through batch operations.

CassandraBatchOperations.delete(…) and ReactiveCassandraBatchOperations.delete(…) methods now accept WriteOptions and subclasses (such as DeleteOptions) to adjust delete behavior.

Related ticket: DATACASS-606.
This commit is contained in:
Mark Paluch
2019-01-28 13:51:26 +01:00
parent 005c9a7078
commit b1879b2e4c
4 changed files with 85 additions and 23 deletions

View File

@@ -80,6 +80,7 @@ public interface CassandraBatchOperations {
* @return {@code this} {@link CassandraBatchOperations}.
* @throws IllegalStateException if the batch was already executed.
* @since 2.0
* @see InsertOptions
*/
CassandraBatchOperations insert(Iterable<?> entities, WriteOptions options);
@@ -109,6 +110,7 @@ public interface CassandraBatchOperations {
* @return {@code this} {@link CassandraBatchOperations}.
* @throws IllegalStateException if the batch was already executed.
* @since 2.0
* @see UpdateOptions
*/
CassandraBatchOperations update(Iterable<?> entities, WriteOptions options);
@@ -129,4 +131,16 @@ public interface CassandraBatchOperations {
* @throws IllegalStateException if the batch was already executed.
*/
CassandraBatchOperations delete(Iterable<?> entities);
/**
* Add a collection of deletes with given {@link WriteOptions} to the batch.
*
* @param entities the entities to delete; must not be {@literal null}.
* @param options the WriteOptions to apply; must not be {@literal null}.
* @return {@code this} {@link CassandraBatchOperations}.
* @throws IllegalStateException if the batch was already executed.
* @since 2.2
* @see DeleteOptions
*/
CassandraBatchOperations delete(Iterable<?> entities, WriteOptions options);
}

View File

@@ -18,7 +18,6 @@ package org.springframework.data.cassandra.core;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.data.cassandra.core.cql.QueryOptions;
import org.springframework.data.cassandra.core.cql.WriteOptions;
import org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity;
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
@@ -118,7 +117,7 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
BasicCassandraPersistentEntity<?> persistentEntity = mappingContext
.getRequiredPersistentEntity(entity.getClass());
batch.add(QueryUtils.createInsertQuery(persistentEntity.getTableName().toCql(), entity, options,
batch.add(EntityQueryUtils.createInsertQuery(persistentEntity.getTableName().toCql(), entity, options,
operations.getConverter(), persistentEntity));
}
@@ -157,7 +156,7 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
for (Object entity : entities) {
Assert.notNull(entity, "Entity must not be null");
batch.add(QueryUtils.createUpdateQuery(getTableName(entity), entity, options, operations.getConverter()));
batch.add(EntityQueryUtils.createUpdateQuery(getTableName(entity), entity, options, operations.getConverter()));
}
return this;
@@ -179,14 +178,22 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
*/
@Override
public CassandraBatchOperations delete(Iterable<?> entities) {
return delete(entities, DeleteOptions.empty());
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#delete(java.lang.Iterable, org.springframework.data.cassandra.core.cql.WriteOptions)
*/
@Override
public CassandraBatchOperations delete(Iterable<?> entities, WriteOptions options) {
assertNotExecuted();
Assert.notNull(entities, "Entities must not be null");
Assert.notNull(options, "WriteOptions must not be null");
for (Object entity : entities) {
Assert.notNull(entity, "Entity must not be null");
batch.add(
QueryUtils.createDeleteQuery(getTableName(entity), entity, QueryOptions.empty(), operations.getConverter()));
batch.add(EntityQueryUtils.createDeleteQuery(getTableName(entity), entity, options, operations.getConverter()));
}
return this;

View File

@@ -35,6 +35,7 @@ import org.springframework.data.cassandra.core.cql.WriteOptions;
* isolation, they're not much more expensive than normal writes.
*
* @author Oleh Dokuka
* @author Mark Paluch
* @since 2.1
*/
public interface ReactiveCassandraBatchOperations {
@@ -91,11 +92,10 @@ public interface ReactiveCassandraBatchOperations {
* @param options the WriteOptions to apply; must not be {@literal null}.
* @return {@code this} {@link ReactiveCassandraBatchOperations}.
* @throws IllegalStateException if the batch was already executed.
* @since 2.0
* @see InsertOptions
*/
ReactiveCassandraBatchOperations insert(Iterable<?> entities, WriteOptions options);
/**
* Add a collection of inserts with given {@link WriteOptions} to the batch.
*
@@ -103,7 +103,7 @@ public interface ReactiveCassandraBatchOperations {
* @param options the WriteOptions to apply; must not be {@literal null}.
* @return {@code this} {@link ReactiveCassandraBatchOperations}.
* @throws IllegalStateException if the batch was already executed.
* @since 2.0
* @see InsertOptions
*/
ReactiveCassandraBatchOperations insert(Mono<? extends Iterable<?>> entities, WriteOptions options);
@@ -141,7 +141,7 @@ public interface ReactiveCassandraBatchOperations {
* @param options the WriteOptions to apply; must not be {@literal null}.
* @return {@code this} {@link ReactiveCassandraBatchOperations}.
* @throws IllegalStateException if the batch was already executed.
* @since 2.0
* @see UpdateOptions
*/
ReactiveCassandraBatchOperations update(Iterable<?> entities, WriteOptions options);
@@ -152,7 +152,7 @@ public interface ReactiveCassandraBatchOperations {
* @param options the WriteOptions to apply; must not be {@literal null}.
* @return {@code this} {@link ReactiveCassandraBatchOperations}.
* @throws IllegalStateException if the batch was already executed.
* @since 2.0
* @see UpdateOptions
*/
ReactiveCassandraBatchOperations update(Mono<? extends Iterable<?>> entities, WriteOptions options);
@@ -182,4 +182,28 @@ public interface ReactiveCassandraBatchOperations {
* @throws IllegalStateException if the batch was already executed.
*/
ReactiveCassandraBatchOperations delete(Mono<? extends Iterable<?>> entities);
/**
* Add a collection of deletes with given {@link WriteOptions} to the batch.
*
* @param entities the entities to delete; must not be {@literal null}.
* @param options the WriteOptions to apply; must not be {@literal null}.
* @return {@code this} {@link ReactiveCassandraBatchOperations}.
* @throws IllegalStateException if the batch was already executed.
* @since 2.2
* @see DeleteOptions
*/
ReactiveCassandraBatchOperations delete(Iterable<?> entities, WriteOptions options);
/**
* Add a collection of deletes with given {@link WriteOptions} to the batch.
*
* @param entities the entities to delete; must not be {@literal null}.
* @param options the WriteOptions to apply; must not be {@literal null}.
* @return {@code this} {@link ReactiveCassandraBatchOperations}.
* @throws IllegalStateException if the batch was already executed.
* @since 2.2
* @see DeleteOptions
*/
ReactiveCassandraBatchOperations delete(Mono<? extends Iterable<?>> entities, WriteOptions options);
}

View File

@@ -27,7 +27,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import org.springframework.data.cassandra.core.convert.CassandraConverter;
import org.springframework.data.cassandra.core.cql.QueryOptions;
import org.springframework.data.cassandra.core.cql.WriteOptions;
import org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity;
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
@@ -178,7 +177,7 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
BasicCassandraPersistentEntity<?> persistentEntity = mappingContext
.getRequiredPersistentEntity(entity.getClass());
insertQueries.add(QueryUtils.createInsertQuery(persistentEntity.getTableName().toCql(), entity, options,
insertQueries.add(EntityQueryUtils.createInsertQuery(persistentEntity.getTableName().toCql(), entity, options,
converter, persistentEntity));
}
@@ -251,7 +250,7 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
Assert.notNull(entity, "Entity must not be null");
updateQueries.add(QueryUtils.createUpdateQuery(getTable(entity), entity, options, converter));
updateQueries.add(EntityQueryUtils.createUpdateQuery(getTable(entity), entity, options, converter));
}
return updateQueries;
@@ -273,13 +272,7 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
*/
@Override
public ReactiveCassandraBatchOperations delete(Iterable<?> entities) {
assertNotExecuted();
Assert.notNull(entities, "Entities must not be null");
batchMonos.add(Mono.just(doDelete(entities)));
return this;
return delete(entities, DeleteOptions.empty());
}
/* (non-Javadoc)
@@ -287,16 +280,40 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
*/
@Override
public ReactiveCassandraBatchOperations delete(Mono<? extends Iterable<?>> entities) {
return delete(entities, DeleteOptions.empty());
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.ReactiveCassandraBatchOperations#delete(java.lang.Iterable, org.springframework.data.cassandra.core.cql.WriteOptions)
*/
@Override
public ReactiveCassandraBatchOperations delete(Iterable<?> entities, WriteOptions options) {
assertNotExecuted();
Assert.notNull(entities, "Entities must not be null");
Assert.notNull(options, "WriteOptions must not be null");
batchMonos.add(entities.map(this::doDelete));
batchMonos.add(Mono.just(doDelete(entities, options)));
return this;
}
private Collection<? extends BuiltStatement> doDelete(Iterable<?> entities) {
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.ReactiveCassandraBatchOperations#delete(reactor.core.publisher.Mono, org.springframework.data.cassandra.core.cql.WriteOptions)
*/
@Override
public ReactiveCassandraBatchOperations delete(Mono<? extends Iterable<?>> entities, WriteOptions options) {
assertNotExecuted();
Assert.notNull(entities, "Entities must not be null");
Assert.notNull(options, "WriteOptions must not be null");
batchMonos.add(entities.map(it -> doDelete(it, options)));
return this;
}
private Collection<? extends BuiltStatement> doDelete(Iterable<?> entities, WriteOptions options) {
List<Delete> deleteQueries = new ArrayList<>();
CassandraConverter converter = operations.getConverter();
@@ -305,7 +322,7 @@ class ReactiveCassandraBatchTemplate implements ReactiveCassandraBatchOperations
Assert.notNull(entity, "Entity must not be null");
deleteQueries.add(QueryUtils.createDeleteQuery(getTable(entity), entity, QueryOptions.empty(), converter));
deleteQueries.add(EntityQueryUtils.createDeleteQuery(getTable(entity), entity, options, converter));
}
return deleteQueries;