Update SaveBatchingAggregateChange to batch InsertRoot actions.

Original pull request #1228
This commit is contained in:
Chirag Tailor
2022-04-13 13:37:14 -05:00
committed by Jens Schauder
parent c1164383d6
commit ec3cec3be3
8 changed files with 318 additions and 68 deletions

View File

@@ -82,6 +82,8 @@ class AggregateChangeExecutor {
try {
if (action instanceof DbAction.InsertRoot) {
executionContext.executeInsertRoot((DbAction.InsertRoot<?>) action);
} else if (action instanceof DbAction.BatchInsertRoot<?>) {
executionContext.executeBatchInsertRoot((DbAction.BatchInsertRoot<?>) action);
} else if (action instanceof DbAction.Insert) {
executionContext.executeInsert((DbAction.Insert<?>) action);
} else if (action instanceof DbAction.BatchInsert) {

View File

@@ -75,6 +75,20 @@ class JdbcAggregateChangeExecutionContext {
add(new DbActionExecutionResult(insert, id));
}
<T> void executeBatchInsertRoot(DbAction.BatchInsertRoot<T> batchInsertRoot) {
List<DbAction.InsertRoot<T>> inserts = batchInsertRoot.getActions();
List<InsertSubject<T>> insertSubjects = inserts.stream()
.map(insert -> InsertSubject.describedBy(insert.getEntity(), Identifier.empty())).collect(Collectors.toList());
Object[] ids = accessStrategy.insert(insertSubjects, batchInsertRoot.getEntityType(),
batchInsertRoot.getBatchValue());
for (int i = 0; i < inserts.size(); i++) {
add(new DbActionExecutionResult(inserts.get(i), ids.length > 0 ? ids[i] : null));
}
}
<T> void executeInsert(DbAction.Insert<T> insert) {
Identifier parentKeys = getParentKeys(insert, converter);

View File

@@ -164,6 +164,32 @@ public class JdbcAggregateChangeExecutorContextUnitTests {
assertThat(content.id).isNull();
}
@Test // GH-537
void batchInsertRootOperation_withGeneratedIds() {
when(accessStrategy.insert(singletonList(InsertSubject.describedBy(root, Identifier.empty())), DummyEntity.class, IdValueSource.GENERATED))
.thenReturn(new Object[] { 123L });
executionContext.executeBatchInsertRoot(new DbAction.BatchInsertRoot<>(singletonList(new DbAction.InsertRoot<>(root, IdValueSource.GENERATED))));
List<DummyEntity> newRoots = executionContext.populateIdsIfNecessary();
assertThat(newRoots).containsExactly(root);
assertThat(root.id).isEqualTo(123L);
}
@Test // GH-537
void batchInsertRootOperation_withoutGeneratedIds() {
when(accessStrategy.insert(singletonList(InsertSubject.describedBy(root, Identifier.empty())), DummyEntity.class, IdValueSource.PROVIDED))
.thenReturn(new Object[] { null });
executionContext.executeBatchInsertRoot(new DbAction.BatchInsertRoot<>(singletonList(new DbAction.InsertRoot<>(root, IdValueSource.PROVIDED))));
List<DummyEntity> newRoots = executionContext.populateIdsIfNecessary();
assertThat(newRoots).containsExactly(root);
assertThat(root.id).isNull();
}
@Test // GH-1201
void updates_whenReferencesWithImmutableIdAreInserted() {
@@ -177,7 +203,8 @@ public class JdbcAggregateChangeExecutorContextUnitTests {
Identifier identifier = Identifier.empty().withPart(SqlIdentifier.quoted("DUMMY_ENTITY"), 123L, Long.class);
when(accessStrategy.insert(contentImmutableId, ContentImmutableId.class, identifier, IdValueSource.GENERATED))
.thenReturn(456L);
executionContext.executeInsert(createInsert(rootUpdate, "contentImmutableId", contentImmutableId, null, IdValueSource.GENERATED));
executionContext.executeInsert(
createInsert(rootUpdate, "contentImmutableId", contentImmutableId, null, IdValueSource.GENERATED));
List<DummyEntity> newRoots = executionContext.populateIdsIfNecessary();
assertThat(newRoots).containsExactly(root);
@@ -197,7 +224,6 @@ public class JdbcAggregateChangeExecutorContextUnitTests {
when(accessStrategy.insert(content1, Content.class, createBackRef(123L), IdValueSource.GENERATED)).thenReturn(11L);
executionContext.executeInsert(createInsert(rootUpdate1, "content", content1, null, IdValueSource.GENERATED));
DummyEntity root2 = new DummyEntity();
DbAction.InsertRoot<DummyEntity> rootInsert2 = new DbAction.InsertRoot<>(root2, IdValueSource.GENERATED);
when(accessStrategy.insert(root2, DummyEntity.class, Identifier.empty(), IdValueSource.GENERATED)).thenReturn(456L);