Merge AutoTune refactor with master

This commit is contained in:
Robin Bygrave committed 2015-09-10 10:27:24 +12:00
commit 262d52ed44
82 files changed
+2538 -2910

No files matched your search

@@ -1,14 +1,5 @@
package com.avaje.ebeaninternal.server.querydefn;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.PersistenceException;
import com.avaje.ebean.*;
import com.avaje.ebean.OrderBy.Property;
import com.avaje.ebean.bean.BeanCollectionTouched;
@@ -27,7 +18,7 @@ import com.avaje.ebeaninternal.api.ManyWhereJoins;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionList;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.autofetch.AutoFetchManager;
import com.avaje.ebeaninternal.server.autofetch.ProfilingListener;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.deploy.DRawSqlSelect;
@@ -37,6 +28,14 @@ import com.avaje.ebeaninternal.server.expression.SimpleExpression;
import com.avaje.ebeaninternal.server.query.CancelableQuery;
import com.avaje.ebeaninternal.util.DefaultExpressionList;
import javax.persistence.PersistenceException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Default implementation of an Object Relational query.
*/
@@ -58,7 +57,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
*/
private transient TableJoin includeTableJoin;
private transient AutoFetchManager autoFetchManager;
private transient ProfilingListener profilingListener;
private transient BeanDescriptor<?> beanDescriptor;
@@ -489,7 +488,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
DefaultOrmQuery<T> copy = new DefaultOrmQuery<T>(beanType, server, expressionFactory, (String) null);
copy.name = name;
copy.includeTableJoin = includeTableJoin;
copy.autoFetchManager = autoFetchManager;
copy.profilingListener = profilingListener;
copy.query = query;
copy.additionalWhere = additionalWhere;
@@ -624,19 +623,23 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
return this;
}
@Override
public DefaultOrmQuery<T> setForUpdate(boolean forUpdate) {
this.forUpdate = forUpdate;
return this;
}
public AutoFetchManager getAutoFetchManager() {
return autoFetchManager;
@Override
public ProfilingListener getProfilingListener() {
return profilingListener;
}
public void setAutoFetchManager(AutoFetchManager autoFetchManager) {
this.autoFetchManager = autoFetchManager;
@Override
public void setProfilingListener(ProfilingListener profilingListener) {
this.profilingListener = profilingListener;
}
@Override
public Mode getMode() {
return mode;
}
@@ -9,6 +9,7 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.PersistenceException;
@@ -80,26 +81,35 @@ public class OrmQueryDetail implements Serializable {
}
/**
* Return true if equal in terms of autofetch (select and joins).
* Return true if equal in terms of autoTune (select and fetch).
*/
public boolean isAutoFetchEqual(OrmQueryDetail otherDetail) {
return autofetchPlanHash() == otherDetail.autofetchPlanHash();
}
public boolean isAutoTuneEqual(OrmQueryDetail otherDetail) {
/**
* Calculate the hash for the query plan.
*/
private int autofetchPlanHash() {
int hc = (baseProps == null ? 1 : baseProps.autofetchPlanHash());
if (fetchPaths != null) {
for (OrmQueryProperties p : fetchPaths.values()) {
hc = hc * 31 + p.autofetchPlanHash();
if (!isSame(baseProps, otherDetail.baseProps)) {
return false;
}
if (fetchPaths == null) {
return otherDetail.fetchPaths == null;
}
if (fetchPaths.size() != otherDetail.fetchPaths.size()) {
return false;
}
Set<Map.Entry<String, OrmQueryProperties>> entries = fetchPaths.entrySet();
for (Map.Entry<String, OrmQueryProperties> entry : entries) {
OrmQueryProperties chunk = otherDetail.getChunk(entry.getKey(), false);
if (!isSame(entry.getValue(), chunk)) {
return false;
}
}
return hc;
return true;
}
private boolean isSame(OrmQueryProperties p1, OrmQueryProperties p2) {
if (p1 == null) {
return p2 == null;
}
return p1.isSame(p2);
}
public String toString() {
@@ -212,7 +222,7 @@ public class OrmQueryDetail implements Serializable {
boolean tuned = false;
OrmQueryProperties tunedRoot = tunedDetail.getChunk(null, false);
if (tunedRoot != null && tunedRoot.hasProperties()) {
if (tunedRoot != null) {
tuned = true;
baseProps.setTunedProperties(tunedRoot);
@@ -27,10 +27,11 @@ public class OrmQueryDetailParser {
this.parser = new SimpleTextParser(oql);
}
public void parse() throws PersistenceException {
public OrmQueryDetail parse() throws PersistenceException {
parser.nextWord();
processInitial();
return detail;
}
protected void assign(DefaultOrmQuery<?> query) {
@@ -42,7 +43,9 @@ public class OrmQueryDetailParser {
}
private void processInitial() {
if (parser.isMatch("find")) {
if (parser.isMatch("select")) {
readSelect();
} else if (parser.isMatch("find")) {
OrmQueryProperties props = readFindFetch();
detail.setBase(props);
} else {
@@ -149,6 +152,19 @@ public class OrmQueryDetailParser {
}
}
private void readSelect() {
String path = null;
String props = parser.nextWord();
if (props.startsWith("(")) {
props = props.substring(1, props.length() - 1);
OrmQueryProperties base = new OrmQueryProperties(path, props);
detail.setBase(base);
parser.nextWord();
} else {
process();
}
}
private OrmQueryProperties readFindFetch() {
boolean readAlias = false;
@@ -215,9 +215,13 @@ public class OrmQueryProperties implements Serializable {
* Set the properties from a matching autofetch tuned properties.
*/
public void setTunedProperties(OrmQueryProperties tunedProperties) {
this.properties = tunedProperties.properties;
this.trimmedProperties = tunedProperties.trimmedProperties;
this.included = tunedProperties.included;
if (tunedProperties.hasProperties()) {
this.properties = tunedProperties.properties;
this.trimmedProperties = tunedProperties.trimmedProperties;
this.included = tunedProperties.included;
this.queryFetchBatch = Math.max(queryFetchBatch, tunedProperties.queryFetchBatch);
this.lazyFetchBatch = Math.max(lazyFetchBatch, tunedProperties.lazyFetchBatch);
}
}
/**
@@ -262,6 +266,7 @@ public class OrmQueryProperties implements Serializable {
copy.parentPath = parentPath;
copy.path = path;
copy.properties = properties;
copy.trimmedProperties = trimmedProperties;
copy.cache = cache;
copy.readOnly = readOnly;
copy.queryFetchAll = queryFetchAll;
@@ -573,4 +578,11 @@ public class OrmQueryProperties implements Serializable {
return Collections.unmodifiableSet(set);
}
public boolean isSame(OrmQueryProperties p2) {
if (included == null) {
return p2.included == null;
}
return included.equals(p2.included);
}
}