add MappingEntityFactory to cache in memory all known entities
This commit is contained in:
parent
8956a2b55c
commit
0adda4f6c1
5 changed files with 48 additions and 12 deletions
|
@ -14,9 +14,9 @@ public abstract class AbstractSessionOperations {
|
|||
|
||||
final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
abstract Session currentSession();
|
||||
abstract public Session currentSession();
|
||||
|
||||
abstract boolean isShowCql();
|
||||
abstract public boolean isShowCql();
|
||||
|
||||
public ResultSet execute(String cql) {
|
||||
|
||||
|
|
|
@ -26,20 +26,22 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
|
|||
private final Session session;
|
||||
private final boolean showCql;
|
||||
private final Set<CasserMappingEntity<?>> dropEntitiesOnClose;
|
||||
private final MappingEntityFactory entityFactory;
|
||||
|
||||
CasserSession(Session session, boolean showCql, Set<CasserMappingEntity<?>> dropEntitiesOnClose) {
|
||||
CasserSession(Session session, boolean showCql, Set<CasserMappingEntity<?>> dropEntitiesOnClose, MappingEntityFactory mappingEntityFactory) {
|
||||
this.session = session;
|
||||
this.showCql = showCql;
|
||||
this.dropEntitiesOnClose = dropEntitiesOnClose;
|
||||
this.entityFactory = mappingEntityFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
Session currentSession() {
|
||||
public Session currentSession() {
|
||||
return session;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean isShowCql() {
|
||||
public boolean isShowCql() {
|
||||
return showCql;
|
||||
}
|
||||
|
||||
|
@ -66,7 +68,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
|
|||
|
||||
Class<?> iface = MappingUtil.getMappingInterface(pojo);
|
||||
|
||||
CasserMappingEntity<?> entity = new CasserMappingEntity(iface);
|
||||
CasserMappingEntity<?> entity = entityFactory.getEntity(iface);
|
||||
|
||||
return new UpsertOperation(this, entity, pojo);
|
||||
}
|
||||
|
@ -75,7 +77,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
|
|||
|
||||
Class<?> iface = MappingUtil.getMappingInterface(dsl);
|
||||
|
||||
CasserMappingEntity<?> entity = new CasserMappingEntity(iface);
|
||||
CasserMappingEntity<?> entity = entityFactory.getEntity(iface);
|
||||
|
||||
return new DeleteOperation(this, entity);
|
||||
}
|
||||
|
|
28
src/main/java/casser/core/MappingEntityFactory.java
Normal file
28
src/main/java/casser/core/MappingEntityFactory.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
package casser.core;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import casser.mapping.CasserMappingEntity;
|
||||
|
||||
public class MappingEntityFactory {
|
||||
|
||||
private ConcurrentMap<Class<?>, CasserMappingEntity<?>> knownEntities = new ConcurrentHashMap<Class<?>, CasserMappingEntity<?>>();
|
||||
|
||||
public CasserMappingEntity<?> getEntity(Class<?> iface) {
|
||||
|
||||
CasserMappingEntity<?> entity = knownEntities.get(iface);
|
||||
|
||||
if (entity == null) {
|
||||
entity = new CasserMappingEntity(iface);
|
||||
|
||||
CasserMappingEntity<?> c = knownEntities.putIfAbsent(iface, entity);
|
||||
if (c != null) {
|
||||
entity = c;
|
||||
}
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
}
|
|
@ -22,6 +22,10 @@ public final class SchemaUtil {
|
|||
private SchemaUtil() {
|
||||
}
|
||||
|
||||
public static String useCql(String keyspace) {
|
||||
return "USE " + keyspace;
|
||||
}
|
||||
|
||||
public static String createTableCql(CasserMappingEntity<?> entity) {
|
||||
|
||||
Create create = SchemaBuilder.createTable(entity.getTableName());
|
||||
|
|
|
@ -17,6 +17,8 @@ public class SessionInitializer extends AbstractSessionOperations {
|
|||
private boolean showCql = false;
|
||||
private Set<CasserMappingEntity<?>> dropEntitiesOnClose = null;
|
||||
|
||||
private MappingEntityFactory entityFactory = new MappingEntityFactory();
|
||||
|
||||
private boolean dropRemovedColumns = false;
|
||||
|
||||
SessionInitializer(Session session) {
|
||||
|
@ -29,7 +31,7 @@ public class SessionInitializer extends AbstractSessionOperations {
|
|||
}
|
||||
|
||||
@Override
|
||||
Session currentSession() {
|
||||
public Session currentSession() {
|
||||
return session;
|
||||
}
|
||||
|
||||
|
@ -49,7 +51,7 @@ public class SessionInitializer extends AbstractSessionOperations {
|
|||
}
|
||||
|
||||
@Override
|
||||
boolean isShowCql() {
|
||||
public boolean isShowCql() {
|
||||
return showCql;
|
||||
}
|
||||
|
||||
|
@ -74,12 +76,12 @@ public class SessionInitializer extends AbstractSessionOperations {
|
|||
}
|
||||
|
||||
public SessionInitializer use(String keyspace) {
|
||||
session.execute("USE " + keyspace);
|
||||
session.execute(SchemaUtil.useCql(keyspace));
|
||||
return this;
|
||||
}
|
||||
|
||||
public CasserSession get() {
|
||||
return new CasserSession(session, showCql, dropEntitiesOnClose);
|
||||
return new CasserSession(session, showCql, dropEntitiesOnClose, entityFactory);
|
||||
}
|
||||
|
||||
private enum AutoDdl {
|
||||
|
@ -101,7 +103,7 @@ public class SessionInitializer extends AbstractSessionOperations {
|
|||
|
||||
Class<?> iface = MappingUtil.getMappingInterface(dsl);
|
||||
|
||||
CasserMappingEntity<?> entity = new CasserMappingEntity(iface);
|
||||
CasserMappingEntity<?> entity = entityFactory.getEntity(iface);
|
||||
|
||||
if (type == AutoDdl.CREATE || type == AutoDdl.CREATE_DROP) {
|
||||
createNewTable(entity);
|
||||
|
|
Loading…
Reference in a new issue