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 ar...@apache.org on 2005/09/06 00:46:15 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker Identity.java

arminw      2005/09/05 15:46:15

  Modified:    src/java/org/apache/ojb/broker Identity.java
  Log:
  changes for best performance
  
  Revision  Changes    Path
  1.44      +49 -36    db-ojb/src/java/org/apache/ojb/broker/Identity.java
  
  Index: Identity.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/Identity.java,v
  retrieving revision 1.43
  retrieving revision 1.44
  diff -u -r1.43 -r1.44
  --- Identity.java	27 Aug 2005 12:23:16 -0000	1.43
  +++ Identity.java	5 Sep 2005 22:46:15 -0000	1.44
  @@ -15,15 +15,6 @@
    * limitations under the License.
    */
   
  -import org.apache.ojb.broker.metadata.ClassDescriptor;
  -import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
  -import org.apache.ojb.broker.core.ValueContainer;
  -import org.apache.ojb.broker.core.proxy.IndirectionHandler;
  -import org.apache.ojb.broker.util.BrokerHelper;
  -import org.apache.commons.lang.builder.HashCodeBuilder;
  -import org.apache.commons.lang.SystemUtils;
  -import org.apache.commons.lang.ArrayUtils;
  -
   import java.io.ByteArrayInputStream;
   import java.io.ByteArrayOutputStream;
   import java.io.ObjectInputStream;
  @@ -33,6 +24,14 @@
   import java.util.zip.GZIPInputStream;
   import java.util.zip.GZIPOutputStream;
   
  +import org.apache.commons.lang.ArrayUtils;
  +import org.apache.commons.lang.SystemUtils;
  +import org.apache.ojb.broker.core.ValueContainer;
  +import org.apache.ojb.broker.core.proxy.IndirectionHandler;
  +import org.apache.ojb.broker.metadata.ClassDescriptor;
  +import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
  +import org.apache.ojb.broker.util.BrokerHelper;
  +
   /**
    * Represents the identity of an object.
    * <br/>
  @@ -67,6 +66,10 @@
   public class Identity implements Serializable
   {
   	static final long serialVersionUID = 3182285550574178710L;
  +    /**
  +     * used for hashCode calculation.
  +     */
  +    private static final int iConstant = 37;
   
       /**
        * the top-level Class of the identified object<br>
  @@ -86,6 +89,9 @@
       private Object[] m_pkValues;
   
       /*
  +    the hashcode of different objects has to be unique across different
  +    JVM and have to be the same for the same object in different JVM.
  +
       In distributed enviroments the Identity object have to recalculate the
       hashCode and toString values, because the hash code of the Class object
       differs in different JVM
  @@ -285,10 +291,10 @@
           if (m_pkValues == null || m_pkValues.length == 0)
           {
               throw createException("OJB needs at least one primary key attribute for class: ", realObject, null);
  -
           }
  -        if(m_pkValues[0] instanceof ValueContainer)
  -            throw new OJBRuntimeException("Can't handle pk values of type "+ValueContainer.class.getName());
  +// arminw: should never happen
  +//        if(m_pkValues[0] instanceof ValueContainer)
  +//            throw new OJBRuntimeException("Can't handle pk values of type "+ValueContainer.class.getName());
       }
   
       /**
  @@ -309,22 +315,27 @@
           if(this == obj) return true;
   
           boolean result = false;
  -        if (obj instanceof org.apache.ojb.broker.Identity)
  +        if (obj instanceof Identity)
           {
               final Identity id = (Identity) obj;
  -            final Object[] otherPkValues = id.getPrimaryKeyValues();
  -
  -            result = getObjectsTopLevelClass().equals(id.getObjectsTopLevelClass())
  -                    && (m_pkValues.length == otherPkValues.length);
  -            for (int i = 0; result && i < m_pkValues.length; i++)
  +            result = m_objectsTopLevelClass.equals(id.m_objectsTopLevelClass);
  +            if(result)
               {
  -                result = (m_pkValues[i] == null) ? (otherPkValues[i] == null)
  -                        : m_pkValues[i].equals(otherPkValues[i]);
  -
  -                // special treatment for byte[]
  -                if (!result && m_pkValues[i] instanceof byte[] && otherPkValues[i] instanceof byte[])
  +                final Object[] otherPkValues = id.m_pkValues;
  +                result = m_pkValues.length == otherPkValues.length;
  +                if(result)
                   {
  -                    result = Arrays.equals((byte[]) m_pkValues[i], (byte[]) otherPkValues[i]);
  +                    for (int i = 0; result && i < m_pkValues.length; i++)
  +                    {
  +                        result = (m_pkValues[i] == null) ? (otherPkValues[i] == null)
  +                                : m_pkValues[i].equals(otherPkValues[i]);
  +
  +                        // special treatment for byte[]
  +                        if (!result && m_pkValues[i] instanceof byte[] && otherPkValues[i] instanceof byte[])
  +                        {
  +                            result = Arrays.equals((byte[]) m_pkValues[i], (byte[]) otherPkValues[i]);
  +                        }
  +                    }
                   }
               }
           }
  @@ -348,20 +359,22 @@
           */
           if(m_hashCode == null)
           {
  -            final HashCodeBuilder hb = new HashCodeBuilder();
  +            int iTotal = 17;
  +            Object obj;
               for (int i = 0; i < m_pkValues.length; i++)
               {
  -            	hb.append(m_pkValues[i]);
  +            	obj = m_pkValues[i];
  +                if(obj instanceof byte[])
  +                {
  +                    iTotal = iTotal * iConstant + ((byte[]) obj).length;
  +                }
  +                else
  +                {
  +                    iTotal = iTotal * iConstant + (obj != null ? obj.hashCode() : 0);
  +                }
               }
  -            /*
  -            the hashcode of different objects to be unique across different
  -            JVM and have to be the same for the same object in different JVM,
  -            so we can't use
  -            hb.append(getObjectsTopLevelClass().hashCode());
  -            because Class identity is not same in different JVM
  -            */
  -            hb.append(getObjectsTopLevelClass().getName());
  -            m_hashCode = new Integer(hb.toHashCode());
  +            iTotal = iTotal * iConstant + getObjectsTopLevelClass().hashCode();
  +            m_hashCode = new Integer(iTotal);
           }
           return m_hashCode.intValue();
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org