directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r423669 - 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 Thu, 20 Jul 2006 00:04:26 GMT
Author: akarasulu
Date: Wed Jul 19 17:04:25 2006
New Revision: 423669

URL: http://svn.apache.org/viewvc?rev=423669&view=rev
Log:
added Attributes serialization code along with some test cases

Added:
    directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributesSerializerTest.java
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/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributesSerializer.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=423669&r1=423668&r2=423669&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 17:04:25 2006
@@ -42,12 +42,12 @@
 {
     private static final long serialVersionUID = -3756830073760754086L;
   
-    private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
+    static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
 
     /** value for type parameter for string (non-binary) attributes */
-    private static final byte STRING_TYPE = 0x00;
+    static final byte STRING_TYPE = 0x00;
     /** value for type parameter for byte array (binary) attributes */
-    private static final byte BYTE_ARRAY_TYPE = 0x01;
+    static final byte BYTE_ARRAY_TYPE = 0x01;
     
 
     // -----------------------------------------------------------------------
@@ -304,7 +304,6 @@
             ioe.initCause( e );
             throw ioe;
         }
-        
         
         return buf;
     }

Modified: directory/branches/apacheds/optimization/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributesSerializer.java
URL: http://svn.apache.org/viewvc/directory/branches/apacheds/optimization/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributesSerializer.java?rev=423669&r1=423668&r2=423669&view=diff
==============================================================================
--- directory/branches/apacheds/optimization/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributesSerializer.java
(original)
+++ directory/branches/apacheds/optimization/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributesSerializer.java
Wed Jul 19 17:04:25 2006
@@ -18,10 +18,20 @@
  */
 package org.apache.directory.server.core.partition.impl.btree.jdbm;
 
+
 import java.io.IOException;
 
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+
+import org.apache.directory.shared.ldap.message.LockableAttributeImpl;
+import org.apache.directory.shared.ldap.message.LockableAttributesImpl;
+
 import jdbm.helper.Serializer;
 
+
 /**
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
@@ -30,23 +40,151 @@
 public class AttributesSerializer implements Serializer
 {
     private static final long serialVersionUID = -3756830073760754086L;
+    private static final byte SEPARATOR = -1;
 
 
-    /* (non-Javadoc)
+    /**
      * @see jdbm.helper.Serializer#deserialize(byte[])
      */
-    public Object deserialize( byte[] arg0 ) throws IOException
+    public Object deserialize( byte[] buf ) throws IOException
     {
-        return null;
+        if ( buf.length == 0 )
+        {
+            return new LockableAttributesImpl();
+        }
+
+        int pos = 0;
+        LockableAttributesImpl attrs = new LockableAttributesImpl();
+        while ( pos < buf.length )
+        {
+            String id = AttributeSerializer.readString( buf, pos );
+            LockableAttributeImpl attr = new LockableAttributeImpl( id );
+            pos += ( id.length() << 1 ) + 4;
+
+            // read the type of the objects stored in this attribute
+            if ( buf[pos] == AttributeSerializer.STRING_TYPE )
+            {
+                pos++;
+                while ( pos < buf.length && buf[pos] != SEPARATOR )
+                {
+                    String value = AttributeSerializer.readString( buf, pos );
+                    pos += ( value.length() << 1 ) + 4;
+                    attr.add( value );
+                }
+            }
+            else
+            {
+                pos++;
+                while ( pos < buf.length && buf[pos] != SEPARATOR )
+                {
+                    byte[] value = AttributeSerializer.readBytes( buf, pos );
+                    pos += value.length + 4;
+                    attr.add( value );
+                }
+            }
+
+            pos++; // skip the separator
+            attrs.put( attr );
+        }
+
+        return attrs;
     }
 
 
-    /* (non-Javadoc)
+    /**
      * @see jdbm.helper.Serializer#serialize(java.lang.Object)
      */
