DATAGRAPH-387 : LabelingNodeTypeRepresentationStrategy - renaming, cypher statement updates & ResourceIterableClosableIterable update
This commit is contained in:
committed by
Michael Hunger
parent
9895bdacac
commit
400f195680
@@ -31,7 +31,7 @@ import org.springframework.data.neo4j.aspects.support.EntityTestBase;
|
||||
import org.springframework.data.neo4j.support.Neo4jTemplate;
|
||||
import org.springframework.data.neo4j.support.mapping.Neo4jMappingContext;
|
||||
import org.springframework.data.neo4j.support.mapping.StoredEntityType;
|
||||
import org.springframework.data.neo4j.support.typerepresentation.IndexingNodeTypeRepresentationStrategy;
|
||||
import org.springframework.data.neo4j.support.typerepresentation.IndexBasedNodeTypeRepresentationStrategy;
|
||||
import org.springframework.test.context.CleanContextCacheTestExecutionListener;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
@@ -51,10 +51,10 @@ import static org.junit.Assert.assertNull;
|
||||
@ContextConfiguration(locations = {"classpath:org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTests-context.xml",
|
||||
"classpath:org/springframework/data/neo4j/aspects/support/IndexingTypeRepresentationStrategyOverride-context.xml"})
|
||||
@TestExecutionListeners({CleanContextCacheTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class})
|
||||
public class IndexingNodeTypeRepresentationStrategyTests extends EntityTestBase {
|
||||
public class IndexBasedNodeTypeRepresentationStrategyTests extends EntityTestBase {
|
||||
|
||||
@Autowired
|
||||
private IndexingNodeTypeRepresentationStrategy nodeTypeRepresentationStrategy;
|
||||
private IndexBasedNodeTypeRepresentationStrategy nodeTypeRepresentationStrategy;
|
||||
|
||||
@Autowired
|
||||
Neo4jTemplate neo4jTemplate;
|
||||
@@ -83,13 +83,13 @@ public class IndexingNodeTypeRepresentationStrategyTests extends EntityTestBase
|
||||
@Test
|
||||
@Transactional
|
||||
public void testPostEntityCreation() throws Exception {
|
||||
Index<Node> typesIndex = graphDatabaseService.index().forNodes(IndexingNodeTypeRepresentationStrategy.INDEX_NAME);
|
||||
IndexHits<Node> thingHits = typesIndex.get(IndexingNodeTypeRepresentationStrategy.INDEX_KEY, thingType.getAlias());
|
||||
Index<Node> typesIndex = graphDatabaseService.index().forNodes(IndexBasedNodeTypeRepresentationStrategy.INDEX_NAME);
|
||||
IndexHits<Node> thingHits = typesIndex.get(IndexBasedNodeTypeRepresentationStrategy.INDEX_KEY, thingType.getAlias());
|
||||
assertEquals(set(node(thing), node(subThing)), IteratorUtil.addToCollection((Iterable<Node>)thingHits, new HashSet<Node>()));
|
||||
IndexHits<Node> subThingHits = typesIndex.get(IndexingNodeTypeRepresentationStrategy.INDEX_KEY, subThingType.getAlias());
|
||||
IndexHits<Node> subThingHits = typesIndex.get(IndexBasedNodeTypeRepresentationStrategy.INDEX_KEY, subThingType.getAlias());
|
||||
assertEquals(node(subThing), subThingHits.getSingle());
|
||||
assertEquals(thingType.getAlias(), node(thing).getProperty(IndexingNodeTypeRepresentationStrategy.TYPE_PROPERTY_NAME));
|
||||
assertEquals(subThingType.getAlias(), node(subThing).getProperty(IndexingNodeTypeRepresentationStrategy.TYPE_PROPERTY_NAME));
|
||||
assertEquals(thingType.getAlias(), node(thing).getProperty(IndexBasedNodeTypeRepresentationStrategy.TYPE_PROPERTY_NAME));
|
||||
assertEquals(subThingType.getAlias(), node(subThing).getProperty(IndexBasedNodeTypeRepresentationStrategy.TYPE_PROPERTY_NAME));
|
||||
thingHits.close();
|
||||
subThingHits.close();
|
||||
}
|
||||
@@ -102,7 +102,7 @@ public class IndexingNodeTypeRepresentationStrategyTests extends EntityTestBase
|
||||
IndexHits<Node> thingHits;
|
||||
IndexHits<Node> subThingHits;
|
||||
try (Transaction tx = graphDatabaseService.beginTx()) {
|
||||
typesIndex = graphDatabaseService.index().forNodes(IndexingNodeTypeRepresentationStrategy.INDEX_NAME);
|
||||
typesIndex = graphDatabaseService.index().forNodes(IndexBasedNodeTypeRepresentationStrategy.INDEX_NAME);
|
||||
tx.success();
|
||||
}
|
||||
|
||||
@@ -112,9 +112,9 @@ public class IndexingNodeTypeRepresentationStrategyTests extends EntityTestBase
|
||||
}
|
||||
|
||||
try (Transaction tx = graphDatabaseService.beginTx()) {
|
||||
thingHits = typesIndex.get(IndexingNodeTypeRepresentationStrategy.INDEX_KEY, thingType.getAlias());
|
||||
thingHits = typesIndex.get(IndexBasedNodeTypeRepresentationStrategy.INDEX_KEY, thingType.getAlias());
|
||||
assertEquals(node(subThing), thingHits.getSingle());
|
||||
subThingHits = typesIndex.get(IndexingNodeTypeRepresentationStrategy.INDEX_KEY, subThingType.getAlias());
|
||||
subThingHits = typesIndex.get(IndexBasedNodeTypeRepresentationStrategy.INDEX_KEY, subThingType.getAlias());
|
||||
assertEquals(node(subThing), subThingHits.getSingle());
|
||||
tx.success();
|
||||
}
|
||||
@@ -125,9 +125,9 @@ public class IndexingNodeTypeRepresentationStrategyTests extends EntityTestBase
|
||||
}
|
||||
|
||||
try (Transaction tx = graphDatabaseService.beginTx()) {
|
||||
thingHits = typesIndex.get(IndexingNodeTypeRepresentationStrategy.INDEX_KEY, thingType.getAlias());
|
||||
thingHits = typesIndex.get(IndexBasedNodeTypeRepresentationStrategy.INDEX_KEY, thingType.getAlias());
|
||||
assertNull(thingHits.getSingle());
|
||||
subThingHits = typesIndex.get(IndexingNodeTypeRepresentationStrategy.INDEX_KEY, subThingType.getAlias());
|
||||
subThingHits = typesIndex.get(IndexBasedNodeTypeRepresentationStrategy.INDEX_KEY, subThingType.getAlias());
|
||||
assertNull(subThingHits.getSingle());
|
||||
tx.success();
|
||||
}
|
||||
@@ -31,8 +31,8 @@ import org.springframework.data.neo4j.aspects.support.EntityTestBase;
|
||||
import org.springframework.data.neo4j.support.Neo4jTemplate;
|
||||
import org.springframework.data.neo4j.support.mapping.EntityStateHandler;
|
||||
import org.springframework.data.neo4j.support.node.Neo4jHelper;
|
||||
import org.springframework.data.neo4j.support.typerepresentation.IndexingNodeTypeRepresentationStrategy;
|
||||
import org.springframework.data.neo4j.support.typerepresentation.IndexingRelationshipTypeRepresentationStrategy;
|
||||
import org.springframework.data.neo4j.support.typerepresentation.IndexBasedNodeTypeRepresentationStrategy;
|
||||
import org.springframework.data.neo4j.support.typerepresentation.IndexBasedRelationshipTypeRepresentationStrategy;
|
||||
import org.springframework.test.context.CleanContextCacheTestExecutionListener;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
@@ -53,10 +53,10 @@ import static org.neo4j.helpers.collection.IteratorUtil.first;
|
||||
@ContextConfiguration(locations = {"classpath:org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTests-context.xml",
|
||||
"classpath:org/springframework/data/neo4j/aspects/support/IndexingTypeRepresentationStrategyOverride-context.xml"})
|
||||
@TestExecutionListeners({CleanContextCacheTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class})
|
||||
public class IndexingRelationshipTypeRepresentationStrategyTests extends EntityTestBase {
|
||||
public class IndexBasedRelationshipTypeRepresentationStrategyTests extends EntityTestBase {
|
||||
|
||||
@Autowired
|
||||
private IndexingRelationshipTypeRepresentationStrategy relationshipTypeRepresentationStrategy;
|
||||
private IndexBasedRelationshipTypeRepresentationStrategy relationshipTypeRepresentationStrategy;
|
||||
|
||||
@Autowired EntityStateHandler entityStateHandler;
|
||||
@Autowired Neo4jTemplate neo4jTemplate;
|
||||
@@ -78,9 +78,9 @@ public class IndexingRelationshipTypeRepresentationStrategyTests extends EntityT
|
||||
@Test
|
||||
@Transactional
|
||||
public void testPostEntityCreationOfRelationshipBacked() throws Exception {
|
||||
Index<Relationship> typesIndex = graphDatabaseService.index().forRelationships(IndexingRelationshipTypeRepresentationStrategy.INDEX_NAME);
|
||||
Index<Relationship> typesIndex = graphDatabaseService.index().forRelationships(IndexBasedRelationshipTypeRepresentationStrategy.INDEX_NAME);
|
||||
final Object alias = neo4jTemplate.getEntityType(Link.class).getAlias();
|
||||
IndexHits<Relationship> linkHits = typesIndex.get(IndexingNodeTypeRepresentationStrategy.INDEX_KEY, alias);
|
||||
IndexHits<Relationship> linkHits = typesIndex.get(IndexBasedNodeTypeRepresentationStrategy.INDEX_KEY, alias);
|
||||
Relationship rel = linkHits.getSingle();
|
||||
assertEquals(rel(link), rel);
|
||||
assertEquals(alias, rel.getProperty("__type__"));
|
||||
@@ -98,8 +98,8 @@ public class IndexingRelationshipTypeRepresentationStrategyTests extends EntityT
|
||||
}
|
||||
|
||||
try (Transaction tx = graphDatabaseService.beginTx()) {
|
||||
Index<Relationship> typesIndex = graphDatabaseService.index().forRelationships(IndexingNodeTypeRepresentationStrategy.INDEX_NAME);
|
||||
IndexHits<Relationship> linkHits = typesIndex.get(IndexingNodeTypeRepresentationStrategy.INDEX_KEY, link.getClass().getName());
|
||||
Index<Relationship> typesIndex = graphDatabaseService.index().forRelationships(IndexBasedNodeTypeRepresentationStrategy.INDEX_NAME);
|
||||
IndexHits<Relationship> linkHits = typesIndex.get(IndexBasedNodeTypeRepresentationStrategy.INDEX_KEY, link.getClass().getName());
|
||||
assertNull(linkHits.getSingle());
|
||||
tx.success();
|
||||
}
|
||||
@@ -30,7 +30,7 @@ import org.springframework.data.neo4j.aspects.support.EntityTestBase;
|
||||
import org.springframework.data.neo4j.support.Neo4jTemplate;
|
||||
import org.springframework.data.neo4j.support.mapping.Neo4jMappingContext;
|
||||
import org.springframework.data.neo4j.support.mapping.StoredEntityType;
|
||||
import org.springframework.data.neo4j.support.typerepresentation.LabelingNodeTypeRepresentationStrategy;
|
||||
import org.springframework.data.neo4j.support.typerepresentation.LabelBasedNodeTypeRepresentationStrategy;
|
||||
import org.springframework.test.context.CleanContextCacheTestExecutionListener;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
@@ -49,10 +49,10 @@ import static org.junit.Assert.*;
|
||||
@ContextConfiguration(locations = {"classpath:org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTests-context.xml",
|
||||
"classpath:org/springframework/data/neo4j/aspects/support/LabelingTypeRepresentationStrategyOverride-context.xml"})
|
||||
@TestExecutionListeners({CleanContextCacheTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class})
|
||||
public class LabelingNodeTypeRepresentationStrategyTests extends EntityTestBase {
|
||||
public class LabelBasedNodeTypeRepresentationStrategyTests extends EntityTestBase {
|
||||
|
||||
@Autowired
|
||||
private LabelingNodeTypeRepresentationStrategy nodeTypeRepresentationStrategy;
|
||||
private LabelBasedNodeTypeRepresentationStrategy nodeTypeRepresentationStrategy;
|
||||
|
||||
@Autowired
|
||||
Neo4jTemplate neo4jTemplate;
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.springframework.data.neo4j.support.mapping;
|
||||
|
||||
import com.tinkerpop.gremlin.Tokens;
|
||||
import org.neo4j.graphdb.Node;
|
||||
import org.neo4j.graphdb.ResourceIterable;
|
||||
import org.neo4j.graphdb.ResourceIterator;
|
||||
@@ -11,9 +12,10 @@ import java.util.Iterator;
|
||||
* @author Nicki Watt
|
||||
* @since 24-09-2013
|
||||
*/
|
||||
public class ResourceIterableClosableIterable implements ClosableIterable {
|
||||
public class ResourceIterableClosableIterable implements ClosableIterable , ResourceIterable {
|
||||
|
||||
private ResourceIterator iterator;
|
||||
|
||||
public ResourceIterableClosableIterable(ResourceIterable<Node> resourceIterable) {
|
||||
this.iterator = resourceIterable.iterator();
|
||||
}
|
||||
@@ -24,7 +26,7 @@ public class ResourceIterableClosableIterable implements ClosableIterable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator iterator() {
|
||||
public ResourceIterator iterator() {
|
||||
return iterator;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.springframework.data.neo4j.support.mapping.StoredEntityType;
|
||||
|
||||
import java.lang.Object;
|
||||
|
||||
public abstract class AbstractIndexingTypeRepresentationStrategy<S extends PropertyContainer> implements
|
||||
public abstract class AbstractIndexBasedTypeRepresentationStrategy<S extends PropertyContainer> implements
|
||||
TypeRepresentationStrategy<S> {
|
||||
|
||||
public static final String TYPE_PROPERTY_NAME = "__type__";
|
||||
@@ -40,8 +40,8 @@ public abstract class AbstractIndexingTypeRepresentationStrategy<S extends Prope
|
||||
private final Class<? extends PropertyContainer> clazz;
|
||||
private Index<S> typesIndex;
|
||||
|
||||
public AbstractIndexingTypeRepresentationStrategy(GraphDatabase graphDb, IndexProvider indexProvider,
|
||||
final String indexName, final Class<? extends PropertyContainer> clazz) {
|
||||
public AbstractIndexBasedTypeRepresentationStrategy(GraphDatabase graphDb, IndexProvider indexProvider,
|
||||
final String indexName, final Class<? extends PropertyContainer> clazz) {
|
||||
this.graphDb = graphDb;
|
||||
this.indexProvider = indexProvider;
|
||||
INDEX_NAME = indexName;
|
||||
@@ -21,12 +21,12 @@ import org.springframework.data.neo4j.core.GraphDatabase;
|
||||
import org.springframework.data.neo4j.core.NodeTypeRepresentationStrategy;
|
||||
import org.springframework.data.neo4j.support.index.IndexProvider;
|
||||
|
||||
public class IndexingNodeTypeRepresentationStrategy extends AbstractIndexingTypeRepresentationStrategy<Node> implements
|
||||
public class IndexBasedNodeTypeRepresentationStrategy extends AbstractIndexBasedTypeRepresentationStrategy<Node> implements
|
||||
NodeTypeRepresentationStrategy {
|
||||
|
||||
public static final String INDEX_NAME = "__types__";
|
||||
|
||||
public IndexingNodeTypeRepresentationStrategy(GraphDatabase graphDb, IndexProvider indexProvider) {
|
||||
public IndexBasedNodeTypeRepresentationStrategy(GraphDatabase graphDb, IndexProvider indexProvider) {
|
||||
super(graphDb, indexProvider, INDEX_NAME, Node.class);
|
||||
}
|
||||
}
|
||||
@@ -21,12 +21,12 @@ import org.springframework.data.neo4j.core.GraphDatabase;
|
||||
import org.springframework.data.neo4j.core.RelationshipTypeRepresentationStrategy;
|
||||
import org.springframework.data.neo4j.support.index.IndexProvider;
|
||||
|
||||
public class IndexingRelationshipTypeRepresentationStrategy extends
|
||||
AbstractIndexingTypeRepresentationStrategy<Relationship> implements RelationshipTypeRepresentationStrategy {
|
||||
public class IndexBasedRelationshipTypeRepresentationStrategy extends
|
||||
AbstractIndexBasedTypeRepresentationStrategy<Relationship> implements RelationshipTypeRepresentationStrategy {
|
||||
|
||||
public static final String INDEX_NAME = "__rel_types__";
|
||||
|
||||
public IndexingRelationshipTypeRepresentationStrategy(GraphDatabase graphDb, IndexProvider indexProvider) {
|
||||
public IndexBasedRelationshipTypeRepresentationStrategy(GraphDatabase graphDb, IndexProvider indexProvider) {
|
||||
super(graphDb, indexProvider, INDEX_NAME, Relationship.class);
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ import java.util.Map;
|
||||
* @author Nicki Watt
|
||||
* @since 24-09-2013
|
||||
*/
|
||||
public class LabelingNodeTypeRepresentationStrategy implements NodeTypeRepresentationStrategy {
|
||||
public class LabelBasedNodeTypeRepresentationStrategy implements NodeTypeRepresentationStrategy {
|
||||
|
||||
public static final Label SDN_LABEL_STRATEGY = DynamicLabel.label("SDN_LABEL_STRATEGY");
|
||||
public static final String LABELSTRATEGY_PREFIX = "__TYPE__";
|
||||
@@ -50,7 +50,7 @@ public class LabelingNodeTypeRepresentationStrategy implements NodeTypeRepresent
|
||||
protected QueryEngine<CypherQuery> queryEngine;
|
||||
private boolean sdnLabelStrategyPresent;
|
||||
|
||||
public LabelingNodeTypeRepresentationStrategy(GraphDatabase graphDb) {
|
||||
public LabelBasedNodeTypeRepresentationStrategy(GraphDatabase graphDb) {
|
||||
this.graphDb = graphDb;
|
||||
this.clazz = Node.class;
|
||||
this.queryEngine = graphDb.queryEngineFor(QueryType.Cypher);
|
||||
@@ -78,7 +78,7 @@ public class LabelingNodeTypeRepresentationStrategy implements NodeTypeRepresent
|
||||
* as the primary SDN marker Label.
|
||||
*/
|
||||
private void addLabelsForEntityHierarchy(Node state, StoredEntityType type) {
|
||||
String addLabelStatement = String.format("start n=node({nodeId}) set n:`%s`:`%s`" , LABELSTRATEGY_PREFIX + type.getAlias(),type.getAlias());
|
||||
String addLabelStatement = String.format("match n where id(n)={nodeId} set n:`%s`:`%s`" , LABELSTRATEGY_PREFIX + type.getAlias(),type.getAlias());
|
||||
for (StoredEntityType superType : type.getSuperTypes()) {
|
||||
addLabelStatement += String.format(":`%s`", superType.getAlias());
|
||||
}
|
||||
@@ -94,11 +94,11 @@ public class LabelingNodeTypeRepresentationStrategy implements NodeTypeRepresent
|
||||
*/
|
||||
private void markSDNLabelStrategyInUseIfNotExists() {
|
||||
if (!sdnLabelStrategyPresent) {
|
||||
String query = String.format("start n=node(%d) match n:`%s` return count(*) ", REFERENCE_NODE_ID, SDN_LABEL_STRATEGY.name());
|
||||
String query = String.format("match n where id(n)=%d and n:`%s` return count(*) ", REFERENCE_NODE_ID, SDN_LABEL_STRATEGY.name());
|
||||
Long labelCount = queryEngine.query(query, Collections.EMPTY_MAP).to(Long.class).single();
|
||||
|
||||
if (labelCount == 0) {
|
||||
String update = String.format("start n=node(%d) set n:`%s` ", REFERENCE_NODE_ID, SDN_LABEL_STRATEGY.name());
|
||||
String update = String.format("match n where id(n)=%d set n:`%s` ", REFERENCE_NODE_ID, SDN_LABEL_STRATEGY.name());
|
||||
queryEngine.query(update, Collections.EMPTY_MAP);
|
||||
}
|
||||
sdnLabelStrategyPresent = true;
|
||||
@@ -124,7 +124,7 @@ public class LabelingNodeTypeRepresentationStrategy implements NodeTypeRepresent
|
||||
if (state == null)
|
||||
throw new IllegalArgumentException("Node is null");
|
||||
|
||||
String query = String.format("start n=node(%d) return labels(n) as labels", state.getId());
|
||||
String query = String.format("match n where id(n)=%d return labels(n) as labels", state.getId());
|
||||
Map queryResult = queryEngine.query(query, Collections.EMPTY_MAP).to(Map.class).single();
|
||||
Iterable<String> labels = (Iterable)queryResult.get("labels");
|
||||
|
||||
@@ -65,12 +65,12 @@ public class TypeRepresentationStrategyFactory {
|
||||
|
||||
private static boolean isAlreadyLabeled(GraphDatabase graphDatabaseService) {
|
||||
return graphDatabaseService.getReferenceNode().hasLabel(
|
||||
LabelingNodeTypeRepresentationStrategy.SDN_LABEL_STRATEGY);
|
||||
LabelBasedNodeTypeRepresentationStrategy.SDN_LABEL_STRATEGY);
|
||||
}
|
||||
|
||||
private static boolean isAlreadyIndexed(GraphDatabase graphDatabaseService) {
|
||||
try {
|
||||
final Index<PropertyContainer> index = graphDatabaseService.getIndex(IndexingNodeTypeRepresentationStrategy.INDEX_NAME);
|
||||
final Index<PropertyContainer> index = graphDatabaseService.getIndex(IndexBasedNodeTypeRepresentationStrategy.INDEX_NAME);
|
||||
return index!=null && Node.class.isAssignableFrom(index.getEntityType());
|
||||
} catch(NoSuchIndexException nsie) {
|
||||
return false;
|
||||
@@ -117,7 +117,7 @@ public class TypeRepresentationStrategyFactory {
|
||||
Labeled {
|
||||
@Override
|
||||
public NodeTypeRepresentationStrategy getNodeTypeRepresentationStrategy(GraphDatabase graphDatabaseService, IndexProvider indexProvider) {
|
||||
return new LabelingNodeTypeRepresentationStrategy(graphDatabaseService);
|
||||
return new LabelBasedNodeTypeRepresentationStrategy(graphDatabaseService);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -128,12 +128,12 @@ public class TypeRepresentationStrategyFactory {
|
||||
Indexed {
|
||||
@Override
|
||||
public NodeTypeRepresentationStrategy getNodeTypeRepresentationStrategy(GraphDatabase graphDatabaseService, IndexProvider indexProvider) {
|
||||
return new IndexingNodeTypeRepresentationStrategy(graphDatabaseService, indexProvider);
|
||||
return new IndexBasedNodeTypeRepresentationStrategy(graphDatabaseService, indexProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RelationshipTypeRepresentationStrategy getRelationshipTypeRepresentationStrategy(GraphDatabase graphDatabaseService, IndexProvider indexProvider) {
|
||||
return new IndexingRelationshipTypeRepresentationStrategy(graphDatabaseService, indexProvider);
|
||||
return new IndexBasedRelationshipTypeRepresentationStrategy(graphDatabaseService, indexProvider);
|
||||
}
|
||||
},
|
||||
Noop {
|
||||
|
||||
Reference in New Issue
Block a user