#1630 - ENH: Improve jdbc batch use when persisting ManyToMany intersection changes

This commit is contained in:
rob bygrave committed 2019-02-05 17:35:58 +13:00
1 parent a02673359d
commit 0c03a8b925
12 files changed
+276 -58

No files matched your search

@@ -3,6 +3,7 @@ package io.ebeaninternal.server.persist;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.server.core.PersistRequest;
import io.ebeaninternal.server.core.PersistRequestBean;
import io.ebeaninternal.server.core.PersistRequestUpdateSql;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
@@ -10,6 +11,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
/**
* Controls the batch ordering of persist requests.
@@ -68,6 +70,9 @@ public final class BatchControl {
*/
private int bufferMax;
private Queue earlyQueue;
private Queue lateQueue;
/**
* Create for a given transaction, PersistExecute, default size and getGeneratedKeys.
*/
@@ -224,14 +229,14 @@ public final class BatchControl {
* Flush without resetting the topOrder (maintains the depth info).
*/
public void flush() throws BatchedSqlException {
flush(false);
flushBuffer(false);
}
/**
* Flush with a reset the topOrder (fully empty the batch).
*/
public void flushReset() throws BatchedSqlException {
flush(true);
flushBuffer(true);
}
/**
@@ -243,10 +248,22 @@ public final class BatchControl {
maxDepth = 0;
}
private void flushBuffer(boolean resetTop) throws BatchedSqlException {
flushInternal(resetTop);
flushQueue(earlyQueue);
flushQueue(lateQueue);
}
private void flushQueue(Queue queue) throws BatchedSqlException {
if (queue != null && queue.flush() && !pstmtHolder.isEmpty()) {
flushPstmtHolder();
}
}
/**
* execute all the requests currently queued or batched.
*/
private void flush(boolean resetTop) throws BatchedSqlException {
private void flushInternal(boolean resetTop) throws BatchedSqlException {
try {
bufferMax = 0;
@@ -367,4 +384,43 @@ public final class BatchControl {
public int[] execute(String key, boolean getGeneratedKeys) throws SQLException {
return pstmtHolder.execute(key, getGeneratedKeys);
}
/**
* Add a SqlUpdate request to execute after flush.
*/
public void addToFlushQueue(PersistRequestUpdateSql request, boolean early) {
if (early) {
// add it to the early queue
if (earlyQueue == null) {
earlyQueue = new Queue();
}
earlyQueue.add(request);
} else {
// add it to the late queue
if (lateQueue == null) {
lateQueue = new Queue();
}
lateQueue.add(request);
}
}
private static class Queue {
private final List<PersistRequestUpdateSql> queue = new ArrayList<>();
boolean flush() {
if (queue.isEmpty()) {
return false;
}
for (PersistRequestUpdateSql request : queue) {
request.executeAddBatch();
}
queue.clear();
return true;
}
void add(PersistRequestUpdateSql request) {
queue.add(request);
}
}
}
@@ -141,12 +141,24 @@ public final class DefaultPersister implements Persister {
}
}
@Override
public void executeOrQueue(SpiSqlUpdate update, SpiTransaction t, boolean queue) {
if (queue) {
addToFlushQueue(update, t, false);
} else {
executeSqlUpdate(update, t);
}
}
private void addToFlushQueue(SpiSqlUpdate update, SpiTransaction t, boolean early) {
new PersistRequestUpdateSql(server, update, t, persistExecute).addToFlushQueue(early);
}
/**
* Execute the updateSql.
*/
@Override
public int executeSqlUpdate(SqlUpdate updSql, Transaction t) {
return executeOrQueue(new PersistRequestUpdateSql(server, (SpiSqlUpdate) updSql, (SpiTransaction) t, persistExecute));
}
@@ -958,12 +970,19 @@ public final class DefaultPersister implements Persister {
}
}
void deleteAssocManyIntersection(EntityBean bean, BeanPropertyAssocMany<?> many, Transaction t, boolean publish) {
void deleteManyIntersection(EntityBean bean, BeanPropertyAssocMany<?> many, SpiTransaction t, boolean publish, boolean queue) {
// delete all intersection rows for this bean
SpiSqlUpdate sqlDelete = deleteAllIntersection(bean, many, publish);
if (queue) {
addToFlushQueue(sqlDelete, t, true);
} else {
executeSqlUpdate(sqlDelete, t);
}
}
private SpiSqlUpdate deleteAllIntersection(EntityBean bean, BeanPropertyAssocMany<?> many, boolean publish) {
IntersectionRow intRow = many.buildManyToManyDeleteChildren(bean, publish);
SqlUpdate sqlDelete = intRow.createDeleteChildren(server);
executeSqlUpdate(sqlDelete, t);
return intRow.createDeleteChildren(server);
}
/**
@@ -1012,7 +1031,7 @@ public final class DefaultPersister implements Persister {
if (many.hasJoinTable()) {
if (deleteMode.isHard()) {
// delete associated rows from intersection table (but not during soft delete)
deleteAssocManyIntersection(parentBean, many, t, request.isPublish());
deleteManyIntersection(parentBean, many, t, request.isPublish(), false);
}
} else {
@@ -1,9 +1,9 @@
package io.ebeaninternal.server.persist;
import io.ebean.SqlUpdate;
import io.ebean.bean.BeanCollection;
import io.ebean.bean.EntityBean;
import io.ebean.bean.EntityBeanIntercept;
import io.ebeaninternal.api.SpiSqlUpdate;
import io.ebeaninternal.server.core.PersistRequest;
import io.ebeaninternal.server.core.PersistRequestBean;
import io.ebeaninternal.server.deploy.BeanCollectionUtil;
@@ -24,7 +24,7 @@ import java.util.Set;
/**
* Saves the details for a OneToMany or ManyToMany relationship (entity beans).
*/
class SaveManyBeans extends SaveManyBase {
public class SaveManyBeans extends SaveManyBase {
private static final Logger log = LoggerFactory.getLogger(SaveManyBeans.class);
@@ -244,14 +244,29 @@ class SaveManyBeans extends SaveManyBase {
if (value == null) {
return;
}
if (request.isQueueManyIntersection()) {
// queue/delay until bean persist request is flushed
this.deleteMissing = deleteMissingChildren;
request.setManyIntersection(this);
} else {
saveAssocManyIntersection(deleteMissingChildren, false);
}
}
/**
* Push intersection table changes onto batch flush queue.
*/
public void saveIntersectionBatch() {
saveAssocManyIntersection(deleteMissing, true);
}
private void saveAssocManyIntersection(boolean deleteMissingChildren, boolean queue) {
//SpiTransaction t = saveManyPropRequest.getTransaction();
boolean vanillaCollection = !(value instanceof BeanCollection<?>);
if (vanillaCollection || deleteMissingChildren) {
// delete all intersection rows and then treat all
// beans in the collection as additions
persister.deleteAssocManyIntersection(parentBean, many, transaction, publish);
persister.deleteManyIntersection(parentBean, many, transaction, publish, queue);
}
Collection<?> deletions = null;
@@ -287,10 +302,6 @@ class SaveManyBeans extends SaveManyBase {
transaction.depth(+1);
if (additions != null && !additions.isEmpty()) {
// ensure any cascade batch has been flushed prior
// to inserting into the intersection table
transaction.flushBatch();
for (Object other : additions) {
EntityBean otherBean = (EntityBean) other;
// the object from the 'other' side of the ManyToMany
@@ -309,24 +320,20 @@ class SaveManyBeans extends SaveManyBase {
} else {
// build a intersection row for 'insert'
IntersectionRow intRow = many.buildManyToManyMapBean(parentBean, otherBean, publish);
SqlUpdate sqlInsert = intRow.createInsert(server);
persister.executeSqlUpdate(sqlInsert, transaction);
SpiSqlUpdate sqlInsert = intRow.createInsert(server);
persister.executeOrQueue(sqlInsert, transaction, queue);
}
}
}
}
if (deletions != null && !deletions.isEmpty()) {
// ensure any cascade batch has been flushed prior
// to inserting into the intersection table
transaction.flushBatch();
for (Object other : deletions) {
EntityBean otherDelete = (EntityBean) other;
// the object from the 'other' side of the ManyToMany
// build a intersection row for 'delete'
IntersectionRow intRow = many.buildManyToManyMapBean(parentBean, otherDelete, publish);
SqlUpdate sqlDelete = intRow.createDelete(server, DeleteMode.HARD);
persister.executeSqlUpdate(sqlDelete, transaction);
SpiSqlUpdate sqlDelete = intRow.createDelete(server, DeleteMode.HARD);
persister.executeOrQueue(sqlDelete, transaction, queue);
}
}