
info.archinnov.achilles.entity.operations.impl.ThriftPersisterImpl Maven / Gradle / Ivy
package info.archinnov.achilles.entity.operations.impl;
import static info.archinnov.achilles.helper.ThriftLoggerHelper.*;
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.ThriftGenericWideRowDao;
import info.archinnov.achilles.entity.metadata.EntityMeta;
import info.archinnov.achilles.entity.metadata.JoinProperties;
import info.archinnov.achilles.entity.metadata.PropertyMeta;
import info.archinnov.achilles.entity.operations.ThriftEntityPersister;
import info.archinnov.achilles.entity.operations.ThriftEntityProxifier;
import info.archinnov.achilles.proxy.MethodInvoker;
import info.archinnov.achilles.type.KeyValue;
import info.archinnov.achilles.validation.Validator;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import me.prettyprint.hector.api.beans.AbstractComposite.ComponentEquality;
import me.prettyprint.hector.api.beans.Composite;
import me.prettyprint.hector.api.mutation.Mutator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* ThriftPersisterImpl
*
* @author DuyHai DOAN
*
*/
public class ThriftPersisterImpl
{
private static final Logger log = LoggerFactory.getLogger(ThriftPersisterImpl.class);
private MethodInvoker invoker = new MethodInvoker();
private ThriftEntityProxifier proxifier = new ThriftEntityProxifier();
private ThriftCompositeFactory thriftCompositeFactory = new ThriftCompositeFactory();
public void batchPersistSimpleProperty(ThriftPersistenceContext context,
PropertyMeta, ?> propertyMeta)
{
Composite name = thriftCompositeFactory.createForBatchInsertSingleValue(propertyMeta);
String value = propertyMeta.writeValueToString(invoker.getValueFromField(
context.getEntity(), propertyMeta.getGetter()));
if (value != null)
{
if (log.isTraceEnabled())
{
log
.trace("Batch persisting simple property {} from entity of class {} and primary key {} with column name {}",
propertyMeta.getPropertyName(), context
.getEntityClass()
.getCanonicalName(), context.getPrimaryKey(), format(name));
}
context.getEntityDao().insertColumnBatch(context.getPrimaryKey(), name, value,
context.getTttO(),
context.getEntityMutator(context.getTableName()));
}
}
public void batchPersistList(List list, ThriftPersistenceContext context,
PropertyMeta, ?> propertyMeta)
{
int count = 0;
for (V value : list)
{
Composite name = thriftCompositeFactory.createForBatchInsertMultiValue(propertyMeta,
count);
String stringValue = propertyMeta.writeValueToString(value);
if (stringValue != null)
{
if (log.isTraceEnabled())
{
log
.trace("Batch persisting list property {} from entity of class {} and primary key {} with column name {}",
propertyMeta.getPropertyName(), context
.getEntityClass()
.getCanonicalName(), context.getPrimaryKey(),
format(name));
}
context.getEntityDao().insertColumnBatch(context.getPrimaryKey(), name,
stringValue, context.getTttO(),
context.getEntityMutator(context.getTableName()));
}
count++;
}
}
public void batchPersistSet(Set set, ThriftPersistenceContext context,
PropertyMeta, ?> propertyMeta)
{
for (V value : set)
{
Composite name = thriftCompositeFactory.createForBatchInsertMultiValue(propertyMeta,
value.hashCode());
String stringValue = propertyMeta.writeValueToString(value);
if (stringValue != null)
{
if (log.isTraceEnabled())
{
log
.trace("Batch persisting set property {} from entity of class {} and primary key {} with column name {}",
propertyMeta.getPropertyName(), context
.getEntityClass()
.getCanonicalName(), context.getPrimaryKey(),
format(name));
}
context.getEntityDao().insertColumnBatch(context.getPrimaryKey(), name,
stringValue, context.getTttO(),
context.getEntityMutator(context.getTableName()));
}
}
}
public void batchPersistMap(Map map, ThriftPersistenceContext context,
PropertyMeta, ?> propertyMeta)
{
for (Entry entry : map.entrySet())
{
Composite name = thriftCompositeFactory.createForBatchInsertMultiValue(propertyMeta,
entry.getKey().hashCode());
String value = propertyMeta.writeValueToString(new KeyValue(entry.getKey(), entry
.getValue()));
if (log.isTraceEnabled())
{
log
.trace("Batch persisting map property {} from entity of class {} and primary key {} with column name {}",
propertyMeta.getPropertyName(), context
.getEntityClass()
.getCanonicalName(), context.getPrimaryKey(), format(name));
}
context.getEntityDao().insertColumnBatch(context.getPrimaryKey(), name, value,
context.getTttO(),
context.getEntityMutator(context.getTableName()));
}
}
public void batchPersistJoinEntity(ThriftPersistenceContext context,
PropertyMeta, ?> propertyMeta, V joinEntity, ThriftEntityPersister persister)
{
JoinProperties joinProperties = propertyMeta.getJoinProperties();
PropertyMeta, ?> idMeta = propertyMeta.joinIdMeta();
Object joinId = invoker.getPrimaryKey(joinEntity, idMeta);
Validator.validateNotNull(joinId, "Primary key for join entity '" + joinEntity
+ "' should not be null");
String joinIdString = idMeta.writeValueToString(joinId);
Composite joinComposite = thriftCompositeFactory
.createForBatchInsertSingleValue(propertyMeta);
context.getEntityDao().insertColumnBatch(context.getPrimaryKey(), joinComposite,
joinIdString, context.getTttO(), context.getEntityMutator(context.getTableName()));
ThriftPersistenceContext joinPersistenceContext = context
.newPersistenceContext(propertyMeta.joinMeta(), proxifier.unproxy(joinEntity));
if (log.isTraceEnabled())
{
log
.trace("Batch persisting join primary key for property {} from entity of class {} and primary key {} with column name {}",
propertyMeta.getPropertyName(), context
.getEntityClass()
.getCanonicalName(), context.getPrimaryKey(),
format(joinComposite));
}
persister.cascadePersistOrEnsureExists(joinPersistenceContext, joinEntity, joinProperties);
}
public void batchPersistJoinCollection(ThriftPersistenceContext context,
PropertyMeta, ?> propertyMeta, Collection joinCollection,
ThriftEntityPersister persister)
{
JoinProperties joinProperties = propertyMeta.getJoinProperties();
EntityMeta joinEntityMeta = joinProperties.getEntityMeta();
PropertyMeta, ?> joinIdMeta = joinEntityMeta.getIdMeta();
int count = 0;
for (V joinEntity : joinCollection)
{
Composite name = thriftCompositeFactory.createForBatchInsertMultiValue(propertyMeta,
count);
Object joinEntityId = invoker.getValueFromField(joinEntity, joinIdMeta.getGetter());
String joinEntityIdStringValue = joinIdMeta.writeValueToString(joinEntityId);
if (joinEntityIdStringValue != null)
{
if (log.isTraceEnabled())
{
log
.trace("Batch persisting join primary keys for property {} from entity of class {} and primary key {} with column name {}",
propertyMeta.getPropertyName(), context
.getEntityClass()
.getCanonicalName(), context.getPrimaryKey(),
format(name));
}
context.getEntityDao().insertColumnBatch(context.getPrimaryKey(), name,
joinEntityIdStringValue, context.getTttO(),
context.getEntityMutator(context.getTableName()));
ThriftPersistenceContext joinPersistenceContext = context
.newPersistenceContext(propertyMeta.joinMeta(),
proxifier.unproxy(joinEntity));
persister.cascadePersistOrEnsureExists(joinPersistenceContext, joinEntity,
joinProperties);
}
count++;
}
}
public void batchPersistJoinMap(ThriftPersistenceContext context,
PropertyMeta, ?> propertyMeta, Map joinMap, ThriftEntityPersister persiter)
{
JoinProperties joinProperties = propertyMeta.getJoinProperties();
EntityMeta joinEntityMeta = joinProperties.getEntityMeta();
PropertyMeta, ?> idMeta = joinEntityMeta.getIdMeta();
for (Entry entry : joinMap.entrySet())
{
Composite name = thriftCompositeFactory.createForBatchInsertMultiValue(propertyMeta,
entry.getKey().hashCode());
V joinEntity = entry.getValue();
Object joinEntityId = invoker.getValueFromField(joinEntity, idMeta.getGetter());
String joinEntityIdStringValue = idMeta.writeValueToString(joinEntityId);
String value = propertyMeta.writeValueToString(new KeyValue(entry.getKey(),
joinEntityIdStringValue));
context.getEntityDao().insertColumnBatch(context.getPrimaryKey(), name, value,
context.getTttO(),
context.getEntityMutator(context.getTableName()));
ThriftPersistenceContext joinPersistenceContext = context
.newPersistenceContext(propertyMeta.joinMeta(), proxifier.unproxy(joinEntity));
if (log.isTraceEnabled())
{
log
.trace("Batch persisting join primary keys for property {} from entity of class {} and primary key {} with column name {}",
propertyMeta.getPropertyName(), context
.getEntityClass()
.getCanonicalName(), context.getPrimaryKey(), format(name));
}
persiter.cascadePersistOrEnsureExists(joinPersistenceContext, joinEntity,
joinProperties);
}
}
public void removeEntityBatch(ThriftPersistenceContext context)
{
EntityMeta entityMeta = context.getEntityMeta();
Object primaryKey = context.getPrimaryKey();
if (!context.isWideRow())
{
log.trace("Batch removing wide row of class {} and primary key {}", context
.getEntityClass()
.getCanonicalName(), context.getPrimaryKey());
Mutator
© 2015 - 2025 Weber Informatics LLC | Privacy Policy