directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r938447 - in /directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap: entry/ entry/client/ schema/
Date Tue, 27 Apr 2010 13:22:41 GMT
Author: elecharny
Date: Tue Apr 27 13:22:40 2010
New Revision: 938447

URL: http://svn.apache.org/viewvc?rev=938447&view=rev
Log:
o Transformed the hashCode() method in Value, Attribute and Entry to be computed only once,
or when the data are being modified
o The hash for Entry is only based on the DN
o The hash for Attribute does not use the values

Modified:
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/AbstractValue.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/BinaryValue.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultEntryAttribute.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultServerEntry.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/ServerEntry.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/StringValue.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntry.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/AbstractSchemaObject.java

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/AbstractValue.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/AbstractValue.java?rev=938447&r1=938446&r2=938447&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/AbstractValue.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/AbstractValue.java
Tue Apr 27 13:22:40 2010
@@ -60,6 +60,9 @@ public abstract class AbstractValue<T> i
     /** A flag set if the normalized data is different from the wrapped data */
     protected transient boolean same;
     
+    /** The computed hashcode. We don't want to compute it each time the hashcode() method
is called */
+    protected volatile int h;
+
     /**
      * {@inheritDoc}
      */
@@ -144,6 +147,9 @@ public abstract class AbstractValue<T> i
             LOG.info( message );
             normalized = false;
         }
+        
+        h=0;
+        hashCode();
     }
 
 
@@ -345,6 +351,8 @@ public abstract class AbstractValue<T> i
     {
         normalized = true;
         normalizedValue = wrappedValue;
+        h = 0;
+        hashCode();
     }
 
 

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/BinaryValue.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/BinaryValue.java?rev=938447&r1=938446&r2=938447&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/BinaryValue.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/BinaryValue.java
Tue Apr 27 13:22:40 2010
@@ -50,10 +50,13 @@ import org.slf4j.LoggerFactory;
 public class BinaryValue extends AbstractValue<byte[]>
 {
     /** Used for serialization */
-    protected static final long serialVersionUID = 2L;
+    public static final long serialVersionUID = 2L;
     
     /** logger for reporting errors that might not be handled properly upstream */
-    protected static final Logger LOG = LoggerFactory.getLogger( BinaryValue.class );
+    private static final Logger LOG = LoggerFactory.getLogger( BinaryValue.class );
+    
+    /** The computed hashcode. We don't want to compute it each time the hashcode() method
is called */
+    private volatile int h;
     
     /**
      * Creates a BinaryValue without an initial wrapped value.
@@ -342,23 +345,24 @@ public class BinaryValue extends Abstrac
     // -----------------------------------------------------------------------
     // Object Methods
     // -----------------------------------------------------------------------
-
-
     /**
      * @see Object#hashCode()
      * @return the instance's hashcode 
      */
     public int hashCode()
     {
-        // return zero if the value is null so only one null value can be
-        // stored in an attribute - the string version does the same
-        if ( isNull() )
+        if ( h == 0 )
         {
-            return 0;
+            // return zero if the value is null so only one null value can be
+            // stored in an attribute - the string version does the same
+            if ( isNull() )
+            {
+                return 0;
+            }
+            
+            byte[] normalizedValue = getNormalizedValueReference();
+            h = Arrays.hashCode( normalizedValue );
         }
-        
-        byte[] normalizedValue = getNormalizedValueReference();
-        int h = Arrays.hashCode( normalizedValue );
 
         return h;
     }
@@ -571,6 +575,8 @@ public class BinaryValue extends Abstrac
                 }
             }
         }
+        
+        h = 0;
     }
 
     

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultEntryAttribute.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultEntryAttribute.java?rev=938447&r1=938446&r2=938447&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultEntryAttribute.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultEntryAttribute.java
Tue Apr 27 13:22:40 2010
@@ -26,6 +26,8 @@ import java.io.ObjectOutput;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
 
 import org.apache.directory.shared.ldap.exception.LdapException;
 import org.apache.directory.shared.ldap.exception.LdapInvalidAttributeValueException;
