Added load-fetch-group to FieldMetaData and StateManager loading

git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@428120 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2006-08-02 19:53:42 +00:00
parent df43989689
commit b3f8f9de48
2 changed files with 61 additions and 16 deletions

View File

@ -2751,13 +2751,20 @@ public class StateManagerImpl
return ret;
}
protected void loadField(int field, int lockLevel, boolean forWrite,
boolean fgs) {
loadField (field, lockLevel, forWrite, fgs, null);
}
/**
* Load the given field's fetch group; the field itself may already be
* loaded if it is being set by the user.
*/
protected void loadField(int field, int lockLevel, boolean forWrite,
boolean fgs) {
FetchConfiguration fetch = _broker.getFetchConfiguration();
boolean fgs, FetchConfiguration fetch) {
if (fetch == null)
fetch = _broker.getFetchConfiguration();
boolean isLoadFetchGroupAdded = false;
FieldMetaData fmd = _meta.getField(field);
BitSet fields = null;
// if this is a dfg field or we need to load our dfg, do so
@ -2768,17 +2775,23 @@ public class StateManagerImpl
// if not a dfg field, use first custom fetch group as load group
//### need to use metadata load-fetch-group
if (!fmd.isInDefaultFetchGroup()) {
if (fmd.getCustomFetchGroups().length > 0) {
String fg = fmd.getCustomFetchGroups()[0];
String lfg = fmd.getLoadFetchGroup();
if (lfg != null) {
FieldMetaData[] fmds = _meta.getFields();
for (int i = 0; i < fmds.length; i++) {
if (!_loaded.get(i) && (i == field
|| fmds[i].isInFetchGroup(fg))) {
|| fmds[i].isInFetchGroup(lfg))) {
if (fields == null)
fields = new BitSet(fmds.length);
fields.set(i);
}
}
// relation field is loaded with the load-fetch-group
// but this addition must be reverted once the load is over
if (isRelation(fmd) && !fetch.hasFetchGroup(lfg)) {
fetch.addFetchGroup(fmd.getLoadFetchGroup());
isLoadFetchGroupAdded = true;
}
} else if (!_loaded.get(fmd.getIndex())) {
if (fields == null)
fields = new BitSet();
@ -2790,6 +2803,16 @@ public class StateManagerImpl
// takes care of things like loading version info and setting PC
// flags
loadFields(fields, fetch, lockLevel, null, forWrite);
// remove the load fetch group
if (isLoadFetchGroupAdded)
fetch.removeFetchGroup(fmd.getLoadFetchGroup());
}
private static boolean isRelation(FieldMetaData fm) {
return fm.isDeclaredTypePC()
|| fm.getElement().isDeclaredTypePC()
|| fm.getKey().isDeclaredTypePC();
}
/**
@ -2863,19 +2886,22 @@ public class StateManagerImpl
if (field != -1) {
FieldMetaData fmd = _meta.getField (field);
if (fmd.isInDefaultFetchGroup()
&& postLoad(_meta.getFetchGroup(FetchGroup.NAME_DEFAULT)))
return;
&& postLoad(_meta.getFetchGroup(FetchGroup.NAME_DEFAULT), fetch))
return;
String[] fgs = fmd.getCustomFetchGroups();
for (int i = 0; i < fgs.length; i++)
if (postLoad(_meta.getFetchGroup(fgs[i])))
if (postLoad(_meta.getFetchGroup(fgs[i]), fetch))
return;
} else {
if (postLoad(_meta.getFetchGroup(FetchGroup.NAME_DEFAULT)))
if (postLoad(_meta.getFetchGroup(FetchGroup.NAME_DEFAULT), fetch))
return;
FetchGroup[] fgs = _meta.getCustomFetchGroups();
for (int i = 0; i < fgs.length; i++)
if (postLoad(fgs[i]))
Iterator fgs = fetch.getFetchGroups().iterator();
for (;fgs.hasNext();) {
FetchGroup fg = _meta.getFetchGroup(fgs.next().toString());
if (fg != null && postLoad(fg, fetch))
return;
}
}
}
@ -2883,16 +2909,18 @@ public class StateManagerImpl
* Perform post-load actions if the given fetch group is a post-load group
* and is fully loaded.
*/
private boolean postLoad(FetchGroup fg) {
private boolean postLoad(FetchGroup fg, FetchConfiguration fetch) {
if (!fg.isPostLoad())
return false;
FieldMetaData[] fmds = _meta.getFields();
for (int i = 0; i < fmds.length; i++)
if (!_loaded.get(i) && fmds[i].isInFetchGroup(fg.getName()))
return false;
if (fmds[i].isInFetchGroup(fg.getName()))
if (!_loaded.get(i))
return false;
_flags |= FLAG_LOADED;
fireLifecycleEvent(LifecycleEvent.AFTER_LOAD);
_broker.fireLifecycleEvent(getManagedInstance(), fetch, _meta,
LifecycleEvent.AFTER_LOAD);
return true;
}

View File

@ -152,6 +152,7 @@ public class FieldMetaData
private int _dfg = 0;
private Set _fgSet = null;
private String[] _fgs = null;
private String _lfg = null;
private Boolean _lrs = null;
private String _extName = null;
private Method _extMethod = DEFAULT_METHOD;
@ -573,6 +574,16 @@ public class FieldMetaData
return _fgs;
}
/**
* Gets the fetch group that is to be loaded when this receiver is loaded.
*
* @return name of the load fetch group. Can be null, if not explicitly
* set.
*/
public String getLoadFetchGroup () {
return _lfg;
}
/**
* Whether this field is in the given fetch group.
*/
@ -610,6 +621,12 @@ public class FieldMetaData
_fgs = null;
}
public void setLoadFetchGroup (String lfg) {
if (StringUtils.isEmpty(lfg))
throw new MetaDataException(_loc.get("empty-fg-name"),this);
_lfg = lfg;
}
/**
* How the data store should treat null values for this field:
* <ul>