You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2008/03/16 09:20:15 UTC

svn commit: r637562 - in /directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src: main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/ test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/

Author: akarasulu
Date: Sun Mar 16 01:20:14 2008
New Revision: 637562

URL: http://svn.apache.org/viewvc?rev=637562&view=rev
Log:
more test cases and better code coverage while making BTreeRedirect use 9 bytes with magic number byte at index 0

Modified:
    directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/BTreeRedirectMarshaller.java
    directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java
    directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/BTreeRedirectMarshallerTest.java

Modified: directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/BTreeRedirectMarshaller.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/BTreeRedirectMarshaller.java?rev=637562&r1=637561&r2=637562&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/BTreeRedirectMarshaller.java (original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/BTreeRedirectMarshaller.java Sun Mar 16 01:20:14 2008
@@ -29,15 +29,16 @@
 /**
  * Serializes and deserializes a BTreeRedirect object to and from a byte[]
  * representation.  The serialized form is a fixed size byte array of length
- * 16.  The first 8 bytes are the ascii values for the String 'redirect' and
- * the last 8 bytes encode the record identifier as a long for the BTree.
+ * 9.  The first byte contains the magic number of value 1 for this kind of
+ * object and the last 8 bytes encode the record identifier as a long for
+ * the BTree.
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$
  */public class BTreeRedirectMarshaller implements Marshaller<BTreeRedirect>
 {
-    /** fixed byte array size of 16 for serialized form */
-    static final int SIZE = 16;
+    /** fixed byte array size of 9 for serialized form */
+    static final int SIZE = 9;
     /** a reusable instance of this Marshaller */
     public static final BTreeRedirectMarshaller INSTANCE = new BTreeRedirectMarshaller();
 
@@ -48,23 +49,16 @@
     {
         byte[] bites = new byte[SIZE];
 
-        bites[0] = 'r';
-        bites[1] = 'e';
-        bites[2] = 'd';
-        bites[3] = 'i';
-        bites[4] = 'r';
-        bites[5] = 'e';
-        bites[6] = 'c';
-        bites[7] = 't';
-
-        bites[8] =  ( byte ) ( redirect.recId >> 56 );
-        bites[9] =  ( byte ) ( redirect.recId >> 48 );
-        bites[10] = ( byte ) ( redirect.recId >> 40 );
-        bites[11] = ( byte ) ( redirect.recId >> 32 );
-        bites[12] = ( byte ) ( redirect.recId >> 24 );
-        bites[13] = ( byte ) ( redirect.recId >> 16 );
-        bites[14] = ( byte ) ( redirect.recId >> 8 );
-        bites[15] = ( byte ) redirect.recId;
+        bites[0] = 1;
+
+        bites[1] = ( byte ) ( redirect.recId >> 56 );
+        bites[2] = ( byte ) ( redirect.recId >> 48 );
+        bites[3] = ( byte ) ( redirect.recId >> 40 );
+        bites[4] = ( byte ) ( redirect.recId >> 32 );
+        bites[5] = ( byte ) ( redirect.recId >> 24 );
+        bites[6] = ( byte ) ( redirect.recId >> 16 );
+        bites[7] = ( byte ) ( redirect.recId >> 8 );
+        bites[8] = ( byte ) redirect.recId;
 
         return bites;
     }
@@ -75,65 +69,48 @@
      */
     public final BTreeRedirect deserialize( byte[] bites ) throws IOException
     {
-        if ( bites.length != SIZE ||
-                  bites[0] != 'r' ||
-                  bites[1] != 'e' ||
-                  bites[2] != 'd' ||
-                  bites[3] != 'i' ||
-                  bites[4] != 'r' ||
-                  bites[5] != 'e' ||
-                  bites[6] != 'c' ||
-                  bites[7] != 't' )
+        if ( bites == null || bites.length != SIZE || bites[0] != 1 )
         {
-            throw new IOException( "Not a serialized BTreeRedirect object: "
-                    + new String( Hex.encodeHex( bites ) ) );
+            if ( bites != null )
+            {
+                throw new IOException( "Not a serialized BTreeRedirect object: "
+                        + new String( Hex.encodeHex( bites ) ) );
+            }
+            else
+            {
+                throw new IOException( "Not a serialized BTreeRedirect object: byte array is null." );
+            }
         }
 
         long recId;
-        recId = bites[8] + ( ( bites[8] < 0 ) ? 256 : 0 );
+        recId = bites[1] + ( ( bites[1] < 0 ) ? 256 : 0 );
         recId <<= 8;
-        recId += bites[9] + ( ( bites[9] < 0 ) ? 256 : 0 );
+        recId += bites[2] + ( ( bites[2] < 0 ) ? 256 : 0 );
         recId <<= 8;
-        recId += bites[10] + ( ( bites[10] < 0 ) ? 256 : 0 );
+        recId += bites[3] + ( ( bites[3] < 0 ) ? 256 : 0 );
         recId <<= 8;
-        recId += bites[11] + ( ( bites[11] < 0 ) ? 256 : 0 );
+        recId += bites[4] + ( ( bites[4] < 0 ) ? 256 : 0 );
         recId <<= 8;
-        recId += bites[12] + ( ( bites[12] < 0 ) ? 256 : 0 );
+        recId += bites[5] + ( ( bites[5] < 0 ) ? 256 : 0 );
         recId <<= 8;
-        recId += bites[13] + ( ( bites[13] < 0 ) ? 256 : 0 );
+        recId += bites[6] + ( ( bites[6] < 0 ) ? 256 : 0 );
         recId <<= 8;
-        recId += bites[14] + ( ( bites[14] < 0 ) ? 256 : 0 );
+        recId += bites[7] + ( ( bites[7] < 0 ) ? 256 : 0 );
         recId <<= 8;
-        recId += bites[15] + ( ( bites[15] < 0 ) ? 256 : 0 );
+        recId += bites[8] + ( ( bites[8] < 0 ) ? 256 : 0 );
 
         return new BTreeRedirect( recId );
     }
 
 
     /**
-     * Checks to see if a byte[] does not contain a redirect.  It's faster
-     * to check invalid bytes then to check for validity.
+     * Checks to see if a byte[] contains a redirect.
      *
-     * @param bites the bites to check for validity
-     * @return true if the bites do not contain a serialized BTreeRedirect,
-     * false if they do
+     * @param bites the bites to check for a redirect
+     * @return true if bites contain BTreeRedirect, false otherwise
      */
-    public static boolean isNotRedirect( byte[] bites )
+    public static boolean isRedirect( byte[] bites )
     {
-        if ( bites == null )
-        {
-            return true;
-        }
-
-        // faster to check if invalid than valid
-        return bites.length != SIZE ||
-                bites[0] != 'r' ||
-                bites[1] != 'e' ||
-                bites[2] != 'd' ||
-                bites[3] != 'i' ||
-                bites[4] != 'r' ||
-                bites[5] != 'e' ||
-                bites[6] != 'c' ||
-                bites[7] != 't';
+        return bites != null && bites.length == SIZE && bites[0] == 1;
     }
 }

Modified: directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java?rev=637562&r1=637561&r2=637562&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java (original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java Sun Mar 16 01:20:14 2008
@@ -673,8 +673,6 @@
                 return value;
             }
             
-            boolean addSuccessful = true;
-            
             if ( set.getSize() > numDupLimit )
             {
                 BTree tree = convertToBTree( set );
@@ -687,13 +685,8 @@
                 replaced = ( V ) bt.insert( key, marshaller.serialize( set ), true );
             }
 
-            // TODO this is a problem here since addSuccessful is not being set
-            if ( addSuccessful )
-            {
-                count++;
-                return replaced;
-            }
-            return null;
+            count++;
+            return replaced;
         }
         
         BTree tree = getBTree( values.getBTreeRedirect() );
