protected InstanceList readCached()
{
assert isCached();
InvocationContext context = getInvocationContext();
Lookup identityMap = new HashTab(4);
boolean bCached = true;
for (;;)
{
boolean bInstanceCaching = true;
boolean bFiltered = false;
boolean bMissed = false;
List tobjList = null;
if (m_persistenceMapping.getCaching() == PersistenceMapping.CACHING_CLASS)
{
Object classKey = Instance.getClassKey(m_metaclass, null);
Pair classResult = getCached(classKey);
Object queryKey = classKey;
Pair queryResult = classResult;
if (m_metaclass.getWhere() != null)
{
Query query = Query.createRead(m_metaclass, null, null, null, -1, 0, false, SEC_NONE, context);
query.setLimit(-1);
query.setCached(false);
query.setCursor(false);
query.prepare(false);
if (query.getWhere() != null)
{
queryKey = Instance.getClassKey(m_metaclass, query.toString(query.getWhere()));
queryResult = getCached(queryKey);
}
}
if (bCached && queryResult != null && classResult != null &&
ObjUtil.equal(queryResult.getTail(), classResult.getTail()))
{
tobjList = (List)queryResult.getHead();
}
if (tobjList == null)
{
Pair attributes = getCachedAttributes();
InstanceList instanceList = readUncached(attributes, null);
int nCount = instanceList.size();
tobjList = new ArrayList(nCount);
for (int i = 0; i < nCount; ++i)
{
tobjList.add(RPCUtil.transfer(instanceList.get(i), attributes, identityMap,
RPCUtil.TF_ALL | RPCUtil.TF_LAZY | RPCUtil.TF_OLD));
identityMap.clear();
}
instanceList = null;
Lookup cacheMap = new HashTab(2);
queryResult = new Pair(tobjList);
if (classKey != queryKey)
{
classResult = getCached(classKey);
if (classResult == null)
{
classResult = new Pair(null, new Object());
cacheMap.put(classKey, classResult);
}
}
if (classResult != null)
{
queryResult.setTail(classResult.getTail());
}
else
{
queryResult.setTail(new Object());
}
cacheMap.put(queryKey, queryResult);
cache(cacheMap, tobjList);
bMissed = true;
}
bInstanceCaching = tobjList.size() > 64 && isUnique();
}
if (bInstanceCaching)
{
TransferObject tobj = null;
Object instanceKey = null;
Pair instanceResult = null;
Object oidKey = null;
Pair oidResult = null;
OID oid;
if (m_oid != null)
{
oid = m_oid;
}
else
{
oidKey = Pair.list(Symbol._CLASS, m_metaclass.getSymbol(), toString(m_where));
oidResult = getCached(oidKey);
oid = (oidResult == null) ? null : (OID)oidResult.getHead();
}
if (oid != null)
{
instanceKey = Instance.getInstanceKey(m_metaclass, oid);
instanceResult = getCached(instanceKey);
if (bCached && instanceResult != null &&
(m_oid != null || ObjUtil.equal(instanceResult.getTail(), oidResult.getTail())))
{
tobj = (TransferObject)instanceResult.getHead();
}
}
if (tobj == null)
{
tobj = NULL_TO;
if (tobjList == null)
{
Pair attributes = getCachedAttributes();
InstanceList instanceList;
if (m_oid != null)
{
instanceList = readUncached(attributes, Pair.attribute("").eq(m_oid));
}
else
{
int nLimitSaved = m_nLimit;
int nOffsetSaved = m_nOffset;
int nMaxCountSaved = m_nMaxCount;
byte nCachedSaved = m_nCached;
m_nLimit = -1;
m_nOffset = 0;
setCached(false);
try
{
instanceList = m_adapter.read(this);
}
finally
{
m_nLimit = nLimitSaved;
m_nOffset = nOffsetSaved;
m_nMaxCount = nMaxCountSaved;
m_nCached = nCachedSaved;
}
bFiltered = true;
for (Iterator itr = getAssocIterator(ASSOC_QUERY); itr.hasNext();)
{
Query assoc = (Query)itr.next();
if (assoc.isJoin() && !assoc.isInverse() && !assoc.getAttribute().isLazy())
{
assoc.join();
}
}
}
if (!instanceList.isEmpty())
{
tobj = (TransferObject)RPCUtil.transfer(instanceList.get(0), attributes,
identityMap, RPCUtil.TF_ALL | RPCUtil.TF_LAZY | RPCUtil.TF_OLD);
identityMap.clear();
oid = tobj.getOID();
}
bMissed = true;
}
else
{
if (m_oid != null)
{
for (int i = 0, n = tobjList.size(); i < n; ++i)
{
TransferObject cur = (TransferObject)tobjList.get(i);
if (m_oid.equals(cur.getOID()))
{
tobj = cur;
break;
}
}
}
else
{
for (int i = 0, n = tobjList.size(); i < n; ++i)
{
TransferObject cur = (TransferObject)tobjList.get(i);
m_instance = cur;
if (m_where == null || !Boolean.FALSE.equals(m_where.getValue()))
{
tobj = cur;
break;
}
}
bFiltered = true;
}
}
if (oid != null)
{
if (instanceKey == null)
{
instanceKey = Instance.getInstanceKey(m_metaclass, oid);
}
Lookup cacheMap = new HashTab(2);
instanceResult = (m_oid != null) ? null : getCached(instanceKey);
if (instanceResult == null)
{
instanceResult = new Pair(tobj);
if (tobj != NULL_TO)
{
if (m_persistenceMapping.getLockingAttribute() != null)
{
instanceResult.setTail(tobj.findValue(m_persistenceMapping.getLockingAttribute().getName()));
}
if (instanceResult.getTail() == null)
{
instanceResult.setTail(new Object());
}
}
cacheMap.put(instanceKey, instanceResult);
}
if (m_oid == null)
{
cacheMap.put(oidKey, new Pair(oid, instanceResult.getTail()));
}
cache(cacheMap, tobj);
}
}