db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/metadata ClassDescriptor.java
Date Mon, 12 May 2003 10:01:58 GMT
arminw      2003/05/12 03:01:58

  Modified:    src/java/org/apache/ojb/broker/metadata ClassDescriptor.java
  Log:
  improvements by Lance Eason:
  
  2) Three methods on ClassDescriptor, getFieldDescriptorByName,
  getObjectReferenceDescriptorByName, and getCollectionDescriptorByName
  all have the same fundamental problem.
  They each try to build a descriptor by name map lazily.
  To do this they look for the name in the map and if they
  don't find it scan through all the descriptors and if found
  add it to the map.  The problem with this is that queries for
  things where the descriptor doesn't exist always end up
  scanning the entire set of descriptors in order to determine
  that it doesn't exist.  In other words this strategy works fine
  for positive queries but is very poor for negative queries.
  It turns out that a fair number of negative queries do occur
  though for things like the field descriptor for OJB_CONCRETE_CLASS.
  I also fixed a bug where ClassDescriptor doesn't rebuild it's
  cached lists of FieldDescriptors when new field descriptors
  are added and I cached the result of isAbstract.
  
  Files modified:
     ClassDescriptor
        - changed map building strategy in getXXXDescriptorByName methods
        - changed setClassOfObject and isAbstract to cache value of isAbstract
        - changed addFieldDescriptor to clear out cached lists of field descriptors and clear
the name map
        - changed addCollectionDescriptor and addObjectReferenceDescriptor to clear their
