Author: elecharny
Date: Sat Mar 16 13:17:07 2013
New Revision: 1457248

URL: http://svn.apache.org/r1457248
Log:
o The Leaf is no longer responsible to write the modified page on disk, it's 
done by either the node referring it, or the BTree if the rootpage is a leaf 
o Added a Node constructor that takes ElementHolder as parameters for managed 
BTrees
o Writing on disk pages when a Leaf has been modified in Node
o Removed a wrong operation on nbElems in the serializePage method

Modified:
    labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java
    labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Leaf.java
    labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Node.java
    
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java

Modified: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java?rev=1457248&r1=1457247&r2=1457248&view=diff
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java 
(original)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java 
Sat Mar 16 13:17:07 2013
@@ -1006,9 +1006,24 @@ public class BTree<K, V>
         {
             ModifyResult<K, V> modifyResult = ( ( ModifyResult<K, V> ) result 
);
 
+            Page<K, V> modifiedPage = modifyResult.getModifiedPage();
+
+            if ( isManaged() )
+            {
+                // Write the modified page on disk
+                // Note that we don't use the holder, the new root page will
+                // remain in memory.
+                ElementHolder<Page<K, V>, K, V> holder = 
recordManager.writePage( this, rootPage, modifiedPage,
+                    revision );
+
+                // Store the offset on disk in the page in memory
+                ( ( AbstractPage<K, V> ) modifiedPage ).setOffset( ( ( 
ReferenceHolder<Page<K, V>, K, V> ) holder )
+                    .getOffset() );
+            }
+
             // The root has just been modified, we haven't split it
             // Get it and make it the current root page
-            rootPage = modifyResult.getModifiedPage();
+            rootPage = modifiedPage;
 
             modifiedValue = modifyResult.getModifiedValue();
         }
@@ -1021,9 +1036,48 @@ public class BTree<K, V>
             K pivot = splitResult.getPivot();
             Page<K, V> leftPage = splitResult.getLeftPage();
             Page<K, V> rightPage = splitResult.getRightPage();
+            Page<K, V> newRootPage = null;
+
+            // If the BTree is managed, we have to write the two pages that 
were created
+            // and to keep a track of the two offsets for the upper node
+            if ( isManaged() )
+            {
+                ElementHolder<Page<K, V>, K, V> holderLeft = 
recordManager.writePage( this, rootPage,
+                    ( ( SplitResult ) result ).getLeftPage(), revision );
+
+                // Store the offset on disk in the page
+                ( ( AbstractPage ) ( ( SplitResult ) result ).getLeftPage() )
+                    .setOffset( ( ( ReferenceHolder ) holderLeft ).getOffset() 
);
+
+                ElementHolder<Page<K, V>, K, V> holderRight = 
recordManager.writePage( this, rootPage,
+                    ( ( SplitResult ) result ).getRightPage(),
+                    revision );
+
+                // Store the offset on disk in the page
+                ( ( AbstractPage<K, V> ) ( ( SplitResult ) result 
).getRightPage() )
+                    .setOffset( ( ( ReferenceHolder ) holderRight 
).getOffset() );
+
+                // Create the new rootPage
+                newRootPage = new Node<K, V>( this, revision, pivot, leftPage, 
rightPage );
+            }
+            else
+            {
+                // Create the new rootPage
+                newRootPage = new Node<K, V>( this, revision, pivot, leftPage, 
rightPage );
+            }
+
+            // If the BTree is managed, we now have to write the page on disk
+            // and to add this page to the list of modified pages
+            if ( isManaged() )
+            {
+                ElementHolder<Page<K, V>, K, V> holder = recordManager
+                    .writePage( this, rootPage, newRootPage, revision );
+
+                // Store the offset on disk in the page
+                ( ( AbstractPage<K, V> ) newRootPage ).setOffset( ( ( 
ReferenceHolder ) holder ).getOffset() );
+            }
 
-            // Create the new rootPage
-            rootPage = new Node<K, V>( this, revision, pivot, leftPage, 
rightPage );
+            rootPage = newRootPage;
         }
 
         // Inject the modification into the modification queue

Modified: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Leaf.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Leaf.java?rev=1457248&r1=1457247&r2=1457248&view=diff
==============================================================================
--- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Leaf.java 
(original)
+++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Leaf.java 
Sat Mar 16 13:17:07 2013
@@ -91,17 +91,6 @@ public class Leaf<K, V> extends Abstract
             // We insert it into a copied page and return the result
             Page<K, V> modifiedPage = addElement( revision, key, value, pos );
 
-            // If the BTree is managed, we now have to write the page on disk
-            // and to add this page to the list of modified pages
-            if ( btree.isManaged() )
-            {
-                ElementHolder holder = btree.getRecordManager()
-                    .writePage( btree, this, modifiedPage, revision );
-
-                // Store the offset on disk in the page
-                ( ( AbstractPage<K, V> ) modifiedPage ).setOffset( ( ( 
ReferenceHolder ) holder ).getOffset() );
-            }
-
             InsertResult<K, V> result = new ModifyResult<K, V>( modifiedPage, 
null );
 
             return result;
@@ -112,27 +101,6 @@ public class Leaf<K, V> extends Abstract
             // after having created two pages.
             InsertResult<K, V> result = addAndSplit( revision, key, value, pos 
);
 
