Skip to content

Commit faf849d

Browse files
committed
GH-2391 - Tune extracting of nodes and relationships.
1 parent 3f90ae1 commit faf849d

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ private Collection<Node> extractNodes(MapAccessor allValues) {
645645
Collection<Node> allNodesInResult = new ArrayList<>();
646646
StreamSupport.stream(allValues.values().spliterator(), false)
647647
.filter(MappingSupport.isListContainingOnly(listType, this.nodeType))
648-
.flatMap(entry -> MappingSupport.extractNodes(listType, entry).stream())
648+
.flatMap(entry -> MappingSupport.extractNodesFromCollection(listType, entry).stream())
649649
.forEach(allNodesInResult::add);
650650

651651
if (allNodesInResult.isEmpty()) {
@@ -670,7 +670,7 @@ private Collection<Relationship> extractRelationships(MapAccessor allValues) {
670670
Collection<Relationship> allRelationshipsInResult = new ArrayList<>();
671671
StreamSupport.stream(allValues.values().spliterator(), false)
672672
.filter(MappingSupport.isListContainingOnly(listType, this.relationshipType))
673-
.flatMap(entry -> MappingSupport.extractRelationships(listType, entry).stream())
673+
.flatMap(entry -> MappingSupport.extractRelationshipsFromCollection(listType, entry).stream())
674674
.forEach(allRelationshipsInResult::add);
675675

676676
if (allRelationshipsInResult.isEmpty()) {

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,36 +109,38 @@ public static Predicate<Value> isListContainingOnly(Type collectionType, Type re
109109
return isList.and(containsOnlyRequiredType);
110110
}
111111

112-
static Collection<Relationship> extractRelationships(Type collectionType, Value entry) {
112+
static Collection<Relationship> extractRelationshipsFromCollection(Type collectionType, Value entry) {
113113

114114
Collection<Relationship> relationships = new HashSet<>();
115-
116-
for (Value listEntry : entry.values()) {
117-
if (listEntry.hasType(collectionType)) {
118-
for (Value listInListEntry : entry.asList(Function.identity())) {
119-
relationships.addAll(extractRelationships(collectionType, listInListEntry));
115+
if (entry.hasType(collectionType)) {
116+
for (Value listWithRelationshipsOrRelationship : entry.values()) {
117+
if (listWithRelationshipsOrRelationship.hasType(collectionType)) {
118+
relationships.addAll(listWithRelationshipsOrRelationship.asList(Value::asRelationship));
119+
} else {
120+
relationships.add(listWithRelationshipsOrRelationship.asRelationship());
120121
}
121-
} else {
122-
relationships.add(listEntry.asRelationship());
123122
}
123+
} else {
124+
relationships.add(entry.asRelationship());
124125
}
125126
return relationships;
126127
}
127128

128-
static Collection<Node> extractNodes(Type collectionType, Value entry) {
129+
static Collection<Node> extractNodesFromCollection(Type collectionType, Value entry) {
129130

130131
// There can be multiple relationships leading to the same node.
131-
// Thus we need a collection implementation that supports duplicates.
132+
// Thus, we need a collection implementation that supports duplicates.
132133
Collection<Node> nodes = new ArrayList<>();
133-
134-
for (Value listEntry : entry.values()) {
135-
if (listEntry.hasType(collectionType)) {
136-
for (Value listInListEntry : entry.asList(Function.identity())) {
137-
nodes.addAll(extractNodes(collectionType, listInListEntry));
134+
if (entry.hasType(collectionType)) {
135+
for (Value listWithNodesOrNode : entry.values()) {
136+
if (listWithNodesOrNode.hasType(collectionType)) {
137+
nodes.addAll(listWithNodesOrNode.asList(Value::asNode));
138+
} else {
139+
nodes.add(listWithNodesOrNode.asNode());
138140
}
139-
} else {
140-
nodes.add(listEntry.asNode());
141141
}
142+
} else {
143+
nodes.add(entry.asNode());
142144
}
143145
return nodes;
144146
}

0 commit comments

Comments
 (0)