make casser as a central entity cache

This commit is contained in:
Albert Shift 2015-04-09 22:18:50 -07:00
parent 00a3e20a98
commit 6b50e86dde
4 changed files with 59 additions and 42 deletions

View file

@ -26,7 +26,8 @@ import com.noorq.casser.config.CasserSettings;
import com.noorq.casser.config.DefaultCasserSettings;
import com.noorq.casser.core.reflect.CasserPropertyNode;
import com.noorq.casser.core.reflect.DslExportable;
import com.noorq.casser.mapping.MappingRepositoryBuilder;
import com.noorq.casser.mapping.CasserMappingEntity;
import com.noorq.casser.support.CasserMappingException;
@ -113,4 +114,41 @@ public final class Casser {
return settings.getMapperInstantiator().instantiate(iface, src, classLoader);
}
public static CasserMappingEntity entity(Class<?> iface) {
Object dsl = dsl(iface);
DslExportable e = (DslExportable) dsl;
return e.getCasserMappingEntity();
}
public static CasserMappingEntity resolve(Object ifaceOrDsl) {
if (ifaceOrDsl == null) {
throw new CasserMappingException("ifaceOrDsl is null");
}
if (ifaceOrDsl instanceof DslExportable) {
DslExportable e = (DslExportable) ifaceOrDsl;
return e.getCasserMappingEntity();
}
if (ifaceOrDsl instanceof Class) {
Class<?> iface = (Class<?>) ifaceOrDsl;
if (!iface.isInterface()) {
throw new CasserMappingException("class is not an interface " + iface);
}
return entity(iface);
}
throw new CasserMappingException("unknown dsl object or mapping interface " + ifaceOrDsl);
}
}

View file

@ -118,25 +118,18 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
public <E> SelectOperation<E> select(Class<E> entityClass) {
Objects.requireNonNull(entityClass, "entityClass is empty");
Class<?> iface = MappingUtil.getMappingInterface(entityClass);
CasserMappingEntity entity = mappingRepository.getEntity(iface);
ColumnValueProvider valueProvider = getValueProvider();
CasserMappingEntity entity = Casser.entity(entityClass);
return new SelectOperation<E>(this, entity, (r) -> {
Map<String, Object> map = new RowProviderMap(r, valueProvider, entity);
return (E) Casser.map(iface, map);
return (E) Casser.map(entityClass, map);
});
}
public <E> SelectOperation<E> select(Class<E> entityClass, Function<Row, E> rowMapper) {
Objects.requireNonNull(entityClass, "entityClass is empty");
Class<?> iface = MappingUtil.getMappingInterface(entityClass);
CasserMappingEntity entity = mappingRepository.getEntity(iface);
return new SelectOperation<E>(this, entity, rowMapper);
return new SelectOperation<E>(this, Casser.entity(entityClass), rowMapper);
}
public <V1> SelectOperation<Tuple1<V1>> select(Getter<V1> getter1) {
@ -251,12 +244,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
public CountOperation count(Object dsl) {
Objects.requireNonNull(dsl, "dsl is empty");
Class<?> iface = MappingUtil.getMappingInterface(dsl);
CasserMappingEntity entity = mappingRepository.getEntity(iface);
return new CountOperation(this, entity);
return new CountOperation(this, Casser.resolve(dsl));
}
public <V> UpdateOperation update() {
@ -280,8 +268,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
Objects.requireNonNull(pojo, "pojo is empty");
Class<?> iface = MappingUtil.getMappingInterface(pojo);
CasserMappingEntity entity = mappingRepository.getEntity(iface);
CasserMappingEntity entity = Casser.entity(iface);
return new InsertOperation(this, entity, pojo, true);
}
@ -294,8 +281,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
Objects.requireNonNull(pojo, "pojo is empty");
Class<?> iface = MappingUtil.getMappingInterface(pojo);
CasserMappingEntity entity = mappingRepository.getEntity(iface);
CasserMappingEntity entity = Casser.entity(iface);
return new InsertOperation(this, entity, pojo, false);
}
@ -306,12 +292,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
public DeleteOperation delete(Object dsl) {
Objects.requireNonNull(dsl, "dsl is empty");
Class<?> iface = MappingUtil.getMappingInterface(dsl);
CasserMappingEntity entity = mappingRepository.getEntity(iface);
return new DeleteOperation(this, entity);
return new DeleteOperation(this, Casser.resolve(dsl));
}
public Session getSession() {

View file

@ -80,18 +80,15 @@ public final class MappingRepositoryBuilder {
public CasserMappingEntity add(Object dsl, Optional<CasserEntityType> type) {
Class<?> iface = MappingUtil.getMappingInterface(dsl);
CasserMappingEntity casserEntity = Casser.resolve(dsl);
Class<?> iface = casserEntity.getMappingInterface();
CasserMappingEntity entity = entityMap.get(iface);
if (entity == null) {
if (!(dsl instanceof DslExportable)) {
dsl = Casser.dsl(iface);
}
DslExportable e = (DslExportable) dsl;
entity = e.getCasserMappingEntity();
entity = casserEntity;
if (type.isPresent() && entity.getType() != type.get()) {
throw new CasserMappingException("unexpected entity type " + entity.getType() + " for " + entity);

View file

@ -168,19 +168,19 @@ public final class MappingUtil {
.apply(iface.getSimpleName());
}
public static Class<?> getMappingInterface(Object entity) {
public static Class<?> getMappingInterface(Object pojo) {
Class<?> iface = null;
if (entity instanceof Class) {
iface = (Class<?>) entity;
if (pojo instanceof Class) {
iface = (Class<?>) pojo;
if (!iface.isInterface()) {
throw new CasserMappingException("expected interface " + iface);
}
} else {
Class<?>[] ifaces = entity.getClass().getInterfaces();
Class<?>[] ifaces = pojo.getClass().getInterfaces();
int len = ifaces.length;
for (int i = 0; i != len; ++i) {
@ -192,7 +192,8 @@ public final class MappingUtil {
}
if (iface.getDeclaredAnnotation(Table.class) != null ||
iface.getDeclaredAnnotation(UserDefinedType.class) != null) {
iface.getDeclaredAnnotation(UserDefinedType.class) != null ||
iface.getDeclaredAnnotation(Tuple.class) != null) {
break;
@ -204,7 +205,7 @@ public final class MappingUtil {
}
if (iface == null) {
throw new CasserMappingException("dsl interface not found for " + entity);
throw new CasserMappingException("dsl interface not found for " + pojo);
}
return iface;