@@ -35,6 +37,7 @@ import org.apache.directory.shared.i18n.
 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
 import org.apache.directory.shared.ldap.schema.AttributeType;
 import org.apache.directory.shared.ldap.schema.SyntaxChecker;
+import org.apache.directory.shared.ldap.util.ArrayUtils;
 import org.apache.directory.shared.ldap.util.StringTools;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -55,20 +58,23 @@ public class DefaultEntryAttribute imple
     private static final Logger LOG = LoggerFactory.getLogger( DefaultEntryAttribute.class
);
 
     /** The associated AttributeType */
-    protected AttributeType attributeType;
+    private AttributeType attributeType;
     
     /** The set of contained values */
-    protected Set<Value<?>> values = new LinkedHashSet<Value<?>>();
+    private Set<Value<?>> values = new LinkedHashSet<Value<?>>();
     
     /** The User provided ID */
-    protected String upId;
+    private String upId;
 
     /** The normalized ID (will be the OID if we have a AttributeType) */
-    protected String id;
+    private String id;
 
     /** Tells if the attribute is Human Readable or not. When not set, 
      * this flag is null. */
-    protected Boolean isHR;
+    private Boolean isHR;
+    
+    /** The computed hashcode. We don't want to compute it each time the hashcode() method
is called */
+    private volatile int h;
     
     //-------------------------------------------------------------------------
     // Helper methods
@@ -527,6 +533,9 @@ public class DefaultEntryAttribute imple
         if ( attributeType == null )
         {
             this.isHR = isHR;
+            
+            // Compute the hashCode
+            rehash();
         }
     }
 
@@ -571,6 +580,9 @@ public class DefaultEntryAttribute imple
                         this.id = newId;
                         this.upId = id;
                         
+                        // Compute the hashCode
+                        rehash();
+                        
                         return;
                     }
                 }
@@ -586,6 +598,9 @@ public class DefaultEntryAttribute imple
 
         this.id = newId;
         this.upId = id;
+        
+        // Compute the hashCode
+        rehash();
     }
 
     
@@ -693,6 +708,10 @@ public class DefaultEntryAttribute imple
             {
                 this.upId = upId;
                 this.id = id;
+                
+                // Compute the hashCode
+                rehash();
+
                 return;
             }    
             else
@@ -701,6 +720,10 @@ public class DefaultEntryAttribute imple
                 {
                     this.upId = upId;
                     this.id = id;
+                    
+                    // Compute the hashCode
+                    rehash();
+
                     return;
                 }
                 else
@@ -715,6 +738,10 @@ public class DefaultEntryAttribute imple
             this.attributeType = attributeType;
             this.upId = attributeType.getName();
             this.id = StringTools.trim( this.upId );
+            
+            // Compute the hashCode
+            rehash();
+
             return;
         }
 
@@ -723,6 +750,10 @@ public class DefaultEntryAttribute imple
             this.upId = upId;
             this.id = id;
             this.attributeType = attributeType;
+            
+            // Compute the hashCode
+            rehash();
+
             return;
         }
 
@@ -2123,6 +2154,9 @@ public class DefaultEntryAttribute imple
         {
             isHR = false;
         }
+        
+        // Compute the hashCode
+        rehash();
     }
     
     
@@ -2167,15 +2201,11 @@ public class DefaultEntryAttribute imple
     // Overloaded Object classes
     //-------------------------------------------------------------------------
     /**
-     * The hashCode is based on the id, the isHR flag and 
-     * on the internal values.
-     *  
-     * @see Object#hashCode()
-     * @return the instance's hashcode 
+     * A helper method to rehash the hashCode
      */
