initial rework for new enhancement

This commit is contained in:
Robin Bygrave committed 2013-06-19 20:36:24 +12:00
1 parent eb1eafe98c
commit 4ec62a5772
193 files changed
+2737 -5285

No files matched your search

@@ -4,6 +4,7 @@ import java.util.Collection;
import java.util.Map;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
import com.avaje.ebeaninternal.server.core.RelationalQueryRequest;
@@ -149,7 +150,7 @@ public final class BeanCollectionWrapper {
/**
* Add the bean to the collection held in this wrapper.
*/
public void add(Object bean) {
public void add(EntityBean bean) {
add(bean, beanCollection);
}
@@ -162,7 +163,7 @@ public final class BeanCollectionWrapper {
* the collection or map to add the bean to
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void add(Object bean, Object collection) {
public void add(EntityBean bean, Object collection) {
if (bean == null) {
return;
}
@@ -82,20 +82,21 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
* Flag set when 'master' bean changed.
*/
boolean loadedBeanChanged;
/**
* The 'master' bean just loaded.
*/
private Object loadedBean;
private EntityBean loadedBean;
/**
* Holds the previous loaded bean.
*/
private Object prevLoadedBean;
private EntityBean prevLoadedBean;
/**
* The detail bean just loaded.
*/
private Object loadedManyBean;
private EntityBean loadedManyBean;
/**
* The previous 'detail' collection remembered so that for manyToMany we can
@@ -414,7 +415,7 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
return persistenceContext;
}
public void setLoadedBean(Object bean, Object id) {
public void setLoadedBean(EntityBean bean, Object id) {
if (id != null && id.equals(loadedBeanId)) {
// master/detail loading with master bean
// unchanged. NB Using id to avoid any issue
@@ -432,15 +433,14 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
}
}
public void setLoadedManyBean(Object manyValue) {
public void setLoadedManyBean(EntityBean manyValue) {
this.loadedManyBean = manyValue;
}
/**
* Return the last read bean.
*/
@SuppressWarnings("unchecked")
public T getLoadedBean() {
public EntityBean getLoadedBean() {
if (manyIncluded) {
if (prevDetailCollection instanceof BeanCollection<?>) {
((BeanCollection<?>) prevDetailCollection).setModifyListening(manyProperty
@@ -453,9 +453,9 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
}
if (prevLoadedBean != null) {
return (T) prevLoadedBean;
return prevLoadedBean;
} else {
return (T) loadedBean;
return loadedBean;
}
}
@@ -641,10 +641,11 @@ public class CQuery<T> implements DbReadContext, CancelableQuery {
}
}
@SuppressWarnings("unchecked")
private void readTheRows(boolean inForeground) throws SQLException {
while (hasNextBean(inForeground)) {
if (queryListener != null) {
queryListener.process(getLoadedBean());
queryListener.process((T)getLoadedBean());
} else {
// add to the list/set/map
@@ -10,6 +10,7 @@ import com.avaje.ebean.BackgroundExecutor;
import com.avaje.ebean.QueryIterator;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.BeanCollectionTouched;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.ObjectGraphNode;
import com.avaje.ebean.config.dbplatform.DatabasePlatform;
import com.avaje.ebeaninternal.api.BeanIdList;
@@ -223,7 +224,7 @@ public class CQueryEngine {
*/
public <T> T find(OrmQueryRequest<T> request) {
T bean = null;
EntityBean bean = null;
CQuery<T> cquery = queryBuilder.buildQuery(request);
@@ -244,7 +245,7 @@ public class CQueryEngine {
request.executeSecondaryQueries(defaultSecondaryQueryBatchSize);
return bean;
return (T)bean;
} catch (SQLException e) {
throw cquery.createPersistenceException(e);
@@ -10,8 +10,12 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.FutureTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.avaje.ebean.BackgroundExecutor;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.EntityBeanIntercept;
import com.avaje.ebean.bean.PersistenceContext;
import com.avaje.ebeaninternal.api.BeanIdList;
@@ -26,8 +30,6 @@ import com.avaje.ebeaninternal.server.deploy.DbReadContext;
import com.avaje.ebeaninternal.server.type.DataBind;
import com.avaje.ebeaninternal.server.type.DataReader;
import com.avaje.ebeaninternal.server.type.RsetDataReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Executes the select row count query.
@@ -299,11 +301,11 @@ public class CQueryFetchIds {
// no-op
}
public void setLoadedBean(Object loadedBean, Object id) {
public void setLoadedBean(EntityBean loadedBean, Object id) {
// no-op
}
public void setLoadedManyBean(Object loadedBean) {
public void setLoadedManyBean(EntityBean loadedBean) {
// no-op
}
@@ -31,7 +31,7 @@ class CQueryIteratorSimple<T> implements QueryIterator<T> {
}
public T next() {
return cquery.getLoadedBean();
return (T)cquery.getLoadedBean();
}
public void close() {
@@ -36,7 +36,7 @@ class CQueryIteratorWithBuffer<T> implements QueryIterator<T> {
int i = -1;
while (moreToLoad && ++i < bufferSize) {
if (cquery.hasNextBean(true)) {
buffer.add(cquery.getLoadedBean());
buffer.add((T)cquery.getLoadedBean());
} else {
moreToLoad = false;
}
@@ -4,6 +4,7 @@ import java.util.Collection;
import com.avaje.ebean.QueryIterator;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.event.BeanFinder;
import com.avaje.ebeaninternal.api.BeanIdList;
import com.avaje.ebeaninternal.api.SpiQuery;
@@ -82,7 +83,7 @@ public class DefaultOrmQueryEngine implements OrmQueryEngine {
BeanDescriptor<T> descriptor = request.getBeanDescriptor();
Collection<T> c = result.getActualDetails();
for (T bean : c) {
descriptor.cachePutBeanData(bean);
descriptor.cachePutBeanData((EntityBean)bean);
}
}
@@ -119,7 +120,7 @@ public class DefaultOrmQueryEngine implements OrmQueryEngine {
}
if (result != null && request.isUseBeanCache()){
request.getBeanDescriptor().cachePutBeanData(result);
request.getBeanDescriptor().cachePutBeanData((EntityBean)result);
}
return result;
@@ -1,7 +1,6 @@
package com.avaje.ebeaninternal.server.query;
import java.sql.SQLException;
import java.util.Set;
import javax.persistence.PersistenceException;
@@ -22,97 +21,73 @@ import com.avaje.ebeaninternal.server.deploy.DbReadContext;
public class SqlBeanLoad {
private final DbReadContext ctx;
private final Object bean;
private final EntityBean bean;
private final EntityBeanIntercept ebi;
private final Class<?> type;
private final Object originalOldValues;
private final boolean isLazyLoad;
private final boolean lazyLoading;
private final boolean refreshLoading;
private final boolean rawSql;
// set of properties to exclude from the refresh because it is
// not a refresh but rather a lazyLoading event.
private final Set<String> excludes;
private final boolean setOriginalOldValues;
private final boolean rawSql;
public SqlBeanLoad(DbReadContext ctx, Class<?> type, Object bean, Mode queryMode) {
this.ctx = ctx;
this.rawSql = ctx.isRawSql();
this.type = type;
this.isLazyLoad = queryMode.equals(Mode.LAZYLOAD_BEAN);
this.bean = bean;
if (bean instanceof EntityBean) {
EntityBeanIntercept ebi = ((EntityBean) bean)._ebean_getIntercept();
public SqlBeanLoad(DbReadContext ctx, Class<?> type, EntityBean bean, Mode queryMode) {
this.excludes = isLazyLoad ? ebi.getLoadedProps() : null;
if (excludes != null) {
// lazy loading a "Partial Object"... which already
// contains some properties and perhaps some oldValues
// and these will need to be maintained...
originalOldValues = ebi.getOldValues();
} else {
originalOldValues = null;
}
this.setOriginalOldValues = originalOldValues != null;
} else {
this.excludes = null;
this.originalOldValues = null;
this.setOriginalOldValues = false;
}
}
this.ctx = ctx;
this.rawSql = ctx.isRawSql();
this.type = type;
this.lazyLoading = queryMode.equals(Mode.LAZYLOAD_BEAN);
this.refreshLoading = queryMode.equals(Mode.REFRESH_BEAN);
this.bean = bean;
this.ebi = bean == null ? null : bean._ebean_getIntercept();
}
/**
* Return true if this is a lazy loading.
*/
public boolean isLazyLoad() {
return isLazyLoad;
/**
* Return true if this is a lazy loading.
*/
public boolean isLazyLoad() {
return lazyLoading;
}
/**
* Increment the resultSet index 1.
*/
public void loadIgnore(int increment) {
ctx.getDataReader().incrementPos(increment);
}
public Object load(BeanProperty prop) throws SQLException {
if (!rawSql && prop.isTransient()) {
return null;
}
/**
* Increment the resultSet index 1.
*/
public void loadIgnore(int increment) {
ctx.getDataReader().incrementPos(increment);
}
public Object load(BeanProperty prop) throws SQLException {
if (!rawSql && prop.isTransient()){
return null;
}
if ((bean == null)
|| (excludes != null && excludes.contains(prop.getName()))
|| (type != null && !prop.isAssignableFrom(type))){
if ((bean == null)
|| (lazyLoading && ebi.isLoadedProperty(prop.getPropertyIndex()))
|| (type != null && !prop.isAssignableFrom(type))) {
// ignore this property
// ... null: bean already in persistence context
// ... excludes: partial bean that is lazy loading
// ... type: inheritance and not assignable to this instance
prop.loadIgnore(ctx);
return null;
}
try {
Object dbVal = prop.read(ctx);
if (isLazyLoad){
prop.setValue(bean, dbVal);
} else {
prop.setValueIntercept(bean, dbVal);
}
if (setOriginalOldValues){
// maintain original oldValues for partially loaded bean
prop.setValue(originalOldValues, dbVal);
}
return dbVal;
} catch (Exception e) {
String msg = "Error loading on " + prop.getFullBeanName();
throw new PersistenceException(msg, e);
}
}
// ignore this property
// ... null: bean already in persistence context
// ... lazyLoading: partial bean that is lazy loading
// ... type: inheritance and not assignable to this instance
prop.loadIgnore(ctx);
return null;
}
try {
Object dbVal = prop.read(ctx);
if (!refreshLoading) {
prop.setValue(bean, dbVal);
} else {
prop.setValueIntercept(bean, dbVal);
}
return dbVal;
} catch (Exception e) {
String msg = "Error loading on " + prop.getFullBeanName();
throw new PersistenceException(msg, e);
}
}
public void loadAssocMany(BeanPropertyAssocMany<?> prop) {
@@ -372,9 +372,6 @@ public class SqlTreeBuilder {
// add the embedded bean (and effectively
// all its properties)
selectProps.add(p);
// also make sure it is added to included properties
// to avoid unnecessary lazy loading
selectProps.getIncludedProperties().add(baseName);
} else {
String m = "property [" + p.getFullBeanName()
@@ -412,12 +409,10 @@ public class SqlTreeBuilder {
}
}
private SqlTreeProperties getBaseSelectPartial(BeanDescriptor<?> desc,
OrmQueryProperties queryProps) {
private SqlTreeProperties getBaseSelectPartial(BeanDescriptor<?> desc, OrmQueryProperties queryProps) {
SqlTreeProperties selectProps = new SqlTreeProperties();
SqlTreeProperties selectProps = new SqlTreeProperties(desc);
selectProps.setReadOnly(queryProps.isReadOnly());
selectProps.setIncludedProperties(queryProps.getAllIncludedProperties());
// add properties in the order in which they appear
// in the query. Gives predictable sql/properties for
@@ -444,7 +439,8 @@ public class SqlTreeBuilder {
return getBaseSelectPartial(desc, queryProps);
}
SqlTreeProperties selectProps = new SqlTreeProperties();
SqlTreeProperties selectProps = new SqlTreeProperties(desc);
selectProps.setAllProperties(true);
// normal simple properties of the bean
selectProps.add(desc.propertiesBaseScalar());
@@ -3,6 +3,7 @@ package com.avaje.ebeaninternal.server.query;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.deploy.DbReadContext;
import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
@@ -42,6 +43,6 @@ public interface SqlTreeNode {
* </p>
*
*/
public void load(DbReadContext ctx, Object parentBean) throws SQLException;
public void load(DbReadContext ctx, EntityBean parentBean) throws SQLException;
}
@@ -4,7 +4,6 @@ import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.EntityBean;
@@ -45,18 +44,6 @@ public class SqlTreeNodeBean implements SqlTreeNode {
*/
final boolean partialObject;
/**
* The set of properties explicitly included in the query. We actually add the
* manyProp names to this as they are references/proxies we add via
* createListProxies().
*/
final Set<String> partialProps;
/**
* The hash of the partialProps (calculate once).
*/
final int partialHash;
final BeanProperty[] properties;
/**
@@ -79,8 +66,6 @@ public class SqlTreeNodeBean implements SqlTreeNode {
final String prefix;
final Set<String> includedProps;
final Map<String, String> pathMap;
public SqlTreeNodeBean(String prefix, BeanPropertyAssoc<?> beanProp, SqlTreeProperties props,
@@ -108,24 +93,10 @@ public class SqlTreeNodeBean implements SqlTreeNode {
this.disableLazyLoad = !readId || desc.isSqlSelectBased();
this.tableJoins = props.getTableJoins();
this.partialObject = props.isPartialObject();
this.partialProps = props.getIncludedProperties();
this.partialHash = partialObject ? partialProps.hashCode() : 0;
this.readOnlyLeaf = props.isReadOnly();
this.properties = props.getProps();
if (partialObject) {
// merge the explicit partialProps with the implicitly added
// list proxies (that are added by createListProxies()) to get
// the full set of 'loaded' properties for this bean.
includedProps = LoadedPropertiesCache.get(partialHash, partialProps, desc);
} else {
includedProps = null;
}
if (myChildren == null) {
children = NO_CHILDREN;
} else {
@@ -156,7 +127,7 @@ public class SqlTreeNodeBean implements SqlTreeNode {
}
}
protected void postLoad(DbReadContext cquery, Object loadedBean, Object id) {
protected void postLoad(DbReadContext cquery, EntityBean loadedBean, Object id) {
}
public void buildSelectExpressionChain(List<String> selectChain) {
@@ -177,15 +148,15 @@ public class SqlTreeNodeBean implements SqlTreeNode {
/**
* read the properties from the resultSet.
*/
public void load(DbReadContext ctx, Object parentBean) throws SQLException {
public void load(DbReadContext ctx, EntityBean parentBean) throws SQLException {
// bean already existing in the persistence context
Object contextBean = null;
EntityBean contextBean = null;
Class<?> localType;
BeanDescriptor<?> localDesc;
IdBinder localIdBinder;
Object localBean;
EntityBean localBean;
if (inheritInfo != null) {
InheritInfo localInfo = inheritInfo.readType(ctx);
@@ -224,7 +195,7 @@ public class SqlTreeNodeBean implements SqlTreeNode {
localBean = null;
} else {
// check the PersistenceContext to see if the bean already exists
contextBean = persistenceContext.putIfAbsent(id, localBean);
contextBean = (EntityBean)persistenceContext.putIfAbsent(id, localBean);
if (contextBean == null) {
// bean just added to the persistenceContext
contextBean = localBean;
@@ -233,10 +204,6 @@ public class SqlTreeNodeBean implements SqlTreeNode {
if (queryMode.isLoadContextBean()) {
// refresh it anyway (lazy loading for example)
localBean = contextBean;
if (localBean instanceof EntityBean) {
// temporarily turn off interception during load
((EntityBean) localBean)._ebean_getIntercept().setIntercepting(false);
}
} else {
// ignore the DB data...
localBean = null;
@@ -297,12 +264,11 @@ public class SqlTreeNodeBean implements SqlTreeNode {
ctx.setCurrentPrefix(prefix, pathMap);
createListProxies(localDesc, ctx, localBean);
localDesc.postLoad(localBean, includedProps);
localDesc.postLoad(localBean, null);
if (localBean instanceof EntityBean) {
EntityBeanIntercept ebi = ((EntityBean) localBean)._ebean_getIntercept();
ebi.setPersistenceContext(persistenceContext);
ebi.setLoadedProps(includedProps);
if (Mode.LAZYLOAD_BEAN.equals(queryMode)) {
// Lazy Load does not reset the dirty state
ebi.setLoadedLazy();
@@ -313,6 +279,8 @@ public class SqlTreeNodeBean implements SqlTreeNode {
if (partialObject) {
ctx.register(null, ebi);
} else {
ebi.setFullyLoadedBean(true);
}
if (disableLazyLoad) {
@@ -347,7 +315,7 @@ public class SqlTreeNodeBean implements SqlTreeNode {
* Create lazy loading proxies for the Many's except for the one that is
* included in the actual query.
*/
private void createListProxies(BeanDescriptor<?> localDesc, DbReadContext ctx, Object localBean) {
private void createListProxies(BeanDescriptor<?> localDesc, DbReadContext ctx, EntityBean localBean) {
BeanPropertyAssocMany<?> fetchedMany = ctx.getManyProperty();
@@ -4,6 +4,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.deploy.DbReadContext;
@@ -120,7 +121,7 @@ public class SqlTreeNodeExtraJoin implements SqlTreeNode {
/**
* Does nothing.
*/
public void load(DbReadContext ctx, Object parentBean) throws SQLException {
public void load(DbReadContext ctx, EntityBean parentBean) throws SQLException {
}
}
@@ -3,6 +3,7 @@ package com.avaje.ebeaninternal.server.query;
import java.sql.SQLException;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.deploy.DbReadContext;
import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
@@ -14,7 +15,7 @@ public final class SqlTreeNodeManyRoot extends SqlTreeNodeBean {
}
@Override
protected void postLoad(DbReadContext cquery, Object loadedBean, Object id) {
protected void postLoad(DbReadContext cquery, EntityBean loadedBean, Object id) {
// put the localBean into the manyValue so that it
// is added to the collection/map
@@ -22,7 +23,7 @@ public final class SqlTreeNodeManyRoot extends SqlTreeNodeBean {
}
@Override
public void load(DbReadContext cquery, Object parentBean) throws SQLException {
public void load(DbReadContext cquery, EntityBean parentBean) throws SQLException {
// pass in null for parentBean because the localBean
// that is built is added to a collection rather than
// being set to the parentBean directly
@@ -4,6 +4,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocOne;
@@ -86,7 +87,7 @@ public class SqlTreeNodeManyWhereJoin implements SqlTreeNode {
// nothing to do here
}
public void load(DbReadContext ctx, Object parentBean) throws SQLException {
public void load(DbReadContext ctx, EntityBean parentBean) throws SQLException {
// nothing to do here
}
@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.query;
import java.util.List;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.DbReadContext;
import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
@@ -28,7 +29,7 @@ public final class SqlTreeNodeRoot extends SqlTreeNodeBean {
}
@Override
protected void postLoad(DbReadContext cquery, Object loadedBean, Object id) {
protected void postLoad(DbReadContext cquery, EntityBean loadedBean, Object id) {
// set the current bean with id...
cquery.setLoadedBean(loadedBean, id);
@@ -5,6 +5,7 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.TableJoin;
@@ -13,10 +14,14 @@ import com.avaje.ebeaninternal.server.deploy.TableJoin;
*/
public class SqlTreeProperties {
/**
* The included Properties that will be used by EntityBeanIntercept
* to determine lazy loading on partial objects.
*/
private static final TableJoin[] EMPTY_TABLE_JOINS = new TableJoin[0];
private final BeanDescriptor<?> desc;
// /**
// * The included Properties that will be used by EntityBeanIntercept
// * to determine lazy loading on partial objects.
// */
Set<String> includedProps;
/**
@@ -29,7 +34,7 @@ public class SqlTreeProperties {
*/
boolean includeId = true;
TableJoin[] tableJoins = new TableJoin[0];
TableJoin[] tableJoins = EMPTY_TABLE_JOINS;
/**
* The bean properties in order.
@@ -40,9 +45,11 @@ public class SqlTreeProperties {
* Maintain a list of property names to detect embedded bean additions.
*/
LinkedHashSet<String> propNames = new LinkedHashSet<String>();
public SqlTreeProperties() {
private boolean allProperties;
public SqlTreeProperties(BeanDescriptor<?> desc) {
this.desc = desc;
}
public boolean containsProperty(String propName){
@@ -56,9 +63,8 @@ public class SqlTreeProperties {
}
public void add(BeanProperty prop) {
propsList.add(prop);
propNames.add(prop.getName());
propsList.add(prop);
propNames.add(prop.getName());
}
public BeanProperty[] getProps() {
@@ -74,15 +80,7 @@ public class SqlTreeProperties {
}
public boolean isPartialObject() {
return includedProps != null;
}
public Set<String> getIncludedProperties() {
return includedProps;
}
public void setIncludedProperties(Set<String> includedProps) {
this.includedProps = includedProps;
return !allProperties;
}
public boolean isReadOnly() {
@@ -101,4 +99,12 @@ public class SqlTreeProperties {
this.tableJoins = tableJoins;
}
public void setAllProperties(boolean allProperties) {
this.allProperties = allProperties;
}
public boolean isAllProperties() {
return allProperties;
}
}