#943 - Refactor internals - Performance tweak / tidy for WhoCreated, WhoModified on OneToMany connections. #942

This commit is contained in:
Rob Bygrave
2017-01-11 21:42:23 +13:00
parent b6f21f8649
commit fbfe5d1ff8
4 changed files with 101 additions and 50 deletions
@@ -3,11 +3,8 @@ package io.ebeaninternal.server.persist.dmlbind;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.server.core.PersistRequestBean;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import io.ebeaninternal.server.deploy.generatedproperty.GeneratedProperty;
import io.ebeaninternal.server.deploy.id.ImportedId;
import io.ebeaninternal.server.persist.dml.DmlMode;
import io.ebeaninternal.server.persist.dml.GenerateDmlRequest;
import org.jetbrains.annotations.Nullable;
import java.sql.SQLException;
import java.util.List;
@@ -15,19 +12,15 @@ import java.util.List;
/**
* Bindable for an ManyToOne or OneToOne associated bean.
*/
public class BindableAssocOne implements Bindable {
class BindableAssocOne implements Bindable {
private final BeanPropertyAssocOne<?> assocOne;
protected final BeanPropertyAssocOne<?> assocOne;
private final ImportedId importedId;
private final DmlMode dmlMode;
private final GeneratedProperty generatedProperty;
protected final ImportedId importedId;
public BindableAssocOne(BeanPropertyAssocOne<?> assocOne, DmlMode mode) {
BindableAssocOne(BeanPropertyAssocOne<?> assocOne) {
this.assocOne = assocOne;
this.importedId = assocOne.getImportedId();
this.dmlMode = mode;
this.generatedProperty = assocOne.getGeneratedProperty();
}
public String toString() {
@@ -40,9 +33,7 @@ public class BindableAssocOne implements Bindable {
}
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
if (generatedProperty != null && generatedProperty.includeInAllUpdates()) {
list.add(this);
} else if (request.isAddToUpdate(assocOne)) {
if (request.isAddToUpdate(assocOne)) {
list.add(this);
}
}
@@ -52,41 +43,15 @@ public class BindableAssocOne implements Bindable {
}
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
if (generatedProperty != null) {
bindGeneratedProperty(request, bean);
} else {
bindNotGeneratedProperty(request, bean);
}
}
private void bindGeneratedProperty(BindableRequest request, EntityBean bean) throws SQLException {
if (DmlMode.INSERT.equals(dmlMode) && generatedProperty.includeInInsert()) {
bindGeneratedInsert(request, bean);
} else if (DmlMode.UPDATE.equals(dmlMode) && generatedProperty.includeInAllUpdates()) {
bindGeneratedUpdate(request, bean);
}
}
private void bindGeneratedUpdate(BindableRequest request, EntityBean bean) throws SQLException {
Object objectValue = generatedProperty.getUpdateValue(assocOne, bean, request.now());
EntityBean generatedValue = castToEntityBean(objectValue);
assocOne.setValueChanged(bean, generatedValue);
registerDeferred(request, bean, generatedValue);
}
private void bindGeneratedInsert(BindableRequest request, EntityBean bean) throws SQLException {
Object objectValue = generatedProperty.getInsertValue(assocOne, bean, request.now());
EntityBean generatedValue = castToEntityBean(objectValue);
assocOne.setValue(bean, generatedValue);
registerDeferred(request, bean, generatedValue);
}
private void bindNotGeneratedProperty(BindableRequest request, EntityBean bean) throws SQLException {
EntityBean assocBean = (EntityBean) assocOne.getValue(bean);
registerDeferred(request, bean, assocBean);
}
private void registerDeferred(BindableRequest request, EntityBean bean, EntityBean assocBean) throws SQLException {
/**
* Bind and register a deferred relationship value.
*/
void registerDeferred(BindableRequest request, EntityBean bean, EntityBean assocBean) throws SQLException {
Object boundValue = importedId.bind(request, assocBean);
if (boundValue == null && assocBean != null) {
// this is the scenario for a derived foreign key
@@ -97,15 +62,15 @@ public class BindableAssocOne implements Bindable {
}
}
@Nullable
private EntityBean castToEntityBean(Object objectValue) {
EntityBean generatedValue;
/**
* Cast to an EntityBean allowing null.
*/
EntityBean castToEntityBean(Object objectValue) {
if (objectValue instanceof EntityBean || objectValue == null) {
generatedValue = (EntityBean) objectValue;
return (EntityBean) objectValue;
} else {
throw new IllegalStateException("Bean " + objectValue.getClass() + " is not enhanced?");
}
return generatedValue;
}
}
@@ -0,0 +1,36 @@
package io.ebeaninternal.server.persist.dmlbind;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.server.core.PersistRequestBean;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import io.ebeaninternal.server.deploy.generatedproperty.GeneratedProperty;
import java.sql.SQLException;
import java.util.List;
/**
* Bindable for generated ManyToOne - likely 'who created'.
*/
class BindableAssocOneGeneratedInsert extends BindableAssocOne {
private final GeneratedProperty generatedProperty;
BindableAssocOneGeneratedInsert(BeanPropertyAssocOne<?> assocOne) {
super(assocOne);
this.generatedProperty = assocOne.getGeneratedProperty();
}
@Override
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
throw new RuntimeException("never called");
}
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
Object objectValue = generatedProperty.getInsertValue(assocOne, bean, request.now());
EntityBean generatedValue = castToEntityBean(objectValue);
assocOne.setValue(bean, generatedValue);
registerDeferred(request, bean, generatedValue);
}
}
@@ -0,0 +1,38 @@
package io.ebeaninternal.server.persist.dmlbind;
import io.ebean.bean.EntityBean;
import io.ebeaninternal.server.core.PersistRequestBean;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import io.ebeaninternal.server.deploy.generatedproperty.GeneratedProperty;
import java.sql.SQLException;
import java.util.List;
/**
* Bindable for generated ManyToOne - likely 'who modified'.
*/
class BindableAssocOneGeneratedUpdate extends BindableAssocOne {
private final GeneratedProperty generatedProperty;
BindableAssocOneGeneratedUpdate(BeanPropertyAssocOne<?> assocOne) {
super(assocOne);
this.generatedProperty = assocOne.getGeneratedProperty();
}
@Override
public void addToUpdate(PersistRequestBean<?> request, List<Bindable> list) {
if (generatedProperty.includeInAllUpdates() || request.isAddToUpdate(assocOne)) {
list.add(this);
}
}
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
Object objectValue = generatedProperty.getUpdateValue(assocOne, bean, request.now());
EntityBean generatedValue = castToEntityBean(objectValue);
assocOne.setValueChanged(bean, generatedValue);
registerDeferred(request, bean, generatedValue);
}
}
@@ -35,7 +35,19 @@ public class FactoryAssocOnes {
}
break;
}
list.add(new BindableAssocOne(one, mode));
if (one.getGeneratedProperty() == null) {
list.add(new BindableAssocOne(one));
} else {
// typically generated 'who' created/modified properties
switch (mode) {
case INSERT:
list.add(new BindableAssocOneGeneratedInsert(one));
break;
case UPDATE:
list.add(new BindableAssocOneGeneratedUpdate(one));
break;
}
}
}
}
}