From 64d9bbbd5536a9604a804633ac2323db4fb53818 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Wed, 1 Jun 2022 13:14:39 +0200 Subject: [PATCH] Polishing. Original pull request #1228 --- .../SaveBatchingAggregateChange.java | 23 +++++++--- .../SaveBatchingAggregateChangeTest.java | 42 +++++++++++++++---- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/SaveBatchingAggregateChange.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/SaveBatchingAggregateChange.java index 090f3e02..c6e76020 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/SaveBatchingAggregateChange.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/SaveBatchingAggregateChange.java @@ -44,6 +44,10 @@ public class SaveBatchingAggregateChange implements BatchingAggregateChange entityType; private final List> rootActions = new ArrayList<>(); + /** + * Holds a list of InsertRoot actions that are compatible with each other, in the sense, that they might be combined + * into a single batch. + */ private final List> insertRootBatchCandidates = new ArrayList<>(); private final Map, Map>>> insertActions = // new HashMap<>(); @@ -85,16 +89,21 @@ public class SaveBatchingAggregateChange implements BatchingAggregateChange aggregateChange) { aggregateChange.forEachAction(action -> { + if (action instanceof DbAction.UpdateRoot rootAction) { - commitBatchCandidates(); + + combineBatchCandidatesIntoSingleBatchRootAction(); rootActions.add(rootAction); } else if (action instanceof DbAction.InsertRoot rootAction) { - if (!insertRootBatchCandidates.isEmpty() && !insertRootBatchCandidates.get(0).getIdValueSource().equals(rootAction.getIdValueSource())) { - commitBatchCandidates(); + + if (!insertRootBatchCandidates.isEmpty() + && !insertRootBatchCandidates.get(0).getIdValueSource().equals(rootAction.getIdValueSource())) { + combineBatchCandidatesIntoSingleBatchRootAction(); } - //noinspection unchecked + // noinspection unchecked insertRootBatchCandidates.add((DbAction.InsertRoot) rootAction); } else if (action instanceof DbAction.Insert insertAction) { + // noinspection unchecked addInsert((DbAction.Insert) insertAction); } else if (action instanceof DbAction.Delete deleteAction) { @@ -103,7 +112,11 @@ public class SaveBatchingAggregateChange implements BatchingAggregateChange 1) { rootActions.add(new DbAction.BatchInsertRoot<>(List.copyOf(insertRootBatchCandidates))); diff --git a/spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/SaveBatchingAggregateChangeTest.java b/spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/SaveBatchingAggregateChangeTest.java index 5e327386..ff177ade 100644 --- a/spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/SaveBatchingAggregateChangeTest.java +++ b/spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/SaveBatchingAggregateChangeTest.java @@ -70,6 +70,7 @@ class SaveBatchingAggregateChangeTest { DbAction.InsertRoot root1Insert = new DbAction.InsertRoot<>(root1, IdValueSource.GENERATED); RootAggregateChange aggregateChange1 = MutableAggregateChange.forSave(root1); aggregateChange1.setRootAction(root1Insert); + Root root2 = new Root(1L, null); DbAction.UpdateRoot root2Update = new DbAction.UpdateRoot<>(root2, null); RootAggregateChange aggregateChange2 = MutableAggregateChange.forSave(root2); @@ -93,11 +94,13 @@ class SaveBatchingAggregateChangeTest { DbAction.InsertRoot root1Insert = new DbAction.InsertRoot<>(root1, IdValueSource.GENERATED); RootAggregateChange aggregateChange1 = MutableAggregateChange.forSave(root1); aggregateChange1.setRootAction(root1Insert); - Root root2 = new Root(1L, null); + + Root root2 = new Root(2L, null); DbAction.InsertRoot root2Insert = new DbAction.InsertRoot<>(root2, IdValueSource.GENERATED); RootAggregateChange aggregateChange2 = MutableAggregateChange.forSave(root2); aggregateChange2.setRootAction(root2Insert); - Root root3 = new Root(1L, null); + + Root root3 = new Root(3L, null); DbAction.UpdateRoot root3Update = new DbAction.UpdateRoot<>(root3, null); RootAggregateChange aggregateChange3 = MutableAggregateChange.forSave(root3); aggregateChange3.setRootAction(root3Update); @@ -138,7 +141,8 @@ class SaveBatchingAggregateChangeTest { DbAction.InsertRoot root1Insert = new DbAction.InsertRoot<>(root1, IdValueSource.GENERATED); RootAggregateChange aggregateChange1 = MutableAggregateChange.forSave(root1); aggregateChange1.setRootAction(root1Insert); - Root root2 = new Root(1L, null); + + Root root2 = new Root(2L, null); DbAction.InsertRoot root2Insert = new DbAction.InsertRoot<>(root2, IdValueSource.PROVIDED); RootAggregateChange aggregateChange2 = MutableAggregateChange.forSave(root2); aggregateChange2.setRootAction(root2Insert); @@ -157,11 +161,13 @@ class SaveBatchingAggregateChangeTest { DbAction.InsertRoot root1Insert = new DbAction.InsertRoot<>(root1, IdValueSource.GENERATED); RootAggregateChange aggregateChange1 = MutableAggregateChange.forSave(root1); aggregateChange1.setRootAction(root1Insert); - Root root2 = new Root(1L, null); + + Root root2 = new Root(2L, null); DbAction.InsertRoot root2Insert = new DbAction.InsertRoot<>(root2, IdValueSource.GENERATED); RootAggregateChange aggregateChange2 = MutableAggregateChange.forSave(root2); aggregateChange2.setRootAction(root2Insert); - Root root3 = new Root(1L, null); + + Root root3 = new Root(3L, null); DbAction.InsertRoot root3Insert = new DbAction.InsertRoot<>(root3, IdValueSource.PROVIDED); RootAggregateChange aggregateChange3 = MutableAggregateChange.forSave(root3); aggregateChange3.setRootAction(root3Insert); @@ -188,7 +194,8 @@ class SaveBatchingAggregateChangeTest { DbAction.InsertRoot root1Insert = new DbAction.InsertRoot<>(root1, IdValueSource.GENERATED); RootAggregateChange aggregateChange1 = MutableAggregateChange.forSave(root1); aggregateChange1.setRootAction(root1Insert); - Root root2 = new Root(1L, null); + + Root root2 = new Root(2L, null); DbAction.InsertRoot root2Insert = new DbAction.InsertRoot<>(root2, IdValueSource.GENERATED); RootAggregateChange aggregateChange2 = MutableAggregateChange.forSave(root2); aggregateChange2.setRootAction(root2Insert); @@ -212,10 +219,12 @@ class SaveBatchingAggregateChangeTest { DbAction.InsertRoot root1Insert = new DbAction.InsertRoot<>(root1, IdValueSource.GENERATED); RootAggregateChange aggregateChange1 = MutableAggregateChange.forSave(root1); aggregateChange1.setRootAction(root1Insert); + Root root2 = new Root(2L, null); DbAction.InsertRoot root2Insert = new DbAction.InsertRoot<>(root2, IdValueSource.GENERATED); RootAggregateChange aggregateChange2 = MutableAggregateChange.forSave(root2); aggregateChange2.setRootAction(root2Insert); + BatchingAggregateChange> change = BatchingAggregateChange.forSave(Root.class); change.add(aggregateChange1); @@ -242,9 +251,11 @@ class SaveBatchingAggregateChangeTest { DbAction.UpdateRoot root1Update = new DbAction.UpdateRoot<>(root1, null); RootAggregateChange aggregateChange1 = MutableAggregateChange.forSave(root1); aggregateChange1.setRootAction(root1Update); + DbAction.Delete root1IntermediateDelete = new DbAction.Delete<>(1L, context.getPersistentPropertyPath("intermediate", Root.class)); aggregateChange1.addAction(root1IntermediateDelete); + Root root2 = new Root(null, null); DbAction.InsertRoot root2Insert = new DbAction.InsertRoot<>(root2, IdValueSource.GENERATED); RootAggregateChange aggregateChange2 = MutableAggregateChange.forSave(root2); @@ -270,12 +281,14 @@ class SaveBatchingAggregateChangeTest { context.getPersistentPropertyPath("intermediate", Root.class)); aggregateChange1.addAction(root1IntermediateDelete); - Root root2 = new Root(1L, null); + Root root2 = new Root(2L, null); RootAggregateChange aggregateChange2 = MutableAggregateChange.forSave(root2); aggregateChange2.setRootAction(new DbAction.UpdateRoot<>(root2, null)); + DbAction.Delete root2LeafDelete = new DbAction.Delete<>(1L, context.getPersistentPropertyPath("intermediate.leaf", Root.class)); aggregateChange2.addAction(root2LeafDelete); + DbAction.Delete root2IntermediateDelete = new DbAction.Delete<>(1L, context.getPersistentPropertyPath("intermediate", Root.class)); aggregateChange2.addAction(root2IntermediateDelete); @@ -325,10 +338,12 @@ class SaveBatchingAggregateChangeTest { DbAction.InsertRoot rootInsert = new DbAction.InsertRoot<>(root, IdValueSource.GENERATED); RootAggregateChange aggregateChange = MutableAggregateChange.forSave(root); aggregateChange.setRootAction(rootInsert); + Intermediate intermediateGeneratedId = new Intermediate(null, "intermediateGeneratedId", null); DbAction.Insert intermediateInsertGeneratedId = new DbAction.Insert<>(intermediateGeneratedId, context.getPersistentPropertyPath("intermediate", Root.class), rootInsert, emptyMap(), IdValueSource.GENERATED); aggregateChange.addAction(intermediateInsertGeneratedId); + Intermediate intermediateProvidedId = new Intermediate(123L, "intermediateProvidedId", null); DbAction.Insert intermediateInsertProvidedId = new DbAction.Insert<>(intermediateProvidedId, context.getPersistentPropertyPath("intermediate", Root.class), rootInsert, emptyMap(), IdValueSource.PROVIDED); @@ -360,11 +375,13 @@ class SaveBatchingAggregateChangeTest { DbAction.InsertRoot root1Insert = new DbAction.InsertRoot<>(root1, IdValueSource.GENERATED); RootAggregateChange aggregateChange1 = MutableAggregateChange.forSave(root1); aggregateChange1.setRootAction(root1Insert); + Intermediate root1Intermediate = new Intermediate(null, "root1Intermediate", null); DbAction.Insert root1IntermediateInsert = new DbAction.Insert<>(root1Intermediate, context.getPersistentPropertyPath("intermediate", Root.class), root1Insert, emptyMap(), IdValueSource.GENERATED); aggregateChange1.addAction(root1IntermediateInsert); + Leaf root1Leaf = new Leaf(null, "root1Leaf"); DbAction.Insert root1LeafInsert = new DbAction.Insert<>(root1Leaf, context.getPersistentPropertyPath("intermediate.leaf", Root.class), root1IntermediateInsert, emptyMap(), @@ -375,6 +392,7 @@ class SaveBatchingAggregateChangeTest { DbAction.InsertRoot root2Insert = new DbAction.InsertRoot<>(root2, IdValueSource.GENERATED); RootAggregateChange aggregateChange2 = MutableAggregateChange.forSave(root2); aggregateChange2.setRootAction(root2Insert); + Intermediate root2Intermediate = new Intermediate(null, "root2Intermediate", null); DbAction.Insert root2IntermediateInsert = new DbAction.Insert<>(root2Intermediate, context.getPersistentPropertyPath("intermediate", Root.class), root2Insert, emptyMap(), @@ -405,11 +423,13 @@ class SaveBatchingAggregateChangeTest { IdValueSource.GENERATED); RootAggregateChange aggregateChange = MutableAggregateChange.forSave(root); aggregateChange.setRootAction(rootInsert); + Intermediate one = new Intermediate(null, "one", null); DbAction.Insert oneInsert = new DbAction.Insert<>(one, context.getPersistentPropertyPath("one", RootWithSameLengthReferences.class), rootInsert, emptyMap(), IdValueSource.GENERATED); aggregateChange.addAction(oneInsert); + Intermediate two = new Intermediate(null, "two", null); DbAction.Insert twoInsert = new DbAction.Insert<>(two, context.getPersistentPropertyPath("two", RootWithSameLengthReferences.class), rootInsert, emptyMap(), @@ -426,8 +446,8 @@ class SaveBatchingAggregateChangeTest { .containsSubsequence( // Tuple.tuple(DbAction.BatchInsert.class, Intermediate.class, IdValueSource.GENERATED), Tuple.tuple(DbAction.BatchInsert.class, Intermediate.class, IdValueSource.GENERATED)); - List, Object>> batchInsertActions = getBatchWithValueActions(actions, Intermediate.class, - DbAction.BatchInsert.class); + List, Object>> batchInsertActions = getBatchWithValueActions( + actions, Intermediate.class, DbAction.BatchInsert.class); assertThat(batchInsertActions).hasSize(2); assertThat(batchInsertActions.get(0).getActions()).containsExactly(oneInsert); assertThat(batchInsertActions.get(1).getActions()).containsExactly(twoInsert); @@ -467,6 +487,7 @@ class SaveBatchingAggregateChangeTest { @Value static class RootWithSameLengthReferences { + @Id Long id; Intermediate one; Intermediate two; @@ -474,12 +495,14 @@ class SaveBatchingAggregateChangeTest { @Value static class Root { + @Id Long id; Intermediate intermediate; } @Value static class Intermediate { + @Id Long id; String name; Leaf leaf; @@ -487,6 +510,7 @@ class SaveBatchingAggregateChangeTest { @Value static class Leaf { + @Id Long id; String name; }