Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
info.archinnov.achilles.entity.operations.impl.ThriftJoinLoaderImpl Maven / Gradle / Ivy
package info.archinnov.achilles.entity.operations.impl;
import static me.prettyprint.hector.api.beans.AbstractComposite.ComponentEquality.*;
import info.archinnov.achilles.composite.ThriftCompositeFactory;
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.operations.ThriftJoinEntityLoader;
import info.archinnov.achilles.type.KeyValue;
import org.apache.cassandra.utils.Pair;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import me.prettyprint.hector.api.beans.Composite;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* ThriftJoinLoaderImpl
*
* @author DuyHai DOAN
*
*/
public class ThriftJoinLoaderImpl
{
private static final Logger log = LoggerFactory.getLogger(ThriftJoinLoaderImpl.class);
private ThriftCompositeFactory thriftCompositeFactory = new ThriftCompositeFactory();
private ThriftJoinEntityLoader joinHelper = new ThriftJoinEntityLoader();
public List loadJoinListProperty(ThriftPersistenceContext context,
PropertyMeta, V> propertyMeta)
{
EntityMeta joinMeta = propertyMeta.joinMeta();
List joinIds = fetchColumns(context, propertyMeta);
log.trace("Loading join entities of class {} having primary keys {}", propertyMeta
.getValueClass()
.getCanonicalName(), joinIds);
ThriftGenericEntityDao joinEntityDao = context.findEntityDao(joinMeta.getTableName());
List joinEntities = new ArrayList();
fillCollectionWithJoinEntities(propertyMeta, joinMeta, joinIds, joinEntityDao, joinEntities);
return joinEntities;
}
public Set loadJoinSetProperty(ThriftPersistenceContext context,
PropertyMeta, V> propertyMeta)
{
EntityMeta joinMeta = propertyMeta.joinMeta();
List joinIds = fetchColumns(context, propertyMeta);
ThriftGenericEntityDao joinEntityDao = context.findEntityDao(joinMeta.getTableName());
Set joinEntities = new HashSet();
fillCollectionWithJoinEntities(propertyMeta, joinMeta, joinIds, joinEntityDao, joinEntities);
return joinEntities;
}
public Map loadJoinMapProperty(ThriftPersistenceContext context,
PropertyMeta propertyMeta)
{
EntityMeta joinMeta = propertyMeta.joinMeta();
ThriftGenericEntityDao joinEntityDao = context.findEntityDao(joinMeta.getTableName());
Composite start = thriftCompositeFactory.createBaseForQuery(propertyMeta, EQUAL);
Composite end = thriftCompositeFactory.createBaseForQuery(propertyMeta, GREATER_THAN_EQUAL);
List> columns = context.getEntityDao().findColumnsRange(
context.getPrimaryKey(), start, end, false, Integer.MAX_VALUE);
PropertyMeta, ?> joinIdMeta = propertyMeta.joinIdMeta();
Map map = new HashMap();
Map partialMap = new HashMap();
Class keyClass = propertyMeta.getKeyClass();
List joinIds = new ArrayList();
for (Pair pair : columns)
{
KeyValue holder = propertyMeta.getKeyValueFromString(pair.right);
Object joinId = joinIdMeta.getValueFromString(holder.getValue());
partialMap.put(keyClass.cast(holder.getKey()), joinId);
joinIds.add(joinId);
}
if (joinIds.size() > 0)
{
log.trace("Loading join entities of class {} having primary keys {}", propertyMeta
.getValueClass()
.getCanonicalName(), joinIds);
Map entitiesMap = joinHelper.loadJoinEntities(propertyMeta.getValueClass(),
joinIds, joinMeta, joinEntityDao);
for (Entry entry : partialMap.entrySet())
{
map.put(entry.getKey(), entitiesMap.get(entry.getValue()));
}
}
return map;
}
private List fetchColumns(ThriftPersistenceContext context,
PropertyMeta, V> propertyMeta)
{
log.trace("Fetching join keys for property {} of class {} ",
propertyMeta.getPropertyName(), context.getEntityClass().getCanonicalName());
Composite start = thriftCompositeFactory.createBaseForQuery(propertyMeta, EQUAL);
Composite end = thriftCompositeFactory.createBaseForQuery(propertyMeta, GREATER_THAN_EQUAL);
List> columns = context.getEntityDao().findColumnsRange(
context.getPrimaryKey(), start, end, false, Integer.MAX_VALUE);
List joinIds = new ArrayList();
PropertyMeta, ?> joinIdMeta = propertyMeta.joinIdMeta();
for (Pair pair : columns)
{
joinIds.add(joinIdMeta.getValueFromString(pair.right));
}
return joinIds;
}
private void fillCollectionWithJoinEntities(PropertyMeta, V> propertyMeta,
EntityMeta joinMeta, List joinIds, ThriftGenericEntityDao joinEntityDao,
Collection joinEntities)
{
if (joinIds.size() > 0)
{
Map entitiesMap = joinHelper.loadJoinEntities(propertyMeta.getValueClass(),
joinIds, joinMeta, joinEntityDao);
for (Object joinId : joinIds)
{
joinEntities.add(entitiesMap.get(joinId));
}
}
}
}