performance improvement, caching was not in effect in rel and partial-entitystate
This commit is contained in:
@@ -21,6 +21,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.data.graph.core.GraphBacked;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -31,8 +32,8 @@ import java.util.Map;
|
||||
public abstract class DefaultEntityState<ENTITY extends GraphBacked<STATE>, STATE> implements EntityState<ENTITY,STATE> {
|
||||
protected final ENTITY entity;
|
||||
protected final Class<? extends ENTITY> type;
|
||||
private final Map<Field, FieldAccessor<ENTITY>> fieldAccessors;
|
||||
private final Map<Field,List<FieldAccessListener<ENTITY,?>>> fieldAccessorListeners;
|
||||
private final Map<Field, FieldAccessor<ENTITY>> fieldAccessors = new HashMap<Field, FieldAccessor<ENTITY>>();
|
||||
private final Map<Field,List<FieldAccessListener<ENTITY,?>>> fieldAccessorListeners = new HashMap<Field, List<FieldAccessListener<ENTITY, ?>>>();
|
||||
private STATE state;
|
||||
protected final static Log log= LogFactory.getLog(DefaultEntityState.class);
|
||||
private final FieldAccessorFactoryProviders<ENTITY> fieldAccessorFactoryProviders;
|
||||
@@ -42,9 +43,13 @@ public abstract class DefaultEntityState<ENTITY extends GraphBacked<STATE>, STAT
|
||||
this.state = underlyingState;
|
||||
this.entity = entity;
|
||||
this.type = type;
|
||||
fieldAccessorFactoryProviders = delegatingFieldAccessorFactory.accessorFactoriesFor(type);
|
||||
this.fieldAccessors = fieldAccessorFactoryProviders.getFieldAccessors();
|
||||
this.fieldAccessorListeners = fieldAccessorFactoryProviders.getFieldAccessListeners();
|
||||
if (delegatingFieldAccessorFactory!=null) {
|
||||
fieldAccessorFactoryProviders = delegatingFieldAccessorFactory.accessorFactoriesFor(type);
|
||||
this.fieldAccessors.putAll(fieldAccessorFactoryProviders.getFieldAccessors());
|
||||
this.fieldAccessorListeners.putAll(fieldAccessorFactoryProviders.getFieldAccessListeners());
|
||||
} else {
|
||||
fieldAccessorFactoryProviders = null; // todo
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.springframework.data.graph.core.NodeBacked;
|
||||
import org.springframework.data.graph.neo4j.repository.DirectGraphRepositoryFactory;
|
||||
import org.springframework.data.graph.neo4j.support.GraphDatabaseContext;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.PersistenceUnitUtil;
|
||||
|
||||
@@ -33,12 +34,14 @@ public class NodeEntityStateFactory {
|
||||
|
||||
private EntityManagerFactory entityManagerFactory;
|
||||
|
||||
private NodeDelegatingFieldAccessorFactory nodeDelegatingFieldAccessorFactory;
|
||||
private NodeDelegatingFieldAccessorFactory nodeDelegatingFieldAccessorFactory;
|
||||
|
||||
public EntityState<NodeBacked,Node> getEntityState(final NodeBacked entity) {
|
||||
private PartialNodeEntityState.PartialNodeDelegatingFieldAccessorFactory delegatingFieldAccessorFactory;
|
||||
|
||||
public EntityState<NodeBacked,Node> getEntityState(final NodeBacked entity) {
|
||||
final NodeEntity graphEntityAnnotation = entity.getClass().getAnnotation(NodeEntity.class); // todo cache ??
|
||||
if (graphEntityAnnotation.partial()) {
|
||||
final PartialNodeEntityState<NodeBacked> partialNodeEntityState = new PartialNodeEntityState<NodeBacked>(null, entity, entity.getClass(), graphDatabaseContext, graphRepositoryFactory,getPersistenceUnitUtils());
|
||||
final PartialNodeEntityState<NodeBacked> partialNodeEntityState = new PartialNodeEntityState<NodeBacked>(null, entity, entity.getClass(), graphDatabaseContext, getPersistenceUnitUtils(), delegatingFieldAccessorFactory);
|
||||
return new DetachedEntityState<NodeBacked, Node>(partialNodeEntityState, graphDatabaseContext) {
|
||||
@Override
|
||||
protected boolean isDetached() {
|
||||
@@ -73,4 +76,9 @@ public class NodeEntityStateFactory {
|
||||
public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
|
||||
this.entityManagerFactory = entityManagerFactory;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void setUp() {
|
||||
this.delegatingFieldAccessorFactory = new PartialNodeEntityState.PartialNodeDelegatingFieldAccessorFactory(graphDatabaseContext, graphRepositoryFactory);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,62 +44,8 @@ public class PartialNodeEntityState<ENTITY extends NodeBacked> extends DefaultEn
|
||||
private final GraphDatabaseContext graphDatabaseContext;
|
||||
private PersistenceUnitUtil persistenceUnitUtil;
|
||||
|
||||
public PartialNodeEntityState(final Node underlyingState, final ENTITY entity, final Class<? extends ENTITY> type, final GraphDatabaseContext graphDatabaseContext, final DirectGraphRepositoryFactory graphRepositoryFactory, PersistenceUnitUtil persistenceUnitUtil) {
|
||||
super(underlyingState, entity, type, new DelegatingFieldAccessorFactory(graphDatabaseContext, graphRepositoryFactory) {
|
||||
|
||||
@Override
|
||||
protected Collection<FieldAccessorListenerFactory<?>> createListenerFactories() {
|
||||
return Arrays.<FieldAccessorListenerFactory<?>>asList(
|
||||
new IndexingPropertyFieldAccessorListenerFactory(
|
||||
getGraphDatabaseContext(),
|
||||
newPropertyFieldAccessorFactory(),
|
||||
newConvertingNodePropertyFieldAccessorFactory()) {
|
||||
@Override
|
||||
public boolean accept(Field f) {
|
||||
return f.isAnnotationPresent(GraphProperty.class) && super.accept(f);
|
||||
}
|
||||
},
|
||||
new JpaIdFieldAccessListenerFactory());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<? extends FieldAccessorFactory<?>> createAccessorFactories() {
|
||||
return Arrays.<FieldAccessorFactory<?>>asList(
|
||||
//new IdFieldAccessorFactory(),
|
||||
//new TransientFieldAccessorFactory(),
|
||||
newPropertyFieldAccessorFactory(),
|
||||
newConvertingNodePropertyFieldAccessorFactory(),
|
||||
new SingleRelationshipFieldAccessorFactory(getGraphDatabaseContext()) {
|
||||
@Override
|
||||
public boolean accept(Field f) {
|
||||
return f.isAnnotationPresent(RelatedTo.class) && super.accept(f);
|
||||
}
|
||||
},
|
||||
new OneToNRelationshipFieldAccessorFactory(getGraphDatabaseContext()),
|
||||
new ReadOnlyOneToNRelationshipFieldAccessorFactory(getGraphDatabaseContext()),
|
||||
new TraversalFieldAccessorFactory(this.graphRepositoryFactory),
|
||||
new OneToNRelationshipEntityFieldAccessorFactory(getGraphDatabaseContext())
|
||||
);
|
||||
}
|
||||
|
||||
private ConvertingNodePropertyFieldAccessorFactory newConvertingNodePropertyFieldAccessorFactory() {
|
||||
return new ConvertingNodePropertyFieldAccessorFactory(getGraphDatabaseContext().getConversionService()) {
|
||||
@Override
|
||||
public boolean accept(Field f) {
|
||||
return f.isAnnotationPresent(GraphProperty.class) && super.accept(f);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private PropertyFieldAccessorFactory newPropertyFieldAccessorFactory() {
|
||||
return new PropertyFieldAccessorFactory(getGraphDatabaseContext().getConversionService()) {
|
||||
@Override
|
||||
public boolean accept(Field f) {
|
||||
return f.isAnnotationPresent(GraphProperty.class) && super.accept(f);
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
public PartialNodeEntityState(final Node underlyingState, final ENTITY entity, final Class<? extends ENTITY> type, final GraphDatabaseContext graphDatabaseContext, PersistenceUnitUtil persistenceUnitUtil, final PartialNodeDelegatingFieldAccessorFactory delegatingFieldAccessorFactory) {
|
||||
super(underlyingState, entity, type, delegatingFieldAccessorFactory);
|
||||
this.graphDatabaseContext = graphDatabaseContext;
|
||||
this.persistenceUnitUtil = persistenceUnitUtil;
|
||||
}
|
||||
@@ -163,4 +109,64 @@ public class PartialNodeEntityState<ENTITY extends NodeBacked> extends DefaultEn
|
||||
public Object getId(final Object entity) {
|
||||
return persistenceUnitUtil!=null ? persistenceUnitUtil.getIdentifier(entity) : null;
|
||||
}
|
||||
|
||||
public static class PartialNodeDelegatingFieldAccessorFactory extends DelegatingFieldAccessorFactory {
|
||||
|
||||
public PartialNodeDelegatingFieldAccessorFactory(GraphDatabaseContext graphDatabaseContext, DirectGraphRepositoryFactory graphRepositoryFactory) {
|
||||
super(graphDatabaseContext, graphRepositoryFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<FieldAccessorListenerFactory<?>> createListenerFactories() {
|
||||
return Arrays.<FieldAccessorListenerFactory<?>>asList(
|
||||
new IndexingPropertyFieldAccessorListenerFactory(
|
||||
getGraphDatabaseContext(),
|
||||
newPropertyFieldAccessorFactory(),
|
||||
newConvertingNodePropertyFieldAccessorFactory()) {
|
||||
@Override
|
||||
public boolean accept(Field f) {
|
||||
return f.isAnnotationPresent(GraphProperty.class) && super.accept(f);
|
||||
}
|
||||
},
|
||||
new JpaIdFieldAccessListenerFactory());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<? extends FieldAccessorFactory<?>> createAccessorFactories() {
|
||||
return Arrays.<FieldAccessorFactory<?>>asList(
|
||||
//new IdFieldAccessorFactory(),
|
||||
//new TransientFieldAccessorFactory(),
|
||||
newPropertyFieldAccessorFactory(),
|
||||
newConvertingNodePropertyFieldAccessorFactory(),
|
||||
new SingleRelationshipFieldAccessorFactory(getGraphDatabaseContext()) {
|
||||
@Override
|
||||
public boolean accept(Field f) {
|
||||
return f.isAnnotationPresent(RelatedTo.class) && super.accept(f);
|
||||
}
|
||||
},
|
||||
new OneToNRelationshipFieldAccessorFactory(getGraphDatabaseContext()),
|
||||
new ReadOnlyOneToNRelationshipFieldAccessorFactory(getGraphDatabaseContext()),
|
||||
new TraversalFieldAccessorFactory(this.graphRepositoryFactory),
|
||||
new OneToNRelationshipEntityFieldAccessorFactory(getGraphDatabaseContext())
|
||||
);
|
||||
}
|
||||
|
||||
private ConvertingNodePropertyFieldAccessorFactory newConvertingNodePropertyFieldAccessorFactory() {
|
||||
return new ConvertingNodePropertyFieldAccessorFactory(getGraphDatabaseContext().getConversionService()) {
|
||||
@Override
|
||||
public boolean accept(Field f) {
|
||||
return f.isAnnotationPresent(GraphProperty.class) && super.accept(f);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private PropertyFieldAccessorFactory newPropertyFieldAccessorFactory() {
|
||||
return new PropertyFieldAccessorFactory(getGraphDatabaseContext().getConversionService()) {
|
||||
@Override
|
||||
public boolean accept(Field f) {
|
||||
return f.isAnnotationPresent(GraphProperty.class) && super.accept(f);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,32 +34,9 @@ public class RelationshipEntityState<ENTITY extends RelationshipBacked> extends
|
||||
|
||||
private final GraphDatabaseContext graphDatabaseContext;
|
||||
|
||||
private final DirectGraphRepositoryFactory graphRepositoryFactory;
|
||||
|
||||
public RelationshipEntityState(final Relationship underlyingState, final ENTITY entity, final Class<? extends ENTITY> type, final GraphDatabaseContext graphDatabaseContext, final DirectGraphRepositoryFactory graphRepositoryFactory) {
|
||||
super(underlyingState, entity, type, new DelegatingFieldAccessorFactory(graphDatabaseContext, graphRepositoryFactory) {
|
||||
@Override
|
||||
protected Collection<FieldAccessorListenerFactory<?>> createListenerFactories() {
|
||||
return Arrays.<FieldAccessorListenerFactory<?>>asList(
|
||||
new IndexingPropertyFieldAccessorListenerFactory(
|
||||
graphDatabaseContext,
|
||||
new PropertyFieldAccessorFactory(graphDatabaseContext.getConversionService()),
|
||||
new ConvertingNodePropertyFieldAccessorFactory(graphDatabaseContext.getConversionService())
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<? extends FieldAccessorFactory<?>> createAccessorFactories() {
|
||||
return Arrays.<FieldAccessorFactory<?>>asList(
|
||||
new TransientFieldAccessorFactory(),
|
||||
new RelationshipNodeFieldAccessorFactory(graphDatabaseContext),
|
||||
new PropertyFieldAccessorFactory(graphDatabaseContext.getConversionService()),
|
||||
new ConvertingNodePropertyFieldAccessorFactory(graphDatabaseContext.getConversionService())
|
||||
);
|
||||
}
|
||||
});
|
||||
public RelationshipEntityState(final Relationship underlyingState, final ENTITY entity, final Class<? extends ENTITY> type, final GraphDatabaseContext graphDatabaseContext, final DirectGraphRepositoryFactory graphRepositoryFactory, final RelationshipStateDelegatingFieldAccessorFactory delegatingFieldAccessorFactory) {
|
||||
super(underlyingState, entity, type, delegatingFieldAccessorFactory);
|
||||
this.graphDatabaseContext = graphDatabaseContext;
|
||||
this.graphRepositoryFactory = graphRepositoryFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -89,4 +66,29 @@ public class RelationshipEntityState<ENTITY extends RelationshipBacked> extends
|
||||
return entity;
|
||||
}
|
||||
|
||||
public static class RelationshipStateDelegatingFieldAccessorFactory extends DelegatingFieldAccessorFactory {
|
||||
public RelationshipStateDelegatingFieldAccessorFactory(GraphDatabaseContext graphDatabaseContext, DirectGraphRepositoryFactory graphRepositoryFactory) {
|
||||
super(graphDatabaseContext, graphRepositoryFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<FieldAccessorListenerFactory<?>> createListenerFactories() {
|
||||
return Arrays.<FieldAccessorListenerFactory<?>>asList(
|
||||
new IndexingPropertyFieldAccessorListenerFactory(
|
||||
graphDatabaseContext,
|
||||
new PropertyFieldAccessorFactory(graphDatabaseContext.getConversionService()),
|
||||
new ConvertingNodePropertyFieldAccessorFactory(graphDatabaseContext.getConversionService())
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<? extends FieldAccessorFactory<?>> createAccessorFactories() {
|
||||
return Arrays.<FieldAccessorFactory<?>>asList(
|
||||
new TransientFieldAccessorFactory(),
|
||||
new RelationshipNodeFieldAccessorFactory(graphDatabaseContext),
|
||||
new PropertyFieldAccessorFactory(graphDatabaseContext.getConversionService()),
|
||||
new ConvertingNodePropertyFieldAccessorFactory(graphDatabaseContext.getConversionService())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,14 +21,17 @@ import org.springframework.data.graph.core.RelationshipBacked;
|
||||
import org.springframework.data.graph.neo4j.repository.DirectGraphRepositoryFactory;
|
||||
import org.springframework.data.graph.neo4j.support.GraphDatabaseContext;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
public class RelationshipEntityStateFactory {
|
||||
|
||||
private GraphDatabaseContext graphDatabaseContext;
|
||||
|
||||
private DirectGraphRepositoryFactory graphRepositoryFactory;
|
||||
private DirectGraphRepositoryFactory graphRepositoryFactory;
|
||||
private RelationshipEntityState.RelationshipStateDelegatingFieldAccessorFactory delegatingFieldAccessorFactory;
|
||||
|
||||
public EntityState<RelationshipBacked, Relationship> getEntityState(final RelationshipBacked entity) {
|
||||
return new RelationshipEntityState<RelationshipBacked>(null,entity,entity.getClass(), graphDatabaseContext, graphRepositoryFactory);
|
||||
public EntityState<RelationshipBacked, Relationship> getEntityState(final RelationshipBacked entity) {
|
||||
return new RelationshipEntityState<RelationshipBacked>(null,entity,entity.getClass(), graphDatabaseContext, graphRepositoryFactory, delegatingFieldAccessorFactory);
|
||||
}
|
||||
|
||||
public void setGraphDatabaseContext(GraphDatabaseContext graphDatabaseContext) {
|
||||
@@ -39,4 +42,9 @@ public class RelationshipEntityStateFactory {
|
||||
this.graphRepositoryFactory = graphRepositoryFactory;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void setUp() {
|
||||
this.delegatingFieldAccessorFactory = new RelationshipEntityState.RelationshipStateDelegatingFieldAccessorFactory(graphDatabaseContext, graphRepositoryFactory);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -41,8 +41,6 @@ public aspect Neo4jRelationshipBacking {
|
||||
protected final Log log = LogFactory.getLog(getClass());
|
||||
|
||||
declare parents : (@RelationshipEntity *) implements RelationshipBacked;
|
||||
declare @type: RelationshipBacked+: @Configurable;
|
||||
|
||||
|
||||
protected pointcut entityFieldGet(RelationshipBacked entity) :
|
||||
get(* RelationshipBacked+.*) &&
|
||||
|
||||
Reference in New Issue
Block a user