wip: Defined all CassnadraOperations.

This commit is contained in:
dwebb
2013-11-13 15:56:31 -05:00
parent 20123c5348
commit 1b12877f04
3 changed files with 395 additions and 65 deletions

View File

@@ -0,0 +1,18 @@
/**
* All BrightMove Code is Copyright 2004-2013 BrightMove Inc.
* Modification of code without the express written consent of
* BrightMove, Inc. is strictly forbidden.
*
* Author: David Webb (dwebb@brightmove.com)
* Created On: Nov 13, 2013
*/
package org.springframework.data.cassandra;
/**
* @author David Webb (dwebb@brightmove.com)
*
*/
public interface Constants {
}

View File

@@ -21,7 +21,6 @@ import org.springframework.data.cassandra.convert.CassandraConverter;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.TableMetadata;
/**
* @author Alex Shvid
@@ -43,14 +42,6 @@ public interface CassandraOperations {
*/
String getTableName(Class<?> entityClass);
/**
* Get the metatdata for the given entityClass table mapping
*
* @param entityClass
* @return The table metadata
*/
TableMetadata getTableMetadata(Class<?> entityClass, final String tableName);
/**
* Execute query and return Cassandra ResultSet
*
@@ -90,21 +81,126 @@ public interface CassandraOperations {
*
* @param object
*/
void insert(Object entity);
<T> T insert(T entity);
/**
* Insert the given object to the table by id.
*
* @param object
*/
void insert(Object entity, String tableName);
<T> List<T> insert(List<T> entities);
/**
* Insert the given object to the table by id.
*
* @param object
*/
<T> T insert(T entity, String tableName);
/**
* Insert the given object to the table by id.
*
* @param object
*/
<T> List<T> insert(List<T> entities, String tableName);
/**
* Insert the given object to the table by id.
*
* @param object
*/
<T> T insertAsynchronously(T entity);
/**
* Insert the given object to the table by id.
*
* @param object
*/
<T> List<T> insertAsynchronously(List<T> entities);
/**
* Insert the given object to the table by id.
*
* @param object
*/
<T> T insertAsynchronously(T entity, String tableName);
/**
* Insert the given object to the table by id.
*
* @param object
*/
<T> List<T> insertAsynchronously(List<T> entities, String tableName);
/**
* Insert the given object to the table by id.
*
* @param object
*/
<T> T update(T entity);
/**
* Insert the given object to the table by id.
*
* @param object
*/
<T> List<T> update(List<T> entities);
/**
* Insert the given object to the table by id.
*
* @param object
*/
<T> T update(T entity, String tableName);
/**
* Insert the given object to the table by id.
*
* @param object
*/
<T> List<T> update(List<T> entities, String tableName);
/**
* Insert the given object to the table by id.
*
* @param object
*/
<T> T updateAsynchronously(T entity);
/**
* Insert the given object to the table by id.
*
* @param object
*/
<T> List<T> updateAsynchronously(List<T> entities);
/**
* Insert the given object to the table by id.
*
* @param object
*/
<T> T updateAsynchronously(T entity, String tableName);
/**
* Insert the given object to the table by id.
*
* @param object
*/
<T> List<T> updateAsynchronously(List<T> entities, String tableName);
/**
* Remove the given object from the table by id.
*
* @param object
*/
void delete(Object object);
<T> void delete(T entity);
/**
* Remove the given object from the table by id.
*
* @param object
*/
<T> void delete(List<T> entities);
/**
* Removes the given object from the given table.
@@ -112,7 +208,45 @@ public interface CassandraOperations {
* @param object
* @param table must not be {@literal null} or empty.
*/
void delete(Object object, String tableName);
<T> void delete(T entity, String tableName);
/**
* Removes the given object from the given table.
*
* @param object
* @param table must not be {@literal null} or empty.
*/
<T> void delete(List<T> entities, String tableName);
/**
* Remove the given object from the table by id.
*
* @param object
*/
<T> void deleteAsychronously(T entity);
/**
* Remove the given object from the table by id.
*
* @param object
*/
<T> void deleteAsychronously(List<T> entities);
/**
* Removes the given object from the given table.
*
* @param object
* @param table must not be {@literal null} or empty.
*/
<T> void deleteAsychronously(T entity, String tableName);
/**
* Removes the given object from the given table.
*
* @param object
* @param table must not be {@literal null} or empty.
*/
<T> void deleteAsychronously(List<T> entities, String tableName);
/**
* Returns the underlying {@link CassandraConverter}.

View File

@@ -46,16 +46,18 @@ import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.TableMetadata;
import com.datastax.driver.core.exceptions.NoHostAvailableException;
/**
* The Cassandra Template is a convenience API for all Cassnadta DML Operations.
*
* @author Alex Shvid
* @author David Webb
*/
public class CassandraTemplate implements CassandraOperations {
private static Logger log = LoggerFactory.getLogger(CassandraTemplate.class);
private static final Collection<String> ITERABLE_CLASSES;
public static final Collection<String> ITERABLE_CLASSES;
static {
Set<String> iterableClasses = new HashSet<String>();
@@ -64,15 +66,14 @@ public class CassandraTemplate implements CassandraOperations {
iterableClasses.add(Iterator.class.getName());
ITERABLE_CLASSES = Collections.unmodifiableCollection(iterableClasses);
}
private final Keyspace keyspace;
private final Session session;
private final CassandraConverter cassandraConverter;
private final MappingContext<? extends CassandraPersistentEntity<?>, CassandraPersistentProperty> mappingContext;
private final PersistenceExceptionTranslator exceptionTranslator = new CassandraExceptionTranslator();
private ClassLoader beanClassLoader;
/**
@@ -278,6 +279,11 @@ public class CassandraTemplate implements CassandraOperations {
}
}
/**
* @param query
* @param readRowCallback
* @return
*/
<T> T selectOneInternal(String query, ReadRowCallback<T> readRowCallback) {
try {
ResultSet resultSet = session.execute(query);
@@ -338,17 +344,17 @@ public class CassandraTemplate implements CassandraOperations {
* Insert a row into a Cassandra CQL Table
*
* @param tableName
* @param objectToSave
* @param entity
*/
protected <T> T doInsert(final String tableName, final T objectToSave) {
protected <T> T doInsert(final String tableName, final T entity) {
CassandraPersistentEntity<?> entity = getEntity(objectToSave);
CassandraPersistentEntity<?> CPEntity = getEntity(entity);
Assert.notNull(entity);
Assert.notNull(CPEntity);
try {
final Query q = CqlUtils.toInsertQuery(keyspace.getKeyspace(), tableName, objectToSave, entity);
final Query q = CqlUtils.toInsertQuery(keyspace.getKeyspace(), tableName, entity, CPEntity);
log.info(q.toString());
return execute(new SessionCallback<T>() {
@@ -357,7 +363,7 @@ public class CassandraTemplate implements CassandraOperations {
s.execute(q);
return objectToSave;
return entity;
}
});
@@ -369,22 +375,6 @@ public class CassandraTemplate implements CassandraOperations {
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.lang.Object)
*/
public void insert(Object objectToSave) {
ensureNotIterable(objectToSave);
insert(objectToSave, determineTableName(objectToSave));
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.lang.Object, java.lang.String)
*/
public void insert(Object objectToSave, String tableName) {
ensureNotIterable(objectToSave);
doInsert(tableName, objectToSave);
}
/**
* Verify the object is not an iterable type
*
@@ -398,30 +388,6 @@ public class CassandraTemplate implements CassandraOperations {
}
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#remove(java.lang.Object)
*/
@Override
public void delete(Object object) {
delete(object, determineTableName(object.getClass()));
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#remove(java.lang.Object, java.lang.String)
*/
@Override
public void delete(Object object, String tableName) {
CassandraPersistentEntity<?> entityClass = getEntity(object);
Assert.notNull(entityClass);
doRemove(object, tableName);
}
/**
* Perform the removal of a Row.
*
@@ -474,11 +440,223 @@ public class CassandraTemplate implements CassandraOperations {
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#getTableMetadata(java.lang.Class, java.lang.String)
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.lang.Object)
*/
@Override
public TableMetadata getTableMetadata(Class<?> entityClass, String tableName) {
public <T> T insert(T entity) {
ensureNotIterable(entity);
return insert(entity, determineTableName(entity));
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.util.List)
*/
@Override
public <T> List<T> insert(List<T> entities) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.lang.Object, java.lang.String)
*/
@Override
public <T> T insert(T entity, String tableName) {
ensureNotIterable(entity);
return doInsert(tableName, entity);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insert(java.util.List, java.lang.String)
*/
@Override
public <T> List<T> insert(List<T> entities, String tableName) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.lang.Object)
*/
@Override
public <T> T insertAsynchronously(T entity) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.util.List)
*/
@Override
public <T> List<T> insertAsynchronously(List<T> entities) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.lang.Object, java.lang.String)
*/
@Override
public <T> T insertAsynchronously(T entity, String tableName) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#insertAsynchronously(java.util.List, java.lang.String)
*/
@Override
public <T> List<T> insertAsynchronously(List<T> entities, String tableName) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.lang.Object)
*/
@Override
public <T> void delete(T entity) {
delete(entity, determineTableName(entity.getClass()));
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.util.List)
*/
@Override
public <T> void delete(List<T> entities) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.lang.Object, java.lang.String)
*/
@Override
public <T> void delete(T entity, String tableName) {
CassandraPersistentEntity<?> entityClass = getEntity(entity);
Assert.notNull(entityClass);
doRemove(entity, tableName);
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#delete(java.util.List, java.lang.String)
*/
@Override
public <T> void delete(List<T> entities, String tableName) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsychronously(java.lang.Object)
*/
@Override
public <T> void deleteAsychronously(T entity) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsychronously(java.util.List)
*/
@Override
public <T> void deleteAsychronously(List<T> entities) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsychronously(java.lang.Object, java.lang.String)
*/
@Override
public <T> void deleteAsychronously(T entity, String tableName) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#deleteAsychronously(java.util.List, java.lang.String)
*/
@Override
public <T> void deleteAsychronously(List<T> entities, String tableName) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#update(java.lang.Object)
*/
@Override
public <T> T update(T entity) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#update(java.util.List)
*/
@Override
public <T> List<T> update(List<T> entities) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#update(java.lang.Object, java.lang.String)
*/
@Override
public <T> T update(T entity, String tableName) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#update(java.util.List, java.lang.String)
*/
@Override
public <T> List<T> update(List<T> entities, String tableName) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.lang.Object)
*/
@Override
public <T> T updateAsynchronously(T entity) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.util.List)
*/
@Override
public <T> List<T> updateAsynchronously(List<T> entities) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.lang.Object, java.lang.String)
*/
@Override
public <T> T updateAsynchronously(T entity, String tableName) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.CassandraOperations#updateAsynchronously(java.util.List, java.lang.String)
*/
@Override
public <T> List<T> updateAsynchronously(List<T> entities, String tableName) {
// TODO Auto-generated method stub
return null;
}
}