#1863 - L2 cache miss on natural key cache when multiple properties that include a ManyToOne

This commit is contained in:
rob bygrave
2019-11-16 17:03:34 +13:00
parent 738f19491a
commit dfefbecf44
16 changed files with 368 additions and 49 deletions
@@ -375,6 +375,8 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
private final BeanProperty[] propertiesGenUpdate;
private final List<BeanProperty[]> propertiesUnique = new ArrayList<>();
private BeanNaturalKey beanNaturalKey;
/**
* The bean class name or the table name for MapBeans.
*/
@@ -752,6 +754,18 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
softDeleteByIdSql = null;
softDeleteByIdInSql = null;
}
initNaturalKey();
}
private void initNaturalKey() {
final String[] naturalKey = cacheHelp.getNaturalKey();
if (naturalKey != null && naturalKey.length != 0) {
BeanProperty[] props = new BeanProperty[naturalKey.length];
for (int i = 0; i < naturalKey.length; i++) {
props[i] = getBeanProperty(naturalKey[i]);
}
this.beanNaturalKey = new BeanNaturalKey(naturalKey, props);
}
}
private boolean hasCircularImportedId() {
@@ -1322,10 +1336,10 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
}
/**
* Return the natural key properties.
* Return the natural key.
*/
public String[] getNaturalKey() {
return cacheHelp.getNaturalKey();
public BeanNaturalKey getNaturalKey() {
return beanNaturalKey;
}
/**
@@ -0,0 +1,80 @@
package io.ebeaninternal.server.deploy;
import java.util.Map;
import java.util.Set;
/**
* Natural key for a bean type.
*/
public class BeanNaturalKey {
private final String[] naturalKey;
private final BeanProperty[] props;
BeanNaturalKey(String[] naturalKey, BeanProperty[] props) {
this.naturalKey = naturalKey;
this.props = props;
}
public int length() {
return naturalKey.length;
}
/**
* Return true if the property name is part of the natural key.
*/
public boolean matchProperty(String propName) {
for (String key : naturalKey) {
if (key.equals(propName)) {
return true;
}
}
return false;
}
/**
* Return true if this is a single property natural key.
*/
public boolean isSingleProperty() {
return props.length == 1;
}
/**
* Return true if the given propertyName is our natural key property.
*/
public boolean matchSingleProperty(String propertyName) {
return naturalKey[0].equals(propertyName);
}
/**
* Return true if all the properties match our natural key.
*/
public boolean matchMultiProperties(Set<String> expressionProperties) {
if (expressionProperties.size() != naturalKey.length) {
return false;
}
for (String key : naturalKey) {
if (!expressionProperties.remove(key)) {
return false;
}
}
return expressionProperties.isEmpty();
}
/**
* Return the cache key given the bind values.
*
* @param map The bind values for the properties.
*/
public Object calculateKey(Map<String, Object> map) {
if (naturalKey.length == 1) {
return map.get(naturalKey[0]);
}
StringBuilder sb = new StringBuilder();
for (BeanProperty prop : props) {
sb.append(prop.naturalKeyVal(map)).append(";");
}
return sb.toString();
}
}
@@ -856,6 +856,13 @@ public class BeanProperty implements ElPropertyValue, Property, STreeProperty {
setValue(bean, cacheData);
}
/**
* Return the cache key value for this property.
*/
Object naturalKeyVal(Map<String, Object> values) {
return values.get(name);
}
@Override
public Object getVal(Object bean) {
return getValue((EntityBean) bean);
@@ -210,6 +210,14 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> implements STr
}
}
Object naturalKeyVal(Map<String, Object> values) {
EntityBean bean = (EntityBean) values.get(name);
if (bean == null) {
return null;
}
return targetIdBinder.cacheKeyFromBean(bean);
}
@Override
public ElPropertyValue buildElPropertyValue(String propName, String remainder, ElPropertyChainBuilder chain, boolean propertyDeploy) {
@@ -213,4 +213,8 @@ public interface IdBinder {
*/
String cacheKey(Object idValue);
/**
* Return a key to use for bean caches given the bean.
*/
String cacheKeyFromBean(EntityBean bean);
}
@@ -501,4 +501,8 @@ public final class IdBinderEmbedded implements IdBinder {
return sb.toString();
}
@Override
public String cacheKeyFromBean(EntityBean bean) {
return cacheKey(embIdProperty.getValue(bean));
}
}
@@ -182,4 +182,8 @@ public final class IdBinderEmpty implements IdBinder {
return null;
}
@Override
public String cacheKeyFromBean(EntityBean bean) {
return null;
}
}
@@ -268,4 +268,9 @@ public final class IdBinderSimple implements IdBinder {
return scalarType.format(value);
}
@Override
public String cacheKeyFromBean(EntityBean bean) {
final Object value = idProperty.getValue(bean);
return scalarType.format(value);
}
}
@@ -48,6 +48,7 @@ import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.server.autotune.ProfilingListener;
import io.ebeaninternal.server.core.SpiOrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanNaturalKey;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.InheritInfo;
import io.ebeaninternal.server.deploy.TableJoin;
@@ -771,8 +772,8 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
if (whereExpressions == null) {
return null;
}
String[] naturalKey = beanDescriptor.getNaturalKey();
if (naturalKey == null || naturalKey.length == 0) {
BeanNaturalKey naturalKey = beanDescriptor.getNaturalKey();
if (naturalKey == null) {
return null;
}