directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r423593 - in /directory/branches/apacheds/optimization/core/src: main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/ test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/
Date Wed, 19 Jul 2006 20:35:37 GMT
Author: akarasulu
Date: Wed Jul 19 13:35:37 2006
New Revision: 423593

URL: http://svn.apache.org/viewvc?rev=423593&view=rev
Log:
converting some methods to final static and adding some static methods to write and read from
a buffer offset

Modified:
    directory/branches/apacheds/optimization/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributeSerializer.java
    directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributeSerializerTest.java

Modified: directory/branches/apacheds/optimization/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributeSerializer.java
URL: http://svn.apache.org/viewvc/directory/branches/apacheds/optimization/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributeSerializer.java?rev=423593&r1=423592&r2=423593&view=diff
==============================================================================
--- directory/branches/apacheds/optimization/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributeSerializer.java
(original)
+++ directory/branches/apacheds/optimization/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributeSerializer.java
Wed Jul 19 13:35:37 2006
@@ -60,7 +60,7 @@
      * 
      * @see jdbm.helper.Serializer#deserialize(byte[])
      */
-    public Object deserialize( byte[] buf ) throws IOException
+    public final Object deserialize( byte[] buf ) throws IOException
     {
         String id = readString( buf );
         LockableAttributeImpl attr = new LockableAttributeImpl( id );
@@ -93,13 +93,73 @@
 
     
     /**
+     * Deserializes an attribute from the custom serialization structure.
+     * 
+     * @see jdbm.helper.Serializer#deserialize(byte[])
+     */
+    public static final DeserializedAttribute deserialize( byte[] buf, int offset ) throws
IOException
+    {
+        final String id = readString( buf, offset );
+        final LockableAttributeImpl attr = new LockableAttributeImpl( id );
+        int pos = ( id.length() << 1 ) + 4 + offset;
+        
+        // read the type of the objects stored in this attribute
+        if ( buf[pos] == STRING_TYPE )
+        {
+            pos++;
+            while ( pos < buf.length )
+            {
+                String value = readString( buf, pos );
+                pos += ( value.length() << 1 ) + 4;
+                attr.add( value );
+            }
+        }
+        else
+        {
+            pos++;
+            while ( pos < buf.length )
+            {
+                byte[] value = readBytes( buf, pos );
+                pos += value.length + 4;
+                attr.add( value );
+            }
+        }
+        
+        return new DeserializedAttribute( attr, pos );
+    }
+
+    
+    final static class DeserializedAttribute 
+    {
+        private final int pos;
+        private final Attribute attr;
+        
+        private DeserializedAttribute( Attribute attr, int pos )
+        {
+            this.pos = pos;
+            this.attr = attr;
+        }
+
+        public int getPos()
+        {
+            return pos;
+        }
+
+        public Attribute getAttr()
+        {
+            return attr;
+        }
+    }
+    
+    
+    /**
      * Reads a String and it's length bytes from a buffer starting at 
      * position 0.
      * 
      * @param buf the buffer to read the length and character bytes from
      * @return the String contained at the start of the buffer
      */
-    String readString( byte[] buf )
+    static final String readString( byte[] buf )
     {
         int length = getLength( buf );
         
@@ -132,7 +192,7 @@
      * @param offset the offset into the buffer to start reading from
      * @return the String contained at the offset in the buffer
      */
-    String readString( byte[] buf, int offset )
+    static final String readString( byte[] buf, int offset )
     {
         int length = getLength( buf, offset );
         
@@ -165,7 +225,7 @@
      * @param offset the offset to start reading from starting with 4-byte length
      * @return the byte array contained in the buffer
      */
-    byte[] readBytes( byte[] buf, int offset )
+    static final byte[] readBytes( byte[] buf, int offset )
     {
         int length = getLength( buf, offset );
         
@@ -250,7 +310,7 @@
     }
     
     
-    int calculateSize( Attribute attr ) throws IOException
+    static final int calculateSize( Attribute attr ) throws IOException
     {
         int size = 4; // start with first length for attribute id
         size += attr.getID().length() << 1; // the length of id * 2 added
@@ -284,19 +344,19 @@
     }
     
     
-    byte[] getLengthBytes( String str )
+    static final byte[] getLengthBytes( String str )
     {
         return getLengthBytes( str.length() << 1 );
     }
     
     
-    byte[] getLengthBytes( byte[] bites )
+    static final byte[] getLengthBytes( byte[] bites )
     {
         return getLengthBytes( bites.length );
     }
 
     
-    byte[] getLengthBytes( int length )
+    static final byte[] getLengthBytes( int length )
     {
         byte[] lengthBytes = new byte[4];
 
@@ -309,7 +369,7 @@
     }
 
     
-    int getLength( byte[] bites )
+    static final int getLength( byte[] bites )
     {
         int length  = bites[0] << 24 & 0xFF000000;
         length     |= bites[1] << 16 & 0x00FF0000;
@@ -320,7 +380,7 @@
     }
 
     
-    int getLength( byte[] bites, int offset )
+    static final int getLength( byte[] bites, int offset )
     {
         int length  = bites[offset]   << 24 & 0xFF000000;
         length     |= bites[offset+1] << 16 & 0x00FF0000;
@@ -331,35 +391,35 @@
     }
 
     
-    int write( byte[] buf, String value )
+    static final int write( byte[] buf, String value )
     {
         int pos = writeLengthBytes( buf, value.length() << 1 );
         return writeValueBytes( buf, value, pos );
     }
     
 
-    int write( byte[] buf, byte[] value )
+    static final int write( byte[] buf, byte[] value )
     {
         int pos = writeLengthBytes( buf, value.length );
         return writeValueBytes( buf, value, pos );
     }
     
 
-    int write( byte[] buf, String value, int offset )
+    static final int write( byte[] buf, String value, int offset )
     {
         offset = writeLengthBytes( buf, value.length() << 1, offset );
         return writeValueBytes( buf, value, offset );
     }
     
 
-    int write( byte[] buf, byte[] value, int offset )
+    static final int write( byte[] buf, byte[] value, int offset )
     {
         offset = writeLengthBytes( buf, value.length, offset );
         return writeValueBytes( buf, value, offset );
     }
     
 
-    int writeValueBytes( byte[] buf, String value )
+    static final int writeValueBytes( byte[] buf, String value )
     {
         if ( ( ( String ) value ).length() == 0 )
         {
@@ -377,7 +437,7 @@
     }
 
     
-    int writeValueBytes( byte[] buf, String value, int offset )
+    static final int writeValueBytes( byte[] buf, String value, int offset )
     {
         if ( ( ( String ) value ).length() == 0 )
         {
@@ -395,7 +455,7 @@
     }
 
     
-    int writeValueBytes( byte[] buf, byte[] value, int offset )
+    static final int writeValueBytes( byte[] buf, byte[] value, int offset )
     {
         if ( value.length == 0 )
         {
@@ -407,7 +467,7 @@
     }
 
     
-    int writeLengthBytes( byte[] buf, int length )
+    static final int writeLengthBytes( byte[] buf, int length )
     {
         buf[0] = ( byte ) ( length >> 24 & 0x000000FF );
         buf[1] = ( byte ) ( length >> 16 & 0x000000FF );
@@ -417,7 +477,7 @@
     }
     
     
-    int writeLengthBytes( byte[] buf, int length, int offset )
+    static final int writeLengthBytes( byte[] buf, int length, int offset )
     {
         buf[0+offset] = ( byte ) ( length >> 24 & 0x000000FF );
         buf[1+offset] = ( byte ) ( length >> 16 & 0x000000FF );

Modified: directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributeSerializerTest.java
URL: http://svn.apache.org/viewvc/directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributeSerializerTest.java?rev=423593&r1=423592&r2=423593&view=diff
==============================================================================
--- directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributeSerializerTest.java
(original)
+++ directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributeSerializerTest.java
Wed Jul 19 13:35:37 2006
@@ -40,37 +40,35 @@
 {
     public void testGetLengthBytes()
     {
-        AttributeSerializer serializer = new AttributeSerializer();
-        
         // test first 10 values
         for ( int ii = 0; ii < 10; ii++ )
         {
-            byte[] bites = serializer.getLengthBytes( ii );
-            int deserialized = serializer.getLength( bites );
+            byte[] bites = AttributeSerializer.getLengthBytes( ii );
+            int deserialized = AttributeSerializer.getLength( bites );
             assertEquals( ii, deserialized );
         }
 
         // test first byte boundry
         for ( int ii = 250; ii < 260; ii++ )
         {
-            byte[] bites = serializer.getLengthBytes( ii );
-            int deserialized = serializer.getLength( bites );
+            byte[] bites = AttributeSerializer.getLengthBytes( ii );
+            int deserialized = AttributeSerializer.getLength( bites );
             assertEquals( ii, deserialized );
         }
 
         // test 2nd byte boundry
         for ( int ii = 65530; ii < 65540; ii++ )
         {
-            byte[] bites = serializer.getLengthBytes( ii );
-            int deserialized = serializer.getLength( bites );
+            byte[] bites = AttributeSerializer.getLengthBytes( ii );
+            int deserialized = AttributeSerializer.getLength( bites );
             assertEquals( ii, deserialized );
         }
 
         // test 3rd byte boundry
         for ( int ii = 16777210; ii < 16777220; ii++ )
         {
-            byte[] bites = serializer.getLengthBytes( ii );
-            int deserialized = serializer.getLength( bites );
+            byte[] bites = AttributeSerializer.getLengthBytes( ii );
+            int deserialized = AttributeSerializer.getLength( bites );
             assertEquals( ii, deserialized );
         }
     }
@@ -78,10 +76,9 @@
     
     public void testWriteLengthBytes0()
     {
-        AttributeSerializer serializer = new AttributeSerializer();
         byte[] buf = new byte[7];
         
-        int pos = serializer.writeLengthBytes( buf, 23 );
+        int pos = AttributeSerializer.writeLengthBytes( buf, 23 );
         assertEquals( 4, pos );
         assertEquals( 0, buf[0] );
         assertEquals( 0, buf[1] );
@@ -91,7 +88,7 @@
         assertEquals( 0, buf[5] );
         assertEquals( 0, buf[6] );
         
-        pos = serializer.writeValueBytes( buf, "a", pos );
+        pos = AttributeSerializer.writeValueBytes( buf, "a", pos );
         assertEquals( 6, pos );
         assertEquals( 0, buf[4] );
         assertEquals( 97, buf[5] );
@@ -101,10 +98,9 @@
 
     public void testWriteValueBytes0()
     {
-        AttributeSerializer serializer = new AttributeSerializer();
         byte[] buf = new byte[20];
         
-        int pos = serializer.writeLengthBytes( buf, 23 );
+        int pos = AttributeSerializer.writeLengthBytes( buf, 23 );
         assertEquals( 4, pos );
         assertEquals( 0, buf[0] );
         assertEquals( 0, buf[1] );
@@ -114,7 +110,7 @@
         assertEquals( 0, buf[5] );
         assertEquals( 0, buf[6] );
         
-        pos = serializer.writeValueBytes( buf, "abc", pos );
+        pos = AttributeSerializer.writeValueBytes( buf, "abc", pos );
         assertEquals( 10,  pos );
         assertEquals( 0,  buf[4] );
         assertEquals( 97, buf[5] );
@@ -126,7 +122,7 @@
         assertEquals( 0,  buf[11] );
         assertEquals( 0,  buf[12] );
         
-        pos = serializer.write( buf, "def", pos );
+        pos = AttributeSerializer.write( buf, "def", pos );
         assertEquals( 20, pos );
         assertEquals( 0, buf[10] );
         assertEquals( 0, buf[11] );
@@ -144,11 +140,10 @@
     
     public void testReadString()
     {
-        AttributeSerializer serializer = new AttributeSerializer();
         byte[] buf = new byte[26];
         
         // let's write the length so we can read it
-        int pos = serializer.writeLengthBytes( buf, 6 );
+        int pos = AttributeSerializer.writeLengthBytes( buf, 6 );
         assertEquals( 4, pos );
         assertEquals( 0, buf[0] );
         assertEquals( 0, buf[1] );
@@ -156,7 +151,7 @@
         assertEquals( 6, buf[3] );
         
         // let's write the value so we can read it
-        pos = serializer.writeValueBytes( buf, "abc", pos );
+        pos = AttributeSerializer.writeValueBytes( buf, "abc", pos );
         assertEquals( 10,  pos );
         assertEquals( 0,  buf[4] );
         assertEquals( 97, buf[5] );
@@ -166,7 +161,7 @@
         assertEquals( 99, buf[9] );
 
         // let's write another string as well
-        pos = serializer.write( buf, "defgh", pos );
+        pos = AttributeSerializer.write( buf, "defgh", pos );
         assertEquals( 24, pos );
         assertEquals( 0, buf[10] );
         assertEquals( 0, buf[11] );
@@ -187,7 +182,7 @@
         assertEquals( 0,   buf[25] );
         
         // now let's read "abc"
-        String s1 = serializer.readString( buf );
+        String s1 = AttributeSerializer.readString( buf );
         assertEquals( "abc", s1 );
     }
     
@@ -229,7 +224,7 @@
     }
     
     
-    public void doSerializerSpeedTest() throws IOException
+    public void testSerializerSpeedTest() throws IOException
     {
         final int limit = 1000000;
         long start = System.currentTimeMillis();



Mime
View raw message