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

info.archinnov.achilles.clustered.ClusteredEntityFactory Maven / Gradle / Ivy

package info.archinnov.achilles.clustered;

import info.archinnov.achilles.composite.ThriftCompositeTransformer;
import info.archinnov.achilles.context.ThriftPersistenceContext;
import info.archinnov.achilles.dao.ThriftGenericEntityDao;
import info.archinnov.achilles.entity.metadata.EntityMeta;
import info.archinnov.achilles.entity.metadata.PropertyMeta;
import info.archinnov.achilles.entity.operations.ThriftJoinEntityLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import me.prettyprint.hector.api.beans.Composite;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.HCounterColumn;
import com.google.common.base.Function;
import com.google.common.collect.Lists;

public class ClusteredEntityFactory
{

    private ThriftCompositeTransformer transformer = new ThriftCompositeTransformer();
    private ThriftJoinEntityLoader joinHelper = new ThriftJoinEntityLoader();

    public  List buildClusteredEntities(Class entityClass,
            ThriftPersistenceContext context,
            List> hColumns)
    {
        PropertyMeta pm = context.getFirstMeta();
        if (hColumns.isEmpty())
        {
            return new ArrayList();
        }
        else if (pm.isJoin())
        {
            return buildJoinClusteredEntities(entityClass, context, hColumns);
        }
        else
        {
            return buildSimpleClusteredEntities(entityClass, context, hColumns);
        }
    }

    private  List buildSimpleClusteredEntities(Class entityClass,
            ThriftPersistenceContext context,
            List> hColumns)
    {

        Function, T> function = transformer
                .buildClusteredEntityTransformer(entityClass,
                        context);

        return Lists.transform(hColumns, function);
    }

    private  List buildJoinClusteredEntities(Class entityClass,
            ThriftPersistenceContext context,
            List> hColumns)
    {
        PropertyMeta pm = (PropertyMeta) context.getFirstMeta();
        EntityMeta joinMeta = pm.joinMeta();

        List joinIds = Lists.transform(hColumns, transformer.buildRawValueTransformer());
        Map joinEntitiesMap = loadJoinEntities(context, pm, joinMeta, joinIds);

        Function, T> function = transformer
                .buildJoinClusteredEntityTransformer(entityClass, context, joinEntitiesMap);

        return Lists.transform(hColumns, function);
    }

    public  List buildCounterClusteredEntities(Class entityClass,
            ThriftPersistenceContext context,
            List> hColumns)
    {
        Function, T> function = transformer
                .buildCounterClusteredEntityTransformer(
                        entityClass, context);

        return Lists.transform(hColumns, function);
    }

    private Map loadJoinEntities(ThriftPersistenceContext context,
            PropertyMeta pm,
            EntityMeta joinMeta, List joinIds)
    {
        ThriftGenericEntityDao joinEntityDao = context.findEntityDao(joinMeta.getTableName());

        Map joinEntities = joinHelper.loadJoinEntities(pm.getValueClass(), joinIds,
                joinMeta, joinEntityDao);

        return joinEntities;
    }
}