singletons for all dsl support

This commit is contained in:
Albert Shift 2015-04-09 19:14:29 -07:00
parent 6e59ccdb29
commit 4a309b10d0
10 changed files with 53 additions and 8 deletions

View file

@ -25,6 +25,8 @@ import com.datastax.driver.core.Session;
import com.noorq.casser.config.CasserSettings; import com.noorq.casser.config.CasserSettings;
import com.noorq.casser.config.DefaultCasserSettings; import com.noorq.casser.config.DefaultCasserSettings;
import com.noorq.casser.core.reflect.CasserPropertyNode; import com.noorq.casser.core.reflect.CasserPropertyNode;
import com.noorq.casser.core.reflect.DslExportable;
import com.noorq.casser.mapping.MappingRepositoryBuilder;
@ -111,4 +113,20 @@ public final class Casser {
return settings.getMapperInstantiator().instantiate(iface, src, classLoader); return settings.getMapperInstantiator().instantiate(iface, src, classLoader);
} }
protected static MappingRepositoryBuilder createMappingRepository() {
MappingRepositoryBuilder builder = new MappingRepositoryBuilder();
for (Object dslProxy : dslCache.values()) {
if (dslProxy instanceof DslExportable) {
DslExportable e = (DslExportable) dslProxy;
builder.addEntity(e.getCasserMappingEntity());
}
}
return builder;
}
} }

View file

@ -44,7 +44,7 @@ public class SessionInitializer extends AbstractSessionOperations {
private boolean showCql = false; private boolean showCql = false;
private Executor executor = MoreExecutors.sameThreadExecutor(); private Executor executor = MoreExecutors.sameThreadExecutor();
private MappingRepositoryBuilder mappingRepository = new MappingRepositoryBuilder(); private MappingRepositoryBuilder mappingRepository;
private boolean dropUnusedColumns = false; private boolean dropUnusedColumns = false;
private boolean dropUnusedIndexes = false; private boolean dropUnusedIndexes = false;
@ -181,6 +181,7 @@ public class SessionInitializer extends AbstractSessionOperations {
Objects.requireNonNull(usingKeyspace, "please define keyspace by 'use' operator"); Objects.requireNonNull(usingKeyspace, "please define keyspace by 'use' operator");
mappingRepository = Casser.createMappingRepository();
initList.forEach(dsl -> mappingRepository.add(dsl)); initList.forEach(dsl -> mappingRepository.add(dsl));
TableOperations tableOps = new TableOperations(this, dropUnusedColumns, dropUnusedIndexes); TableOperations tableOps = new TableOperations(this, dropUnusedColumns, dropUnusedIndexes);

View file

@ -75,6 +75,8 @@ public final class CountOperation extends AbstractFilterOperation<Long, CountOpe
entity = p.getEntity(); entity = p.getEntity();
} }
else if (entity != p.getEntity()) { else if (entity != p.getEntity()) {
System.out.println("entity1=" + entity.hashCode());
System.out.println("entity2=" + p.getEntity().hashCode());
throw new CasserMappingException("you can count columns only in single entity " + entity.getMappingInterface() + " or " + p.getEntity().getMappingInterface()); throw new CasserMappingException("you can count columns only in single entity " + entity.getMappingInterface() + " or " + p.getEntity().getMappingInterface());
} }
} }

View file

@ -15,11 +15,16 @@
*/ */
package com.noorq.casser.core.reflect; package com.noorq.casser.core.reflect;
import com.noorq.casser.mapping.CasserMappingEntity;
public interface DslParentExportable {
public interface DslExportable {
public static final String GET_ENTITY_METHOD = "getCasserMappingEntity";
public static final String GET_PARENT_METHOD = "getParentDslCasserPropertyNode"; public static final String GET_PARENT_METHOD = "getParentDslCasserPropertyNode";
CasserMappingEntity getCasserMappingEntity();
CasserPropertyNode getParentDslCasserPropertyNode(); CasserPropertyNode getParentDslCasserPropertyNode();
} }

