Skip to content

Commit b611a62

Browse files
committed
GH-2138 - Clean up.
1 parent a3c8afa commit b611a62

8 files changed

+36
-34
lines changed

src/main/java/org/springframework/data/neo4j/core/Neo4jTemplate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ private GenericQueryAndParameters createQueryAndParameters(Neo4jPersistentEntity
701701
final Set<Long> relationshipIds = new HashSet<>();
702702
final Set<Long> relatedNodeIds = new HashSet<>();
703703

704-
for (RelationshipDescription relationshipDescription : entityMetaData.getRelationshipsUpAndDown(fieldName -> queryFragments.includeField(fieldName))) {
704+
for (RelationshipDescription relationshipDescription : entityMetaData.getRelationshipsInHierarchy(fieldName -> queryFragments.includeField(fieldName))) {
705705

706706
Statement statement = cypherGenerator
707707
.prepareMatchOf(entityMetaData, relationshipDescription, queryFragments.getMatchOn(), queryFragments.getCondition())
@@ -720,7 +720,7 @@ private GenericQueryAndParameters createQueryAndParameters(Neo4jPersistentEntity
720720
private void iterateNextLevel(Collection<Long> nodeIds, Neo4jPersistentEntity<?> target, Set<Long> relationshipIds,
721721
Set<Long> relatedNodeIds) {
722722

723-
Collection<RelationshipDescription> relationships = target.getRelationshipsUpAndDown(s -> true);
723+
Collection<RelationshipDescription> relationships = target.getRelationshipsInHierarchy(s -> true);
724724
for (RelationshipDescription relationshipDescription : relationships) {
725725

726726
Node node = anyNode(Constants.NAME_OF_ROOT_NODE);

src/main/java/org/springframework/data/neo4j/core/ReactiveNeo4jTemplate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ private Mono<GenericQueryAndParameters> createQueryAndParameters(Neo4jPersistent
469469
Set<Long> rootNodeIds = ctx.get("rootNodes");
470470
Set<Long> processedRelationshipIds = ctx.get("processedRelationships");
471471
Set<Long> processedNodeIds = ctx.get("processedNodes");
472-
return Flux.fromIterable(entityMetaData.getRelationshipsUpAndDown(fieldName -> queryFragments.includeField(fieldName)))
472+
return Flux.fromIterable(entityMetaData.getRelationshipsInHierarchy(fieldName -> queryFragments.includeField(fieldName)))
473473
.flatMap(relationshipDescription -> {
474474

475475
Statement statement = cypherGenerator.prepareMatchOf(entityMetaData, relationshipDescription,
@@ -509,7 +509,7 @@ private Flux<Tuple2<Collection<Long>, Collection<Long>>> iterateNextLevel(Collec
509509

510510
NodeDescription<?> target = relationshipDescription.getTarget();
511511

512-
return Flux.fromIterable(target.getRelationshipsUpAndDown(s -> true))
512+
return Flux.fromIterable(target.getRelationshipsInHierarchy(s -> true))
513513
.flatMap(relDe -> {
514514
Node node = anyNode(Constants.NAME_OF_ROOT_NODE);
515515

src/main/java/org/springframework/data/neo4j/core/mapping/CypherGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ private MapProjection projectPropertiesAndRelationships(NodeDescription<?> nodeD
502502
List<Object> propertiesProjection = projectNodeProperties(nodeDescription, nodeName, includedProperties);
503503
List<Object> contentOfProjection = new ArrayList<>(propertiesProjection);
504504

505-
Collection<RelationshipDescription> relationships = nodeDescription.getRelationshipsUpAndDown(includedProperties);
505+
Collection<RelationshipDescription> relationships = nodeDescription.getRelationshipsInHierarchy(includedProperties);
506506
relationships.removeIf(r -> !includedProperties.test(r.getFieldName()));
507507

508508
contentOfProjection.addAll(generateListsFor(relationships, nodeName, processedRelationships));

src/main/java/org/springframework/data/neo4j/core/mapping/DefaultNeo4jEntityConverter.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ private <ET> ET map(MapAccessor queryResult, MapAccessor allValues, Neo4jPersist
251251
Predicate<String> includeAllFields = (field) -> true;
252252

253253
Collection<RelationshipDescription> relationships = nodeDescription
254-
.getRelationshipsUpAndDown(includeAllFields);
254+
.getRelationshipsInHierarchy(includeAllFields);
255255

256256
ET instance = instantiate(concreteNodeDescription, queryResult, allValues, relationships,
257257
nodeDescriptionAndLabels.getDynamicLabels(), lastMappedEntity);
@@ -299,6 +299,17 @@ private Long getInternalId(@NonNull MapAccessor queryResult) {
299299
: queryResult.get(Constants.NAME_OF_INTERNAL_ID).asLong();
300300
}
301301

302+
@NonNull
303+
private Neo4jPersistentEntity<?> getMostConcreteTargetNodeDescription(
304+
Neo4jPersistentEntity<?> genericTargetNodeDescription, MapAccessor possibleValueNode) {
305+
306+
List<String> allLabels = getLabels(possibleValueNode, null);
307+
NodeDescriptionAndLabels nodeDescriptionAndLabels = NodeDescriptionStore
308+
.deriveConcreteNodeDescription(genericTargetNodeDescription, allLabels);
309+
return (Neo4jPersistentEntity<?>) nodeDescriptionAndLabels
310+
.getNodeDescription();
311+
}
312+
302313
/**
303314
* Returns the list of labels for the entity to be created from the "main" node returned.
304315
*
@@ -458,11 +469,8 @@ private Optional<Object> createInstanceOfRelationships(Neo4jPersistentProperty p
458469
for (Relationship possibleRelationship : allMatchingTypeRelationshipsInResult) {
459470
if (targetIdSelector.apply(possibleRelationship) == targetNodeId && sourceIdSelector.apply(possibleRelationship).equals(sourceNodeId)) {
460471

461-
List<String> allLabels = getLabels(possibleValueNode, null);
462-
NodeDescriptionAndLabels nodeDescriptionAndLabels = NodeDescriptionStore
463-
.deriveConcreteNodeDescription(genericTargetNodeDescription, allLabels);
464-
Neo4jPersistentEntity<?> concreteTargetNodeDescription = (Neo4jPersistentEntity<?>) nodeDescriptionAndLabels
465-
.getNodeDescription();
472+
Neo4jPersistentEntity<?> concreteTargetNodeDescription =
473+
getMostConcreteTargetNodeDescription(genericTargetNodeDescription, possibleValueNode);
466474

467475
Object mappedObject = map(possibleValueNode, allValues, concreteTargetNodeDescription);
468476
if (relationshipDescription.hasRelationshipProperties()) {
@@ -483,11 +491,8 @@ private Optional<Object> createInstanceOfRelationships(Neo4jPersistentProperty p
483491
} else {
484492
for (Value relatedEntity : list.asList(Function.identity())) {
485493

486-
List<String> allLabels = getLabels(relatedEntity, null);
487-
NodeDescriptionAndLabels nodeDescriptionAndLabels = NodeDescriptionStore
488-
.deriveConcreteNodeDescription(genericTargetNodeDescription, allLabels);
489-
Neo4jPersistentEntity<?> concreteTargetNodeDescription = (Neo4jPersistentEntity<?>) nodeDescriptionAndLabels
490-
.getNodeDescription();
494+
Neo4jPersistentEntity<?> concreteTargetNodeDescription =
495+
getMostConcreteTargetNodeDescription(genericTargetNodeDescription, relatedEntity);
491496

492497
Object valueEntry = map(relatedEntity, allValues, concreteTargetNodeDescription);
493498

src/main/java/org/springframework/data/neo4j/core/mapping/DefaultNeo4jPersistentEntity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ public Collection<RelationshipDescription> getRelationships() {
422422
}
423423

424424
@NonNull
425-
public Collection<RelationshipDescription> getRelationshipsUpAndDown(Predicate<String> propertyFilter) {
425+
public Collection<RelationshipDescription> getRelationshipsInHierarchy(Predicate<String> propertyFilter) {
426426

427427
Collection<RelationshipDescription> relationships = new HashSet<>(getRelationships());
428428
for (NodeDescription<?> childDescription : getChildNodeDescriptionsInHierarchy()) {
@@ -496,7 +496,7 @@ public boolean containsPossibleCircles(Predicate<String> includeField) {
496496
}
497497

498498
private boolean calculatePossibleCircles(Predicate<String> includeField) {
499-
Collection<RelationshipDescription> relationships = new HashSet<>(getRelationshipsUpAndDown(includeField));
499+
Collection<RelationshipDescription> relationships = new HashSet<>(getRelationshipsInHierarchy(includeField));
500500

501501
Set<RelationshipDescription> processedRelationships = new HashSet<>();
502502
for (RelationshipDescription relationship : relationships) {
@@ -515,7 +515,7 @@ private boolean calculatePossibleCircles(Predicate<String> includeField) {
515515
}
516516

517517
private boolean calculatePossibleCircles(NodeDescription<?> nodeDescription, Set<RelationshipDescription> processedRelationships) {
518-
Collection<RelationshipDescription> relationships = nodeDescription.getRelationshipsUpAndDown(s -> true);
518+
Collection<RelationshipDescription> relationships = nodeDescription.getRelationshipsInHierarchy(s -> true);
519519

520520
for (RelationshipDescription relationship : relationships) {
521521
if (processedRelationships.contains(relationship)) {

src/main/java/org/springframework/data/neo4j/core/mapping/NestedRelationshipContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public static NestedRelationshipContext of(Association<Neo4jPersistentProperty>
124124
Object value = propertyAccessor.getProperty(inverse);
125125
boolean inverseValueIsEmpty = value == null;
126126

127-
RelationshipDescription relationship = neo4jPersistentEntity.getRelationshipsUpAndDown(s -> true).stream()
127+
RelationshipDescription relationship = neo4jPersistentEntity.getRelationshipsInHierarchy(s -> true).stream()
128128
.filter(r -> r.getFieldName().equals(inverse.getName())).findFirst().get();
129129

130130
// if we have a relationship with properties, the targetNodeType is the map key

src/main/java/org/springframework/data/neo4j/core/mapping/NodeDescription.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ default boolean isUsingInternalIds() {
103103
*/
104104
Collection<RelationshipDescription> getRelationships();
105105

106-
Collection<RelationshipDescription> getRelationshipsUpAndDown(Predicate<String> propertyPredicate);
106+
/**
107+
* This returns the relationships this node, its parent and child has to other nodes.
108+
*
109+
* @param propertyPredicate - Predicate to filter the fields on this node description to
110+
* @return The relationships defined by instances of this node.
111+
*/
112+
Collection<RelationshipDescription> getRelationshipsInHierarchy(Predicate<String> propertyPredicate);
107113

108114
/**
109115
* Register a direct child node description for this entity.

src/main/java/org/springframework/data/neo4j/core/mapping/NodeDescriptionStore.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ public NodeDescription<?> getNodeDescription(Class<?> targetType) {
7878

7979
public static NodeDescriptionAndLabels deriveConcreteNodeDescription(Neo4jPersistentEntity<?> entityDescription,
8080
List<String> labels) {
81+
8182
if (labels == null || labels.isEmpty()) {
8283
return new NodeDescriptionAndLabels(entityDescription, Collections.emptyList());
8384
}
84-
NodeDescriptionAndLabels nodeDescriptionAndLabels = null;
85+
8586
for (NodeDescription<?> childNodeDescription : entityDescription.getChildNodeDescriptionsInHierarchy()) {
8687
String primaryLabel = childNodeDescription.getPrimaryLabel();
8788
List<String> additionalLabels = new ArrayList<>(childNodeDescription.getAdditionalLabels());
@@ -90,20 +91,10 @@ public static NodeDescriptionAndLabels deriveConcreteNodeDescription(Neo4jPersis
9091
Set<String> surplusLabels = new HashSet<>(labels);
9192
surplusLabels.remove(primaryLabel);
9293
surplusLabels.removeAll(additionalLabels);
93-
// if we find more than one, we have to distinguish between the options in the mapping logic later.
94-
if (nodeDescriptionAndLabels == null) {
95-
nodeDescriptionAndLabels = new NodeDescriptionAndLabels(childNodeDescription, surplusLabels);
96-
} else {
97-
surplusLabels = new HashSet<>(labels);
98-
surplusLabels.remove(entityDescription.getPrimaryLabel());
99-
surplusLabels.removeAll(entityDescription.getAdditionalLabels());
100-
return new NodeDescriptionAndLabels(entityDescription, surplusLabels);
101-
}
94+
95+
return new NodeDescriptionAndLabels(childNodeDescription, surplusLabels);
10296
}
10397
}
104-
if (nodeDescriptionAndLabels != null) {
105-
return nodeDescriptionAndLabels;
106-
}
10798

10899
Set<String> surplusLabels = new HashSet<>(labels);
109100
surplusLabels.remove(entityDescription.getPrimaryLabel());

0 commit comments

Comments
 (0)