respective name maps
  
  Revision  Changes    Path
  1.61      +82 -74    db-ojb/src/java/org/apache/ojb/broker/metadata/ClassDescriptor.java
  
  Index: ClassDescriptor.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/ClassDescriptor.java,v
  retrieving revision 1.60
  retrieving revision 1.61
  diff -u -r1.60 -r1.61
  --- ClassDescriptor.java	12 May 2003 09:18:06 -0000	1.60
  +++ ClassDescriptor.java	12 May 2003 10:01:57 -0000	1.61
  @@ -141,6 +141,10 @@
   	 */
   	private Class m_Class = null;
   	/**
  +	 * whether the described class is abstract
  +	 */
  +	private boolean isAbstract = false;
  +	/**
   	 * the table name used to store the scalar attributes of this class
   	 */
   	private String m_TableName = null;
  @@ -172,7 +176,7 @@
   	 * the read/write FieldDescriptors BRJ
   	 */
   	private FieldDescriptor[] m_RwFieldDescriptors = null;
  -    private FieldDescriptor[] m_RwNonPkFieldDescriptors = null;
  +	private FieldDescriptor[] m_RwNonPkFieldDescriptors = null;
   	/**
   	 * the optimistic lockingFieldDescriptors BRJ
   	 */
  @@ -233,9 +237,9 @@
   	 */
   	private boolean acceptLocks = true;
   	private FieldDescriptor m_autoIncrementField = null;
  -	private Map m_fieldDescriptorNameMap = new HashMap();
  -	private Map m_collectionDescriptorNameMap = new HashMap();
  -	private Map m_objectReferenceDescriptorsNameMap = new HashMap();
  +	private Map m_fieldDescriptorNameMap = null;
  +	private Map m_collectionDescriptorNameMap = null;
  +	private Map m_objectReferenceDescriptorsNameMap = null;
   
   	/**
   	 * if true instances of this class are always refreshed
  @@ -335,6 +339,7 @@
   	public void setClassOfObject(Class c)
   	{
   		m_Class = c;
  +		isAbstract = Modifier.isAbstract(m_Class.getModifiers());
   		// TODO : Shouldn't the HashMap in DescriptorRepository be updated as well?
   	}
   
  @@ -349,7 +354,6 @@
   		{
   			m_FieldDescriptions = new FieldDescriptor[1];
   			m_FieldDescriptions[0] = fld;
  -			m_fieldDescriptorNameMap = new HashMap();
   		}
   		else
   		{
  @@ -361,6 +365,13 @@
   			// 2. Sort fields according to their getOrder() Property
   			Arrays.sort(m_FieldDescriptions, FieldDescriptor.getComparator());
   		}
  +
  +		m_fieldDescriptorNameMap = null;
  +		m_PkFieldDescriptors = null;
  +		m_nonPkFieldDescriptors = null;
  +		m_lockingFieldDescriptors = null;
  +		m_RwFieldDescriptors = null;
  +		m_RwNonPkFieldDescriptors = null;
   	}
   
   	/**
  @@ -379,6 +390,8 @@
   	{
   		m_CollectionDescriptors.add(cod);
   		cod.setClassDescriptor(this); // BRJ
  +
  +		m_collectionDescriptorNameMap = null;
   	}
   
   	/**
  @@ -396,10 +409,12 @@
   	{
   		m_ObjectReferenceDescriptors.add(ord);
   		ord.setClassDescriptor(this); // BRJ
  +
  +		m_objectReferenceDescriptorsNameMap = null;
   	}
   
   	/**
  -	 * Get an ObjectReferenceDescriptor by name	BRJ
  +	 * Get an ObjectReferenceDescriptor by name    BRJ
   	 * @param name
   	 * @return ObjectReferenceDescriptor or null
   	 */
  @@ -410,27 +425,25 @@
   			return null;
   		}
   
  -		ObjectReferenceDescriptor retval = (ObjectReferenceDescriptor) this.m_objectReferenceDescriptorsNameMap.get(name);
  -		if (retval == null)
  +		if (m_objectReferenceDescriptorsNameMap == null)
   		{
  -			Vector descr = getObjectReferenceDescriptors();
  -			int size = descr.size();
  -			for (int i = 0; i < size; i++)
  +			HashMap nameMap = new HashMap();
  +
  +			Vector descriptors = getObjectReferenceDescriptors();
  +			for (int i = descriptors.size() - 1; i >= 0; i--)
   			{
  -				ObjectReferenceDescriptor ord = (ObjectReferenceDescriptor) descr.elementAt(i);
  -				if (ord.getPersistentField().getName().equals(name))
  -				{
  -					retval = ord;
  -					m_objectReferenceDescriptorsNameMap.put(name, retval);
  -					break;
  -				}
  +				ObjectReferenceDescriptor ord = (ObjectReferenceDescriptor) descriptors.get(i);
  +				nameMap.put(ord.getPersistentField().getName(), ord);
   			}
  +
  +			m_objectReferenceDescriptorsNameMap = nameMap;
   		}
  -		return retval;
  +
  +		return (ObjectReferenceDescriptor) m_objectReferenceDescriptorsNameMap.get(name);
   	}
   
   	/**
  -	 * Get an CollectionDescriptor by name	BRJ
  +	 * Get an CollectionDescriptor by name    BRJ
   	 * @param name
   	 * @return CollectionDescriptor or null
   	 */
  @@ -441,23 +454,21 @@
   			return null;
   		}
   
  -		CollectionDescriptor retval = (CollectionDescriptor) this.m_collectionDescriptorNameMap.get(name);
  -		if (retval == null)
  +		if (m_collectionDescriptorNameMap == null)
   		{
  -			Vector descr = getCollectionDescriptors();
  -			int size = descr.size();
  -			for (int i = 0; i < size; i++)
  +			HashMap nameMap = new HashMap();
  +
  +			Vector descriptors = getCollectionDescriptors();
  +			for (int i = descriptors.size() - 1; i >= 0; i--)
   			{
  -				CollectionDescriptor ord = (CollectionDescriptor) descr.elementAt(i);
  -				if (ord.getPersistentField().getName().equals(name))
  -				{
  -					retval = ord;
  -					m_collectionDescriptorNameMap.put(name, retval);
  -					break;
  -				}
  +				CollectionDescriptor cod = (CollectionDescriptor) descriptors.get(i);
  +				nameMap.put(cod.getPersistentField().getName(), cod);
   			}
  +
  +			m_collectionDescriptorNameMap = nameMap;
   		}
  -		return retval;
  +
  +		return (CollectionDescriptor) m_collectionDescriptorNameMap.get(name);
   	}
   
   	/**
  @@ -616,27 +627,25 @@
   	public FieldDescriptor getFieldDescriptorByName(String name)
   	{
   		if (name == null || m_FieldDescriptions == null)
  -        {
  -            return null;
  -        }
  +		{
  +			return null;
  +		}
   
  -		FieldDescriptor retval = (FieldDescriptor) m_fieldDescriptorNameMap.get(name);
  -		if (retval == null)
  +		if (m_fieldDescriptorNameMap == null)
   		{
  -			int size = m_FieldDescriptions.length;
  -			FieldDescriptor temp = null;
  -			for (int i = 0;((i < size) && (retval == null)); i++)
  +			HashMap nameMap = new HashMap();
  +
  +			FieldDescriptor[] descriptors = getFieldDescriptions();
  +			for (int i = descriptors.length - 1; i >= 0; i--)
   			{
  -				temp = m_FieldDescriptions[i];
  -				if (name.equals(temp.getPersistentField().getName()))
  -				{
  -					retval = temp;
  -					m_fieldDescriptorNameMap.put(name, retval);
  -					break;
  -				}
  +				FieldDescriptor fld = descriptors[i];
  +				nameMap.put(fld.getPersistentField().getName(), fld);
   			}
  +
  +			m_fieldDescriptorNameMap = nameMap;
   		}
  -		return retval;
  +
  +		return (FieldDescriptor) m_fieldDescriptorNameMap.get(name);
   	}
   
   	/**
  @@ -1292,11 +1301,11 @@
   			result += "    " + tags.getAttribute(INITIALIZATION_METHOD, this.getInitializationMethod().getName())
+ eol;
   		}
   
  -        //reference refresh is optional, disabled by default
  -        if (isAlwaysRefresh())
  -        {
  -            result += "    " + tags.getAttribute(REFRESH, "true") + eol;
  -        }
  +		//reference refresh is optional, disabled by default
  +		if (isAlwaysRefresh())
  +		{
  +			result += "    " + tags.getAttribute(REFRESH, "true") + eol;
  +		}
   
   
   		result += "  >" + eol;
  @@ -1446,7 +1455,7 @@
   	 */
   	public boolean isAbstract()
   	{
  -		return Modifier.isAbstract(getClassOfObject().getModifiers());
  +		return isAbstract;
   	}
   
   	/**
  @@ -1537,7 +1546,7 @@
   	}
   
   	/**
  -	 * Answer Table name including schema	BRJ
  +	 * Answer Table name including schema    BRJ
   	 */
   	public String getFullTableName()
   	{
  @@ -1651,23 +1660,22 @@
   		return this.initializationMethod;
   	}
   
  -    /**
  -     * if true instances of this class are always refreshed
  +	/**
  +	 * if true instances of this class are always refreshed
   	 * even if they are already in the cache.
  -     * @return boolean
  -     */
  -    public boolean isAlwaysRefresh()
  -    {
  -        return alwaysRefresh;
  -    }
  -
  -    /**
  -     * Sets the alwaysRefresh parameter.
  -     * @param alwaysRefresh The value to set
  -     */
  -    public void setAlwaysRefresh(boolean alwaysRefresh)
  -    {
  -        this.alwaysRefresh = alwaysRefresh;
  -    }
  +	 * @return boolean
  +	 */
  +	public boolean isAlwaysRefresh()
  +	{
  +		return alwaysRefresh;
  +	}
   
  -}
  +	/**
  +	 * Sets the alwaysRefresh parameter.
  +	 * @param alwaysRefresh The value to set
  +	 */
  +	public void setAlwaysRefresh(boolean alwaysRefresh)
  +	{
  +		this.alwaysRefresh = alwaysRefresh;
  +	}
  +}
  \ No newline at end of file
  
  
  

Mime
View raw message