View file

@ -70,7 +70,11 @@ public class DslInvocationHandler<E> implements InvocationHandler {
return entity.toString(); return entity.toString();
} }
if (DslParentExportable.GET_PARENT_METHOD.equals(method.getName())) { if (DslExportable.GET_ENTITY_METHOD.equals(method.getName())) {
return entity;
}
if (DslExportable.GET_PARENT_METHOD.equals(method.getName())) {
return parent.get(); return parent.get();
} }

View file

@ -30,7 +30,7 @@ public enum ReflectionDslInstantiator implements DslInstantiator {
DslInvocationHandler<E> handler = new DslInvocationHandler<E>(iface, classLoader, parent); DslInvocationHandler<E> handler = new DslInvocationHandler<E>(iface, classLoader, parent);
E proxy = (E) Proxy.newProxyInstance( E proxy = (E) Proxy.newProxyInstance(
classLoader, classLoader,
new Class[] { iface, DslParentExportable.class }, new Class[] { iface, DslExportable.class },
handler); handler);
return proxy; return proxy;
} }

View file

@ -37,6 +37,9 @@ public class CasserMappingRepository {
.putAll(builder.getEntityMap()) .putAll(builder.getEntityMap())
.build(); .build();
for (CasserMappingEntity e : entityMap.values()) {
System.out.println("e = " + e.getMappingInterface() + ", h = " + e.hashCode());
}
} }
public UserType findUserType(String name) { public UserType findUserType(String name) {

View file

@ -65,6 +65,16 @@ public final class MappingRepositoryBuilder {
return add(dsl, Optional.empty()); return add(dsl, Optional.empty());
} }
public void addEntity(CasserMappingEntity entity) {
CasserMappingEntity concurrentEntity = entityMap.putIfAbsent(entity.getMappingInterface(), entity);
if (concurrentEntity == null) {
addUserDefinedTypes(entity.getMappingProperties());
}
}
public CasserMappingEntity add(Object dsl, Optional<CasserEntityType> type) { public CasserMappingEntity add(Object dsl, Optional<CasserEntityType> type) {
Class<?> iface = MappingUtil.getMappingInterface(dsl); Class<?> iface = MappingUtil.getMappingInterface(dsl);
@ -91,6 +101,8 @@ public final class MappingRepositoryBuilder {
return entity; return entity;
} }
private void addUserDefinedTypes(Collection<CasserMappingProperty> props) { private void addUserDefinedTypes(Collection<CasserMappingProperty> props) {
for (CasserMappingProperty prop : props) { for (CasserMappingProperty prop : props) {

View file

@ -21,7 +21,7 @@ import java.util.Optional;
import com.noorq.casser.core.Casser; import com.noorq.casser.core.Casser;
import com.noorq.casser.core.Getter; import com.noorq.casser.core.Getter;
import com.noorq.casser.core.reflect.CasserPropertyNode; import com.noorq.casser.core.reflect.CasserPropertyNode;
import com.noorq.casser.core.reflect.DslParentExportable; import com.noorq.casser.core.reflect.DslExportable;
import com.noorq.casser.core.reflect.MapExportable; import com.noorq.casser.core.reflect.MapExportable;
import com.noorq.casser.support.CasserMappingException; import com.noorq.casser.support.CasserMappingException;
import com.noorq.casser.support.DslPropertyException; import com.noorq.casser.support.DslPropertyException;
@ -217,8 +217,8 @@ public final class MappingUtil {
try { try {
Object childDsl = getter.get(); Object childDsl = getter.get();
if (childDsl instanceof DslParentExportable) { if (childDsl instanceof DslExportable) {
DslParentExportable e = (DslParentExportable) childDsl; DslExportable e = (DslExportable) childDsl;
return e.getParentDslCasserPropertyNode(); return e.getParentDslCasserPropertyNode();
} }