-    public int hashCode()
+    private void rehash()
     {
-        int h = 37;
+        h = 37;
         
         if ( isHR != null )
         {
@@ -2187,15 +2217,58 @@ public class DefaultEntryAttribute imple
             h = h*17 + id.hashCode();
         }
         
-        for ( Value<?> value:values )
+        /*
+        // We have to sort the values if we wnt to correctly compare two Attributes
+        if ( isHR )
         {
-            h = h*17 + value.hashCode();
+            SortedSet<String> sortedSet = new TreeSet<String>();
+
+            for ( Value<?> value:values )
+            {
+                sortedSet.add( (String)value.getNormalizedValueReference() );
+            }
+            
+            for ( String value:sortedSet )
+            {
+                h = h*17 + value.hashCode();
+            }
         }
+        else
+        {
+            SortedSet<byte[]> sortedSet = new TreeSet<byte[]>();
+            
+            for ( Value<?> value:values )
+            {
+                sortedSet.add( (byte[])value.getNormalizedValueReference() );
+            }
+            
+            for ( byte[] value:sortedSet )
+            {
+                h = h*17 + ArrayUtils.hashCode( value );
+            }
+        }
+        */
         
         if ( attributeType != null )
         {
             h = h*17 + attributeType.hashCode();
         }
+    }
+
+    
+    /**
+     * The hashCode is based on the id, the isHR flag and 
+     * on the internal values.
+     *  
+     * @see Object#hashCode()
+     * @return the instance's hashcode 
+     */
+    public int hashCode()
+    {
+        if ( h == 0 )
+        {
+            rehash();
+        }
         
         return h;
     }

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultServerEntry.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultServerEntry.java?rev=938447&r1=938446&r2=938447&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultServerEntry.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultServerEntry.java
Tue Apr 27 13:22:40 2010
@@ -537,27 +537,6 @@ public final class DefaultServerEntry ex
             }
         }
     }
-    
-    
-    /**
-    * Gets the hashCode of this ServerEntry.
-    *
-    * @see java.lang.Object#hashCode()
-     * @return the instance's hash code 
-     */
-    public int hashCode()
-    {
-        int result = 37;
-        
-        result = result*17 + dn.hashCode();
-        
-        for ( EntryAttribute attribute:attributes.values() )
-        {
-            result = result*17 + attribute.hashCode();
-        }
-
-        return result;
-    }
 
     
     /**

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/ServerEntry.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/ServerEntry.java?rev=938447&r1=938446&r2=938447&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/ServerEntry.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/ServerEntry.java
Tue Apr 27 13:22:40 2010
@@ -19,13 +19,6 @@
 package org.apache.directory.shared.ldap.entry;
 
 
-import java.util.List;
-import java.util.Set;
-
-import org.apache.directory.shared.ldap.exception.LdapException;
-import org.apache.directory.shared.ldap.schema.AttributeType;
-
-
 /**
  * A server side entry which is schema aware.
  *

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/StringValue.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/StringValue.java?rev=938447&r1=938446&r2=938447&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/StringValue.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/StringValue.java
Tue Apr 27 13:22:40 2010
@@ -51,8 +51,7 @@ public class StringValue extends Abstrac
     private static final long serialVersionUID = 2L;
     
     /** logger for reporting errors that might not be handled properly upstream */
-    protected static final Logger LOG = LoggerFactory.getLogger( StringValue.class );
-
+    private static final Logger LOG = LoggerFactory.getLogger( StringValue.class );
 
     // -----------------------------------------------------------------------
     // Constructors
