DATACASS-443 - Polishing.

Add author tags. Extend year range in license headers. Enhance javadoc. Reformat code. Add assertions and minor cleanups.

Original pull request: #102.
This commit is contained in:
Mark Paluch
2017-05-24 09:40:56 +02:00
parent 001dc0749e
commit 4e30a54da0
4 changed files with 64 additions and 84 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@ import org.springframework.cassandra.core.WriteOptions;
* isolation, they're not much more expensive than normal writes.
*
* @author Mark Paluch
* @author Anup Sabbi
* @since 1.5
*/
public interface CassandraBatchOperations {
@@ -71,12 +72,13 @@ public interface CassandraBatchOperations {
CassandraBatchOperations insert(Iterable<?> entities);
/**
* Add a collection of inserts with given {@code options} to the batch.
* Add a collection of inserts with given {@link WriteOptions} to the batch.
*
* @param entities the entities to insert; must not be {@literal null}.
* @param options the WriteOptions to apply.
* @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.0
*/
CassandraBatchOperations insert(Iterable<?> entities, WriteOptions options);
@@ -99,12 +101,13 @@ public interface CassandraBatchOperations {
CassandraBatchOperations update(Iterable<?> entities);
/**
* Add a collection of updates with given {@code options} to the batch.
* Add a collection of updates with given {@link WriteOptions} to the batch.
*
* @param entities the entities to update; must not be {@literal null}.
* @param options the WriteOptions to apply.
* @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.0
*/
CassandraBatchOperations update(Iterable<?> entities, WriteOptions options);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
package org.springframework.data.cassandra.core;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.cassandra.core.WriteOptions;
@@ -30,27 +29,19 @@ import com.datastax.driver.core.querybuilder.QueryBuilder;
*
* @author Mark Paluch
* @author John Blum
* @author Anup Sabbi
* @since 1.5
*/
class CassandraBatchTemplate implements CassandraBatchOperations {
private static final WriteOptions EMPTY = new WriteOptions();
private AtomicBoolean executed = new AtomicBoolean();
private final Batch batch;
private final CassandraOperations operations;
/* (non-Javadoc) */
@SafeVarargs
private static <T> Iterable<T> nullSafeIterable(T... array) {
return (array == null ? Collections.emptyList() : Arrays.asList(array));
}
/* (non-Javadoc) */
private static <T> Iterable<T> nullSafeIterable(Iterable<T> iterable) {
return (iterable != null ? iterable : Collections::emptyIterator);
}
/**
* Create a new {@link CassandraBatchTemplate} given {@link CassandraOperations}.
*
@@ -64,8 +55,7 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
this.batch = QueryBuilder.batch();
}
/*
* (non-Javadoc)
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#execute()
*/
@Override
@@ -79,8 +69,7 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
assertNotExecuted();
}
/*
* (non-Javadoc)
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#withTimestamp(long)
*/
@Override
@@ -93,34 +82,37 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
return this;
}
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#insert(Object...)
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#insert(java.lang.Object[])
*/
@Override
public CassandraBatchOperations insert(Object... entities) {
return insert(nullSafeIterable(entities));
Assert.notNull(entities, "Entities must not be null");
return insert(Arrays.asList(entities));
}
/*
* (non-Javadoc)
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#insert(java.lang.Iterable)
*/
@Override
public CassandraBatchOperations insert(Iterable<?> entities) {
return insert(entities, null);
return insert(entities, EMPTY);
}
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#insert(java.lang.Iterable, WriteOptions)
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#insert(java.lang.Iterable, org.springframework.cassandra.core.WriteOptions)
*/
@Override
public CassandraBatchOperations insert(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) {
for (Object entity : nullSafeIterable(entities)) {
Assert.notNull(entity, "Entity must not be null");
batch.add(QueryUtils.createInsertQuery(getTableName(entity), entity, options, operations.getConverter()));
}
@@ -128,34 +120,37 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
return this;
}
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#update(Object...)
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#update(java.lang.Object[])
*/
@Override
public CassandraBatchOperations update(Object... entities) {
return update(nullSafeIterable(entities));
Assert.notNull(entities, "Entities must not be null");
return update(Arrays.asList(entities));
}
/*
* (non-Javadoc)
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#update(java.lang.Iterable)
*/
@Override
public CassandraBatchOperations update(Iterable<?> entities) {
return update(entities, null);
return update(entities, EMPTY);
}
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#update(java.lang.Iterable, WriteOptions)
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#update(java.lang.Iterable, org.springframework.cassandra.core.WriteOptions)
*/
@Override
public CassandraBatchOperations update(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) {
for (Object entity : nullSafeIterable(entities)) {
Assert.notNull(entity, "Entity must not be null");
batch.add(QueryUtils.createUpdateQuery(getTableName(entity), entity, options, operations.getConverter()));
}
@@ -163,25 +158,27 @@ class CassandraBatchTemplate implements CassandraBatchOperations {
return this;
}
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#delete(Object...)
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#delete(java.lang.Object[])
*/
@Override
public CassandraBatchOperations delete(Object... entities) {
return delete(nullSafeIterable(entities));
Assert.notNull(entities, "Entities must not be null");
return delete(Arrays.asList(entities));
}
/*
* (non-Javadoc)
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraBatchOperations#delete(java.lang.Iterable)
*/
@Override
public CassandraBatchOperations delete(Iterable<?> entities) {
assertNotExecuted();
Assert.notNull(entities, "Entities must not be null");
for (Object entity : nullSafeIterable(entities)) {
for (Object entity : entities) {
Assert.notNull(entity, "Entity must not be null");
batch.add(QueryUtils.createDeleteQuery(getTableName(entity), entity, null, operations.getConverter()));
}

View File

@@ -37,11 +37,15 @@ import com.datastax.driver.core.Row;
* Integration tests for {@link CassandraBatchTemplate}.
*
* @author Mark Paluch
* @author Anup Sabbi
*/
public class CassandraBatchTemplateIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest {
CassandraTemplate template;
Group walter = new Group(new GroupKey("users", "0x1", "walter"));
Group mike = new Group(new GroupKey("users", "0x1", "mike"));
@Before
public void setUp() throws Exception {
@@ -52,14 +56,14 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractKeyspaceCrea
SchemaTestUtils.truncate(Group.class, template);
SchemaTestUtils.truncate(FlatGroup.class, template);
template.insert(walter);
template.insert(mike);
}
@Test // DATACASS-288
public void shouldInsertEntities() {
Group walter = new Group(new GroupKey("users", "0x1", "walter"));
Group mike = new Group(new GroupKey("users", "0x1", "mike"));
CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template);
batchOperations.insert(walter).insert(mike).execute();
@@ -71,9 +75,6 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractKeyspaceCrea
@Test // DATACASS-288
public void shouldInsertCollectionOfEntities() {
Group walter = new Group(new GroupKey("users", "0x1", "walter"));
Group mike = new Group(new GroupKey("users", "0x1", "mike"));
CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template);
batchOperations.insert(Arrays.asList(walter, mike)).execute();
@@ -85,14 +86,11 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractKeyspaceCrea
@Test // DATACASS-443
public void shouldInsertCollectionOfEntitiesWithTtl() {
Group walter = new Group(new GroupKey("users", "0x1", "walter"));
Group mike = new Group(new GroupKey("users", "0x1", "mike"));
walter.setEmail("walter@white.com");
mike.setEmail("mike@sauls.com");
int ttl = 30;
WriteOptions options = WriteOptions.builder().ttl(ttl).build();
WriteOptions options = WriteOptions.builder().ttl(30).build();
CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template);
batchOperations.insert(Arrays.asList(walter, mike), options).execute();
@@ -102,16 +100,13 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractKeyspaceCrea
assertThat(resultSet.getAvailableWithoutFetching()).isEqualTo(2);
for (Row row : resultSet) {
assertThat(row.getInt(0)).isBetween(1,ttl);
assertThat(row.getInt(0)).isBetween(1, ttl);
}
}
@Test // DATACASS-288
public void shouldUpdateEntities() {
Group walter = template.insert(new Group(new GroupKey("users", "0x1", "walter")));
Group mike = template.insert(new Group(new GroupKey("users", "0x1", "mike")));
walter.setEmail("walter@white.com");
mike.setEmail("mike@sauls.com");
@@ -126,9 +121,6 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractKeyspaceCrea
@Test // DATACASS-288
public void shouldUpdateCollectionOfEntities() {
Group walter = template.insert(new Group(new GroupKey("users", "0x1", "walter")));
Group mike = template.insert(new Group(new GroupKey("users", "0x1", "mike")));
walter.setEmail("walter@white.com");
mike.setEmail("mike@sauls.com");
@@ -143,9 +135,6 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractKeyspaceCrea
@Test // DATACASS-443
public void shouldUpdateCollectionOfEntitiesWithTtl() {
Group walter = template.insert(new Group(new GroupKey("users", "0x1", "walter")));
Group mike = template.insert(new Group(new GroupKey("users", "0x1", "mike")));
walter.setEmail("walter@white.com");
mike.setEmail("mike@sauls.com");
@@ -160,7 +149,7 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractKeyspaceCrea
assertThat(resultSet.getAvailableWithoutFetching()).isEqualTo(2);
for (Row row : resultSet) {
assertThat(row.getInt(0)).isBetween(1,ttl);
assertThat(row.getInt(0)).isBetween(1, ttl);
}
}
@@ -184,9 +173,6 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractKeyspaceCrea
@Test // DATACASS-288
public void shouldDeleteEntities() {
Group walter = template.insert(new Group(new GroupKey("users", "0x1", "walter")));
Group mike = template.insert(new Group(new GroupKey("users", "0x1", "mike")));
CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template);
batchOperations.delete(walter).delete(mike).execute();
@@ -199,9 +185,6 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractKeyspaceCrea
@Test // DATACASS-288
public void shouldDeleteCollectionOfEntities() {
Group walter = template.insert(new Group(new GroupKey("users", "0x1", "walter")));
Group mike = template.insert(new Group(new GroupKey("users", "0x1", "mike")));
CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template);
batchOperations.delete(Arrays.asList(walter, mike)).execute();
@@ -214,9 +197,6 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractKeyspaceCrea
@Test // DATACASS-288
public void shouldApplyTimestampToAllEntities() {
Group walter = new Group(new GroupKey("users", "0x1", "walter"));
Group mike = new Group(new GroupKey("users", "0x1", "mike"));
walter.setEmail("walter@white.com");
mike.setEmail("mike@sauls.com");
@@ -238,7 +218,7 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractKeyspaceCrea
public void shouldNotExecuteTwice() {
CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template);
batchOperations.insert(new Group(new GroupKey("users", "0x1", "walter"))).execute();
batchOperations.insert(walter).execute();
batchOperations.execute();
@@ -249,7 +229,7 @@ public class CassandraBatchTemplateIntegrationTests extends AbstractKeyspaceCrea
public void shouldNotAllowModificationAfterExecution() {
CassandraBatchOperations batchOperations = new CassandraBatchTemplate(template);
batchOperations.insert(new Group(new GroupKey("users", "0x1", "walter"))).execute();
batchOperations.insert(walter).execute();
batchOperations.update(new Group());

View File

@@ -51,7 +51,7 @@ import com.datastax.driver.core.utils.UUIDs;
*/
public class CassandraTemplateIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest {
final static Version CASSANDRA_3 = Version.parse("3.0");
static final Version CASSANDRA_3 = Version.parse("3.0");
Version cassandraVersion;