Author: elecharny
Date: Thu Jun 14 17:51:05 2012
New Revision: 1350351

URL: http://svn.apache.org/viewvc?rev=1350351&view=rev
Log:
o Added constructors in Leaf, and the methods used to copy a page and to 
replace a value in a page
o Created some new constructors in AbstractPage
o Moved the initialization code in the AbstractPage constructor from the Node 
class

Modified:
    
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/AbstractPage.java
    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

Modified: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/AbstractPage.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/AbstractPage.java?rev=1350351&r1=1350350&r2=1350351&view=diff
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/AbstractPage.java
 (original)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/AbstractPage.java
 Thu Jun 14 17:51:05 2012
@@ -45,6 +45,32 @@ public class AbstractPage<K, V> implemen
     /** The number of current values in the Page */
     protected int nbElems;
     
+    
+    /**
+     * Creates a default empty AbstractPage
+     * 
+     * @param btree The associated BTree
+     */
+    protected AbstractPage( BTree<K, V> btree )
+    {
+        this.btree = btree;
+    }
+    
+    
+    /**
+     * Internal constructor used to create Page instance used when a page is 
being copied or overflow
+     */
+    @SuppressWarnings("unchecked") // Cannot create an array of generic objects
+    protected AbstractPage( BTree<K, V> btree, long revision, int nbElems )
+    {
+        this.btree = btree;
+        this.revision = revision;
+        this.nbElems = nbElems;
+        this.keys = (K[])new Object[nbElems];
+        recordId = btree.generateRecordId();
+    }
+
+    
     /**
      * {@inheritDoc}
      */

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=1350351&r1=1350350&r2=1350351&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 
Thu Jun 14 17:51:05 2012
@@ -93,9 +93,9 @@ public class BTree<K, V>
         // Initialize the revision counter
         revision = new AtomicLong(0);
         
-        // Create the first root page, with revision 0L. It wil be empty
+        // Create the first root page, with revision 0L. It will be empty
         // and increment the revision at the same time
-        rootPage = new Leaf<K, V>();
+        rootPage = new Leaf<K, V>( this );
         roots.put( revision.getAndIncrement(), rootPage );
     }
     

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=1350351&r1=1350350&r2=1350351&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 
Thu Jun 14 17:51:05 2012
@@ -38,6 +38,28 @@ public class Leaf<K, V> extends Abstract
     /** The page that comes previous to this one */
     protected Leaf<K, V> prevPage;
     
+    
+    /**
+     * Empty constructor
+     */
+    public Leaf( BTree<K, V> btree )
+    {
+        super( btree );
+    }
+    
+    
+    /**
+     * Internal constructor used to create Page instance used when a page is 
being copied or overflow
+     */
+    @SuppressWarnings("unchecked") // Cannot create an array of generic objects
+    private Leaf( BTree<K, V> btree, long revision, int nbElems )
+    {
+        super( btree, revision, nbElems );
+
+        this.values = (V[])new Object[nbElems];
+    }
+    
+    
     /**
      * {@inheritDoc}
      */
@@ -79,6 +101,24 @@ public class Leaf<K, V> extends Abstract
     
     
     /**
+     * Copy the current page and all of the keys, values and children, if it's 
not a leaf.
+     * 
+     * @param revision The new revision
+     * @return The copied page
+     */
+    private Page<K, V> copy( long revision, int nbElems )
+    {
+        Leaf<K, V> newLeaf = new Leaf<K, V>( btree, revision, nbElems );
+
+        // Copy the keys and the values
+        System.arraycopy( keys, 0, newLeaf.keys, 0, nbElems );
+        System.arraycopy( values, 0, newLeaf.values, 0, nbElems );
+
+        return newLeaf;
+    }
+
+    
+    /**
      * Copy the current page if needed, and replace the value at the position 
we have found the key.
      * 
      * @param revision The new page revision
@@ -88,7 +128,22 @@ public class Leaf<K, V> extends Abstract
      */
     private InsertResult<K, V> replaceValue( long revision, int index, V value 
)
     {
-        return null;
+        Leaf<K, V> newPage = this;
+        
+        if ( this.revision != revision )
+        {
+            // The page hasn't been modified yet, we need to copy it first
+            newPage = (Leaf<K, V>)copy( revision, nbElems );
+        }
+        
+        // Now we can inject the value and get back the previous one
+        V oldValue = newPage.values[index];
+        newPage.values[index] = value;
+        
+        // Create the result
+        InsertResult<K, V> result = new ModifyResult<K, V>( newPage, oldValue 
);
+        
+        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=1350351&r1=1350350&r2=1350351&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 
Thu Jun 14 17:51:05 2012
@@ -47,10 +47,7 @@ public class Node<K, V> extends Abstract
      */
     /* No qualifier */ Node( BTree<K, V> btree, long revision, K key, Page<K, 
V> leftPage, Page<K, V> rightPage )
     {
-        // Store the common values
-        this.btree = btree;
-        this.revision = revision;
-        nbElems = 1;
+        super( btree, revision, 1 );
         
         // Create the children array, and store the left and right children
         children = (Page<K, V>[])new Object[btree.getPageSize()];



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

Reply via email to