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

info.archinnov.achilles.entity.operations.ThriftSliceQueryExecutor Maven / Gradle / Ivy

package info.archinnov.achilles.entity.operations;

import info.archinnov.achilles.clustered.ClusteredEntityFactory;
import info.archinnov.achilles.consistency.AchillesConsistencyLevelPolicy;
import info.archinnov.achilles.context.ConfigurationContext;
import info.archinnov.achilles.context.ThriftPersistenceContext;
import info.archinnov.achilles.context.ThriftPersistenceContextFactory;
import info.archinnov.achilles.entity.metadata.PropertyType;
import info.archinnov.achilles.entity.operations.impl.ThriftQueryExecutorImpl;
import info.archinnov.achilles.exception.AchillesException;
import info.archinnov.achilles.iterator.ThriftClusteredEntityIterator;
import info.archinnov.achilles.iterator.ThriftCounterClusteredEntityIterator;
import info.archinnov.achilles.iterator.ThriftCounterSliceIterator;
import info.archinnov.achilles.iterator.ThriftJoinSliceIterator;
import info.archinnov.achilles.iterator.ThriftSliceIterator;
import info.archinnov.achilles.query.SliceQuery;
import info.archinnov.achilles.type.ConsistencyLevel;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
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.collect.Lists;

public class ThriftSliceQueryExecutor extends SliceQueryExecutor
{

    private AchillesConsistencyLevelPolicy consistencyPolicy;

    private ClusteredEntityFactory factory = new ClusteredEntityFactory();
    private ThriftQueryExecutorImpl executorImpl = new ThriftQueryExecutorImpl();
    private ThriftPersistenceContextFactory contextFactory;

    public ThriftSliceQueryExecutor(ThriftPersistenceContextFactory contextFactory, ConfigurationContext configContext)
    {
        super(new ThriftEntityProxifier());
        this.contextFactory = contextFactory;
        this.consistencyPolicy = configContext.getConsistencyPolicy();
        defaultReadLevel = consistencyPolicy.getDefaultGlobalReadConsistencyLevel();
    }

    @Override
    public  List get(final SliceQuery sliceQuery)
    {
        ThriftPersistenceContext context = buildContextForQuery(sliceQuery);
        List getters = context.isValueless() ? Arrays. asList() : Arrays.asList(context
                .getFirstMeta()
                .getGetter());
        PropertyType type = context.isValueless() ? PropertyType.SIMPLE : context.getFirstMeta().type();
        List clusteredEntities = null;
        switch (type)
        {
            case JOIN_SIMPLE:
            case SIMPLE:
                List> hColumns = executorImpl
                        .findColumns(sliceQuery, context);
                clusteredEntities = factory.buildClusteredEntities(sliceQuery.getEntityClass(), context,
                        hColumns);
                break;
            case COUNTER:
                List> hCounterColumns = executorImpl.findCounterColumns(
                        sliceQuery, context);
                clusteredEntities = factory.buildCounterClusteredEntities(sliceQuery.getEntityClass(),
                        context, hCounterColumns);
                break;
            default:
                throw new AchillesException("Cannot get entities for clustered value of type '"
                        + type.name() + "' and clustered entity class '"
                        + sliceQuery.getEntityClass().getCanonicalName() + "'");
        }

        return Lists.transform(clusteredEntities, getProxyTransformer(sliceQuery, getters));
    }

    @Override
    public  Iterator iterator(final SliceQuery sliceQuery)
    {
        ThriftPersistenceContext context = buildContextForQuery(sliceQuery);
        PropertyType type = context.isValueless() ? PropertyType.SIMPLE : context.getFirstMeta().type();
        Class entityClass = sliceQuery.getEntityClass();

        switch (type)
        {
            case SIMPLE:
                ThriftSliceIterator columnsIterator = executorImpl
                        .getColumnsIterator(sliceQuery, context);
                return new ThriftClusteredEntityIterator(entityClass,
                        columnsIterator, context);

            case JOIN_SIMPLE:
                ThriftJoinSliceIterator joinColumnsIterator = executorImpl
                        .getJoinColumnsIterator(sliceQuery, context);
                return new ThriftClusteredEntityIterator(entityClass,
                        joinColumnsIterator, context);
            case COUNTER:
                ThriftCounterSliceIterator counterColumnsIterator = executorImpl
                        .getCounterColumnsIterator(sliceQuery, context);
                return new ThriftCounterClusteredEntityIterator(entityClass,
                        counterColumnsIterator, context);
            default:
                throw new AchillesException("Cannot get iterator for clustered value of type '"
                        + type.name() + "' and clustered entity class '"
                        + entityClass.getCanonicalName() + "'");
        }
    }

    @Override
    public  void remove(final SliceQuery sliceQuery)
    {
        ThriftPersistenceContext context = buildContextForQuery(sliceQuery);
        PropertyType type = context.isValueless() ? PropertyType.SIMPLE : context.getFirstMeta().type();

        if (sliceQuery.hasNoComponent() && sliceQuery.isLimitSet() == false)
        {
            executorImpl.removeRow(sliceQuery.getPartitionKey(), context, sliceQuery.getConsistencyLevel());
        }
        else
        {
            switch (type)
            {
                case JOIN_SIMPLE:
                case SIMPLE:
                    List> hColumns = executorImpl
                            .findColumns(sliceQuery, context);
                    executorImpl.removeColumns(hColumns, sliceQuery.getConsistencyLevel(), context);
                    break;
                case COUNTER:
                    List> hCounterColumns = executorImpl.findCounterColumns(
                            sliceQuery, context);
                    executorImpl.removeCounterColumns(hCounterColumns, sliceQuery.getConsistencyLevel(),
                            context);
                    break;
                default:
                    throw new AchillesException("Cannot remove clustered value of type '"
                            + type.name() + "' and clustered entity class '"
                            + sliceQuery.getEntityClass().getCanonicalName() + "'");
            }
        }
    }

    @Override
    protected  ThriftPersistenceContext buildContextForQuery(SliceQuery sliceQuery)
    {
        ConsistencyLevel cl = sliceQuery.getConsistencyLevel() == null ? defaultReadLevel : sliceQuery
                .getConsistencyLevel();
        return contextFactory.newContextForSliceQuery(sliceQuery.getEntityClass(), sliceQuery.getPartitionKey(), cl);
    }

    @Override
    protected  ThriftPersistenceContext buildNewContext(final SliceQuery sliceQuery, T clusteredEntity)
    {
        return contextFactory.newContext(clusteredEntity);
    }
}