FetchPlan with recursive scheme.

- modified select building and load cycles in JDBCStoreManager with FetchState traversal 
- loadAll(), findAll() methods reverted to their method signature with FetchConfiguration instead of FetchState
- StateManager.postLoad checks for fetch group with postload set to true 

git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@425312 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2006-07-25 07:10:26 +00:00
parent 98e8533cb0
commit 884ff1b863
24 changed files with 661 additions and 610 deletions

View File

@ -441,10 +441,7 @@ public class JDBCStoreManager
public boolean load(OpenJPAStateManager sm, BitSet fields,
FetchState fetchState, int lockLevel, Object context) {
JDBCFetchState jfetchState = (fetchState == null) ? (JDBCFetchState) getFetchConfiguration()
.newFetchState()
: (JDBCFetchState) fetchState;
JDBCFetchState jfetchState = (JDBCFetchState) fetchState;
JDBCFetchConfiguration jfetch = getFetchConfiguration(jfetchState);
// get a connection, or reuse current one
@ -492,9 +489,10 @@ public class JDBCStoreManager
// now allow the fields to load themselves individually too
for (int i = 0, len = fields.length(); i < len; i++)
if (fields.get(i) && !sm.getLoaded().get(i))
mapping.getFieldMapping(i).load(sm, this, jfetchState);
if (fields.get(i) && !sm.getLoaded().get(i)) {
FieldMapping fm = mapping.getFieldMapping(i);
fm.load(sm, this, (JDBCFetchState)jfetchState.traverse(fm));
}
mapping.getVersion().afterLoad(sm, this);
return true;
} catch (ClassNotFoundException cnfe) {
@ -514,8 +512,8 @@ public class JDBCStoreManager
}
public Collection loadAll(Collection sms, PCState state, int load,
FetchState fetchState, Object context) {
return ImplHelper.loadAll(sms, this, state, load, fetchState, context);
FetchConfiguration fetch, Object context) {
return ImplHelper.loadAll(sms, this, state, load, fetch, context);
}
public void beforeStateChange(OpenJPAStateManager sm, PCState fromState,
@ -838,14 +836,16 @@ public class JDBCStoreManager
if (eagerToMany == null && fms[i].isEagerSelectToMany())
eagerToMany = fms[i];
else
fms[i].loadEagerJoin(sm, this, fetchState, res);
fms[i].loadEagerJoin(sm, this,
(JDBCFetchState)fetchState.traverse(fms[i]), res);
} else if (eres != null) {
processed = fms[i].loadEagerParallel(sm, this, fetchState,
eres);
processed = fms[i].loadEagerParallel(sm, this,
(JDBCFetchState)fetchState.traverse(fms[i]), eres);
if (processed != eres)
res.putEager(fms[i], processed);
} else
fms[i].load(sm, this, fetchState, res);
fms[i].load(sm, this,
(JDBCFetchState)fetchState.traverse(fms[i]), res);
} finally {
res.endDataRequest();
}
@ -931,11 +931,11 @@ public class JDBCStoreManager
int jtype;
int mode;
for (int i = 0; i < fms.length; i++) {
if (!requiresSelect(fms[i], sm, fields, fetchState))
continue;
mode = fms[i].getEagerFetchMode();
if (mode == fetch.EAGER_NONE)
continue;
if (!requiresSelect(fms[i], sm, fields, fetchState))
continue;
// try to select with join first
jtype = (fms[i].getNullValue() == fms[i].NULL_EXCEPTION) ? sel.EAGER_INNER
@ -985,7 +985,7 @@ public class JDBCStoreManager
if (sm != null && sm.getPCState() != PCState.TRANSIENT
&& sm.getLoaded().get(fm.getIndex()))
return false;
return fetchState.requiresSelect(fm, true);
return fetchState.requiresFetch(fm);
}
/**
@ -1059,17 +1059,19 @@ public class JDBCStoreManager
esel = sel.getEager(fms[i]);
if (esel != null) {
if (esel == sel)
fms[i].selectEagerJoin(sel, sm, this, fetchState, eager);
fms[i].selectEagerJoin(sel, sm, this,
(JDBCFetchState)fetchState.traverse(fms[i]), eager);
else
fms[i].selectEagerParallel(esel, sm, this, fetchState,
eager);
fms[i].selectEagerParallel(esel, sm, this,
(JDBCFetchState)fetchState.traverse(fms[i]), eager);
seld = Math.max(0, seld);
} else if (requiresSelect(fms[i], sm, fields, fetchState)) {
fseld = fms[i].select(sel, sm, this, fetchState, eager);
fseld = fms[i].select(sel, sm, this,
(JDBCFetchState)fetchState.traverse(fms[i]), eager);
seld = Math.max(fseld, seld);
} else if (optSelect(fms[i], sel, sm, fetchState)) {
fseld = fms[i].select(sel, sm, this, fetchState,
fetch.EAGER_NONE);
fseld = fms[i].select(sel, sm, this,
(JDBCFetchState)fetchState.traverse(fms[i]), fetch.EAGER_NONE);
// don't upgrade seld to > 0 based on these fields, since
// they're not in the calculated field set
@ -1100,7 +1102,7 @@ public class JDBCStoreManager
.getLoaded().get(fm.getIndex()))
&& fm.supportsSelect(sel, sel.TYPE_TWO_PART, sm, this,
getFetchConfiguration(fetchState)) > 0
&& fetchState.requiresSelect(fm, true);
&& fetchState.requiresFetch(fm);
}
/**
@ -1141,12 +1143,15 @@ public class JDBCStoreManager
fms = subMappings[i].getDefinedFieldMappings();
for (int j = 0; j < fms.length; j++) {
// make sure in one of configured fetch groups
if (!fms[j].isInDefaultFetchGroup()
&& !fetch.hasFetchGroup(fms[j].getFetchGroups())
&& !fetch.hasField(fms[j].getFullName())
&& (fms[j].isDefaultFetchGroupExplicit() || fms[j]
.supportsSelect(sel, sel.TYPE_TWO_PART, sm, this, fetch) <= 0))
if (fetchState.requiresFetch(fms[j])
|| fms[j].supportsSelect(sel, sel.TYPE_TWO_PART, sm, this, fetch) <= 0)
continue;
// if (!fms[j].isInDefaultFetchGroup()
// && !fetch.hasAnyFetchGroup(fms[j].getFetchGroups())
// && !fetch.hasField(fms[j].getFullName())
// && (fms[j].isDefaultFetchGroupExplicit() || fms[j]
// .supportsSelect(sel, sel.TYPE_TWO_PART, sm, this, fetch) <= 0))
// continue;
// if we can join to the subclass, do so; much better chance
// that the field will be able to select itself without joins
@ -1159,9 +1164,10 @@ public class JDBCStoreManager
// if can select with tables already selected, do it
if (fms[j].supportsSelect(sel, sel.TYPE_JOINLESS, sm, this,
fetch) > 0)
fms[j]
.select(sel, null, this, fetchState, fetch.EAGER_NONE);
fetch) > 0 && fetchState.requiresFetch(fms[j]))
fms[j].select(sel, null, this,
(JDBCFetchState)fetchState.traverse(fms[j]),
fetch.EAGER_NONE);
}
}
}

View File

@ -92,7 +92,7 @@ public class PagingResultObjectProvider
BitSet paged = null;
for (int i = 0; i < fms.length; i++) {
if (fetchState != null
&& !fetchState.requiresSelect(fms[i], false))
&& !fetchState.requiresFetch(fms[i]))
continue;
if (fms[i].supportsSelect(sel, sel.EAGER_PARALLEL, null, store,

View File

@ -233,8 +233,8 @@ public abstract class AbstractStoreManager
* advantageous.
*/
public Collection loadAll(Collection sms, PCState state, int load,
FetchState fetchState, Object context) {
return ImplHelper.loadAll(sms, this, state, load, fetchState, context);
FetchConfiguration fetch, Object context) {
return ImplHelper.loadAll(sms, this, state, load, fetch, context);
}
/**

View File

@ -408,12 +408,9 @@ public class DataCacheStoreManager
}
public Collection loadAll(Collection sms, PCState state, int load,
FetchState fetchState, Object edata) {
FetchConfiguration fetch = (fetchState == null)
? _ctx.getFetchConfiguration()
: fetchState.getFetchConfiguration();
FetchConfiguration fetch, Object edata) {
if (isLocking(fetch))
return super.loadAll(sms, state, load, fetchState, edata);
return super.loadAll(sms, state, load, fetch, edata);
Map unloaded = null;
OpenJPAStateManager sm;
@ -435,7 +432,7 @@ public class DataCacheStoreManager
//### the 'data.type' access here probably needs
//### to be addressed for bug 511
sm.initialize(data.getType(), state);
data.load(sm, fetchState, edata);
data.load(sm, fetch.newFetchState(), edata);
} else
unloaded = addUnloaded(sm, null, unloaded);
} else if (load != FORCE_LOAD_NONE
@ -443,6 +440,7 @@ public class DataCacheStoreManager
data = cache.get(sm.getObjectId());
if (data != null) {
// load unloaded fields
FetchState fetchState = fetch.newFetchState();
fields = sm.getUnloaded(fetchState);
data.load(sm, fields, fetchState, edata);
if (fields.length() > 0)
@ -458,7 +456,7 @@ public class DataCacheStoreManager
// load with delegate
Collection failed = super.loadAll(unloaded.keySet(), state, load,
fetchState, edata);
fetch, edata);
if (!_ctx.getPopulateDataCache())
return failed;

View File

@ -575,7 +575,7 @@ public class PCDataGenerator
code.invokevirtual().setMethod(FieldMetaData.class,
"getFetchGroups", Set.class, null);
code.invokeinterface().setMethod
(FetchConfiguration.class, "hasFetchGroup",
(FetchConfiguration.class, "hasAnyFetchGroup",
boolean.class, new Class[]{ Set.class });
JumpInstruction ifins = code.ifne();
code.aload().setLocal(fetch);

View File

@ -694,11 +694,13 @@ public class BrokerImpl
int flags = OID_COPY | OID_ALLOW_NEW | OID_NODELETED;
if (!validate)
flags |= OID_NOVALIDATE;
return find(oid, null, null, null, flags, call);
return find(oid, _fc.newFetchState (), null, null, flags, call);
}
public Object find(Object oid, FetchState fetchState, BitSet exclude,
Object edata, int flags) {
if (fetchState == null)
fetchState = _fc.newFetchState ();
return find(oid, fetchState, exclude, edata, flags, null);
}
@ -717,8 +719,6 @@ public class BrokerImpl
}
beginOperation(true);
if (fetchState == null)
fetchState = _fc.newFetchState();
try {
assertNontransactionalRead();
@ -842,18 +842,18 @@ public class BrokerImpl
int flags = OID_COPY | OID_ALLOW_NEW | OID_NODELETED;
if (!validate)
flags |= OID_NOVALIDATE;
return findAll(oids, null, null, null, flags, call);
return findAll(oids, _fc, null, null, flags, call);
}
public Object[] findAll(Collection oids, FetchState fetchState,
public Object[] findAll(Collection oids, FetchConfiguration fetch,
BitSet exclude, Object edata, int flags) {
return findAll(oids, fetchState, exclude, edata, flags, null);
return findAll(oids, fetch, exclude, edata, flags, null);
}
/**
* Internal finder.
*/
protected Object[] findAll(Collection oids, FetchState fetchState,
protected Object[] findAll(Collection oids, FetchConfiguration fetch,
BitSet exclude, Object edata, int flags, FindCallbacks call) {
// throw any exceptions for null oids up immediately
if (oids == null)
@ -867,9 +867,6 @@ public class BrokerImpl
_loading = new HashMap((int) (oids.size() * 1.33 + 1));
if (call == null)
call = this;
if (fetchState == null)
fetchState = _fc.newFetchState();
FetchConfiguration fetch = fetchState.getFetchConfiguration();
beginOperation(true);
try {
assertNontransactionalRead();
@ -915,7 +912,7 @@ public class BrokerImpl
PCState state = (transState) ? PCState.PCLEAN
: PCState.PNONTRANS;
Collection failed = _store.loadAll(load, state,
StoreManager.FORCE_LOAD_NONE, fetchState, edata);
StoreManager.FORCE_LOAD_NONE, _fc, edata);
// set failed instances to null
if (failed != null && !failed.isEmpty()) {
@ -937,8 +934,8 @@ public class BrokerImpl
sm = (StateManagerImpl) _loading.get(oid);
if (sm != null && requiresLoad(sm, true, edata, flags)) {
try {
sm.load(fetchState, StateManagerImpl.LOAD_FGS, exclude,
edata, false);
sm.load(fetch.newFetchState(), StateManagerImpl.LOAD_FGS,
exclude, edata, false);
if (active) {
_lm.lock(sm, level, fetch.getLockTimeout(), edata);
sm.readLocked(level, fetch.getWriteLockLevel());
@ -2687,7 +2684,7 @@ public class BrokerImpl
// refresh all
if (load != null) {
Collection failed = _store.loadAll(load, null,
_store.FORCE_LOAD_REFRESH, _fc.newFetchState(), null);
_store.FORCE_LOAD_REFRESH, _fc, null);
if (failed != null && !failed.isEmpty())
exceps = add(exceps, newObjectNotFoundException(failed));
@ -2812,8 +2809,7 @@ public class BrokerImpl
if (load != null) {
int mode = (dfgOnly) ? _store.FORCE_LOAD_DFG
: _store.FORCE_LOAD_ALL;
failed = _store.loadAll(load, null, mode, _fc.newFetchState(),
null);
failed = _store.loadAll(load, null, mode, _fc, null);
if (failed != null && !failed.isEmpty())
exceps = add(exceps, newObjectNotFoundException(failed));
}
@ -3196,7 +3192,7 @@ public class BrokerImpl
Collection failed = null;
if (load != null) {
failed = _store.loadAll(load, null, _store.FORCE_LOAD_NONE,
_fc.newFetchState(), null);
_fc, null);
if (failed != null && !failed.isEmpty())
exceps = add(exceps,
newObjectNotFoundException(failed));
@ -4087,7 +4083,7 @@ public class BrokerImpl
Object oid = ApplicationIds.create(pc, meta);
if (oid == null)
return false;
return find(oid, _fc.newFetchState(), EXCLUDE_ALL, null, 0) != null;
return find(oid, null, EXCLUDE_ALL, null, 0) != null;
}
public OpenJPAStateManager getStateManager(Object obj) {

View File

@ -198,10 +198,10 @@ public class DelegatingBroker
}
}
public Object[] findAll(Collection oids, FetchState fetchState,
public Object[] findAll(Collection oids, FetchConfiguration fetch,
BitSet exclude, Object edata, int flags) {
try {
return _broker.findAll(oids, fetchState, exclude, edata, flags);
return _broker.findAll(oids, fetch, exclude, edata, flags);
} catch (RuntimeException re) {
throw translate(re);
}

View File

@ -229,9 +229,9 @@ public class DelegatingFetchConfiguration
}
}
public boolean hasFetchGroup(Set groups) {
public boolean hasAnyFetchGroup(Set groups) {
try {
return _fetch.hasFetchGroup(groups);
return _fetch.hasAnyFetchGroup(groups);
} catch (RuntimeException re) {
throw translate(re);
}

View File

@ -114,8 +114,8 @@ public abstract class DelegatingStoreManager
}
public Collection loadAll(Collection sms, PCState state, int load,
FetchState fetchState, Object context) {
return _store.loadAll(sms, state, load, fetchState, context);
FetchConfiguration fetch, Object context) {
return _store.loadAll(sms, state, load, fetch, context);
}
public void beforeStateChange(OpenJPAStateManager sm, PCState fromState,

View File

@ -223,7 +223,7 @@ public class DetachManager
FieldMetaData[] fmds = sm.getMetaData().getFields();
for (int i = 0; i < fmds.length; i++) {
if (fmds[i].isPrimaryKey() || fmds[i].isInDefaultFetchGroup()
|| fetch.hasFetchGroup(fmds[i].getFetchGroups())
|| fetch.hasAnyFetchGroup(fmds[i].getFetchGroups())
|| fetch.hasField(fmds[i].getFullName()))
idxs.set(i);
}

View File

@ -136,9 +136,10 @@ public class DetachedStateManager
load.set(i);
}
}
FetchConfiguration fc = broker.getFetchConfiguration();
FetchState fetchState = fc.newFetchState();
sm.loadFields(load, fetchState, fc.getWriteLockLevel(), null, true);
sm.loadFields(load, null, broker.getFetchConfiguration().
getWriteLockLevel(), null, true);
}
sm.setVersion(_version);

View File

@ -45,6 +45,7 @@ public interface FetchConfiguration
public static final String FETCH_GROUP_ALL =
"org.apache.openjpa.kernel.FetchConfiguration.FETCH_GROUP_ALL";
public static final String FETCH_GROUP_DEFAULT = "default";
/**
* Return the context assiciated with this configuration;
* may be null if it has not been set or this object has been serialized.
@ -144,7 +145,7 @@ public interface FetchConfiguration
*
* @since 4.1
*/
public boolean hasFetchGroup(Set groups);
public boolean hasAnyFetchGroup(Set groups);
/**
* Adds <code>group</code> to the set of fetch group names to

View File

@ -108,6 +108,7 @@ public class FetchConfigurationImpl
public void copy(FetchConfiguration fetch) {
setFetchBatchSize(fetch.getFetchBatchSize());
setMaxFetchDepth(fetch.getMaxFetchDepth());
setQueryCache(fetch.getQueryCache());
setFlushBeforeQueries(fetch.getFlushBeforeQueries());
setLockTimeout(fetch.getLockTimeout());
@ -172,11 +173,11 @@ public class FetchConfigurationImpl
public synchronized boolean hasFetchGroup(String group) {
return _fetchGroups != null
&& ((group != null && _fetchGroups.contains(group))
&& (_fetchGroups.contains(group)
|| _fetchGroups.contains(FETCH_GROUP_ALL));
}
public synchronized boolean hasFetchGroup(Set groups) {
public synchronized boolean hasAnyFetchGroup(Set groups) {
if (_fetchGroups != null && groups != null) {
Iterator iter = groups.iterator();
while (iter.hasNext()) {

View File

@ -1,12 +1,9 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -15,16 +12,24 @@
*/
package org.apache.openjpa.kernel;
import java.io.Serializable;
import java.util.List;
import org.apache.openjpa.meta.FieldMetaData;
/**
* Defines the decision to include fields for selection or loading during
* a fetch operation.
* A state is resulted by traversal of a relationship except the <em>root</em>
* state.
*
* @author <A HREF="mailto:pinaki.poddar@gmail.com>Pinaki Poddar</A>
* @since 4.1
*/
public interface FetchState {
public interface FetchState
extends Serializable, Cloneable {
public static final int INFINITE_DEPTH = -1;
/**
* Returns the immutable fetch configuration this receiver is based on.
@ -32,24 +37,16 @@ public interface FetchState {
public FetchConfiguration getFetchConfiguration();
/**
* Affirms if the given field requires to be selected in the context
* Affirms if the given field requires to be fetched in the context
* of current fetch operation.
* The response can be stateful as the same field argument would generate
* different responses on different invocation based on the current
* state of this receiver if changeState flag is true.
*
* @param fm field metadata. must not be null.
* @param changeState true implies that the state of the receiver will
* change due to invocation.
*/
public boolean requiresSelect(FieldMetaData fm, boolean changeState);
public boolean requiresFetch(FieldMetaData fm);
/**
* Affirms if the given field of the given instance requires to be loaded
* in the context of current fetch operation.
* The response is stateful as the same arguments would generate different
* responses on different invocation based on the current state of this
* receiver.
*
* @param sm state manager being populated
* @param fm field metadata
@ -57,12 +54,82 @@ public interface FetchState {
public boolean requiresLoad(OpenJPAStateManager sm, FieldMetaData fm);
/**
* Affirms if the given field is to be loaded as default. The field itself
* is aware of whether it belongs to the default fetch group. This method
* adds an extra constraint to verify that current configuration includes
* default fetch group.
* Traverse the given field to generate (possibly) a new state.
*
* @param fm field metadata
* @param fm
* @return a new state resulting out of traversal. If the given field is
* not a relation then return itself.
*/
public boolean isDefault(FieldMetaData fm);
public FetchState traverse (FieldMetaData fm);
/**
* Gets the available depth i.e. number of relations that can be traveresed
* from this receiver.
*
* @return a positive integer with positive infinity designated as
* <code>-1</code>.
*/
public int getAvailableFetchDepth ();
/**
* Gets the root state where this receiver is derived from.
* @return itself if the state is not derived from another state.
*
*/
public FetchState getRoot ();
/**
* Affirms if this receiver is the root state i.e. not derived as a result
* of traversing a relationship.
*
*/
public boolean isRoot ();
/**
* Gets the parent state.
* @return can be null for the root state.
*/
public FetchState getParent ();
/**
* Gets an ordered list of states from this receiver to its root.
*
* @return the order starts from this receiver and ends in the
* root. An empty list if this receiver is the root.
*/
public List getPath ();
/**
* Gets an ordered list of relation fields from this receiver to its root.
* These relations denote the path traversals from the root that resulted
* in the current state.
*
* @return the list starts from relation traversal of which resulted in this
* receiver and ends in the relation traversed from the root.
* An empty list, if this receiver itself is the root.
*/
public List getRelationPath ();
/**
* Gets the number of times the given field is traversed to arrive
* at this state.
*
* @param fm
* @return
*/
public int getCurrentRecursionDepth (FieldMetaData fm);
/**
* Gets the recursion depth of the given field.
*
* @param fm a relation field
* @return If the field has multiple fetch groups in the current
* configuration, then the recursion depth is maximum of all the recursion
* depths.
* The default recursion depth, if none is explictly specified, is 1.
* The infinite i.e. unlimited recursion depth is designated as
* <code>-1</code>
*/
public int getRecursionDepth (FieldMetaData fm);
}

View File

@ -1,12 +1,9 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -18,17 +15,12 @@
*/
package org.apache.openjpa.kernel;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.*;
import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.enhance.StateManager;
import org.apache.openjpa.meta.FetchGroup;
import org.apache.openjpa.meta.FieldMetaData;
import org.apache.openjpa.meta.JavaTypes;
import org.apache.openjpa.util.InternalException;
/**
* Holds dynamic status of fetch operation. Decides whether a field
@ -37,18 +29,12 @@ import org.apache.openjpa.meta.JavaTypes;
* @author <A HREF="mailto:pinaki.poddar@gmail.com>Pinaki Poddar</A>
* @nojavadoc
*/
public class FetchStateImpl
implements FetchState, Serializable {
public class FetchStateImpl implements FetchState {
private final FetchConfiguration _config;
private Map _selectTraversals;
private Map _loadTraversals;
private Map _recursionDepths;
private Map _depths;
private int _depth;
private final Set _knownExcludes;
private static final int INFINITE_DEPTH = -1;
private FetchState _parent;
private FieldMetaData _relation;
private int _availableDepth;
/**
* Supply configuration.
@ -56,72 +42,105 @@ public class FetchStateImpl
* @param fc must not be null.
*/
public FetchStateImpl(FetchConfiguration fc) {
super();
_config = fc;
_knownExcludes = new HashSet();
_selectTraversals = new HashMap();
_loadTraversals = new HashMap();
_depths = new HashMap();
_recursionDepths = new HashMap();
_parent = null;
_relation = null;
_availableDepth = _config.getMaxFetchDepth();
}
public FetchConfiguration getFetchConfiguration() {
return _config;
}
public boolean isDefault(FieldMetaData fm) {
return _config.hasFetchGroup(FetchGroup.getDefaultGroupName())
&& fm.isInDefaultFetchGroup();
public FetchState getParent () {
return _parent;
}
public boolean requiresSelect(FieldMetaData fm, boolean changeState) {
if (_knownExcludes.contains(fm))
return false;
boolean selectable = isDefault(fm)
|| _config.hasFetchGroup(fm.getFetchGroups())
|| _config.hasField(fm.getFullName());
if (!selectable)
_knownExcludes.add(fm);
public boolean isRoot () {
return _parent == null;
}
if (selectable && JavaTypes.maybePC(fm)) // relation field
{
if (canTraverse(fm)) {
if (changeState)
traverse(fm);
} else
selectable = false;
public FetchState getRoot() {
return (isRoot()) ? this : getParent().getRoot();
}
public int getAvailableFetchDepth() {
return _availableDepth;
}
public List getPath () {
if (isRoot())
return Collections.EMPTY_LIST;
List result = new ArrayList();
result.add (this);
return ((FetchStateImpl)_parent).trackPath (result);
}
private List trackPath (List path) {
if (isRoot())
return path;
path.add(this);
return ((FetchStateImpl)_parent).trackPath(path);
}
public List getRelationPath () {
if (isRoot())
return Collections.EMPTY_LIST;
List result = new ArrayList();
result.add (_relation);
return ((FetchStateImpl)_parent).trackRelationPath (result);
}
private List trackRelationPath (List path) {
if (isRoot())
return path;
path.add(_relation);
return ((FetchStateImpl)_parent).trackRelationPath(path);
}
public int getCurrentRecursionDepth (FieldMetaData fm) {
if (isRoot())
return 0;
int rd = (_relation == fm) ? 1 : 0;
return rd + _parent.getCurrentRecursionDepth(fm);
}
public boolean isDefault(FieldMetaData fm) {
return (_config.hasFetchGroup(FetchConfiguration.FETCH_GROUP_DEFAULT)
&& fm.isInDefaultFetchGroup())
|| _config.hasFetchGroup(FetchConfiguration.FETCH_GROUP_ALL);
}
public boolean requiresFetch(FieldMetaData fm) {
boolean selectable = isDefault(fm)
|| _config.hasAnyFetchGroup(fm.getFetchGroups())
|| _config.hasField(fm.getFullName());
if (selectable && isRelation(fm)) {
int rd = getRecursionDepth(fm);
int crd = getCurrentRecursionDepth(fm);
selectable = (_availableDepth==INFINITE_DEPTH || _availableDepth>0)
&& ( rd == INFINITE_DEPTH || crd <rd);
}
return selectable;
}
public boolean requiresLoad(OpenJPAStateManager sm, FieldMetaData fm) {
if (sm!=null && sm.getLoaded().get(fm.getIndex()))
return false;
boolean loadable = isDefault(fm)
|| _config.hasFetchGroup(fm.getFetchGroups())
|| _config.hasAnyFetchGroup(fm.getFetchGroups())
|| _config.hasField(fm.getFullName());
if (!loadable)
_knownExcludes.add(fm);
// relation field
if (loadable && JavaTypes.maybePC(fm)) {
int d = getLoadCount(fm);
loadable = (d < (getTraversalCount(fm) - 1));
if (loadable)
_loadTraversals.put(fm, new Integer(d + 1));
if (loadable && isRelation(fm)) {
int rd = getRecursionDepth(fm);
int crd = getCurrentRecursionDepth(fm);
loadable = (_availableDepth==INFINITE_DEPTH || _availableDepth>0)
&& (rd == INFINITE_DEPTH || crd<rd);
}
return loadable;
}
/**
* Get the recusion depth for the given field.
*
* @param fm is the field to look for
* @return 0 if the field does not appear in the given map.
*/
protected int getRecursionDepth(FieldMetaData fm) {
if (_recursionDepths.containsKey(fm)) {
return ((Integer) _recursionDepths.get(fm)).intValue();
}
return initalizeRecusrionDepth(fm);
}
/**
* Sets the recursion depth for the given field as the maximum recusion
@ -131,17 +150,16 @@ public class FetchStateImpl
* @return maximum recursion depth across common fetch groups. -1 is treated
* as positive infinity.
*/
protected int initalizeRecusrionDepth(FieldMetaData fm) {
public int getRecursionDepth(FieldMetaData fm) {
Set commonFGNs = new HashSet();
commonFGNs.addAll(_config.getFetchGroups());
commonFGNs.retainAll(fm.getFetchGroups());
int dMax = (commonFGNs.isEmpty()) ? FetchGroup.DEFAULT_RECURSION_DEPTH
: 0;
int dMax =
(commonFGNs.isEmpty()) ? FetchGroup.DEFAULT_RECURSION_DEPTH : 0;
Iterator i = commonFGNs.iterator();
while (i.hasNext()) {
FetchGroup fg = fm.getDeclaringMetaData()
.getFetchGroup(i.next().toString(), false);
.getFetchGroup(i.next().toString());
int d = fg.getDepthFor(fm);
if (d == INFINITE_DEPTH) {
dMax = INFINITE_DEPTH;
@ -149,57 +167,68 @@ public class FetchStateImpl
}
dMax = Math.max(d, dMax);
}
_recursionDepths.put(fm, new Integer(dMax));
int maxDepth = _config.getMaxFetchDepth();
if (maxDepth != INFINITE_DEPTH)
if (dMax != INFINITE_DEPTH)
dMax = Math.min (maxDepth, dMax);
else
dMax = maxDepth;
return dMax;
}
boolean canTraverse(FieldMetaData fm) {
int maxDepth = _config.getMaxFetchDepth();
if (maxDepth != INFINITE_DEPTH && _depth > maxDepth)
return false;
int sourceDepth = getDepth(fm.getDeclaringMetaData());
int traversalCount = getTraversalCount(fm);
int recursionDepth = getRecursionDepth(fm);
int newtargetDepth = sourceDepth + traversalCount + 1;
boolean isRecursive = fm.getDeclaringMetaData() ==
fm.getDeclaredTypeMetaData();
boolean traversable = (maxDepth == INFINITE_DEPTH)
|| (recursionDepth == INFINITE_DEPTH);
if (isRecursive)
traversable = traversable || (traversalCount < recursionDepth);
else
traversable = traversable || (newtargetDepth <= maxDepth);
return traversable;
public FetchState traverse(FieldMetaData fm) {
if (isRelation(fm)) {
try
{
FetchStateImpl clone = (FetchStateImpl)clone();
clone._parent = this;
clone._relation = fm;
clone._availableDepth = reduce(_availableDepth);
return clone;
}
catch (CloneNotSupportedException e)
{
// ignore
}
}
return this;
}
void traverse(FieldMetaData fm) {
int sourceDepth = getDepth(fm.getDeclaringMetaData());
int traversalCount = getTraversalCount(fm);
boolean isRecursive = fm.getDeclaringMetaData() ==
fm.getDeclaredTypeMetaData();
if (!isRecursive) {
int newDepth = sourceDepth + traversalCount;
_depths.put(fm.getDeclaredTypeMetaData(), new Integer(newDepth));
_depth = Math.max(_depth, newDepth);
}
_selectTraversals.put(fm, new Integer(traversalCount + 1));
int reduce (int d) {
if (d==0)
return 0;//throw new InternalException(this.toString());
if (d==INFINITE_DEPTH)
return INFINITE_DEPTH;
return d-1;
}
int getTraversalCount(FieldMetaData fm) {
Integer n = (Integer) _selectTraversals.get(fm);
return (n == null) ? 0 : n.intValue();
protected boolean isRelation (FieldMetaData fm) {
return fm != null &&
(fm.isDeclaredTypePC()
|| (fm.getElement() != null && fm.getElement().isTypePC())
|| (fm.getKey() != null && fm.getKey().isTypePC())
|| (fm.getValue() != null && fm.getValue().isTypePC()));
}
int getLoadCount(FieldMetaData fm) {
Integer n = (Integer) _loadTraversals.get(fm);
return (n == null) ? 0 : n.intValue();
public String toString () {
return System.identityHashCode(this) + "("+_availableDepth+"): "
+ printPath();
}
int getDepth(ClassMetaData cm) {
if (_depths.containsKey(cm))
return ((Integer) _depths.get(cm)).intValue();
return 0;
private String printPath ()
{
List path = getRelationPath();
if (path.isEmpty())
return "";
StringBuffer tmp = new StringBuffer();
Iterator i = path.iterator();
tmp.append(((FieldMetaData)i.next()).getName());
for (;i.hasNext();)
tmp.append(".").append(((FieldMetaData)i.next()).getName());
return tmp.toString();
}
}

View File

@ -102,8 +102,7 @@ class PNonTransState
if (context.getDirty().length() > 0)
context.saveFields(true);
context.clearFields();
context.load(context.getBroker().getFetchConfiguration().
newFetchState(), context.LOAD_FGS, null, null, true);
context.load(null, context.LOAD_FGS, null, null, true);
}
return PDIRTY;
}

View File

@ -18,14 +18,7 @@ package org.apache.openjpa.kernel;
import java.io.IOException;
import java.io.ObjectOutput;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.TimeZone;
import java.util.*;
import org.apache.commons.lang.StringUtils;
import org.apache.openjpa.conf.OpenJPAConfiguration;
@ -325,6 +318,8 @@ public class StateManagerImpl
if (!forWrite && (!isPersistent() || isNew() || isDeleted()))
return false;
if (fetchState==null)
fetchState = _broker.getFetchConfiguration().newFetchState();
// if any fields being loaded, do state transitions for read
BitSet fields = getUnloadedInternal(fetchState, loadMode, exclude);
boolean active = _broker.isActive();
@ -2760,8 +2755,8 @@ public class StateManagerImpl
protected void loadField(int field, int lockLevel, boolean forWrite,
boolean fgs) {
loadField(field, _broker.getFetchConfiguration().newFetchState(),
lockLevel, forWrite, fgs);
FetchConfiguration fc = _broker.getFetchConfiguration();
loadField(field, fc.newFetchState(),lockLevel, forWrite, fgs);
}
/**
@ -2799,7 +2794,7 @@ public class StateManagerImpl
// call this method even if there are no unloaded fields; loadFields
// takes care of things like loading version info and setting PC
// flags
loadFields(fields, null, lockLevel, null, forWrite);
loadFields(fields, fetchState, lockLevel, null, forWrite);
}
/**
@ -2869,15 +2864,12 @@ public class StateManagerImpl
// is this field in the dfg?
FieldMetaData[] fmds = _meta.getDefaultFetchGroupFields();
if (fmds.length > 0 && field != -1
&& !requiredByDefault(_meta.getField(field), fetchState))
return;
// see if the dfg is fully loaded
// see if any fetch group with postLoad=true is fully loaded
boolean isLoaded = true;
for (int i = 0; isLoaded && i < fmds.length; i++)
if (!_loaded.get(fmds[i].getIndex())
&& requiredByDefault(fmds[i], fetchState))
&& requiresPostLoadCallabck(fmds[i], fetchState))
isLoaded = false;
if (isLoaded) {
_flags |= FLAG_DFG;
@ -2885,15 +2877,18 @@ public class StateManagerImpl
}
}
private boolean requiredByDefault(FieldMetaData fm, FetchState fetchState) {
private boolean requiresPostLoadCallabck(FieldMetaData fm, FetchState fetchState) {
if (fm == null)
return false;
if (fetchState == null)
return fm.isInDefaultFetchGroup()
&& _broker.getFetchConfiguration().hasFetchGroup
(FetchGroup.getDefaultGroupName());
else
return fetchState.isDefault(fm);
Set fetchGroups = fm.getFetchGroups();
for (Iterator i = fetchGroups.iterator(); i.hasNext();)
{
String fg = i.next().toString();
if (_broker.getFetchConfiguration().hasFetchGroup(fg)
&& fm.getDeclaringMetaData().getFetchGroup(fg).isPostLoad())
return true;
}
return false;
}
/**

View File

@ -143,7 +143,7 @@ public interface StoreContext {
*
* @see #find(Object,FetchState,BitSet,Object,int)
*/
public Object[] findAll(Collection oids, FetchState fetchState,
public Object[] findAll(Collection oids, FetchConfiguration fetch,
BitSet exclude, Object edata, int flags);
/**

View File

@ -210,7 +210,7 @@ public interface StoreManager
* @see org.apache.openjpa.util.ImplHelper#loadAll
*/
public Collection loadAll(Collection sms, PCState state, int load,
FetchState fetchState, Object edata);
FetchConfiguration fetch, Object edata);
/**
* Notification that the given state manager is about to change its

View File

@ -130,7 +130,7 @@ class VersionAttachStrategy
break;
case DETACH_FGS:
if (fmds[i].isInDefaultFetchGroup()
|| fetch.hasFetchGroup(fmds[i].getFetchGroups())
|| fetch.hasAnyFetchGroup(fmds[i].getFetchGroups())
|| fetch.hasField(fmds[i].getFullName()))
attachField(manager, toAttach, sm, fmds[i], true);
break;

View File

@ -1922,9 +1922,11 @@ public class ClassMetaData
* the same name.
*/
public synchronized FetchGroup addFetchGroup(String name) {
if (name == null || name.trim().length()==0)
throw new MetaDataException(_loc.get("empty-fg-name", this));
FetchGroup fg = (FetchGroup) _fgs.get(name);
if (fg == null) {
fg = newFetchGroup(name);
fg = new FetchGroup(this, name);
_fgs.put(name, fg);
}
@ -1933,33 +1935,19 @@ public class ClassMetaData
/**
* Gets a named fecth group. If not available in this receiver then looks
* up the inheritence hierarchy. Creates if it does not exist and
* <code>mustBe</code> is false.
* up the inheritence hierarchy.
*
* @param name name of a fetch group.
* @param mustBe if true then the named group must exist in this receiver
* or any of its persistent super classes.
* @return an existing or newly created fecth group of the given name.
* @return an existing fecth group of the given name if known to this
* receiver or any of its superclasses. Otherwise null.
*/
public synchronized FetchGroup getFetchGroup(String name, boolean mustBe) {
public synchronized FetchGroup getFetchGroup(String name) {
FetchGroup fg = (FetchGroup) _fgs.get(name);
if (fg == null) {
ClassMetaData scm = getPCSuperclassMetaData();
if (scm != null)
fg = scm.getFetchGroup(name, false);
fg = scm.getFetchGroup(name);
}
if (fg == null)
if (mustBe)
return null;
else
fg = addFetchGroup(name);
return fg;
}
protected FetchGroup newFetchGroup(String name) {
FetchGroup fg = new FetchGroup(this, name);
return fg;
}

View File

@ -1,12 +1,9 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -28,39 +25,31 @@ import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.openjpa.lib.meta.SourceTracker;
import org.apache.openjpa.lib.util.Localizer;
import org.apache.openjpa.util.MetaDataException;
import org.apache.openjpa.util.UserException;
/**
* Captures fetch group metadata.
*/
public class FetchGroup
implements SourceTracker {
public class FetchGroup implements SourceTracker {
private final String _name;
private final ClassMetaData _declaringClass;
private List _includes;
private Map _depths;
private boolean _postLoad;
public static final int DEFAULT_RECURSION_DEPTH = 1;
private static String DEFAULT_GROUP_NAME = "default";
private static final Localizer _loc = Localizer.forPackage
(FetchGroup.class);
private static final Localizer _loc =
Localizer.forPackage(FetchGroup.class);
/**
* Supply immutable name.
*
* @param name must not by null or empty.
* @param must not by null or empty.
*/
FetchGroup(ClassMetaData cm, String name) {
super();
if (cm == null)
throw new MetaDataException(_loc.get("null-class-fg", name));
if (StringUtils.isEmpty(name))
throw new MetaDataException(_loc.get("invalid-fg-name", cm,
name));
throw new UserException(_loc.get("invalid-fg-name", cm, name));
_name = name;
_declaringClass = cm;
}
@ -77,12 +66,11 @@ public class FetchGroup
*/
public void addInclude(FetchGroup fg) {
if (fg == this)
throw new MetaDataException(_loc.get("self-include-fg", this));
throw new UserException(_loc.get("self-include-fg", this));
if (fg == null)
throw new MetaDataException(_loc.get("null-include-fg", this));
throw new UserException(_loc.get("null-include-fg", this));
if (fg.includes(this, true))
throw new MetaDataException(_loc.get("cyclic-fg", this, fg));
throw new UserException(_loc.get("cyclic-fg", this, fg));
if (_includes == null)
_includes = new ArrayList();
_includes.add(fg);
@ -94,18 +82,17 @@ public class FetchGroup
* @param fg
* @param recurse if true then recursively checks within the included
* fecth groups. Otherwise just checks within direct includes.
* @return
*/
public boolean includes(FetchGroup fg, boolean recurse) {
if (_includes == null)
return false;
if (_includes.contains(fg))
return true;
if (recurse)
for (Iterator i = _includes.iterator(); i.hasNext();)
if (((FetchGroup) i.next()).includes(fg, true))
return true;
return false;
}
@ -117,12 +104,10 @@ public class FetchGroup
*/
public void setDepthFor(FieldMetaData fm, int depth) {
if (depth < -1)
throw new MetaDataException(_loc.get("invalid-fetch-depth",
throw new UserException(_loc.get("invalid-fetch-depth",
_name, fm, new Integer(depth)));
if (_depths == null)
_depths = new HashMap();
_depths.put(fm, new Integer(depth));
}
@ -135,31 +120,16 @@ public class FetchGroup
public int getDepthFor(FieldMetaData fm) {
if (_depths == null || !_depths.containsKey(fm))
return DEFAULT_RECURSION_DEPTH;
return ((Integer) _depths.get(fm)).intValue();
}
/**
* Set the name for default group.
* It is expected to be set only once by a compliant implementation.
* If multiple attempts are made to set the <em>default</em> group name,
* then an attempt will succeed only for the first time or if the given
* name matches with the current name.
*
* @param name of the default fetch group
*/
public static void setDefaultGroupName(String name) {
//###JDO2 -- better mechanics required to set default group name
DEFAULT_GROUP_NAME = name;
public boolean isPostLoad () {
return _postLoad;
}
/**
* Get the name in which <em>default</em> fetch group is known.
*
* @return name of the default group. Can be null, if not set.
*/
public static final String getDefaultGroupName() {
return DEFAULT_GROUP_NAME;
public void setPostLoad (boolean flag) {
_postLoad = flag;
}
/**
@ -171,7 +141,6 @@ public class FetchGroup
return _name.equals(that._name)
&& _declaringClass.equals(that._declaringClass);
}
return false;
}
@ -186,7 +155,6 @@ public class FetchGroup
/////////////////
// SourceTracker
/////////////////
public File getSourceFile() {
return _declaringClass.getSourceFile();
}

View File

@ -39,6 +39,7 @@ import org.apache.commons.collections.comparators.ComparatorChain;
import org.apache.commons.lang.StringUtils;
import org.apache.openjpa.conf.OpenJPAConfiguration;
import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.kernel.FetchConfiguration;
import org.apache.openjpa.kernel.OpenJPAStateManager;
import org.apache.openjpa.kernel.StoreContext;
import org.apache.openjpa.lib.conf.Configurations;
@ -849,13 +850,13 @@ public class FieldMetaData
*/
public void addFetchGroup(String fg) {
if (StringUtils.isEmpty(fg))
return;
if (getDeclaringMetaData().getFetchGroup(fg, false) == null)
throw new MetaDataException(_loc.get("bad-fg", fg));
if (getDeclaringMetaData().getFetchGroup(fg) == null)
throw new MetaDataException(_loc.get("unknown-fg", fg));
if (_fgs == null)
_fgs = new HashSet();
_fgs.add(fg);
if (fg.equals(FetchGroup.getDefaultGroupName()))
if (fg.equals(FetchConfiguration.FETCH_GROUP_DEFAULT))
setInDefaultFetchGroup(true);
}
@ -863,7 +864,7 @@ public class FieldMetaData
if (_fgs == null)
return;
_fgs.remove(fg);
if (fg != null && fg.equals(FetchGroup.getDefaultGroupName()))
if (FetchConfiguration.FETCH_GROUP_DEFAULT.equals(fg))
setInDefaultFetchGroup(false);
}

View File

@ -23,6 +23,7 @@ import java.util.Iterator;
import org.apache.commons.lang.StringUtils;
import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.kernel.FetchConfiguration;
import org.apache.openjpa.kernel.FetchState;
import org.apache.openjpa.kernel.LockManager;
import org.apache.openjpa.kernel.OpenJPAStateManager;
@ -90,18 +91,18 @@ public class ImplHelper {
* @since 4.0
*/
public static Collection loadAll(Collection sms, StoreManager store,
PCState state, int load, FetchState fetchState, Object context) {
PCState state, int load, FetchConfiguration fetch, Object context) {
Collection failed = null;
OpenJPAStateManager sm;
LockManager lm;
for (Iterator itr = sms.iterator(); itr.hasNext();) {
sm = (OpenJPAStateManager) itr.next();
FetchState fetchState = fetch.newFetchState();
if (sm.getManagedInstance() == null) {
if (!store.initialize(sm, state, fetchState, context))
failed = addFailedId(sm, failed);
} else if (load != StoreManager.FORCE_LOAD_NONE
|| sm.getPCState() == PCState.HOLLOW) {
lm = sm.getContext().getLockManager();
if (!store.load(sm, sm.getUnloaded(fetchState),
fetchState, lm.getLockLevel(sm), context))