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

info.archinnov.achilles.context.ThriftDaoContextBuilder Maven / Gradle / Ivy

There is a newer version: 2.0.9
Show newest version
package info.archinnov.achilles.context;

import static info.archinnov.achilles.helper.PropertyHelper.isSupportedType;
import info.archinnov.achilles.consistency.AchillesConsistencyLevelPolicy;
import info.archinnov.achilles.consistency.ThriftConsistencyLevelPolicy;
import info.archinnov.achilles.dao.ThriftCounterDao;
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.type.Counter;
import info.archinnov.achilles.type.Pair;

import java.util.HashMap;
import java.util.Map;

import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.Composite;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

	private Map entityDaosMap = new HashMap();
	private Map wideRowDaosMap = new HashMap();
	private ThriftCounterDao thriftCounterDao;

	public ThriftDaoContext buildDao(Cluster cluster, Keyspace keyspace,
			Map, EntityMeta> entityMetaMap, ConfigurationContext configContext,
			boolean hasSimpleCounter)
	{
		if (hasSimpleCounter)
		{
			thriftCounterDao = new ThriftCounterDao(cluster, keyspace,
					configContext.getConsistencyPolicy(), //
					new Pair, Class>(Composite.class, Long.class));
			log.debug("Build achillesCounterCF dao");
		}

		for (EntityMeta entityMeta : entityMetaMap.values())
		{
			if (!entityMeta.isWideRow())
			{
				buildEntityDao(cluster, keyspace, configContext, entityMeta);
			}

			for (PropertyMeta propertyMeta : entityMeta.getPropertyMetas().values())
			{
				if (propertyMeta.type().isWideMap())
				{
					if (propertyMeta.type().isJoinColumn())
					{
						EntityMeta joinEntityMeta = propertyMeta
								.getJoinProperties()
								.getEntityMeta();
						buildWideRowDaoForJoinWideMap(cluster, keyspace, configContext,
								propertyMeta, joinEntityMeta);
					}
					else
					{
						buildWideRowDao(cluster, keyspace, configContext, entityMeta, propertyMeta);
					}
				}
			}
		}
		return new ThriftDaoContext(entityDaosMap, wideRowDaosMap, thriftCounterDao);
	}

	private void buildEntityDao(Cluster cluster, Keyspace keyspace,
			ConfigurationContext configContext, EntityMeta entityMeta)
	{
		String columnFamilyName = entityMeta.getTableName();
		ThriftGenericEntityDao entityDao = new ThriftGenericEntityDao(//
				cluster, //
				keyspace, //
				columnFamilyName, //
				configContext.getConsistencyPolicy(), //
				new Pair, Class>(entityMeta.getIdClass(), String.class));

		entityDaosMap.put(columnFamilyName, entityDao);
		log.debug("Build entity dao for column family {}", columnFamilyName);
	}

	private  void buildWideRowDao(Cluster cluster, Keyspace keyspace,
			ConfigurationContext configContext, EntityMeta entityMeta,
			PropertyMeta propertyMeta)
	{
		Class valueClass = propertyMeta.getValueClass();
		ThriftGenericWideRowDao dao;

		String externalCFName = propertyMeta.getExternalTableName();
		AchillesConsistencyLevelPolicy consistencyPolicy = configContext.getConsistencyPolicy();
		if (isSupportedType(valueClass))
		{
			dao = new ThriftGenericWideRowDao(cluster, keyspace, //
					externalCFName, consistencyPolicy, //
					new Pair, Class>(entityMeta.getIdClass(),
							propertyMeta.getValueClass()));
		}
		else if (Counter.class.isAssignableFrom(valueClass))
		{
			dao = new ThriftGenericWideRowDao(cluster, keyspace, //
					externalCFName, consistencyPolicy,//
					new Pair, Class>(entityMeta.getIdClass(), Long.class));
		}
		else
		{
			dao = new ThriftGenericWideRowDao(cluster, keyspace, //
					externalCFName, consistencyPolicy, //
					new Pair, Class>(entityMeta.getIdClass(), String.class));
		}
		wideRowDaosMap.put(externalCFName, dao);
		log.debug("Build column family dao for wide row {}", externalCFName);

	}

	private  void buildWideRowDaoForJoinWideMap(Cluster cluster, Keyspace keyspace,
			ConfigurationContext configContext, PropertyMeta propertyMeta,
			EntityMeta joinEntityMeta)
	{

		ThriftGenericWideRowDao joinDao = new ThriftGenericWideRowDao(
				cluster, //
				keyspace, //
				propertyMeta.getExternalTableName(),//
				(ThriftConsistencyLevelPolicy) configContext.getConsistencyPolicy(),//
				new Pair, Class>(propertyMeta.getIdClass(), joinEntityMeta.getIdClass()));

		log.debug("Building join dao for wide row {}", propertyMeta.getExternalTableName());

		wideRowDaosMap.put(propertyMeta.getExternalTableName(), joinDao);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy