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.DefaultCasserSettings;
import com.noorq.casser.core.reflect.CasserPropertyNode;
import com.noorq.casser.core.reflect.DslExportable;
import com.noorq.casser.mapping.MappingRepositoryBuilder;
@ -110,5 +112,21 @@ public final class Casser {
public static <E> E map(Class<E> iface, Map<String, Object> src, ClassLoader 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 Executor executor = MoreExecutors.sameThreadExecutor();
private MappingRepositoryBuilder mappingRepository = new MappingRepositoryBuilder();
private MappingRepositoryBuilder mappingRepository;
private boolean dropUnusedColumns = false;
private boolean dropUnusedIndexes = false;
@ -181,6 +181,7 @@ public class SessionInitializer extends AbstractSessionOperations {
Objects.requireNonNull(usingKeyspace, "please define keyspace by 'use' operator");
mappingRepository = Casser.createMappingRepository();
initList.forEach(dsl -> mappingRepository.add(dsl));
TableOperations tableOps = new TableOperations(this, dropUnusedColumns, dropUnusedIndexes);

View file

@ -75,6 +75,8 @@ public final class CountOperation extends AbstractFilterOperation<Long, CountOpe
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());
}
}

View file

@ -15,11 +15,16 @@
*/
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";
CasserMappingEntity getCasserMappingEntity();
CasserPropertyNode getParentDslCasserPropertyNode();
}

View file

@ -70,7 +70,11 @@ public class DslInvocationHandler<E> implements InvocationHandler {
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();
}

View file

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

View file

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

View file

@ -65,6 +65,16 @@ public final class MappingRepositoryBuilder {
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) {
Class<?> iface = MappingUtil.getMappingInterface(dsl);
@ -91,6 +101,8 @@ public final class MappingRepositoryBuilder {
return entity;
}
private void addUserDefinedTypes(Collection<CasserMappingProperty> 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.Getter;
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.support.CasserMappingException;
import com.noorq.casser.support.DslPropertyException;
@ -217,8 +217,8 @@ public final class MappingUtil {
try {
Object childDsl = getter.get();
if (childDsl instanceof DslParentExportable) {
DslParentExportable e = (DslParentExportable) childDsl;
if (childDsl instanceof DslExportable) {
DslExportable e = (DslExportable) childDsl;
return e.getParentDslCasserPropertyNode();
}

View file

@ -81,7 +81,7 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
session.update(user::name, "albert")
.set(user::age, 35)
.where(user::id, Operator.EQ, 123L).sync();
long cnt = session.count(user).where(user::id, Operator.EQ, 123L).sync();
Assert.assertEquals(1L, cnt);