All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.archinnov.achilles.composite.ThriftCompositeTransformer Maven / Gradle / Ivy

package info.archinnov.achilles.composite;

import info.archinnov.achilles.compound.ThriftCompoundKeyMapper;
import info.archinnov.achilles.context.ThriftPersistenceContext;
import info.archinnov.achilles.entity.ThriftEntityMapper;
import info.archinnov.achilles.entity.metadata.PropertyMeta;
import info.archinnov.achilles.iterator.ThriftHColumn;
import java.util.List;
import java.util.Map;
import me.prettyprint.hector.api.beans.AbstractComposite.Component;
import me.prettyprint.hector.api.beans.Composite;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.HCounterColumn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Function;

/**
 * ThriftCompositeTransformer
 * 
 * @author DuyHai DOAN
 * 
 */
public class ThriftCompositeTransformer
{
    private static final Logger log = LoggerFactory.getLogger(ThriftCompositeTransformer.class);

    private ThriftCompoundKeyMapper compoundKeyMapper = new ThriftCompoundKeyMapper();
    private ThriftEntityMapper mapper = new ThriftEntityMapper();

    public Function, ?> buildRawValueTransformer()
    {
        return new Function, Object>()
        {
            @Override
            public Object apply(HColumn hColumn)
            {
                return hColumn.getValue();
            }
        };
    }

    // //////////////////// Clustered Entities

    public  Function, T> buildClusteredEntityTransformer(
            final Class entityClass,
            final ThriftPersistenceContext context)
    {
        return new Function, T>()
        {
            @Override
            public T apply(HColumn hColumn)
            {
                return buildClusteredEntity(entityClass, context, hColumn);
            }
        };
    }

    public  Function, T> buildJoinClusteredEntityTransformer(
            final Class entityClass, final ThriftPersistenceContext context,
            final Map joinEntitiesMap)
    {
        return new Function, T>()
        {
            @Override
            public T apply(HColumn hColumn)
            {
                Object joinId = hColumn.getValue();
                Object clusteredValue = joinEntitiesMap.get(joinId);
                ThriftHColumn hCol = new ThriftHColumn(
                        hColumn.getName(), clusteredValue);
                return buildClusteredEntity(entityClass, context, hCol);
            }
        };
    }

    public  Function, T> buildCounterClusteredEntityTransformer(
            final Class entityClass, final ThriftPersistenceContext context)
    {
        return new Function, T>()
        {
            @Override
            public T apply(HCounterColumn hColumn)
            {
                return buildCounterClusteredEntity(entityClass, context, hColumn);
            }
        };
    }

    public  T buildClusteredEntity(Class entityClass, ThriftPersistenceContext context,
            HColumn hColumn)
    {
        PropertyMeta idMeta = context.getIdMeta();
        PropertyMeta pm = context.getFirstMeta();
        Object embeddedId = buildEmbeddedIdFromComponents(context, hColumn
                .getName()
                .getComponents());
        Object clusteredValue = hColumn.getValue();
        Object value = pm.castValue(clusteredValue);
        return mapper.createClusteredEntityWithValue(entityClass, idMeta, pm, embeddedId, value);
    }

    public  T buildCounterClusteredEntity(Class entityClass,
            ThriftPersistenceContext context,
            HCounterColumn hColumn)
    {
        PropertyMeta idMeta = context.getIdMeta();
        Object embeddedId = buildEmbeddedIdFromComponents(context, hColumn
                .getName()
                .getComponents());
        return mapper.initClusteredEntity(entityClass, idMeta, embeddedId);
    }

    private Object buildEmbeddedIdFromComponents(ThriftPersistenceContext context,
            List> components)
    {
        Object partitionKey = context.getPartitionKey();
        PropertyMeta idMeta = context.getIdMeta();
        return compoundKeyMapper.fromCompositeToEmbeddedId(idMeta, components, partitionKey);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy