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

info.archinnov.achilles.entity.operations.impl.ThriftQueryExecutorImpl Maven / Gradle / Ivy

package info.archinnov.achilles.entity.operations.impl;

import info.archinnov.achilles.composite.ThriftCompositeFactory;
import info.archinnov.achilles.context.ThriftPersistenceContext;
import info.archinnov.achilles.context.execution.SafeExecutionContext;
import info.archinnov.achilles.dao.ThriftGenericEntityDao;
import info.archinnov.achilles.dao.ThriftGenericWideRowDao;
import info.archinnov.achilles.entity.metadata.EntityMeta;
import info.archinnov.achilles.entity.metadata.PropertyMeta;
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.util.List;

import me.prettyprint.hector.api.beans.Composite;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.HCounterColumn;
import me.prettyprint.hector.api.mutation.Mutator;

/**
 * ThriftQueryExecutorImpl
 * 
 * @author DuyHai DOAN
 * 
 */
public class ThriftQueryExecutorImpl
{
	private ThriftCompositeFactory compositeFactory = new ThriftCompositeFactory();

	public  List> findColumns(final SliceQuery query,
			ThriftPersistenceContext context)
	{
		EntityMeta meta = query.getMeta();
		final ThriftGenericWideRowDao wideRowDao = context.getWideRowDao();
		PropertyMeta idMeta = meta.getIdMeta();

		final Composite[] composites = compositeFactory.createForClusteredQuery(idMeta,
				query.getClusteringsFrom(),
				query.getClusteringsTo(),
				query.getBounding(), query.getOrdering());

		return context.executeWithReadConsistencyLevel(
				new SafeExecutionContext>>()
				{
					@Override
					public List> execute()
					{
						return wideRowDao.findRawColumnsRange(
								query.getPartitionKey(), composites[0], composites[1],
								query.getLimit(), query.getOrdering().isReverse());
					}
				}, query.getConsistencyLevel());
	}

	public  ThriftSliceIterator getColumnsIterator(
			final SliceQuery query,
			ThriftPersistenceContext context)
	{
		EntityMeta meta = query.getMeta();
		final ThriftGenericWideRowDao wideRowDao = context.getWideRowDao();
		PropertyMeta idMeta = meta.getIdMeta();

		final Composite[] composites = compositeFactory.createForClusteredQuery(idMeta,
				query.getClusteringsFrom(),
				query.getClusteringsTo(),
				query.getBounding(), query.getOrdering());

		return context.executeWithReadConsistencyLevel(
				new SafeExecutionContext>()
				{
					@Override
					public ThriftSliceIterator execute()
					{
						return wideRowDao.getColumnsIterator(
								query.getPartitionKey(), composites[0], composites[1],
								query.getOrdering().isReverse(), query.getBatchSize());
					}
				}, query.getConsistencyLevel());
	}

	public  ThriftJoinSliceIterator getJoinColumnsIterator(
			final SliceQuery query,
			ThriftPersistenceContext context)
	{
		EntityMeta meta = query.getMeta();
		final PropertyMeta idMeta = meta.getIdMeta();
		final PropertyMeta pm = (PropertyMeta) meta.getFirstMeta();

		final ThriftGenericWideRowDao wideRowDao = context.getWideRowDao();

		final ThriftGenericEntityDao joinEntityDao = context.findEntityDao(pm
				.joinMeta().getTableName());

		final Composite[] composites = compositeFactory.createForClusteredQuery(idMeta,
				query.getClusteringsFrom(),
				query.getClusteringsTo(),
				query.getBounding(), query.getOrdering());

		return context.executeWithReadConsistencyLevel(
				new SafeExecutionContext>()
				{
					@Override
					public ThriftJoinSliceIterator execute()
					{
						return wideRowDao.getJoinColumnsIterator(joinEntityDao, pm,
								query.getPartitionKey(), composites[0], composites[1],
								query.getOrdering().isReverse(), query.getBatchSize());
					}
				}, query.getConsistencyLevel());
	}

	public void removeColumns(List> columns,
			final ConsistencyLevel consistencyLevel,
			final ThriftPersistenceContext context)
	{
		Object partitionKey = context.getPartitionKey();
		final ThriftGenericWideRowDao wideRowDao = context.getWideRowDao();
		final Mutator mutator = wideRowDao.buildMutator();
		for (HColumn column : columns)
		{
			wideRowDao.removeColumnBatch(partitionKey, column.getName(), mutator);
		}

		context.executeWithWriteConsistencyLevel(new SafeExecutionContext()
		{
			@Override
			public Void execute()
			{
				wideRowDao.executeMutator(mutator);
				return null;
			}
		}, consistencyLevel);

	}

	public  List> findCounterColumns(final SliceQuery query,
			ThriftPersistenceContext context)
	{
		EntityMeta meta = query.getMeta();
		final ThriftGenericWideRowDao wideRowDao = context.getWideRowDao();
		PropertyMeta idMeta = meta.getIdMeta();

		final Composite[] composites = compositeFactory.createForClusteredQuery(idMeta,
				query.getClusteringsFrom(),
				query.getClusteringsTo(),
				query.getBounding(), query.getOrdering());

		return context.executeWithReadConsistencyLevel(
				new SafeExecutionContext>>()
				{
					@Override
					public List> execute()
					{
						return wideRowDao.findCounterColumnsRange(
								query.getPartitionKey(), composites[0], composites[1],
								query.getLimit(), query.getOrdering().isReverse());
					}
				}, query.getConsistencyLevel());
	}

	public  ThriftCounterSliceIterator getCounterColumnsIterator(
			final SliceQuery query,
			ThriftPersistenceContext context)
	{
		EntityMeta meta = query.getMeta();
		final ThriftGenericWideRowDao wideRowDao = context.getWideRowDao();
		PropertyMeta idMeta = meta.getIdMeta();

		final Composite[] composites = compositeFactory.createForClusteredQuery(idMeta,
				query.getClusteringsFrom(),
				query.getClusteringsTo(),
				query.getBounding(), query.getOrdering());

		return context.executeWithReadConsistencyLevel(
				new SafeExecutionContext>()
				{
					@Override
					public ThriftCounterSliceIterator execute()
					{
						return wideRowDao.getCounterColumnsIterator(
								query.getPartitionKey(), composites[0], composites[1],
								query.getOrdering().isReverse(), query.getBatchSize());
					}
				}, query.getConsistencyLevel());
	}

	public void removeCounterColumns(List> counterColumns,
			final ConsistencyLevel consistencyLevel,
			final ThriftPersistenceContext context)
	{
		Object partitionKey = context.getPartitionKey();
		final ThriftGenericWideRowDao wideRowDao = context.getWideRowDao();
		final Mutator mutator = wideRowDao.buildMutator();
		for (HCounterColumn counterColumn : counterColumns)
		{
			wideRowDao.removeCounterBatch(partitionKey, counterColumn.getName(), mutator);
		}

		context.executeWithWriteConsistencyLevel(new SafeExecutionContext()
		{
			@Override
			public Void execute()
			{
				wideRowDao.executeMutator(mutator);
				return null;
			}
		}, consistencyLevel);

	}

}