directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1529080 - in /directory/mavibot/trunk/mavibot/src: main/java/org/apache/directory/mavibot/btree/managed/ test/java/org/apache/directory/mavibot/btree/managed/
Date Fri, 04 Oct 2013 05:45:55 GMT
Author: elecharny
Date: Fri Oct  4 05:45:54 2013
New Revision: 1529080

URL: http://svn.apache.org/r1529080
Log:
o Changed the key serialization so that we load KeyHolders instead of Keys

Modified:
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/AbstractPage.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeFactory.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/KeyHolder.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Leaf.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/RecordManager.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/ReadTest.java

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/AbstractPage.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/AbstractPage.java?rev=1529080&r1=1529079&r2=1529080&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/AbstractPage.java
(original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/AbstractPage.java
Fri Oct  4 05:45:54 2013
@@ -22,6 +22,7 @@ package org.apache.directory.mavibot.btr
 
 import java.io.IOException;
 import java.lang.reflect.Array;
+import java.nio.ByteBuffer;
 
 
 /**
@@ -288,7 +289,19 @@ import java.lang.reflect.Array;
      */
     /* No qualifier*/void setKey( int pos, K key )
     {
-        keys[pos] = new KeyHolder( key, null, btree.getKeySerializer() );
+        keys[pos] = new KeyHolder<K>( key, null, btree.getKeySerializer() );
+    }
+
+
+    /**
+     * Sets the key at a give position
+     * 
+     * @param pos The position in the keys array
+     * @param key the key to inject
+     */
+    /* No qualifier*/void setKey( int pos, ByteBuffer buffer, K key )
+    {
+        keys[pos] = new KeyHolder<K>( key, buffer, btree.getKeySerializer() );
     }
 
 

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeFactory.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeFactory.java?rev=1529080&r1=1529079&r2=1529080&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeFactory.java
(original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeFactory.java
Fri Oct  4 05:45:54 2013
@@ -21,6 +21,7 @@ package org.apache.directory.mavibot.btr
 
 
 import java.io.IOException;
+import java.nio.ByteBuffer;
 import java.util.LinkedList;
 
 import org.apache.directory.mavibot.btree.serializer.ElementSerializer;
@@ -224,6 +225,17 @@ public class BTreeFactory
 
 
     /**
+     * Set the key at a give position
+     * @param pos The position in the keys array
+     * @param key the key to inject
+     */
+    public static <K, V> void setKey( Page<K, V> page, int pos, ByteBuffer buffer,
K key )
+    {
+        ( ( AbstractPage<K, V> ) page ).setKey( pos, buffer, key );
+    }
+
+
+    /**
      * Set the value at a give position
      * @param pos The position in the values array
      * @param value the value to inject

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/KeyHolder.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/KeyHolder.java?rev=1529080&r1=1529079&r2=1529080&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/KeyHolder.java
(original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/KeyHolder.java
Fri Oct  4 05:45:54 2013
@@ -99,6 +99,12 @@ public class KeyHolder<K>
     }
 
 
+    /* No qualifier */ByteBuffer getBuffer()
+    {
+        return raw;
+    }
+
+
     /**
      * @see Object#toString()
      */
@@ -108,20 +114,22 @@ public class KeyHolder<K>
 
         sb.append( "KeyHolder[" );
 
-        if ( key == null )
+        if ( key != null )
         {
-            if ( raw.isDirect() )
-            {
-                sb.append( "raw" );
-            }
-            else
-            {
-                Strings.dumpBytes( raw.array() );
-            }
+            sb.append( key );
+            sb.append( ", " );
+        }
+
+        if ( raw.isDirect() )
+        {
+            byte[] bytes = new byte[raw.limit()];
+            raw.get( bytes );
+            raw.rewind();
+            Strings.dumpBytes( bytes );
         }
         else
         {
-            sb.append( key );
+            sb.append( Strings.dumpBytes( raw.array() ) );
         }
 
         sb.append( "]" );

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Leaf.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Leaf.java?rev=1529080&r1=1529079&r2=1529080&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Leaf.java
(original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Leaf.java
Fri Oct  4 05:45:54 2013
@@ -562,6 +562,22 @@ import org.apache.directory.mavibot.btre
     /**
      * {@inheritDoc}
      */
+    /* No qualifier */KeyHolder<K> getKeyHolder( int pos )
+    {
+        if ( pos < nbElems )
+        {
+            return keys[pos];
+        }
+        else
+        {
+            return null;
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public DuplicateKeyVal<V> getValues( K key ) throws KeyNotFoundException, IOException,
IllegalArgumentException
     {

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/RecordManager.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/RecordManager.java?rev=1529080&r1=1529079&r2=1529080&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/RecordManager.java
(original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/RecordManager.java
Fri Oct  4 05:45:54 2013
@@ -576,19 +576,28 @@ public class RecordManager
         Page<K, V> page = null;
 
         // Reads the bytes containing all the keys and values, if we have some
+        // We read  big blog of data into  ByteBuffer, then we will process
+        // this ByteBuffer
         ByteBuffer byteBuffer = readBytes( pageIos, position );
 
+        // Now, deserialize the data block
         if ( nbElems >= 0 )
         {
-            // Its a leaf
+            // Its a leaf, create it
             page = BTreeFactory.createLeaf( btree, revision, nbElems );
 
+            // Store the page offset on disk
             ( ( AbstractPage<K, V> ) page ).setOffset( pageIos[0].getOffset() );
             ( ( AbstractPage<K, V> ) page ).setLastOffset( pageIos[pageIos.length -
1].getOffset() );
 
-            // Read each value and key
+            int[] keyLengths = new int[nbElems];
+            int[] valueLengths = new int[nbElems];
+
+            // Read each key and value
             for ( int i = 0; i < nbElems; i++ )
             {
+                //valueLengths[i] = byteBuffer.getInt();
+
                 ElementHolder<V, K, V> valueHolder;
 
                 if ( btree.isAllowDuplicates() )
@@ -622,9 +631,11 @@ public class RecordManager
 
                 BTreeFactory.setValue( ( ( Leaf<K, V> ) page ), i, valueHolder );
 
-                K key = btree.getKeySerializer().deserialize( byteBuffer );
-
-                BTreeFactory.setKey( page, i, key );
+                keyLengths[i] = byteBuffer.getInt();
+                ByteBuffer slice = byteBuffer.slice();
+                slice.limit( keyLengths[i] );
+                byteBuffer.position( byteBuffer.position() + keyLengths[i] );
+                BTreeFactory.setKey( page, i, slice, null );
             }
         }
         else
@@ -1148,7 +1159,9 @@ public class RecordManager
             serializedData.add( buffer );
             serializedSize += buffer.length;
 
-            // Iterate on the keys
+            // Iterate on the keys and values. We first serialize the value, then the key
+            // until we are done with all of them. If w are serializing a page, we have
+            // to serialize one more value
             for ( int pos = 0; pos < nbElems; pos++ )
             {
                 // Start with the value
@@ -1202,9 +1215,30 @@ public class RecordManager
                 }
 
                 // and the key
-                buffer = btree.getKeySerializer().serialize( page.getKey( pos ) );
-                serializedData.add( buffer );
-                dataSize += buffer.length;
+                if ( page instanceof Leaf )
+                {
+                    KeyHolder<K> keyHolder = ( ( Leaf<K, V> ) page ).getKeyHolder(
pos );
+                    ByteBuffer keyData = keyHolder.getBuffer();
+
+                    if ( keyData != null )
+                    {
+                        serializedData.add( IntSerializer.serialize( keyData.limit() ) );
+                        serializedData.add( keyData.array() );
+                        dataSize += keyData.limit() + 4;
+                    }
+                    else
+                    {
+                        serializedData.add( IntSerializer.serialize( 4 ) );
+                        serializedData.add( Strings.EMPTY_BYTES );
+                        dataSize += 4;
+                    }
+                }
+                else
+                {
+                    buffer = btree.getKeySerializer().serialize( page.getKey( pos ) );
+                    serializedData.add( buffer );
+                    dataSize += buffer.length;
+                }
             }
 
             // Nodes have one more value to serialize

Modified: directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/ReadTest.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/ReadTest.java?rev=1529080&r1=1529079&r2=1529080&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/ReadTest.java
(original)
+++ directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/ReadTest.java
Fri Oct  4 05:45:54 2013
@@ -42,6 +42,7 @@ public class ReadTest
     @Rule
     public TemporaryFolder tempFolder = new TemporaryFolder();
 
+
     /**
      * Test the readInt method
      */
@@ -261,33 +262,33 @@ public class ReadTest
         storeMethod.invoke( recordManager, 0L, bytes, pageIos );
 
         // Read the bytes back
-        byte[] readBytes = ( byte[] ) readBytesMethod.invoke( recordManager, pageIos, 0L
);
+        ByteBuffer readBytes = ( ByteBuffer ) readBytesMethod.invoke( recordManager, pageIos,
0L );
 
         // The byte length
         int pos = 0;
         assertNotNull( readBytes );
-        assertEquals( 4, readBytes.length );
+        assertEquals( 4, readBytes.limit() );
         // The data
-        assertEquals( 0x01, readBytes[pos++] );
-        assertEquals( 0x23, readBytes[pos++] );
-        assertEquals( 0x45, readBytes[pos++] );
-        assertEquals( 0x67, readBytes[pos++] );
+        assertEquals( 0x01, readBytes.get() );
+        assertEquals( 0x23, readBytes.get() );
+        assertEquals( 0x45, readBytes.get() );
+        assertEquals( 0x67, readBytes.get() );
 
         // Set the bytes at the end of the first page
         storeMethod.invoke( recordManager, 12L, bytes, pageIos );
 
         // Read the bytes back
-        readBytes = ( byte[] ) readBytesMethod.invoke( recordManager, pageIos, 12L );
+        readBytes = ( ByteBuffer ) readBytesMethod.invoke( recordManager, pageIos, 12L );
 
         // The byte length
         pos = 0;
         assertNotNull( readBytes );
-        assertEquals( 4, readBytes.length );
+        assertEquals( 4, readBytes.limit() );
         // The data
-        assertEquals( 0x01, readBytes[pos++] );
-        assertEquals( 0x23, readBytes[pos++] );
-        assertEquals( 0x45, readBytes[pos++] );
-        assertEquals( 0x67, readBytes[pos++] );
+        assertEquals( 0x01, readBytes.get() );
+        assertEquals( 0x23, readBytes.get() );
+        assertEquals( 0x45, readBytes.get() );
+        assertEquals( 0x67, readBytes.get() );
 
         // Set A full page of bytes in the first page 
         bytes = new byte[16];
@@ -300,33 +301,33 @@ public class ReadTest
         storeMethod.invoke( recordManager, 0L, bytes, pageIos );
 
         // Read the bytes back
-        readBytes = ( byte[] ) readBytesMethod.invoke( recordManager, pageIos, 0L );
+        readBytes = ( ByteBuffer ) readBytesMethod.invoke( recordManager, pageIos, 0L );
 
         // The byte length
         pos = 0;
         assertNotNull( readBytes );
-        assertEquals( 16, readBytes.length );
+        assertEquals( 16, readBytes.limit() );
 
         // The data
         for ( int i = 0; i < 16; i++ )
         {
-            assertEquals( i + 1, readBytes[pos++] );
+            assertEquals( i + 1, readBytes.get() );
         }
 
         // Write the bytes over 2 pages
         storeMethod.invoke( recordManager, 15L, bytes, pageIos );
 
         // Read the bytes back
-        readBytes = ( byte[] ) readBytesMethod.invoke( recordManager, pageIos, 15L );
+        readBytes = ( ByteBuffer ) readBytesMethod.invoke( recordManager, pageIos, 15L );
 
         // The byte length
         pos = 0;
         assertNotNull( readBytes );
-        assertEquals( 16, readBytes.length );
+        assertEquals( 16, readBytes.limit() );
         // The data
         for ( int i = 0; i < 16; i++ )
         {
-            assertEquals( i + 1, readBytes[pos++] );
+            assertEquals( i + 1, readBytes.get() );
         }
 
         // Write the bytes over 4 pages
@@ -340,16 +341,16 @@ public class ReadTest
         storeMethod.invoke( recordManager, 2L, bytes, pageIos );
 
         // Read the bytes back
-        readBytes = ( byte[] ) readBytesMethod.invoke( recordManager, pageIos, 2L );
+        readBytes = ( ByteBuffer ) readBytesMethod.invoke( recordManager, pageIos, 2L );
 
         // The byte length
         pos = 0;
         assertNotNull( readBytes );
-        assertEquals( 80, readBytes.length );
+        assertEquals( 80, readBytes.limit() );
         // The data
         for ( int i = 0; i < 80; i++ )
         {
-            assertEquals( i + 1, readBytes[pos++] );
+            assertEquals( i + 1, readBytes.get() );
         }
     }
 }



Mime
View raw message