-    public byte[] serialize( Object arg0 ) throws IOException
+    public byte[] serialize( Object attrsObj ) throws IOException
     {
-        return null;
+        Attributes attrs = ( Attributes ) attrsObj;
+
+        // calculate the size of the entire byte[] and allocate
+        byte[] buf = new byte[calculateSize( attrs )];
+        int pos = 0;
+        try
+        {
+            for ( NamingEnumeration ii = attrs.getAll(); ii.hasMore(); /**/)
+            {
+                // get an attribute at a time
+                Attribute attr = ( Attribute ) ii.next();
+
+                // write the length of the id and it's value
+                pos = AttributeSerializer.write( buf, attr.getID(), pos );
+
+                // write the type or is-binary field
+                Object first = attr.get();
+                if ( first instanceof String )
+                {
+                    buf[pos] = AttributeSerializer.STRING_TYPE;
+                    pos++;
+
+                    // write out each value to the buffer whatever type it may be
+                    for ( NamingEnumeration jj = attr.getAll(); jj.hasMore(); /**/)
+                    {
+                        String value = ( String ) jj.next();
+                        pos = AttributeSerializer.write( buf, value, pos );
+                    }
+                }
+                else
+                {
+                    buf[pos] = AttributeSerializer.BYTE_ARRAY_TYPE;
+                    pos++;
+
+                    // write out each value to the buffer whatever type it may be
+                    for ( NamingEnumeration jj = attr.getAll(); jj.hasMore(); /**/)
+                    {
+                        byte[] value = ( byte[] ) jj.next();
+                        pos = AttributeSerializer.write( buf, value, pos );
+                    }
+                }
+                
+                if ( ii.hasMore() )
+                {
+                    buf[pos] = SEPARATOR;
+                    pos++;
+                }
+            }
+        }
+        catch ( NamingException e )
+        {
+            IOException ioe = new IOException( "Failed while accesssing attributes and/or
their values." );
+            ioe.initCause( e );
+            throw ioe;
+        }
+
+        return buf;
     }
 
+
+    public int calculateSize( Attributes attrs ) throws IOException
+    {
+        int size = 0;
+
+        try
+        {
+            for ( NamingEnumeration ii = attrs.getAll(); ii.hasMore(); /**/)
+            {
+                Attribute attr = ( Attribute ) ii.next();
+
+                if ( ii.hasMore() )
+                {
+                    // augment by attribute size and 1 for the separator
+                    size += AttributeSerializer.calculateSize( attr ) + 1;
+                }
+                else
+                {
+                    // augment by attribute size only since there are no more attributes
left
+                    size += AttributeSerializer.calculateSize( attr );
+                }
+            }
+        }
+        catch ( NamingException e )
+        {
+            IOException ioe = new IOException( "Failed while accesssing attributes." );
+            ioe.initCause( e );
+            throw ioe;
+        }
+
+        return size;
+    }
 }

Added: directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributesSerializerTest.java
URL: http://svn.apache.org/viewvc/directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributesSerializerTest.java?rev=423669&view=auto
==============================================================================
--- directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributesSerializerTest.java
(added)
+++ directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/AttributesSerializerTest.java
Wed Jul 19 17:04:25 2006
@@ -0,0 +1,94 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 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.
+ *
+ */
+package org.apache.directory.server.core.partition.impl.btree.jdbm;
+
+
+import java.io.IOException;
+
+import org.apache.directory.shared.ldap.message.LockableAttributeImpl;
+import org.apache.directory.shared.ldap.message.LockableAttributesImpl;
+import org.apache.directory.shared.ldap.util.ArrayUtils;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Tests the {@link AttributeSerializer}.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class AttributesSerializerTest extends TestCase
+{
+    public void testFullCycle() throws IOException
+    {
+        LockableAttributesImpl attrs = new LockableAttributesImpl();
+        LockableAttributeImpl attr0 = new LockableAttributeImpl( "attr0" );
+        attr0.add( "value0" );
+        attr0.add( "val1" );
+        attr0.add( "anything over here!" );
+        
+        LockableAttributeImpl attr1 = new LockableAttributeImpl( "attr1" );
+        byte[] ba0 = new byte[2];
+        ba0[0] = 7;
+        ba0[1] = 23;
+        attr1.add( ba0 );
+        byte[] ba1 = new byte[3];
+        ba1[0] = 34;
+        ba1[1] = 111;
+        ba1[2] = 67;
+        attr1.add( ba1 );
+        
+        attrs.put( attr0 );
+        attrs.put( attr1 );
+        AttributesSerializer serializer = new AttributesSerializer();
+        byte[] buf = serializer.serialize( attrs );
+        LockableAttributesImpl deserialized = ( LockableAttributesImpl ) serializer.deserialize(
buf );
+
+        LockableAttributeImpl attrDeserialized0 = ( LockableAttributeImpl ) deserialized.get(
"attr0" );
+        assertEquals( "value0", attrDeserialized0.get() );
+        assertEquals( "val1", attrDeserialized0.get( 1 ) );
+        assertEquals( "anything over here!", attrDeserialized0.get( 2 ) );
+        
+        LockableAttributeImpl attrDeserialized1 = ( LockableAttributeImpl ) deserialized.get(
"attr1" );
+        ArrayUtils.isEquals( ba0, ( byte[] ) attrDeserialized1.get() );
+        ArrayUtils.isEquals( ba1, ( byte[] ) attrDeserialized1.get( 1 ) );
+    }
+    
+    
+//    public void doSerializerSpeedTest() throws IOException
+//    {
+//        final int limit = 1000000;
+//        long start = System.currentTimeMillis();
+//        for ( int ii = 0; ii < limit; ii++ )
+//        {
+//            LockableAttributeImpl attr = new LockableAttributeImpl( "testing" );
+//            AttributeSerializer serializer = new AttributeSerializer();
+//            attr.add( "value0" );
+//            attr.add( "val1" );
+//            attr.add( "anything over here!" );
+//            
+//            byte[] serialized = serializer.serialize( attr );
+//            serializer.deserialize( serialized );
+//        }
+//        
+//        System.out.println( limit + " attributes with 3 values each were serialized and
deserialized in " 
+//            + ( System.currentTimeMillis() - start ) + " (ms)" );
+//    }
+}



Mime
View raw message