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

info.archinnov.achilles.iterator.ThriftJoinSliceIterator Maven / Gradle / Ivy

package info.archinnov.achilles.iterator;

import static info.archinnov.achilles.dao.ThriftAbstractDao.DEFAULT_LENGTH;
import static info.archinnov.achilles.iterator.ThriftAbstractSliceIterator.IteratorType.THRIFT_JOIN_SLICE_ITERATOR;
import info.archinnov.achilles.consistency.AchillesConsistencyLevelPolicy;
import info.archinnov.achilles.context.execution.SafeExecutionContext;
import info.archinnov.achilles.dao.ThriftGenericEntityDao;
import info.archinnov.achilles.entity.metadata.PropertyMeta;
import info.archinnov.achilles.entity.operations.ThriftJoinEntityLoader;
import org.apache.cassandra.utils.Pair;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
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.query.SliceQuery;

/**
 * ThriftJoinSliceIterator
 * 
 * @author DuyHai DOAN
 * 
 *         Modification of original version from Hector ColumnSliceIterator
 * 
 */
public class ThriftJoinSliceIterator extends
        ThriftAbstractSliceIterator>
{

    private SliceQuery query;
    private PropertyMeta propertyMeta;
    private ThriftJoinEntityLoader joinHelper = new ThriftJoinEntityLoader();
    private ThriftGenericEntityDao joinEntityDao;

    public ThriftJoinSliceIterator( //
            AchillesConsistencyLevelPolicy policy, //
            ThriftGenericEntityDao joinEntityDao, //
            String cf, PropertyMeta propertyMeta, //
            SliceQuery query, Composite start, //
            final Composite finish, boolean reversed)
    {
        this(policy, joinEntityDao, cf, propertyMeta, query, start, finish, reversed,
                DEFAULT_LENGTH);
    }

    public ThriftJoinSliceIterator(AchillesConsistencyLevelPolicy policy, //
            ThriftGenericEntityDao joinEntityDao, //
            String cf, PropertyMeta propertyMeta, //
            SliceQuery query, Composite start, //
            final Composite finish, boolean reversed, int count)
    {
        this(policy, joinEntityDao, cf, propertyMeta, query, start, new ColumnSliceFinish()
        {
            @Override
            public Composite function()
            {
                return finish;
            }
        }, reversed, count);
    }

    public ThriftJoinSliceIterator(AchillesConsistencyLevelPolicy policy, //
            ThriftGenericEntityDao joinEntityDao, //
            String cf, PropertyMeta propertyMeta, //
            SliceQuery query, Composite start, //
            ColumnSliceFinish finish, boolean reversed)
    {
        this(policy, joinEntityDao, cf, propertyMeta, query, start, finish, reversed,
                DEFAULT_LENGTH);
    }

    public ThriftJoinSliceIterator(AchillesConsistencyLevelPolicy policy, //
            ThriftGenericEntityDao joinEntityDao, //
            String cf, PropertyMeta propertyMeta, //
            SliceQuery query, Composite start, //
            ColumnSliceFinish finish, boolean reversed, int count)
    {
        super(policy, cf, start, finish, reversed, count);
        this.joinEntityDao = joinEntityDao;
        this.propertyMeta = propertyMeta;
        this.query = query;
        this.query.setRange(this.start, this.finish.function(), this.reversed, this.count);
    }

    @Override
    protected Iterator> fetchData()
    {

        Iterator> iter = executeWithInitialConsistencyLevel(new SafeExecutionContext>>()
        {
            @Override
            public Iterator> execute()
            {
                return query.execute().get().getColumns().iterator();
            }
        });

        List joinIds = new ArrayList();
        Map> hColumMap = new HashMap>();
        List> joinedHColumns = new ArrayList>();

        while (iter.hasNext())
        {
            HColumn hColumn = iter.next();

            PropertyMeta joinIdMeta = propertyMeta.joinIdMeta();

            Object joinId;
            if (propertyMeta.isJoin())
            {
                joinId = joinIdMeta.castValue(hColumn.getValue());
            }
            else
            {
                joinId = joinIdMeta.getValueFromString(hColumn.getValue());
            }
            joinIds.add(joinId);
            hColumMap
                    .put(joinId, Pair.create(hColumn.getName(), hColumn.getTtl()));
        }

        if (joinIds.size() > 0)
        {

            Map loadedEntities = joinHelper.loadJoinEntities(
                    propertyMeta.getValueClass(), joinIds, propertyMeta.joinMeta(), joinEntityDao);

            for (Object joinId : joinIds)
            {
                Pair pair = hColumMap.get(joinId);
                Composite name = pair.left;
                Integer ttl = pair.right;

                HColumn joinedHColumn = new ThriftHColumn();

                joinedHColumn.setName(name).setValue(loadedEntities.get(joinId)).setTtl(ttl);
                joinedHColumns.add(joinedHColumn);
            }
        }
        return joinedHColumns.iterator();
    }

    @Override
    public HColumn next()
    {
        HColumn column = iterator.next();
        start = column.getName();
        columns++;

        return column;
    }

    @Override
    public void remove()
    {
        iterator.remove();
    }

    @Override
    protected void changeQueryRange()
    {
        query.setRange(start, finish.function(), reversed, count);
    }

    @Override
    protected void resetStartColumn(HColumn column)
    {
        start = column.getName();
    }

    @Override
    public IteratorType type()
    {
        return THRIFT_JOIN_SLICE_ITERATOR;
    }
}