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

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

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

import static info.archinnov.achilles.helper.ThriftLoggerHelper.format;
import static me.prettyprint.hector.api.beans.AbstractComposite.ComponentEquality.*;
import info.archinnov.achilles.composite.ThriftCompositeFactory;
import info.archinnov.achilles.context.AchillesPersistenceContext;
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.metadata.PropertyType;
import info.archinnov.achilles.entity.operations.ThriftEntityLoader;
import info.archinnov.achilles.helper.ThriftEntityMapper;
import info.archinnov.achilles.proxy.AchillesMethodInvoker;
import info.archinnov.achilles.type.KeyValue;
import info.archinnov.achilles.type.Pair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import me.prettyprint.hector.api.beans.AbstractComposite.ComponentEquality;
import me.prettyprint.hector.api.beans.Composite;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

	private ThriftEntityMapper mapper = new ThriftEntityMapper();
	private AchillesMethodInvoker invoker = new AchillesMethodInvoker();
	private ThriftCompositeFactory thriftCompositeFactory = new ThriftCompositeFactory();

	public  T load(ThriftPersistenceContext context, Class entityClass) throws Exception
	{
		log.trace("Loading entity of class {} with primary key {}", context
				.getEntityClass()
				.getCanonicalName(), context.getPrimaryKey());
		EntityMeta entityMeta = context.getEntityMeta();
		Object primaryKey = context.getPrimaryKey();

		List> columns = context.getEntityDao().eagerFetchEntity(primaryKey);
		T entity = null;
		if (columns.size() > 0)
		{
			log.trace("Mapping data from Cassandra columns to entity");

			entity = entityClass.newInstance();
			mapper.setEagerPropertiesToEntity(primaryKey, columns, entityMeta, entity);
			invoker.setValueToField(entity, entityMeta.getIdMeta().getSetter(), primaryKey);
		}

		return (T) entity;
	}

	public  Long loadVersionSerialUID(Object key, ThriftGenericEntityDao dao)
	{
		Composite composite = new Composite();
		composite.addComponent(0, PropertyType.SERIAL_VERSION_UID.flag(), ComponentEquality.EQUAL);
		composite.addComponent(1, PropertyType.SERIAL_VERSION_UID.name(), ComponentEquality.EQUAL);
		composite.addComponent(2, 0, ComponentEquality.EQUAL);

		String serialVersionUIDString = dao.getValue(key, composite);
		if (StringUtils.isNotBlank(serialVersionUIDString))
		{
			log.trace("Serial version UID {} found for column family {} and primary key {}",
					serialVersionUIDString, dao.getColumnFamily(), key);
			return Long.parseLong(serialVersionUIDString);
		}
		else
		{
			log.trace("No serial version UID found for column family {} and primary key {}",
					dao.getColumnFamily(), key);
			return null;
		}
	}

	public  V loadSimpleProperty(ThriftPersistenceContext context,
			PropertyMeta propertyMeta)
	{
		Composite composite = thriftCompositeFactory.createBaseForGet(propertyMeta);
		if (log.isTraceEnabled())
		{
			log
					.trace("Loading simple property {} of class {} from column family {} with primary key {} and composite column name {}",
							propertyMeta.getPropertyName(), propertyMeta.getEntityClassName(),
							context.getEntityMeta().getTableName(), context.getPrimaryKey(),
							format(composite));
		}
		return propertyMeta.getValueFromString(context.getEntityDao().getValue(
				context.getPrimaryKey(), composite));
	}

	public  List loadListProperty(ThriftPersistenceContext context,
			PropertyMeta propertyMeta)
	{
		log.trace("Loading list property {} of class {} from column family {} with primary key {}",
				propertyMeta.getPropertyName(), propertyMeta.getEntityClassName(), context
						.getEntityMeta()
						.getTableName(), context.getPrimaryKey());
		List> columns = fetchColumns(context, propertyMeta);
		List list = null;
		if (columns.size() > 0)
		{
			list = new ArrayList();
			for (Pair pair : columns)
			{
				list.add(propertyMeta.getValueFromString(pair.right));
			}
		}
		return list;
	}

	public  Set loadSetProperty(ThriftPersistenceContext context,
			PropertyMeta propertyMeta)
	{
		log.trace("Loading set property {} of class {} from column family {} with primary key {}",
				propertyMeta.getPropertyName(), propertyMeta.getEntityClassName(), context
						.getEntityMeta()
						.getTableName(), context.getPrimaryKey());
		List> columns = fetchColumns(context, propertyMeta);
		Set set = null;
		if (columns.size() > 0)
		{
			set = new HashSet();
			for (Pair pair : columns)
			{
				set.add(propertyMeta.getValueFromString(pair.right));
			}
		}
		return set;
	}

	public  Map loadMapProperty(ThriftPersistenceContext context,
			PropertyMeta propertyMeta)
	{
		log.trace("Loading map property {} of class {} from column family {} with primary key {}",
				propertyMeta.getPropertyName(), propertyMeta.getEntityClassName(), context
						.getEntityMeta()
						.getTableName(), context.getPrimaryKey());
		List> columns = fetchColumns(context, propertyMeta);
		Class keyClass = propertyMeta.getKeyClass();
		Map map = null;
		if (columns.size() > 0)
		{
			map = new HashMap();
			for (Pair pair : columns)
			{
				KeyValue holder = propertyMeta.getKeyValueFromString(pair.right);

				map.put(keyClass.cast(holder.getKey()), propertyMeta.castValue(holder.getValue()));
			}
		}
		return map;
	}

	private  List> fetchColumns(ThriftPersistenceContext context,
			PropertyMeta propertyMeta)
	{

		Composite start = thriftCompositeFactory.createBaseForQuery(propertyMeta, EQUAL);
		Composite end = thriftCompositeFactory.createBaseForQuery(propertyMeta, GREATER_THAN_EQUAL);
		if (log.isTraceEnabled())
		{
			log.trace("Fetching columns from Cassandra with column names {} / {}", format(start),
					format(end));
		}
		List> columns = context.getEntityDao().findColumnsRange(
				context.getPrimaryKey(), start, end, false, Integer.MAX_VALUE);
		return columns;
	}

	public  V loadJoinSimple(ThriftPersistenceContext context, PropertyMeta propertyMeta,
			ThriftEntityLoader loader)
	{
		EntityMeta joinMeta = propertyMeta.joinMeta();
		PropertyMeta joinIdMeta = propertyMeta.joinIdMeta();

		Composite composite = thriftCompositeFactory.createBaseForGet(propertyMeta);

		if (log.isTraceEnabled())
		{
			log
					.trace("Loading join primary key for property {} of class {} from column family {} with primary key {} and column name {}",
							propertyMeta.getPropertyName(), propertyMeta.getEntityClassName(),
							context.getEntityMeta().getTableName(), context.getPrimaryKey(),
							format(composite));
		}
		String stringJoinId = context.getEntityDao().getValue(context.getPrimaryKey(), composite);

		if (stringJoinId != null)
		{
			Object joinId = joinIdMeta.getValueFromString(stringJoinId);
			AchillesPersistenceContext joinContext = context.newPersistenceContext(
					propertyMeta.getValueClass(), joinMeta, joinId);
			return loader. load(joinContext, propertyMeta.getValueClass());

		}
		else
		{
			return (V) null;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy