Author: elecharny
Date: Thu Mar  7 15:51:00 2013
New Revision: 1453930

URL: http://svn.apache.org/r1453930
Log:
o Fixed the getFreePageIOs( size )method
o Added a test for this method
o Added some more elements in the RecordManager test
o Fixed the PageIO toString() method

Added:
    
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/RecordManagerPrivateMethodTest.java
Modified:
    
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/PageIO.java
    
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java
    
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/RecordManagerTest.java

Modified: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/PageIO.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/PageIO.java?rev=1453930&r1=1453929&r2=1453930&view=diff
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/PageIO.java
 (original)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/PageIO.java
 Thu Mar  7 15:51:00 2013
@@ -195,12 +195,7 @@ public class PageIO
 
         sb.append( "]" );
 
-        int start = 8;
-
-        if ( size != -1 )
-        {
-            start += 4;
-        }
+        int start = 0;
 
         byte[] array = null;
 

Modified: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java?rev=1453930&r1=1453929&r2=1453930&view=diff
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java
 (original)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java
 Thu Mar  7 15:51:00 2013
@@ -238,7 +238,7 @@ public class RecordManager
      * We then store the BTree managing the pages that have been copied when 
we have added
      * or deleted an element in the BTree. They are associated with a version.
      */
-    public void initRecordManager() throws IOException
+    private void initRecordManager() throws IOException
     {
         // Create a new Header
         // The page size
@@ -290,7 +290,7 @@ public class RecordManager
      * @throws IllegalAccessException 
      * @throws ClassNotFoundException 
      */
-    public void loadRecordManager() throws IOException, 
ClassNotFoundException, IllegalAccessException,
+    private void loadRecordManager() throws IOException, 
ClassNotFoundException, IllegalAccessException,
         InstantiationException
     {
         if ( fileChannel.size() != 0 )
@@ -492,7 +492,7 @@ public class RecordManager
         int pageNb = computePageNb( position );
 
         // Compute the position in the current page
-        int pagePos = ( int ) ( position + ( pageNb + 1 ) * 8 + 4 ) - pageNb * 
pageSize;
+        int pagePos = ( int ) ( position + ( pageNb + 1 ) * LONG_SIZE + 
INT_SIZE ) - pageNb * pageSize;
 
         ByteBuffer pageData = pageIos[pageNb].getData();
         int remaining = pageData.capacity() - pagePos;
@@ -549,7 +549,7 @@ public class RecordManager
         int pageNb = computePageNb( position );
 
         // Compute the position in the current page
-        int pagePos = ( int ) ( position + ( pageNb + 1 ) * 8 + 4 ) - pageNb * 
pageSize;
+        int pagePos = ( int ) ( position + ( pageNb + 1 ) * LONG_SIZE + 
INT_SIZE ) - pageNb * pageSize;
 
         ByteBuffer pageData = pageIos[pageNb].getData();
         int remaining = pageData.capacity() - pagePos;
@@ -615,7 +615,7 @@ public class RecordManager
         int pageNb = computePageNb( position );
 
         // Compute the position in the current page
-        int pagePos = ( int ) ( position + ( pageNb + 1 ) * 8 + 4 ) - pageNb * 
pageSize;
+        int pagePos = ( int ) ( position + ( pageNb + 1 ) * LONG_SIZE + 
INT_SIZE ) - pageNb * pageSize;
 
         ByteBuffer pageData = pageIos[pageNb].getData();
         int remaining = pageData.capacity() - pagePos;
@@ -736,16 +736,16 @@ public class RecordManager
                 LONG_SIZE + // the number of element
                 LONG_SIZE; // The root offset
 
+        // Get the pageIOs we need to store the data. We may need more than 
one.
+        PageIO[] pageIos = getFreePageIOs( bufferSize );
+
         // Get a free page to store the RootPage
         PageIO rootPageIo = fetchNewPage();
 
-        // Update the number of elements to 0
+        // Update the number of elements to 0, as it's a new page
+        // We have to do that as the page might contain garbage
         store( 0L, 0L, rootPageIo );
-
-        flushPages( rootPageIo );
-
-        // Get the pageIOs we need to store the data. We may need more than 
one.
-        PageIO[] pageIos = getFreePageIOs( bufferSize );
+        rootPageIo.setSize( LONG_SIZE );
 
         // Now store the BTree data in the pages :
         // - the BTree name
@@ -759,28 +759,29 @@ public class RecordManager
         long position = 0L;
 
         // The tree name
-        position = store( position, btreeNameBytes, rootPageIo );
+        position = store( position, btreeNameBytes, pageIos );
 
         // The keySerializer FQCN
-        position = store( position, keySerializerBytes, rootPageIo );
+        position = store( position, keySerializerBytes, pageIos );
 
         // The valueSerialier FQCN
-        position = store( position, valueSerializerBytes, rootPageIo );
+        position = store( position, valueSerializerBytes, pageIos );
 
         // The BTree page size
-        position = store( position, btree.getPageSize(), rootPageIo );
+        position = store( position, btree.getPageSize(), pageIos );
 
         // The BTree current revision
-        position = store( position, btree.getRevision(), rootPageIo );
+        position = store( position, btree.getRevision(), pageIos );
 
         // The nb elems in the tree
-        position = store( position, btree.getNbElems(), rootPageIo );
+        position = store( position, btree.getNbElems(), pageIos );
 
         // The BTree rootPage offset
-        position = store( position, rootPageIo.getOffset(), rootPageIo );
+        position = store( position, rootPageIo.getOffset(), pageIos );
 
         // And flush the pages to disk now
         flushPages( pageIos );
+        flushPages( rootPageIo );
     }
 
 
@@ -860,7 +861,7 @@ public class RecordManager
             ByteBuffer pageData = pageIos[pageNb].getData();
 
             // Compute the position in the current page
-            int pagePos = ( int ) ( position + ( pageNb + 1 ) * 8 + 4 ) - 
pageNb * pageSize;
+            int pagePos = ( int ) ( position + ( pageNb + 1 ) * LONG_SIZE + 
INT_SIZE ) - pageNb * pageSize;
 
             // Compute the remaining size in the page
             int remaining = pageData.capacity() - pagePos;
@@ -920,7 +921,7 @@ public class RecordManager
         int pageNb = computePageNb( position );
 
         // Compute the position in the current page
-        int pagePos = ( int ) ( position + ( pageNb + 1 ) * 8 + 4 ) - pageNb * 
pageSize;
+        int pagePos = ( int ) ( position + ( pageNb + 1 ) * LONG_SIZE + 
INT_SIZE ) - pageNb * pageSize;
 
         // Get back the buffer in this page
         ByteBuffer pageData = pageIos[pageNb].getData();
@@ -928,7 +929,7 @@ public class RecordManager
         // Compute the remaining size in the page
         int remaining = pageData.capacity() - pagePos;
 
-        if ( remaining < 4 )
+        if ( remaining < INT_SIZE )
         {
             // We have to copy the serialized length on two pages
 
@@ -973,7 +974,7 @@ public class RecordManager
         }
 
         // Increment the position to reflect the addition of an Int (4 bytes)
-        position += 4;
+        position += INT_SIZE;
 
         return position;
     }
@@ -995,7 +996,7 @@ public class RecordManager
         int pageNb = computePageNb( position );
 
         // Compute the position in the current page
-        int pagePos = ( int ) ( position + ( pageNb + 1 ) * 8 + 4 ) - pageNb * 
pageSize;
+        int pagePos = ( int ) ( position + ( pageNb + 1 ) * LONG_SIZE + 
INT_SIZE ) - pageNb * pageSize;
 
         // Get back the buffer in this page
         ByteBuffer pageData = pageIos[pageNb].getData();
@@ -1003,7 +1004,7 @@ public class RecordManager
         // Compute the remaining size in the page
         int remaining = pageData.capacity() - pagePos;
 
-        if ( remaining < 8 )
+        if ( remaining < LONG_SIZE )
         {
             // We have to copy the serialized length on two pages
 
@@ -1080,7 +1081,7 @@ public class RecordManager
         }
 
         // Increment the position to reflect the addition of an Long (8 bytes)
-        position += 8;
+        position += LONG_SIZE;
 
         return position;
     }
@@ -1094,6 +1095,12 @@ public class RecordManager
      */
     private PageIO[] getFreePageIOs( int dataSize ) throws IOException
     {
+        if ( dataSize == 0 )
+        {
+            return new PageIO[]
+                {};
+        }
+
         // Compute the number of pages needed.
         // Considering that each page coan contain PageSize bytes,
         // but that the first 8 bytes are used for links and we 
@@ -1101,16 +1108,17 @@ public class RecordManager
         // pages is :
         // NbPages = ( (dataSize - (PageSize - 8 - 4 )) / (PageSize - 8) ) + 1 
         // NbPages += ( if (dataSize - (PageSize - 8 - 4 )) % (PageSize - 8) > 
0 : 1 : 0 )
-        int availableSize = ( pageSize - 8 );
+        int availableSize = ( pageSize - LONG_SIZE );
         int nbNeededPages = 1;
 
         // Compute the number of pages that will be full but the first page
-        if ( dataSize > availableSize + 4 )
+        if ( dataSize > availableSize - INT_SIZE )
         {
-            int remainingSize = dataSize - ( availableSize + 4 );
+            int remainingSize = dataSize - ( availableSize - INT_SIZE );
             nbNeededPages += remainingSize / availableSize;
+            int remain = remainingSize % availableSize;
 
-            if ( remainingSize % availableSize > 0 )
+            if ( remain > 0 )
             {
                 nbNeededPages++;
             }
@@ -1121,7 +1129,6 @@ public class RecordManager
         // The first page : set the size
         pageIOs[0] = fetchNewPage();
         pageIOs[0].setSize( dataSize );
-        long offset = pageIOs[0].getOffset() + pageSize;
 
         for ( int i = 1; i < nbNeededPages; i++ )
         {
@@ -1129,10 +1136,6 @@ public class RecordManager
 
             // Create the link
             pageIOs[i - 1].setNextPage( pageIOs[i].getOffset() );
-
-            // Update the offset
-            pageIOs[i].setOffset( offset );
-            offset += pageSize;
         }
 
         return pageIOs;
@@ -1159,7 +1162,7 @@ public class RecordManager
 
             newPage.setData( data );
             newPage.setNextPage( NO_PAGE );
-            newPage.setSize( -1 );
+            newPage.setSize( 0 );
 
             return newPage;
         }

Added: 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/RecordManagerPrivateMethodTest.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/RecordManagerPrivateMethodTest.java?rev=1453930&view=auto
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/RecordManagerPrivateMethodTest.java
 (added)
+++ 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/RecordManagerPrivateMethodTest.java
 Thu Mar  7 15:51:00 2013
@@ -0,0 +1,76 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.mavibot.btree.store;
+
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.junit.Test;
+
+
+/**
+ * Test some of the RecordManager prvate methods
+ * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
+ */
+public class RecordManagerPrivateMethodTest
+{
+    /**
+     * Test the getFreePageIOs method
+     */
+    @Test
+    public void testGetFreePageIos() throws IOException, 
NoSuchMethodException, InvocationTargetException,
+        IllegalAccessException
+    {
+        File tempFile = File.createTempFile( "mavibot", ".db" );
+        String tempFileName = tempFile.getAbsolutePath();
+        tempFile.deleteOnExit();
+
+        RecordManager recordManager = new RecordManager( tempFileName, 32 );
+        Method getFreePageIOsMethod = RecordManager.class.getDeclaredMethod( 
"getFreePageIOs", int.class );
+        getFreePageIOsMethod.setAccessible( true );
+
+        PageIO[] pages = ( PageIO[] ) getFreePageIOsMethod.invoke( 
recordManager, 0 );
+
+        assertEquals( 0, pages.length );
+
+        for ( int i = 1; i < 20; i++ )
+        {
+            pages = ( PageIO[] ) getFreePageIOsMethod.invoke( recordManager, i 
);
+            assertEquals( 1, pages.length );
+        }
+
+        for ( int i = 21; i < 44; i++ )
+        {
+            pages = ( PageIO[] ) getFreePageIOsMethod.invoke( recordManager, i 
);
+            assertEquals( 2, pages.length );
+        }
+
+        for ( int i = 45; i < 68; i++ )
+        {
+            pages = ( PageIO[] ) getFreePageIOsMethod.invoke( recordManager, i 
);
+            assertEquals( 3, pages.length );
+        }
+    }
+}

Modified: 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/RecordManagerTest.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/RecordManagerTest.java?rev=1453930&r1=1453929&r2=1453930&view=diff
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/RecordManagerTest.java
 (original)
+++ 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/store/RecordManagerTest.java
 Thu Mar  7 15:51:00 2013
@@ -25,6 +25,10 @@ import static org.junit.Assert.assertNot
 import java.io.File;
 import java.io.IOException;
 
+import org.apache.mavibot.btree.BTree;
+import org.apache.mavibot.btree.exception.BTreeAlreadyManagedException;
+import org.apache.mavibot.btree.serializer.LongSerializer;
+import org.apache.mavibot.btree.serializer.StringSerializer;
 import org.junit.Test;
 
 
@@ -36,15 +40,25 @@ public class RecordManagerTest
 {
 
     @Test
-    public void testRecordManager() throws IOException
+    public void testRecordManager() throws IOException, 
BTreeAlreadyManagedException
     {
         File tempFile = File.createTempFile( "mavibot", ".db" );
         String tempFileName = tempFile.getAbsolutePath();
         tempFile.deleteOnExit();
 
-        RecordManager recordManager = new RecordManager( tempFileName );
+        RecordManager recordManager = new RecordManager( tempFileName, 32 );
 
         assertNotNull( recordManager );
+
+        // Create a new BTree
+        BTree<Long, String> btree = new BTree<Long, String>( "test", new 
LongSerializer(), new StringSerializer() );
+
+        // And make it managed by the RM
+        recordManager.manage( btree );
+
+        // Inject an element into the btree
+        btree.insert( 1L, "V1" );
+
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to