Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.netflix.astyanax.entitystore;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.PersistenceException;
import org.apache.commons.lang.StringUtils;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.netflix.astyanax.ColumnListMutation;
import com.netflix.astyanax.model.ColumnList;
import com.netflix.astyanax.model.Equality;
import com.netflix.astyanax.query.ColumnPredicate;
/**
* Mapper from a CompositeType to an embedded entity. The composite entity is expected
* to have an @Id annotation for each composite component and a @Column annotation for
* the value.
*
* @author elandau
*
*/
public class CompositeColumnEntityMapper {
/**
* Class of embedded entity
*/
private final Class> clazz;
/**
* List of serializers for the composite parts
*/
private List> components = Lists.newArrayList();
/**
* List of valid (i.e. existing) column names
*/
private Set validNames = Sets.newHashSet();
/**
* Mapper for the value part of the entity
*/
private FieldMapper> valueMapper;
/**
* Largest buffer size
*/
private int bufferSize = 64;
/**
* Parent field
*/
private final Field containerField;
public CompositeColumnEntityMapper(Field field) {
ParameterizedType containerEntityType = (ParameterizedType) field.getGenericType();
this.clazz = (Class>) containerEntityType.getActualTypeArguments()[0];
this.containerField = field;
this.containerField.setAccessible(true);
Field[] declaredFields = clazz.getDeclaredFields();
for (Field f : declaredFields) {
// The value
Column columnAnnotation = f.getAnnotation(Column.class);
if ((columnAnnotation != null)) {
f.setAccessible(true);
FieldMapper fieldMapper = new FieldMapper(f);
components.add(fieldMapper);
validNames.add(fieldMapper.getName());
}
}
// Last one is always treated as the 'value'
valueMapper = components.remove(components.size() - 1);
}
/**
* Iterate through the list and create a column for each element
* @param clm
* @param entity
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public void fillMutationBatch(ColumnListMutation clm, Object entity) throws IllegalArgumentException, IllegalAccessException {
List> list = (List>) containerField.get(entity);
if (list != null) {
for (Object element : list) {
fillColumnMutation(clm, element);
}
}
}
public void fillMutationBatchForDelete(ColumnListMutation clm, Object entity) throws IllegalArgumentException, IllegalAccessException {
List> list = (List>) containerField.get(entity);
if (list == null) {
clm.delete();
}
else {
for (Object element : list) {
clm.deleteColumn(toColumnName(element));
}
}
}
/**
* Add a column based on the provided entity
*
* @param clm
* @param entity
*/
public void fillColumnMutation(ColumnListMutation clm, Object entity) {
try {
ByteBuffer columnName = toColumnName(entity);
ByteBuffer value = valueMapper.toByteBuffer(entity);
clm.putColumn(columnName, value);
} catch(Exception e) {
throw new PersistenceException("failed to fill mutation batch", e);
}
}
/**
* Return the column name byte buffer for this entity
*
* @param obj
* @return
*/
public ByteBuffer toColumnName(Object obj) {
SimpleCompositeBuilder composite = new SimpleCompositeBuilder(bufferSize, Equality.EQUAL);
// Iterate through each component and add to a CompositeType structure
for (FieldMapper> mapper : components) {
try {
composite.addWithoutControl(mapper.toByteBuffer(obj));
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
return composite.get();
}
/**
* Set the collection field using the provided column list of embedded entities
* @param entity
* @param name
* @param column
* @return
* @throws Exception
*/
public boolean setField(Object entity, ColumnList columns) throws Exception {
List