DATACASS-33 - reduced all of SimpleCassandraRepository's methods to one-liners
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package org.springframework.cassandra.core.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CollectionUtils {
|
||||
|
||||
public static <T> List<T> toList(Iterable<T> i) {
|
||||
|
||||
List<T> list = null;
|
||||
if (i instanceof List) {
|
||||
list = (List<T>) i;
|
||||
} else {
|
||||
list = new ArrayList<T>();
|
||||
for (T t : i) {
|
||||
list.add(t);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -461,4 +461,6 @@ public interface CassandraOperations extends CqlOperations {
|
||||
void deleteById(Class<?> clazz, Object id);
|
||||
|
||||
<T> List<T> selectByIds(Class<T> clazz, Iterable<?> ids);
|
||||
|
||||
<T> List<T> selectAll(Class<T> clazz);
|
||||
}
|
||||
|
||||
@@ -142,8 +142,12 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
|
||||
|
||||
@Override
|
||||
public <T> void delete(List<T> entities) {
|
||||
|
||||
Assert.notEmpty(entities);
|
||||
|
||||
String tableName = getTableName(entities.get(0).getClass());
|
||||
Assert.notNull(tableName);
|
||||
|
||||
delete(entities, tableName);
|
||||
}
|
||||
|
||||
@@ -394,13 +398,32 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation
|
||||
return insert(tableName, entity, options, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> selectAll(Class<T> selectClass) {
|
||||
|
||||
Assert.notNull(selectClass);
|
||||
|
||||
CassandraPersistentEntity<?> entity = mappingContext.getPersistentEntity(selectClass);
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException(String.format("unknown persistent class [%s]", selectClass.getName()));
|
||||
}
|
||||
return select(QueryBuilder.select().all().from(entity.getTableName()), selectClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> select(Select cql, Class<T> selectClass) {
|
||||
|
||||
Assert.notNull(cql);
|
||||
|
||||
return select(cql.getQueryString(), selectClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> select(String cql, Class<T> selectClass) {
|
||||
|
||||
Assert.hasText(cql);
|
||||
Assert.notNull(selectClass);
|
||||
|
||||
return select(cql, new ReadRowCallback<T>(cassandraConverter, selectClass));
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.springframework.data.cassandra.mapping.CassandraPersistentProperty;
|
||||
import org.springframework.data.cassandra.repository.query.CassandraEntityInformation;
|
||||
import org.springframework.data.mapping.model.BeanWrapper;
|
||||
import org.springframework.data.repository.core.support.AbstractEntityInformation;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link CassandraEntityInformation} implementation using a {@link CassandraPersistentEntity} instance to lookup the
|
||||
@@ -63,17 +64,14 @@ public class MappingCassandraEntityInformation<T, ID extends Serializable> exten
|
||||
@Override
|
||||
public ID getId(T entity) {
|
||||
|
||||
CassandraPersistentProperty idProperty = entityMetadata.getIdProperty();
|
||||
Assert.notNull(entity);
|
||||
|
||||
CassandraPersistentProperty idProperty = entityMetadata.getIdProperty();
|
||||
if (idProperty == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return (ID) BeanWrapper.create(entity, null).getProperty(idProperty);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return (ID) BeanWrapper.create(entity, null).getProperty(idProperty);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -16,34 +16,26 @@
|
||||
package org.springframework.data.cassandra.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.cassandra.core.CqlOperations;
|
||||
import org.springframework.data.cassandra.core.CassandraOperations;
|
||||
import org.springframework.cassandra.core.util.CollectionUtils;
|
||||
import org.springframework.data.cassandra.core.CassandraTemplate;
|
||||
import org.springframework.data.cassandra.repository.CassandraRepository;
|
||||
import org.springframework.data.cassandra.repository.query.CassandraEntityInformation;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.datastax.driver.core.querybuilder.Clause;
|
||||
import com.datastax.driver.core.querybuilder.Delete;
|
||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
|
||||
import com.datastax.driver.core.querybuilder.Select;
|
||||
import com.datastax.driver.core.querybuilder.Select.Where;
|
||||
|
||||
/**
|
||||
* Repository base implementation for Cassandra.
|
||||
*
|
||||
* @author Alex Shvid
|
||||
*
|
||||
* @author Matthew T. Adams
|
||||
*/
|
||||
|
||||
public class SimpleCassandraRepository<T, ID extends Serializable> implements CassandraRepository<T, ID> {
|
||||
|
||||
protected final CassandraTemplate cassandraTemplate;
|
||||
protected final CassandraEntityInformation<T, ID> entityInformation;
|
||||
protected CassandraTemplate template;
|
||||
protected CassandraEntityInformation<T, ID> entityInformation;
|
||||
|
||||
/**
|
||||
* Creates a new {@link SimpleCassandraRepository} for the given {@link CassandraEntityInformation} and
|
||||
@@ -52,147 +44,71 @@ public class SimpleCassandraRepository<T, ID extends Serializable> implements Ca
|
||||
* @param metadata must not be {@literal null}.
|
||||
* @param template must not be {@literal null}.
|
||||
*/
|
||||
public SimpleCassandraRepository(CassandraEntityInformation<T, ID> metadata, CassandraTemplate cassandraTemplate) {
|
||||
public SimpleCassandraRepository(CassandraEntityInformation<T, ID> metadata, CassandraTemplate template) {
|
||||
|
||||
Assert.notNull(cassandraTemplate);
|
||||
Assert.notNull(template);
|
||||
Assert.notNull(metadata);
|
||||
|
||||
this.entityInformation = metadata;
|
||||
this.cassandraTemplate = cassandraTemplate;
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public <S extends T> S save(S entity) {
|
||||
|
||||
Assert.notNull(entity, "Entity must not be null!");
|
||||
cassandraTemplate.insert(entity, entityInformation.getTableName());
|
||||
return entity;
|
||||
return template.insert(entity, entityInformation.getTableName());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
|
||||
*/
|
||||
@Override
|
||||
public <S extends T> List<S> save(Iterable<S> entities) {
|
||||
|
||||
Assert.notNull(entities, "The given Iterable of entities not be null!");
|
||||
|
||||
List<S> result = new ArrayList<S>();
|
||||
|
||||
for (S entity : entities) {
|
||||
save(entity);
|
||||
result.add(entity);
|
||||
}
|
||||
|
||||
return result;
|
||||
return template.insert(CollectionUtils.toList(entities));
|
||||
}
|
||||
|
||||
@Override
|
||||
public T findOne(ID id) {
|
||||
return cassandraTemplate.selectOneById(entityInformation.getJavaType(), id);
|
||||
return template.selectOneById(entityInformation.getJavaType(), id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists(ID id) {
|
||||
return cassandraTemplate.countById(entityInformation.getJavaType(), id) >= 1; // TODO: == instead of >= ?
|
||||
return template.countById(entityInformation.getJavaType(), id) >= 1; // TODO: == instead of >= ?
|
||||
}
|
||||
|
||||
@Override
|
||||
public long count() {
|
||||
return cassandraTemplate.count(entityInformation.getTableName());
|
||||
return template.count(entityInformation.getTableName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(ID id) {
|
||||
cassandraTemplate.deleteById(entityInformation.getJavaType(), id);
|
||||
template.deleteById(entityInformation.getJavaType(), id);
|
||||
}
|
||||
|
||||
/*
|
||||
* (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!");
|
||||
delete(entityInformation.getId(entity));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
|
||||
*/
|
||||
@Override
|
||||
public void delete(Iterable<? extends T> entities) {
|
||||
|
||||
Assert.notNull(entities, "The given Iterable of entities not be null!");
|
||||
|
||||
for (T entity : entities) {
|
||||
delete(entity);
|
||||
}
|
||||
template.delete(CollectionUtils.toList(entities));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#deleteAll()
|
||||
*/
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
cassandraTemplate.truncate(entityInformation.getTableName());
|
||||
template.truncate(entityInformation.getTableName());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#findAll()
|
||||
*/
|
||||
@Override
|
||||
public List<T> findAll() {
|
||||
Select select = QueryBuilder.select().all().from(entityInformation.getTableName());
|
||||
return findAll(select);
|
||||
return template.selectAll(entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<T> findAll(Iterable<ID> ids) {
|
||||
|
||||
return cassandraTemplate.selectByIds(entityInformation.getJavaType(), ids);
|
||||
return template.selectByIds(entityInformation.getJavaType(), ids);
|
||||
}
|
||||
|
||||
protected List<T> findAll(Select query) {
|
||||
|
||||
if (query == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return cassandraTemplate.select(query, entityInformation.getJavaType());
|
||||
return template.select(query, entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the underlying {@link CqlOperations} instance.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected CqlOperations getCassandraOperations() {
|
||||
return this.cassandraTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the underlying {@link CassandraOperations} instance.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected CassandraOperations getCassandraDataOperations() {
|
||||
return this.cassandraTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the entityInformation
|
||||
*/
|
||||
protected CassandraEntityInformation<T, ID> getEntityInformation() {
|
||||
return entityInformation;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user