initial add of EbeanORM server based on v2.8.1

This commit is contained in:
rbygrave committed 2012-09-14 00:40:39 +12:00
1 parent 2b74d0f9d9
commit 96ce4c0ddf
1188 files changed
+135034

No files matched your search

@@ -0,0 +1,274 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.loadcontext;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.avaje.ebean.bean.BeanLoader;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.bean.EntityBeanIntercept;
import com.avaje.ebean.bean.ObjectGraphNode;
import com.avaje.ebean.bean.PersistenceContext;
import com.avaje.ebeaninternal.api.LoadBeanContext;
import com.avaje.ebeaninternal.api.LoadBeanRequest;
import com.avaje.ebeaninternal.api.LoadContext;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.querydefn.OrmQueryProperties;
/**
* Default implementation of LoadBeanContext.
*
*/
public class DLoadBeanContext implements LoadBeanContext, BeanLoader {
private static final Logger logger = Logger.getLogger(DLoadBeanContext.class.getName());
protected final DLoadContext parent;
protected final BeanDescriptor<?> desc;
protected final String path;
protected final String fullPath;
private final DLoadList<EntityBeanIntercept> weakList;
private final OrmQueryProperties queryProps;
private int batchSize;
public DLoadBeanContext(DLoadContext parent, BeanDescriptor<?> desc, String path, int batchSize,
OrmQueryProperties queryProps, DLoadList<EntityBeanIntercept> weakList) {
this.parent = parent;
this.desc = desc;
this.path = path;
this.batchSize = batchSize;
this.queryProps = queryProps;
this.weakList = weakList;
if (parent.getRelativePath() == null) {
this.fullPath = path;
} else {
this.fullPath = parent.getRelativePath() + "." + path;
}
}
public void configureQuery(SpiQuery<?> query, String lazyLoadProperty) {
// propagate the readOnly state
if (parent.isReadOnly() != null) {
query.setReadOnly(parent.isReadOnly());
}
query.setParentNode(getObjectGraphNode());
query.setLazyLoadProperty(lazyLoadProperty);
if (queryProps != null) {
queryProps.configureBeanQuery(query);
}
if (parent.isUseAutofetchManager()) {
query.setAutofetch(true);
}
}
public String getFullPath() {
return fullPath;
}
public PersistenceContext getPersistenceContext() {
return parent.getPersistenceContext();
}
public OrmQueryProperties getQueryProps() {
return queryProps;
}
public ObjectGraphNode getObjectGraphNode() {
return parent.getObjectGraphNode(path);
}
public String getPath() {
return path;
}
public String getName() {
return parent.getEbeanServer().getName();
}
public int getBatchSize() {
return batchSize;
}
public void setBatchSize(int batchSize) {
this.batchSize = batchSize;
}
public BeanDescriptor<?> getBeanDescriptor() {
return desc;
}
public LoadContext getGraphContext() {
return parent;
}
public void register(EntityBeanIntercept ebi){
int pos = weakList.add(ebi);
ebi.setBeanLoader(pos, this, parent.getPersistenceContext());
}
/**
* Check if we can load the bean from L2 cache. If so avoid loading from the DB.
*/
private boolean loadBeanFromCache(EntityBeanIntercept ebi, int position) {
if (!desc.loadFromCache(ebi)) {
return false;
}
// we loaded the bean from cache
weakList.removeEntry(position);
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Loading path:" + fullPath + " - bean loaded from L2 cache, position[" + position + "]");
}
return true;
}
/**
* Load this bean and potentially a batch of similar beans.
*/
public void loadBean(EntityBeanIntercept ebi) {
// A synchronized (this) is effectively held by EntityBeanIntercept.loadBean()
if (desc.lazyLoadMany(ebi)) {
// lazy load property was a Many
return;
}
int position = ebi.getBeanLoaderIndex();
boolean hitCache = !parent.isExcludeBeanCache() && desc.isBeanCaching();
if (hitCache && loadBeanFromCache(ebi, position)) {
// successfully hit the L2 cache so don't invoke DB lazy loading
return;
}
// Get a batch of beans to lazy load
List<EntityBeanIntercept> batch = null;
try {
batch = weakList.getLoadBatch(position, batchSize);
} catch (IllegalStateException e) {
logger.log(Level.SEVERE, "type["+desc.getFullName()+"] fullPath[" + fullPath + "] batchSize["+batchSize+"]", e);
}
if (hitCache && batchSize > 1) {
// Check each of the beans in the batch to see if they are in the L2 cache.
// Add more as necessary to make up our batch that will be loaded.
batch = loadBeanCheckBatch(batch);
}
if (logger.isLoggable(Level.FINER)) {
for (int i = 0; i < batch.size(); i++) {
EntityBeanIntercept entityBeanIntercept = batch.get(i);
EntityBean owner = entityBeanIntercept.getOwner();
Object id = desc.getId(owner);
logger.finer("LoadBean type["+owner.getClass().getName()+"] fullPath["+fullPath+"] id["+id+"] batchIndex["+i+"] beanLoaderIndex["+entityBeanIntercept.getBeanLoaderIndex()+"]");
}
}
LoadBeanRequest req = new LoadBeanRequest(this, batch, null, batchSize, true, ebi.getLazyLoadProperty(), hitCache);
parent.getEbeanServer().loadBean(req);
}
/**
* Check each of the beans in the batch to see if they are in the cache.
* Get more beans out as necessary to get our desired batch size.
*/
private List<EntityBeanIntercept> loadBeanCheckBatch(List<EntityBeanIntercept> batch) {
List<EntityBeanIntercept> actualLoadBatch = new ArrayList<EntityBeanIntercept>(batchSize);
List<EntityBeanIntercept> batchToCheck = batch;
int loadedFromCache = 0;
while (true) {
// check each bean (not already checked) to see if it is in the cache
for (int i = 0; i < batchToCheck.size(); i++) {
if (!desc.loadFromCache(batchToCheck.get(i))) {
actualLoadBatch.add(batchToCheck.get(i));
} else {
loadedFromCache++;
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Loading path:" + fullPath + " - bean loaded from L2 cache(batch)");
}
}
}
if (batchToCheck.isEmpty()) {
// we have exhausted all the beans that need lazy loading
break;
}
int more = batchSize - actualLoadBatch.size();
if (more <= 0 || loadedFromCache > 500) {
break;
}
// get some more to check as we loaded some from L2 cache
batchToCheck = weakList.getNextBatch(more);
}
return actualLoadBatch;
}
public void loadSecondaryQuery(OrmQueryRequest<?> parentRequest, int requestedBatchSize, boolean all) {
synchronized (this) {
do {
List<EntityBeanIntercept> batch = weakList.getNextBatch(requestedBatchSize);
if (batch.size() == 0) {
// there are no beans to load
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Loading path:" + fullPath + " - no more beans to load");
}
return;
}
boolean loadCache = false;
LoadBeanRequest req = new LoadBeanRequest(this, batch, parentRequest.getTransaction(), requestedBatchSize, false, null, loadCache);
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Loading path:" + fullPath + " - secondary query batch load [" + batch.size() + "] beans");
}
parent.getEbeanServer().loadBean(req);
if (!all) {
break;
}
} while (true);
}
}
}
@@ -0,0 +1,337 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.loadcontext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.EntityBeanIntercept;
import com.avaje.ebean.bean.ObjectGraphNode;
import com.avaje.ebean.bean.ObjectGraphOrigin;
import com.avaje.ebean.bean.PersistenceContext;
import com.avaje.ebean.config.GlobalProperties;
import com.avaje.ebeaninternal.api.LoadContext;
import com.avaje.ebeaninternal.api.LoadSecondaryQuery;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
import com.avaje.ebeaninternal.server.querydefn.OrmQueryProperties;
/**
* Default implementation of LoadContext.
*
* @author rbygrave
*/
public class DLoadContext implements LoadContext {
private final SpiEbeanServer ebeanServer;
private final BeanDescriptor<?> rootDescriptor;
private final Map<String, DLoadBeanContext> beanMap = new HashMap<String, DLoadBeanContext>();
private final Map<String, DLoadManyContext> manyMap = new HashMap<String, DLoadManyContext>();
private final DLoadBeanContext rootBeanContext;
private final Boolean readOnly;
private final boolean excludeBeanCache;
private final int defaultBatchSize;
/**
* The path relative to the root of the object graph.
*/
private final String relativePath;
private final ObjectGraphOrigin origin;
private final boolean useAutofetchManager;
private final boolean hardRefs;
private final Map<String,ObjectGraphNode> nodePathMap = new HashMap<String, ObjectGraphNode>();
private PersistenceContext persistenceContext;
private List<OrmQueryProperties> secQuery;
public DLoadContext(SpiEbeanServer ebeanServer, BeanDescriptor<?> rootDescriptor, Boolean readOnly, SpiQuery<?> query) {
this(ebeanServer, rootDescriptor, readOnly,
Boolean.FALSE.equals(query.isUseBeanCache()),
query.getParentNode(),
query.getAutoFetchManager() != null);
}
public DLoadContext(SpiEbeanServer ebeanServer, BeanDescriptor<?> rootDescriptor, Boolean readOnly,
boolean excludeBeanCache, ObjectGraphNode parentNode, boolean useAutofetchManager) {
this.ebeanServer = ebeanServer;
this.hardRefs = GlobalProperties.getBoolean("ebean.hardrefs", false);
this.defaultBatchSize = ebeanServer.getLazyLoadBatchSize();
this.rootDescriptor = rootDescriptor;
this.rootBeanContext = new DLoadBeanContext(this, rootDescriptor, null, defaultBatchSize, null, createBeanLoadList());
this.readOnly = readOnly;
this.excludeBeanCache = excludeBeanCache;
this.useAutofetchManager = useAutofetchManager;
if (parentNode != null){
this.origin = parentNode.getOriginQueryPoint();
this.relativePath = parentNode.getPath();
} else {
this.origin = null;
this.relativePath = null;
}
}
protected boolean isExcludeBeanCache() {
return excludeBeanCache;
}
/**
* Return the minimum batch size when using QueryIterator with query joins.
*/
public int getSecondaryQueriesMinBatchSize(OrmQueryRequest<?> parentRequest, int defaultQueryBatch) {
if (secQuery == null){
return -1;
}
int maxBatch = 0;
for (int i = 0; i < secQuery.size(); i++) {
int batchSize = secQuery.get(i).getQueryFetchBatch();
if (batchSize == 0){
batchSize = defaultQueryBatch;
}
maxBatch = Math.max(maxBatch, batchSize);
}
return maxBatch;
}
/**
* Execute all the secondary queries.
*/
public void executeSecondaryQueries(OrmQueryRequest<?> parentRequest, int defaultQueryBatch) {
if (secQuery != null){
for (int i = 0; i < secQuery.size(); i++) {
OrmQueryProperties properties = secQuery.get(i);
int batchSize = properties.getQueryFetchBatch();
if (batchSize == 0){
batchSize = defaultQueryBatch;
}
LoadSecondaryQuery load = getLoadSecondaryQuery(properties.getPath());
load.loadSecondaryQuery(parentRequest, batchSize, properties.isQueryFetchAll());
}
}
}
/**
* Return the LoadBeanContext or LoadManyContext for the given path.
*/
private LoadSecondaryQuery getLoadSecondaryQuery(String path){
LoadSecondaryQuery beanLoad = beanMap.get(path);
if (beanLoad == null){
beanLoad = manyMap.get(path);
}
return beanLoad;
}
/**
* Remove the +query and +lazy secondary queries and
* register them with their appropriate LoadBeanContext
* or LoadManyContext.
* <p>
* The parts of the secondary queries are removed and used
* by LoadBeanContext/LoadManyContext to build the appropriate
* queries.
* </p>
*/
public void registerSecondaryQueries(SpiQuery<?> query) {
secQuery = query.removeQueryJoins();
if (secQuery != null){
for (int i = 0; i < secQuery.size(); i++) {
OrmQueryProperties props = secQuery.get(i);
registerSecondaryQuery(props);
}
}
List<OrmQueryProperties> lazyQueries = query.removeLazyJoins();
if (lazyQueries != null){
for (int i = 0; i < lazyQueries.size(); i++) {
OrmQueryProperties lazyProps = lazyQueries.get(i);
registerSecondaryQuery(lazyProps);
}
}
}
/**
* Setup the load context at this path with OrmQueryProperties which is
* used to build the appropriate query for +query or +lazy loading.
*/
private void registerSecondaryQuery(OrmQueryProperties props) {
String propName = props.getPath();
ElPropertyValue elGetValue = rootDescriptor.getElGetValue(propName);
boolean many = elGetValue.getBeanProperty().containsMany();
registerSecondaryNode(many, props);
}
public ObjectGraphNode getObjectGraphNode(String path) {
ObjectGraphNode node = nodePathMap.get(path);
if (node == null){
node = createObjectGraphNode(path);
nodePathMap.put(path, node);
}
return node;
}
private ObjectGraphNode createObjectGraphNode(String path) {
if (relativePath != null){
if (path == null){
path = relativePath;
} else {
path = relativePath+"."+path;
}
}
return new ObjectGraphNode(origin, path);
}
public boolean isUseAutofetchManager() {
return useAutofetchManager;
}
public String getRelativePath() {
return relativePath;
}
protected SpiEbeanServer getEbeanServer() {
return ebeanServer;
}
/**
* Return the parent state which defines the sharedInstance and readOnly status
* which needs to be propagated to other beans and collections.
*/
protected Boolean isReadOnly() {
return readOnly;
}
public PersistenceContext getPersistenceContext() {
return persistenceContext;
}
public void setPersistenceContext(PersistenceContext persistenceContext) {
this.persistenceContext = persistenceContext;
}
public void register(String path, EntityBeanIntercept ebi){
getBeanContext(path).register(ebi);
}
public void register(String path, BeanCollection<?> bc){
getManyContext(path).register(bc);
}
private DLoadBeanContext getBeanContext(String path) {
if (path == null){
return rootBeanContext;
}
DLoadBeanContext beanContext = beanMap.get(path);
if (beanContext == null){
beanContext = createBeanContext(path, defaultBatchSize, null);
beanMap.put(path, beanContext);
}
return beanContext;
}
private void registerSecondaryNode(boolean many, OrmQueryProperties props) {
String path = props.getPath();
int lazyJoinBatch = props.getLazyFetchBatch();
int batchSize = lazyJoinBatch > 0 ? lazyJoinBatch : defaultBatchSize;
if (many){
DLoadManyContext manyContext = createManyContext(path, batchSize, props);
manyMap.put(path, manyContext);
} else {
DLoadBeanContext beanContext = createBeanContext(path, batchSize, props);
beanMap.put(path, beanContext);
}
}
private DLoadManyContext getManyContext(String path) {
if (path == null){
throw new RuntimeException("path is null?");
}
DLoadManyContext ctx = manyMap.get(path);
if (ctx == null){
ctx = createManyContext(path, defaultBatchSize, null);
manyMap.put(path, ctx);
}
return ctx;
}
private DLoadManyContext createManyContext(String path, int batchSize, OrmQueryProperties queryProps) {
BeanPropertyAssocMany<?> p = (BeanPropertyAssocMany<?>)getBeanProperty(rootDescriptor, path);
return new DLoadManyContext(this, p, path, batchSize, queryProps, createBeanCollectionLoadList());
}
private DLoadList<EntityBeanIntercept> createBeanLoadList() {
if (hardRefs){
return new DLoadHardList<EntityBeanIntercept>();
} else {
return new DLoadWeakList<EntityBeanIntercept>();
}
}
private DLoadList<BeanCollection<?>> createBeanCollectionLoadList() {
if (hardRefs){
return new DLoadHardList<BeanCollection<?>>();
} else {
return new DLoadWeakList<BeanCollection<?>>();
}
}
private DLoadBeanContext createBeanContext(String path, int batchSize, OrmQueryProperties queryProps) {
BeanPropertyAssoc<?> p = (BeanPropertyAssoc<?>)getBeanProperty(rootDescriptor, path);
BeanDescriptor<?> targetDescriptor = p.getTargetDescriptor();
return new DLoadBeanContext(this, targetDescriptor, path, batchSize, queryProps, createBeanLoadList());
}
private BeanProperty getBeanProperty(BeanDescriptor<?> desc, String path){
return desc.getBeanPropertyFromPath(path);
}
}
@@ -0,0 +1,125 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.loadcontext;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DLoadHardList<T> implements DLoadList<T> {
private static final Logger logger = Logger.getLogger(DLoadHardList.class.getName());
protected final ArrayList<T> list = new ArrayList<T>();
protected int removedFromTop;
protected DLoadHardList() {
}
public int add(T e) {
synchronized (this) {
int i = list.size();
list.add(e);
return i;
}
}
public void removeEntry(int position) {
synchronized (this) {
T object = list.get(position);
if (object == null) {
logger.log(Level.WARNING, "removeEntry found no Object for position[" + position + "]");
} else {
// just set the entry to null
list.set(position, null);
}
if (position == removedFromTop) {
removedFromTop++;
}
}
}
public List<T> getNextBatch(int batchSize) {
if (removedFromTop >= list.size()){
return new ArrayList<T>(0);
}
return getLoadBatch(removedFromTop, batchSize, true);
}
public List<T> getLoadBatch(int position, int batchSize) {
return getLoadBatch(position, batchSize, false);
}
private List<T> getLoadBatch(int position, int batchSize, boolean ignoreMissing) {
synchronized (this) {
if (batchSize < 1) {
throw new RuntimeException("batchSize " + batchSize + " < 1 ??!!");
}
ArrayList<T> batch = new ArrayList<T>();
if (!addObjectToBatchAt(batch, position) && !ignoreMissing) {
String msg = "getLoadBatch position[" + position + "] didn't find a bean in the list?";
throw new IllegalStateException(msg);
}
for (int i = position; i < list.size(); i++) {
addObjectToBatchAt(batch, i);
if (batch.size() == batchSize) {
// found enough beans going forward
return batch;
}
}
// search the front of the list to fill our batch
for (int i = removedFromTop; i < position; i++) {
addObjectToBatchAt(batch, i);
if (batch.size() == batchSize) {
// found enough beans going forward from start of list
return batch;
}
}
return batch;
}
}
private boolean addObjectToBatchAt(ArrayList<T> batch, int i) {
boolean found = false;
T object = list.get(i);
if (object != null) {
found = true;
batch.add(object);
// set it to null saying we have loaded this one
list.set(i, null);
}
if (i == removedFromTop) {
removedFromTop++;
}
return found;
}
}
@@ -0,0 +1,21 @@
package com.avaje.ebeaninternal.server.loadcontext;
import java.util.List;
public interface DLoadList<T> {
public int add(T e);
/**
* Return the next batch of entries from the top.
*/
public abstract List<T> getNextBatch(int batchSize);
public void removeEntry(int position);
/**
* Return the batch of entries based on the position and batch size.
*/
public List<T> getLoadBatch(int position, int batchSize);
}
@@ -0,0 +1,180 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.loadcontext;
import java.util.List;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.bean.BeanCollectionLoader;
import com.avaje.ebean.bean.ObjectGraphNode;
import com.avaje.ebean.bean.PersistenceContext;
import com.avaje.ebeaninternal.api.LoadManyContext;
import com.avaje.ebeaninternal.api.LoadManyRequest;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import com.avaje.ebeaninternal.server.querydefn.OrmQueryProperties;
public class DLoadManyContext implements LoadManyContext, BeanCollectionLoader {
protected final DLoadContext parent;
protected final String fullPath;
private final BeanDescriptor<?> desc;
private final BeanPropertyAssocMany<?> property;
private final String path;
private final int batchSize;
private final OrmQueryProperties queryProps;
private final DLoadList<BeanCollection<?>> weakList;
public DLoadManyContext(DLoadContext parent, BeanPropertyAssocMany<?> p,
String path, int batchSize, OrmQueryProperties queryProps, DLoadList<BeanCollection<?>> weakList) {
this.parent = parent;
this.property = p;
this.desc = p.getBeanDescriptor();
this.path = path;
this.batchSize = batchSize;
this.queryProps = queryProps;
this.weakList = weakList;//new DLoadWeakList<BeanCollection<?>>();
if (parent.getRelativePath() == null){
this.fullPath = path;
} else {
this.fullPath = parent.getRelativePath()+"."+path;
}
}
public void configureQuery(SpiQuery<?> query){
// propagate the readOnly state
if (parent.isReadOnly() != null){
query.setReadOnly(parent.isReadOnly());
}
query.setParentNode(getObjectGraphNode());
if (queryProps != null){
queryProps.configureManyQuery(query);
}
if (parent.isUseAutofetchManager()){
query.setAutofetch(true);
}
}
public ObjectGraphNode getObjectGraphNode() {
// we return the parent node ... as we actually
// query on the parent selecting just it's id
int pos = path.lastIndexOf('.');
if (pos == -1){
return parent.getObjectGraphNode(null);
} else {
String parentPath = path.substring(0, pos);
return parent.getObjectGraphNode(parentPath);
}
}
public String getFullPath() {
return fullPath;
}
public PersistenceContext getPersistenceContext() {
return parent.getPersistenceContext();
}
public int getBatchSize() {
return batchSize;
}
public BeanPropertyAssocMany<?> getBeanProperty() {
return property;
}
public BeanDescriptor<?> getBeanDescriptor() {
return desc;
}
public String getPath() {
return path;
}
public String getName() {
return parent.getEbeanServer().getName();
}
public void register(BeanCollection<?> bc){
int pos = weakList.add(bc);
bc.setLoader(pos, this);
}
public void loadMany(BeanCollection<?> bc, boolean onlyIds) {
int position = bc.getLoaderIndex();
LoadManyRequest req;
synchronized (weakList) {
boolean hitCache = desc.isBeanCaching() && !onlyIds && !parent.isExcludeBeanCache();
if (hitCache){
Object ownerBean = bc.getOwnerBean();
BeanDescriptor<? extends Object> parentDesc = desc.getBeanDescriptor(ownerBean.getClass());
Object parentId = parentDesc.getId(ownerBean);
if (parentDesc.cacheLoadMany(property, bc, parentId, parent.isReadOnly(), false)) {
// we loaded the bean from cache
weakList.removeEntry(position);
return;
}
}
List<BeanCollection<?>> loadBatch = weakList.getLoadBatch(position, batchSize);
req = new LoadManyRequest(this, loadBatch, null, batchSize, true, onlyIds, hitCache);
}
parent.getEbeanServer().loadMany(req);
}
public void loadSecondaryQuery(OrmQueryRequest<?> parentRequest, int requestedBatchSize, boolean all){
do {
LoadManyRequest req;
synchronized (weakList) {
List<BeanCollection<?>> batch = weakList.getNextBatch(requestedBatchSize);
if (batch.size() == 0){
return;
}
req = new LoadManyRequest(this, batch, parentRequest.getTransaction(), requestedBatchSize, false, false, false);
}
parent.getEbeanServer().loadMany(req);
if (!all){
// queryFirst(batch)
break;
}
} while (true);
}
}
@@ -0,0 +1,134 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.loadcontext;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DLoadWeakList<T> implements DLoadList<T> {
private static final Logger logger = Logger.getLogger(DLoadWeakList.class.getName());
protected final ArrayList<WeakReference<T>> list = new ArrayList<WeakReference<T>>();
protected int removedFromTop;
protected DLoadWeakList() {
}
public int add(T e) {
synchronized (this) {
int i = list.size();
list.add(new WeakReference<T>(e));
return i;
}
}
public void removeEntry(int position) {
synchronized (this) {
WeakReference<T> wref = list.get(position);
if (wref == null) {
logger.log(Level.WARNING, "removeEntry found no WeakReference for position[" + position + "]");
} else {
// just set the entry to null
list.set(position, null);
T object = wref.get();
if (object == null) {
logger.log(Level.WARNING, "removeEntry found no Object held by WeakReference for position[" + position + "]");
}
}
if (position == removedFromTop) {
removedFromTop++;
}
}
}
public List<T> getNextBatch(int batchSize) {
if (removedFromTop >= list.size()){
return new ArrayList<T>(0);
}
return getLoadBatch(removedFromTop, batchSize, true);
}
public List<T> getLoadBatch(int position, int batchSize) {
return getLoadBatch(position, batchSize, false);
}
private List<T> getLoadBatch(int position, int batchSize, boolean ignoreMissing) {
synchronized (this) {
if (batchSize < 1) {
throw new RuntimeException("batchSize " + batchSize + " < 1 ??!!");
}
ArrayList<T> batch = new ArrayList<T>();
if (!addObjectToBatchAt(batch, position) && !ignoreMissing) {
String msg = "getLoadBatch position[" + position + "] didn't find a bean in the list?";
throw new IllegalStateException(msg);
}
for (int i = position; i < list.size(); i++) {
addObjectToBatchAt(batch, i);
if (batch.size() == batchSize) {
// found enough beans going forward
return batch;
}
}
// search the front of the list to fill our batch
for (int i = removedFromTop; i < position; i++) {
addObjectToBatchAt(batch, i);
if (batch.size() == batchSize) {
// found enough beans going forward from start of list
return batch;
}
}
return batch;
}
}
private boolean addObjectToBatchAt(ArrayList<T> batch, int i) {
boolean found = false;
WeakReference<T> wref = list.get(i);
if (wref != null) {
T object = wref.get();
if (object == null) {
logger.log(Level.WARNING, "Bean is null from weak reference");
} else {
found = true;
batch.add(object);
}
// set it to null saying we have loaded this one
list.set(i, null);
}
if (i == removedFromTop) {
removedFromTop++;
}
return found;
}
}
@@ -0,0 +1,4 @@
/**
* Load context (supports batch lazy loading).
*/
package com.avaje.ebeaninternal.server.loadcontext;