-            // If the BTree is managed, we have to write the two pages
-            // and to keep a track of the two offsets for the upper node
-            if ( btree.isManaged() )
-            {
-                ElementHolder holderLeft = btree.getRecordManager().writePage( 
btree, this,
-                    ( ( SplitResult ) result ).getLeftPage(),
-                    revision );
-
-                // Store the offset on disk in the page
-                ( ( AbstractPage ) ( ( SplitResult ) result ).getLeftPage() )
-                    .setOffset( ( ( ReferenceHolder ) holderLeft ).getOffset() 
);
-
-                ElementHolder holderRight = 
btree.getRecordManager().writePage( btree, this,
-                    ( ( SplitResult ) result ).getRightPage(),
-                    revision );
-
-                // Store the offset on disk in the page
-                ( ( AbstractPage<K, V> ) ( ( SplitResult ) result 
).getRightPage() )
-                    .setOffset( ( ( ReferenceHolder ) holderRight 
).getOffset() );
-            }
-
             return result;
         }
     }

Modified: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Node.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Node.java?rev=1457248&r1=1457247&r2=1457248&view=diff
==============================================================================
--- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Node.java 
(original)
+++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Node.java 
Sat Mar 16 13:17:07 2013
@@ -103,6 +103,40 @@ public class Node<K, V> extends Abstract
 
 
     /**
+     * Create a new Node which will contain only one key, with references to
+     * a left and right page. This is a specific constructor used by the btree
+     * when the root was full when we added a new value.
+     * 
+     * @param btree the parent BTree
+     * @param revision the Node revision
+     * @param key The new key
+     * @param leftPage The left page
+     * @param rightPage The right page
+     */
+    @SuppressWarnings("unchecked")
+    /* No qualifier */Node( BTree<K, V> btree, long revision, K key, 
ElementHolder<Page<K, V>, K, V> leftPage,
+        ElementHolder<Page<K, V>, K, V> rightPage )
+    {
+        super( btree, revision, 1 );
+
+        // Create the children array, and store the left and right children
+        children = ( ReferenceHolder<Page<K, V>, K, V>[] ) Array.newInstance( 
ReferenceHolder.class,
+            btree.getPageSize() );
+
+        children[0] = leftPage;
+        children[1] = rightPage;
+
+        // Create the keys array and store the pivot into it
+        // We get the type of array to create from the btree
+        // Yes, this is an hack...
+        Class<?> keyType = btree.getKeyType();
+        keys = ( K[] ) Array.newInstance( keyType, btree.getPageSize() );
+
+        keys[0] = key;
+    }
+
+
+    /**
      * {@inheritDoc}
      */
     public InsertResult<K, V> insert( long revision, K key, V value ) throws 
IOException
@@ -860,8 +894,9 @@ public class Node<K, V> extends Abstract
      * @param result The modified page
      * @param pos The position of the found key
      * @return A modified page
+     * @throws IOException 
      */
-    private InsertResult<K, V> replaceChild( long revision, ModifyResult<K, V> 
result, int pos )
+    private InsertResult<K, V> replaceChild( long revision, ModifyResult<K, V> 
result, int pos ) throws IOException
     {
         // Just copy the current page and update its revision
         Page<K, V> newPage = copy( revision );
@@ -869,7 +904,24 @@ public class Node<K, V> extends Abstract
         // Last, we update the children table of the newly created page
         // to point on the modified child
         Page<K, V> modifiedPage = result.getModifiedPage();
-        ( ( Node<K, V> ) newPage ).children[pos] = btree.createHolder( 
modifiedPage );
+
+        // If the BTree is managed, we now have to write the modified page on 
disk
+        // and to add this page to the list of modified pages
+        if ( btree.isManaged() )
+        {
+            ElementHolder<Page<K, V>, K, V> holder = 
btree.getRecordManager().writePage( btree, this, modifiedPage,
+                revision );
+
+            // Store the offset on disk in the page in memory
+            ( ( AbstractPage<K, V> ) modifiedPage ).setOffset( ( ( 
ReferenceHolder<Page<K, V>, K, V> ) holder )
+                .getOffset() );
+
+            ( ( Node<K, V> ) newPage ).children[pos] = holder;
+        }
+        else
+        {
+            ( ( Node<K, V> ) newPage ).children[pos] = btree.createHolder( 
modifiedPage );
+        }
 
         // We can return the result, where we update the modifiedPage,
         // to avoid the creation of a new object

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=1457248&r1=1457247&r2=1457248&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
 Sat Mar 16 13:17:07 2013
@@ -919,12 +919,6 @@ public class RecordManager
     {
         int nbElems = page.getNbElems();
 
-        // Make it a negative value if it's a Node
-        if ( page instanceof Node )
-        {
-            nbElems = -nbElems;
-        }
-
         if ( nbElems == 0 )
         {
             // We will have 1 single page if we have no elements
@@ -971,6 +965,12 @@ public class RecordManager
             serializedSize += buffer.length;
 
             // The number of elements
+            // Make it a negative value if it's a Node
+            if ( page instanceof Node )
+            {
+                nbElems = -nbElems;
+            }
+
             buffer = IntSerializer.serialize( nbElems );
             serializedData.add( buffer );
             serializedSize += buffer.length;



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

Reply via email to