mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1863 - L2 cache miss on natural key cache when multiple properties that include a ManyToOne
This commit is contained in:
@@ -2,6 +2,7 @@ package io.ebeaninternal.api;
|
||||
|
||||
|
||||
import io.ebean.Pairs;
|
||||
import io.ebeaninternal.server.deploy.BeanNaturalKey;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -19,7 +20,7 @@ public class NaturalKeyEntry {
|
||||
/**
|
||||
* Used when query query just has a series of EQ expressions (no IN clause).
|
||||
*/
|
||||
public NaturalKeyEntry(String[] naturalKey, List<NaturalKeyEq> eqList) {
|
||||
NaturalKeyEntry(BeanNaturalKey naturalKey, List<NaturalKeyEq> eqList) {
|
||||
load(eqList);
|
||||
this.key = calculateKey(naturalKey);
|
||||
}
|
||||
@@ -27,7 +28,7 @@ public class NaturalKeyEntry {
|
||||
/**
|
||||
* Create when query uses an IN clause.
|
||||
*/
|
||||
public NaturalKeyEntry(String[] naturalKey, List<NaturalKeyEq> eqList, String inProperty, Object inValue) {
|
||||
NaturalKeyEntry(BeanNaturalKey naturalKey, List<NaturalKeyEq> eqList, String inProperty, Object inValue) {
|
||||
load(eqList);
|
||||
if (inProperty != null) {
|
||||
map.put(inProperty, inValue);
|
||||
@@ -39,7 +40,7 @@ public class NaturalKeyEntry {
|
||||
/**
|
||||
* Create when query uses an IN PAIRS clause.
|
||||
*/
|
||||
public NaturalKeyEntry(String[] naturalKey, List<NaturalKeyEq> eqList,
|
||||
NaturalKeyEntry(BeanNaturalKey naturalKey, List<NaturalKeyEq> eqList,
|
||||
String inMapProperty0, String inMapProperty1, Pairs.Entry pair) {
|
||||
load(eqList);
|
||||
map.put(inMapProperty0, pair.getA());
|
||||
@@ -56,18 +57,8 @@ public class NaturalKeyEntry {
|
||||
}
|
||||
}
|
||||
|
||||
private Object calculateKey(String[] naturalKey) {
|
||||
|
||||
if (naturalKey.length == 1) {
|
||||
return map.get(naturalKey[0]);
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String key : naturalKey) {
|
||||
sb.append(map.get(key)).append(";");
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
private Object calculateKey(BeanNaturalKey naturalKey) {
|
||||
return naturalKey.calculateKey(map);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,7 +71,7 @@ public class NaturalKeyEntry {
|
||||
/**
|
||||
* Return the inValue (used to remove from IN clause of original query).
|
||||
*/
|
||||
public Object getInValue() {
|
||||
Object getInValue() {
|
||||
return inValue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.ebeaninternal.api;
|
||||
|
||||
import io.ebean.Pairs;
|
||||
import io.ebeaninternal.server.deploy.BeanNaturalKey;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
@@ -12,7 +13,7 @@ import java.util.Set;
|
||||
*/
|
||||
public class NaturalKeyQueryData<T> {
|
||||
|
||||
private final String[] naturalKey;
|
||||
private final BeanNaturalKey naturalKey;
|
||||
|
||||
/**
|
||||
* Only one of IN or IN PAIRS is allowed.
|
||||
@@ -34,18 +35,12 @@ public class NaturalKeyQueryData<T> {
|
||||
|
||||
private int hitCount;
|
||||
|
||||
public NaturalKeyQueryData(String[] naturalKey) {
|
||||
public NaturalKeyQueryData(BeanNaturalKey naturalKey) {
|
||||
this.naturalKey = naturalKey;
|
||||
}
|
||||
|
||||
private boolean matchProperty(String propName) {
|
||||
|
||||
for (String key : naturalKey) {
|
||||
if (key.equals(propName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return naturalKey.matchProperty(propName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,13 +127,8 @@ public class NaturalKeyQueryData<T> {
|
||||
* Return true if the properties match the natural key properties.
|
||||
*/
|
||||
private boolean matchProperties() {
|
||||
if (naturalKey.length == 1) {
|
||||
// simple single property case
|
||||
if (inProperty != null) {
|
||||
return inProperty.equals(naturalKey[0]);
|
||||
} else {
|
||||
return eqList.get(0).property.equals(naturalKey[0]);
|
||||
}
|
||||
if (naturalKey.isSingleProperty()) {
|
||||
naturalKey.matchSingleProperty((inProperty != null) ? inProperty : eqList.get(0).property);
|
||||
}
|
||||
|
||||
// multiple properties case
|
||||
@@ -157,27 +147,17 @@ public class NaturalKeyQueryData<T> {
|
||||
exprProps.add(eq.property);
|
||||
}
|
||||
}
|
||||
if (exprProps.size() != naturalKey.length) {
|
||||
return false;
|
||||
}
|
||||
for (String key : naturalKey) {
|
||||
if (!exprProps.remove(key)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return exprProps.isEmpty();
|
||||
return naturalKey.matchMultiProperties(exprProps);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that all the natural key properties are defined.
|
||||
*/
|
||||
private boolean expressionCount() {
|
||||
|
||||
int defined = (inValues == null) ? 0 : 1;
|
||||
defined += (inPairs == null) ? 0 : 2;
|
||||
defined += (eqList == null) ? 0 : eqList.size();
|
||||
return defined == naturalKey.length;
|
||||
return defined == naturalKey.length();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user