Skip to content

Commit 16a6cae

Browse files
committed
Refactor WritingContext#insertAll to use Collectors.partitioningBy instead of managing the partitions manually.
1 parent 9ed9e1b commit 16a6cae

File tree

1 file changed

+6
-7
lines changed
  • spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion

1 file changed

+6
-7
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/WritingContext.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class WritingContext {
4747
private final PersistentPropertyPaths<?, RelationalPersistentProperty> paths;
4848
private final Map<PathNode, DbAction<?>> previousActions = new HashMap<>();
4949
private final Map<PersistentPropertyPath<RelationalPersistentProperty>, List<PathNode>> nodesCache = new HashMap<>();
50-
private boolean rootIncludeId;
50+
private final boolean rootIncludeId;
5151

5252
WritingContext(RelationalMappingContext context, Object root, MutableAggregateChange<?> aggregateChange) {
5353

@@ -126,9 +126,7 @@ private List<DbAction<?>> insertReferenced() {
126126
private List<? extends DbAction<?>> insertAll(PersistentPropertyPath<RelationalPersistentProperty> path) {
127127

128128
RelationalPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(path.getRequiredLeafProperty());
129-
TreeMap<Boolean, List<DbAction.Insert<Object>>> insertsPartitionedByHasId = new TreeMap<>(BooleanComparator.TRUE_HIGH);
130-
insertsPartitionedByHasId.put(true, new ArrayList<>());
131-
insertsPartitionedByHasId.put(false, new ArrayList<>());
129+
List<DbAction.Insert<Object>> inserts = new ArrayList<>();
132130
from(path).forEach(node -> {
133131

134132
DbAction.WithEntity<?> parentAction = getAction(node.getParent());
@@ -150,11 +148,12 @@ private List<? extends DbAction<?>> insertAll(PersistentPropertyPath<RelationalP
150148
}
151149
boolean includeId = persistentEntity.getIdentifierAccessor(instance).getIdentifier() != null;
152150
DbAction.Insert<Object> insert = new DbAction.Insert<>(instance, path, parentAction, qualifiers, includeId);
153-
insertsPartitionedByHasId.get(includeId)
154-
.add(insert);
151+
inserts.add(insert);
155152
previousActions.put(node, insert);
156153
});
157-
return insertsPartitionedByHasId.entrySet().stream()
154+
return inserts.stream()
155+
.collect(Collectors.partitioningBy(DbAction.Insert::isIncludeId))
156+
.entrySet().stream()
158157
.filter(entry -> (!entry.getValue().isEmpty()))
159158
.map(entry -> {
160159
List<DbAction.Insert<Object>> batch = entry.getValue();

0 commit comments

Comments
 (0)