You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by br...@apache.org on 2005/04/24 13:54:05 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/broker/accesslayer ProxyRsIterator.java RsIterator.java RsIteratorFactoryImpl.java
brj 2005/04/24 04:54:05
Modified: src/java/org/apache/ojb/broker/accesslayer RsIterator.java
RsIteratorFactoryImpl.java
Added: src/java/org/apache/ojb/broker/accesslayer
ProxyRsIterator.java
Log:
special RsIterator for proxies. may be enhanced to load the pk-columns only.
Revision Changes Path
1.75 +75 -122 db-ojb/src/java/org/apache/ojb/broker/accesslayer/RsIterator.java
Index: RsIterator.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/RsIterator.java,v
retrieving revision 1.74
retrieving revision 1.75
diff -u -r1.74 -r1.75
--- RsIterator.java 13 Apr 2005 20:04:55 -0000 1.74
+++ RsIterator.java 24 Apr 2005 11:54:04 -0000 1.75
@@ -104,11 +104,6 @@
private RsQueryObject m_queryObject;
/**
- * the proxy class to be used or null
- */
- private Class m_itemProxyClass;
-
- /**
* the top-level class of the item objects
*/
private Class m_itemTopLevelClass = null;
@@ -162,16 +157,6 @@
setBroker(broker);
setQueryObject(queryObject);
- Class classToPrefetch = broker.getReferenceBroker().getClassToPrefetch();
- if ((classToPrefetch != null) && classToPrefetch.isAssignableFrom(queryObject.getClassDescriptor().getClassOfObject()))
- {
- setItemProxyClass(null);
- }
- else
- {
- setItemProxyClass(queryObject.getClassDescriptor().getProxyClass());
- }
-
/*
* arminw: to improve performance we only use this instance to fire
* events and set the target object on every use TODO: Find a better
@@ -181,7 +166,7 @@
try
{
- setRsAndStmt(queryObject.performQuery(broker.serviceJdbcAccess()));
+ setRsAndStmt(executeQuery(queryObject, broker));
prefetchRelationships(queryObject.getCriteriaBasedQuery());
if (logger.isDebugEnabled())
{
@@ -410,117 +395,84 @@
protected Object getObjectFromResultSet() throws PersistenceBrokerException
{
getRow().clear();
- /**
- * MBAIRD if a proxy is to be used, return a proxy instance and dont
- * perfom a full materialization. NOTE: Potential problem here with
- * multi-mapped table. The itemProxyClass is for the m_cld
- * classdescriptor. The object you are materializing might not be of
- * that type, it could be a subclass. We should get the concrete class
- * type out of the resultset then check the proxy from that.
- * itemProxyClass should NOT be a member variable.
- */
- // in any case we need the PK values of result set row
+ // we need the PK values of result set row
// provide m_row with primary key data of current row
RowReader reader = getBroker().getRowReaderFor(getQueryObject().getClassDescriptor());
-
reader.readPkValuesFrom(getRsAndStmt().m_rs, getRow());
- if (getItemProxyClass() != null)
- {
- // assert: m_row is filled with primary key values from db
- return getProxyFromResultSet();
- }
- else
+ // 1.read Identity
+ Identity oid = getIdentityFromResultSet();
+ Object result = null;
+
+ // 2. check if Object is in cache. if so return cached version.
+ result = getCache().lookup(oid);
+ if (result == null)
{
- // 1.read Identity
- Identity oid = getIdentityFromResultSet();
- Object result = null;
-
- // 2. check if Object is in cache. if so return cached version.
- result = getCache().lookup(oid);
- if (result == null)
- {
- // map all field values from the current result set
- reader.readObjectArrayFrom(getRsAndStmt().m_rs, getRow());
- // 3. If Object is not in cache
- // materialize Object with primitive attributes filled from current row
- result = reader.readObjectFrom(getRow());
- // result may still be null!
- if (result != null)
+ // map all field values from the current result set
+ reader.readObjectArrayFrom(getRsAndStmt().m_rs, getRow());
+ // 3. If Object is not in cache
+ // materialize Object with primitive attributes filled from current row
+ result = reader.readObjectFrom(getRow());
+ // result may still be null!
+ if (result != null)
+ {
+ /*
+ * synchronize on result so the ODMG-layer can take a
+ * snapshot only of fully cached (i.e. with all references +
+ * collections) objects
+ */
+ synchronized (result)
{
- /*
- * synchronize on result so the ODMG-layer can take a
- * snapshot only of fully cached (i.e. with all references +
- * collections) objects
- */
- synchronized (result)
+ getCache().enableMaterializationCache();
+ try
+ {
+ getCache().doLocalCache(oid, result, LocalCache.TYPE_READ_NEW);
+
+ /**
+ * MBAIRD if you have multiple classes mapped to a
+ * table, and you query on the base class you could get
+ * back NON base class objects, so we shouldn't pass
+ * m_cld, but rather the class descriptor for the
+ * actual class.
+ */
+ // fill reference and collection attributes
+ ClassDescriptor cld = getQueryObject().getClassDescriptor().getRepository().getDescriptorFor(
+ result.getClass());
+ // don't force loading of reference
+ final boolean unforced = false;
+ // Maps ReferenceDescriptors to HashSets of owners
+ getBroker().getReferenceBroker().retrieveReferences(result, cld, unforced);
+ getBroker().getReferenceBroker().retrieveCollections(result, cld, unforced);
+
+ getCache().disableMaterializationCache();
+ }
+ catch (RuntimeException e)
{
- getCache().enableMaterializationCache();
- try
- {
- getCache().doLocalCache(oid, result, LocalCache.TYPE_READ_NEW);
-
- /**
- * MBAIRD if you have multiple classes mapped to a
- * table, and you query on the base class you could get
- * back NON base class objects, so we shouldn't pass
- * m_cld, but rather the class descriptor for the
- * actual class.
- */
- // fill reference and collection attributes
- ClassDescriptor cld = getQueryObject().getClassDescriptor().getRepository().getDescriptorFor(result.getClass());
- // don't force loading of reference
- final boolean unforced = false;
- // Maps ReferenceDescriptors to HashSets of owners
- getBroker().getReferenceBroker().retrieveReferences(result, cld, unforced);
- getBroker().getReferenceBroker().retrieveCollections(result, cld, unforced);
-
- getCache().disableMaterializationCache();
- }
- catch(RuntimeException e)
- {
- // catch runtime exc. to guarantee clearing of internal buffer on failure
- getCache().reset(false);
- throw e;
- }
+ // catch runtime exc. to guarantee clearing of internal buffer on failure
+ getCache().reset(false);
+ throw e;
}
}
}
- else // Object is in cache
+ }
+ else
+ // Object is in cache
+ {
+ ClassDescriptor cld = getBroker().getClassDescriptor(result.getClass());
+ // if refresh is required, read all values from the result set and
+ // update the cache instance from the db
+ if (cld.isAlwaysRefresh())
{
- ClassDescriptor cld = getBroker().getClassDescriptor(result.getClass());
- // if refresh is required, read all values from the result set and
- // update the cache instance from the db
- if (cld.isAlwaysRefresh())
- {
- // map all field values from the current result set
- reader.readObjectArrayFrom(getRsAndStmt().m_rs, getRow());
- reader.refreshObject(result, getRow());
- }
- getBroker().refreshRelationships(result, cld);
+ // map all field values from the current result set
+ reader.readObjectArrayFrom(getRsAndStmt().m_rs, getRow());
+ reader.refreshObject(result, getRow());
}
-
- return result;
+ getBroker().refreshRelationships(result, cld);
}
- }
- /**
- * Reads primary key information from current RS row and generates a
- *
- * corresponding Identity, and returns a proxy from the Identity.
- *
- * @throws PersistenceBrokerException
- * if there was an error creating the proxy class
- */
- protected Object getProxyFromResultSet() throws PersistenceBrokerException
- {
- // 1. get Identity of current row:
- Identity oid = getIdentityFromResultSet();
-
- // 2. return a Proxy instance:
- return getBroker().createProxy(getItemProxyClass(), oid);
+ return result;
}
/**
@@ -792,6 +744,17 @@
}
}
+ /**
+ * Execute the Query. Load all columns.
+ * @param queryObject the query
+ * @param broker the broker
+ * @return ResultSetAndStatement
+ */
+ protected ResultSetAndStatement executeQuery(RsQueryObject queryObject, PersistenceBrokerInternal broker)
+ {
+ return queryObject.performQuery(broker.serviceJdbcAccess());
+ }
+
public boolean isClosed()
{
return resourcesReleased;
@@ -901,16 +864,6 @@
return m_queryObject;
}
- protected void setItemProxyClass(Class itemProxyClass)
- {
- this.m_itemProxyClass = itemProxyClass;
- }
-
- protected Class getItemProxyClass()
- {
- return m_itemProxyClass;
- }
-
protected void setRow(Map row)
{
m_row = row;
1.3 +13 -3 db-ojb/src/java/org/apache/ojb/broker/accesslayer/RsIteratorFactoryImpl.java
Index: RsIteratorFactoryImpl.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/RsIteratorFactoryImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- RsIteratorFactoryImpl.java 13 Apr 2005 19:48:28 -0000 1.2
+++ RsIteratorFactoryImpl.java 24 Apr 2005 11:54:05 -0000 1.3
@@ -25,7 +25,7 @@
*
* @see RsIteratorFactoryFactory
*
- * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
+ * @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id$
*/
class RsIteratorFactoryImpl implements RsIteratorFactory
@@ -35,7 +35,17 @@
*/
public RsIterator createRsIterator(Query query, ClassDescriptor cld, PersistenceBrokerInternal broker)
{
- return new RsIterator(RsQueryObject.get(cld, query), broker);
+ Class classToPrefetch = broker.getReferenceBroker().getClassToPrefetch();
+
+ //BRJ: if we are simply loading proxies, we can use the optimized ProxyRsIterator
+ if (cld.getProxyClass() != null && classToPrefetch == null)
+ {
+ return new ProxyRsIterator(RsQueryObject.get(cld, query), broker);
+ }
+ else
+ {
+ return new RsIterator(RsQueryObject.get(cld, query), broker);
+ }
}
/**
1.1 db-ojb/src/java/org/apache/ojb/broker/accesslayer/ProxyRsIterator.java
Index: ProxyRsIterator.java
===================================================================
package org.apache.ojb.broker.accesslayer;
/* Copyright 2002-2004 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.PersistenceBrokerException;
import org.apache.ojb.broker.PersistenceBrokerInternal;
/**
* RSIterator used to load proxies.
*
* @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id: ProxyRsIterator.java,v 1.1 2005/04/24 11:54:04 brj Exp $
*/
public class ProxyRsIterator extends RsIterator
{
/**
* @param queryObject
* @param broker
*/
public ProxyRsIterator(RsQueryObject queryObject, PersistenceBrokerInternal broker)
{
super(queryObject, broker);
}
/**
* returns a Proxy from the current row of the
* underlying resultset. Works as follows: - read Identity from the primary
* key values of current row - create the proxy
*/
protected Object getObjectFromResultSet() throws PersistenceBrokerException
{
getRow().clear();
// provide m_row with primary key data of current row
RowReader reader = getBroker().getRowReaderFor(getQueryObject().getClassDescriptor());
reader.readPkValuesFrom(getRsAndStmt().m_rs, getRow());
// assert: m_row is filled with primary key values from db
// 1. get Identity of current row:
Identity oid = getIdentityFromResultSet();
// 2. return a Proxy instance:
return getBroker().createProxy(getQueryObject().getClassDescriptor().getProxyClass(), oid);
}
/**
* exectute the Query.
* TODO: load the pk columns only.
* @see org.apache.ojb.broker.accesslayer.RsIterator#executeQuery(org.apache.ojb.broker.accesslayer.RsQueryObject, org.apache.ojb.broker.PersistenceBrokerInternal)
*/
protected ResultSetAndStatement executeQuery(RsQueryObject queryObject, PersistenceBrokerInternal broker)
{
return queryObject.performQuery(broker.serviceJdbcAccess());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org