@@ -801,7 +794,7 @@
 
         byte[] serialized = ( byte[] ) returned;
 
-        if ( BTreeRedirectMarshaller.isNotRedirect( serialized ) )
+        if ( ! BTreeRedirectMarshaller.isRedirect( serialized ) )
         {
             //noinspection unchecked
             AvlTree<V> set = marshaller.deserialize( serialized );
@@ -898,7 +891,7 @@
             return new DupsContainer<V>( new AvlTree<V>( valueComparator ) );
         }
 
-        if ( BTreeRedirectMarshaller.isNotRedirect( serialized ) )
+        if ( ! BTreeRedirectMarshaller.isRedirect( serialized ) )
         {
             return new DupsContainer<V>( marshaller.deserialize( serialized ) );
         }

Modified: directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/BTreeRedirectMarshallerTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/BTreeRedirectMarshallerTest.java?rev=637562&r1=637561&r2=637562&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/BTreeRedirectMarshallerTest.java (original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/BTreeRedirectMarshallerTest.java Sun Mar 16 01:20:14 2008
@@ -45,14 +45,7 @@
     @Before
     public void setup()
     {
-        bites[0] = 'r';
-        bites[1] = 'e';
-        bites[2] = 'd';
-        bites[3] = 'i';
-        bites[4] = 'r';
-        bites[5] = 'e';
-        bites[6] = 'c';
-        bites[7] = 't';
+        bites[0] = 1;
 
         for ( int ii = 8; ii < BTreeRedirectMarshaller.SIZE; ii++ )
         {
@@ -72,7 +65,7 @@
     @Test
     public void testOne() throws IOException
     {
-        bites[15] = 1;
+        bites[8] = 1;
         assertEquals( 1, marshaller.deserialize( bites ).getRecId() );
         assertTrue( ArrayUtils.isEquals( bites, marshaller.serialize( new BTreeRedirect( 1 ) ) ) );
     }
@@ -81,7 +74,7 @@
     @Test
     public void testNegativeOne() throws IOException
     {
-        for ( int ii = 8; ii < BTreeRedirectMarshaller.SIZE; ii++ )
+        for ( int ii = 1; ii < BTreeRedirectMarshaller.SIZE; ii++ )
         {
             bites[ii] =  ( byte ) 0xFF;
         }
@@ -94,7 +87,7 @@
     @Test
     public void testLongMinValue() throws IOException
     {
-        bites[8] = ( byte ) 0x80;
+        bites[1] = ( byte ) 0x80;
         assertEquals( Long.MIN_VALUE, marshaller.deserialize( bites ).getRecId() );
         assertTrue( ArrayUtils.isEquals( bites, marshaller.serialize( new BTreeRedirect( Long.MIN_VALUE ) ) ) );
     }
@@ -103,9 +96,9 @@
     @Test
     public void testLongMaxValue() throws IOException
     {
-        bites[8] = ( byte ) 0x7F;
+        bites[1] = ( byte ) 0x7F;
 
-        for ( int ii = 9; ii < BTreeRedirectMarshaller.SIZE; ii++ )
+        for ( int ii = 2; ii < BTreeRedirectMarshaller.SIZE; ii++ )
         {
             bites[ii] =  ( byte ) 0xFF;
         }
@@ -124,6 +117,33 @@
             long orig = random.nextLong();
             bites = marshaller.serialize( new BTreeRedirect( orig ) );
             assertEquals( orig, marshaller.deserialize( bites ).getRecId() );
+        }
+    }
+
+
+    @Test
+    public void testMiscellaneous()
+    {
+        assertNotNull( new BTreeRedirect( 1 ).toString() );
+        assertFalse( BTreeRedirectMarshaller.isRedirect( null ) );
+
+        try
+        {
+            marshaller.deserialize( null );
+            fail( "Should not get here." );
+        }
+        catch ( IOException e )
+        {
+        }
+
+
+        try
+        {
+            marshaller.deserialize( "bogus".getBytes() );
+            fail( "Should not get here." );
+        }
+        catch ( IOException e )
+        {
         }
     }
 }