@@ -322,40 +321,41 @@ public class StringValue extends Abstrac
      */
     public int hashCode()
     {
-        // return zero if the value is null so only one null value can be
-        // stored in an attribute - the binary version does the same 
-        if ( isNull() )
+        if ( h == 0 )
         {
-            if ( attributeType != null )
+            // return zero if the value is null so only one null value can be
+            // stored in an attribute - the binary version does the same 
+            if ( isNull() )
             {
-                // return the OID hashcode if the value is null. 
-                return attributeType.getOid().hashCode();
+                if ( attributeType != null )
+                {
+                    // return the OID hashcode if the value is null. 
+                    return attributeType.getOid().hashCode();
+                }
+                
+                return 0;
             }
+    
+            // If the normalized value is null, will default to wrapped
+            // which cannot be null at this point.
+            // If the normalized value is null, will default to wrapped
+            // which cannot be null at this point.
+            String normalized = getNormalizedValue();
             
-            return 0;
-        }
-
-        // If the normalized value is null, will default to wrapped
-        // which cannot be null at this point.
-        // If the normalized value is null, will default to wrapped
-        // which cannot be null at this point.
-        int h = 0;
-
-        String normalized = getNormalizedValue();
-        
-        if ( normalized != null )
-        {
-            h = normalized.hashCode();
-        }
-        else
-        {
-            h = 17;
-        }
-        
-        // Add the OID hashcode if we have an AttributeType
-        if ( attributeType != null )
-        {
-            h = h*37 + attributeType.getOid().hashCode();
+            if ( normalized != null )
+            {
+                h = normalized.hashCode();
+            }
+            else
+            {
+                h = 17;
+            }
+            
+            // Add the OID hashcode if we have an AttributeType
+            if ( attributeType != null )
+            {
+                h = h*37 + attributeType.getOid().hashCode();
+            }
         }
         
         return h;
@@ -515,6 +515,8 @@ public class StringValue extends Abstrac
                 normalizedValue = in.readUTF();
             }
         }
+        
+        h = 0;
     }
 
     

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntry.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntry.java?rev=938447&r1=938446&r2=938447&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntry.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntry.java
Tue Apr 27 13:22:40 2010
@@ -30,8 +30,6 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.SortedMap;
-import java.util.TreeMap;
 
 import org.apache.directory.shared.ldap.NotImplementedException;
 import org.apache.directory.shared.ldap.exception.LdapException;
@@ -78,6 +76,9 @@ public class DefaultClientEntry implemen
     /** The SchemaManager */
     protected SchemaManager schemaManager;
 
+    /** The computed hashcode. We don't want to compute it each time the hashcode() method
is called */
+    private volatile int h;
+
     
     //-------------------------------------------------------------------------
     // Constructors
@@ -2112,6 +2113,9 @@ public class DefaultClientEntry implemen
     public void setDn( DN dn )
     {
         this.dn = dn;
+        
+        // Resash the object
+        rehash();
     }
     
     
@@ -2264,18 +2268,13 @@ public class DefaultClientEntry implemen
     }
     
     
-    /**
-     * Get the hash code of this ClientEntry.
-     *
-     * @see java.lang.Object#hashCode()
-     * @return the instance's hash code 
-     */
-    public int hashCode()
+    private void rehash()
     {
-        int result = 37;
-        
-        result = result*17 + dn.hashCode();
+        h = 37;
+        h = h*17 + dn.hashCode();
         
+        /*
+        // We have to sort the Attributes if we want to compare two entries
         SortedMap<String, EntryAttribute> sortedMap = new TreeMap<String, EntryAttribute>();
         
         for ( String id:attributes.keySet() )
@@ -2285,10 +2284,26 @@ public class DefaultClientEntry implemen
         
         for ( String id:sortedMap.keySet() )
         {
-            result = result*17 + sortedMap.get( id ).hashCode();
+            h = h*17 + sortedMap.get( id ).hashCode();
+        }
+        */
+    }
+    
+    /**
+     * Get the hash code of this ClientEntry. The Attributes will be sorted
+     * before the comparison can be done.
+     *
+     * @see java.lang.Object#hashCode()
+     * @return the instance's hash code 
+     */
+    public int hashCode()
+    {
+        if ( h == 0 )
+        {
+            rehash();
         }
         
-        return result;
+        return h;
     }
 
     

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/AbstractSchemaObject.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/AbstractSchemaObject.java?rev=938447&r1=938446&r2=938447&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/AbstractSchemaObject.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/AbstractSchemaObject.java
Tue Apr 27 13:22:40 2010
@@ -109,7 +109,7 @@ public abstract class AbstractSchemaObje
     /** A locked to avoid modifications when set to true */
     protected volatile boolean locked;
     
-    /** The hashcoe for this schemaObject */
+    /** The hashcode for this schemaObject */
     private int h;
 
 



Mime
View raw message