From 6b50e86dde00300d3b2c4e40125830c3ed5f6e93 Mon Sep 17 00:00:00 2001 From: Albert Shift Date: Thu, 9 Apr 2015 22:18:50 -0700 Subject: [PATCH] make casser as a central entity cache --- .../java/com/noorq/casser/core/Casser.java | 40 ++++++++++++++++++- .../com/noorq/casser/core/CasserSession.java | 35 ++++------------ .../mapping/MappingRepositoryBuilder.java | 13 +++--- .../com/noorq/casser/mapping/MappingUtil.java | 13 +++--- 4 files changed, 59 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/noorq/casser/core/Casser.java b/src/main/java/com/noorq/casser/core/Casser.java index 9ae5522..b12efc5 100644 --- a/src/main/java/com/noorq/casser/core/Casser.java +++ b/src/main/java/com/noorq/casser/core/Casser.java @@ -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); + } + } diff --git a/src/main/java/com/noorq/casser/core/CasserSession.java b/src/main/java/com/noorq/casser/core/CasserSession.java index 1a2beeb..e742e8b 100644 --- a/src/main/java/com/noorq/casser/core/CasserSession.java +++ b/src/main/java/com/noorq/casser/core/CasserSession.java @@ -117,26 +117,19 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl } public SelectOperation select(Class entityClass) { - Objects.requireNonNull(entityClass, "entityClass is empty"); - - Class iface = MappingUtil.getMappingInterface(entityClass); - CasserMappingEntity entity = mappingRepository.getEntity(iface); + Objects.requireNonNull(entityClass, "entityClass is empty"); ColumnValueProvider valueProvider = getValueProvider(); - + CasserMappingEntity entity = Casser.entity(entityClass); return new SelectOperation(this, entity, (r) -> { Map map = new RowProviderMap(r, valueProvider, entity); - return (E) Casser.map(iface, map); + return (E) Casser.map(entityClass, map); }); } public SelectOperation select(Class entityClass, Function rowMapper) { Objects.requireNonNull(entityClass, "entityClass is empty"); - - Class iface = MappingUtil.getMappingInterface(entityClass); - CasserMappingEntity entity = mappingRepository.getEntity(iface); - - return new SelectOperation(this, entity, rowMapper); + return new SelectOperation(this, Casser.entity(entityClass), rowMapper); } public SelectOperation> select(Getter 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 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() { diff --git a/src/main/java/com/noorq/casser/mapping/MappingRepositoryBuilder.java b/src/main/java/com/noorq/casser/mapping/MappingRepositoryBuilder.java index 8c206a2..46ce420 100644 --- a/src/main/java/com/noorq/casser/mapping/MappingRepositoryBuilder.java +++ b/src/main/java/com/noorq/casser/mapping/MappingRepositoryBuilder.java @@ -80,18 +80,15 @@ public final class MappingRepositoryBuilder { public CasserMappingEntity add(Object dsl, Optional 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); diff --git a/src/main/java/com/noorq/casser/mapping/MappingUtil.java b/src/main/java/com/noorq/casser/mapping/MappingUtil.java index 094c800..fd8763e 100644 --- a/src/main/java/com/noorq/casser/mapping/MappingUtil.java +++ b/src/main/java/com/noorq/casser/mapping/MappingUtil.java @@ -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;