directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1551187 [1/2] - in /directory/mavibot/trunk/mavibot/src: main/java/org/apache/directory/mavibot/btree/ main/java/org/apache/directory/mavibot/btree/memory/ main/java/org/apache/directory/mavibot/btree/persisted/ test/java/org/apache/direct...
Date Mon, 16 Dec 2013 11:46:33 GMT
Author: elecharny
Date: Mon Dec 16 11:46:32 2013
New Revision: 1551187

URL: http://svn.apache.org/r1551187
Log:
o Fixed a NPE in the Array holding for values
o Added a PageHolder common class for in-memory and persisted btrees
o The abstract in-memory and persisted page class have been removed
o The in-memory/persisted Leaf classes have been removed to InMemoryLeaf/PersistedLeaf

Added:
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/PageHolder.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryLeaf.java
      - copied, changed from r1551071, directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Leaf.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedLeaf.java
      - copied, changed from r1551070, directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/Leaf.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedPageHolder.java
      - copied, changed from r1551070, directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PageHolder.java
Removed:
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/AbstractInMemoryPage.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Leaf.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/AbstractPersistedPage.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/Leaf.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PageHolder.java
Modified:
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractPage.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractTupleCursor.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractValueHolder.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTreeFactory.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTree.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeBuilder.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryValueHolder.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Node.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/TupleCursorImpl.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/BTreeFactory.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/Node.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedBTree.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBuilder.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedValueHolder.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/RecordManager.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/TupleCursorImpl.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTest.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/LeafTest.java

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractPage.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractPage.java?rev=1551187&r1=1551186&r2=1551187&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractPage.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractPage.java Mon Dec 16 11:46:32 2013
@@ -20,6 +20,9 @@
 package org.apache.directory.mavibot.btree;
 
 
+import java.io.IOException;
+import java.lang.reflect.Array;
+
 import org.apache.directory.mavibot.btree.KeyHolder;
 import org.apache.directory.mavibot.btree.Page;
 import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
@@ -42,6 +45,9 @@ public abstract class AbstractPage<K, V>
     /** Keys of children nodes */
     protected KeyHolder<K>[] keys;
 
+    /** Children pages associated with keys. */
+    protected PageHolder<K, V>[] children;
+
     /** The number of current values in the Page */
     protected int nbElems;
 
@@ -69,6 +75,19 @@ public abstract class AbstractPage<K, V>
     }
 
     /**
+     * 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 = ( KeyHolder[] ) Array.newInstance( KeyHolder.class, nbElems );
+    }
+
+    /**
      * {@inheritDoc}
      */
     public int getNbElems()
@@ -102,6 +121,76 @@ public abstract class AbstractPage<K, V>
     /**
      * {@inheritDoc}
      */
+    public Page<K, V> getPage( int pos )
+    {
+        if ( ( pos >= 0 ) && ( pos < children.length ) )
+        {
+            return children[pos].getValue();
+        }
+        else
+        {
+            return null;
+        }
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public void setPageHolder( int pos, PageHolder<K, V> pageHolder )
+    {
+        if ( ( pos >= 0 ) && ( pos < children.length ) )
+        {
+            children[pos] = pageHolder;
+        }
+    }
+
+
+    /**
+     * Selects the sibling (the previous or next page with the same parent) which has
+     * the more element assuming it's above N/2
+     * 
+     * @param parent The parent of the current page
+     * @param The position of the current page reference in its parent
+     * @return The position of the sibling, or -1 if we have'nt found any sibling
+     * @throws IOException If we have an error while trying to access the page
+     */
+    protected int selectSibling( Page<K, V> parent, int parentPos ) throws IOException
+    {
+        if ( parentPos == 0 )
+        {
+            // The current page is referenced on the left of its parent's page :
+            // we will not have a previous page with the same parent
+            return 1;
+        }
+
+        if ( parentPos == parent.getNbElems() )
+        {
+            // The current page is referenced on the right of its parent's page :
+            // we will not have a next page with the same parent
+            return parentPos - 1;
+        }
+
+        Page<K, V> prevPage = ((AbstractPage<K, V>)parent).getPage( parentPos - 1 );
+        Page<K, V> nextPage = ((AbstractPage<K, V>)parent).getPage( parentPos + 1 );
+
+        int prevPageSize = prevPage.getNbElems();
+        int nextPageSize = nextPage.getNbElems();
+
+        if ( prevPageSize >= nextPageSize )
+        {
+            return parentPos - 1;
+        }
+        else
+        {
+            return parentPos + 1;
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
     public K getLeftMostKey()
     {
         return keys[0].getKey();
@@ -163,6 +252,18 @@ public abstract class AbstractPage<K, V>
 
 
     /**
+     * Sets the key at a give position
+     * 
+     * @param pos The position in the keys array
+     * @param key the key to inject
+     */
+    public void setKey( int pos, KeyHolder<K> key )
+    {
+        keys[pos] = key;
+    }
+
+
+    /**
      * @param revision the keys to set
      */
     public void setKeys( KeyHolder<K>[] keys )
@@ -311,6 +412,15 @@ public abstract class AbstractPage<K, V>
 
 
     /**
+     * @return the btree
+     */
+    public BTree<K, V> getBtree()
+    {
+        return btree;
+    }
+
+
+    /**
      * {@inheritDoc}
      */
     public String dumpPage( String tabs )

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractTupleCursor.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractTupleCursor.java?rev=1551187&r1=1551186&r2=1551187&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractTupleCursor.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractTupleCursor.java Mon Dec 16 11:46:32 2013
@@ -60,56 +60,6 @@ public abstract class AbstractTupleCurso
     
     /**
      * {@inheritDoc}
-     *
-    public void afterLast() throws IOException
-    {
-        // First check that we have elements in the BTree
-        if ( ( stack == null ) || ( stack.length == 0 ) )
-        {
-            return;
-        }
-
-        Page<K, V> child = null;
-
-        for ( int i = 0; i < depth; i++ )
-        {
-            ParentPos<K, V> parentPos = stack[i];
-            
-            if ( child != null )
-            {
-                parentPos.page = child;
-                parentPos.pos = child.getNbElems();
-            }
-            else
-            {
-                // We have N+1 children if the page is a Node, so we don't decrement the nbElems field
-                parentPos.pos = parentPos.page.getNbElems();
-            }
-
-            child = ((Node<K, V>)parentPos.page).children[parentPos.pos];
-        }
-        
-        // and leaf
-        ParentPos<K, V> parentPos = stack[depth];
-
-        if ( child == null )
-        {
-            parentPos.pos = parentPos.page.getNbElems() - 1;
-        }
-        else
-        {
-            parentPos.page = child;
-            parentPos.pos = child.getNbElems() - 1;
-        }
-
-        parentPos.valueCursor = ((Leaf<K, V>)parentPos.page).values[parentPos.pos].getCursor();
-        parentPos.valueCursor.afterLast();
-        parentPos.pos = AFTER_LAST;
-    }
-
-
-    /**
-     * {@inheritDoc}
      */
     public void close()
     {

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractValueHolder.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractValueHolder.java?rev=1551187&r1=1551186&r2=1551187&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractValueHolder.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractValueHolder.java Mon Dec 16 11:46:32 2013
@@ -48,6 +48,9 @@ public abstract class AbstractValueHolde
     protected int valueThresholdUp = 1;
     protected int valueThresholdLow = 1;
 
+    protected int nbArrayElems;
+    
+
     /**
      * {@inheritDoc}
      */
@@ -272,7 +275,7 @@ public abstract class AbstractValueHolde
     private void addInArray( V value )
     {
         // We have to check that we have reached the threshold or not
-        if ( valueArray.length >= valueThresholdUp )
+        if ( size() >= valueThresholdUp )
         {
             // Ok, transform the array into a btree
             createSubTree();
@@ -287,6 +290,7 @@ public abstract class AbstractValueHolde
                 }
 
                 // We can delete the array now
+                nbArrayElems = 0;
                 valueArray = null;
 
                 // And inject the new value
@@ -300,27 +304,37 @@ public abstract class AbstractValueHolde
         }
         else
         {
-            // First check that the value is not already present in the ValueHolder
-            int pos = findPos( value );
-
-            if ( pos >= 0 )
+            // Create the array if it's null
+            if ( valueArray == null )
             {
-                // The value exists : nothing to do
-                return;
+                valueArray = ( V[] ) Array.newInstance( valueSerializer.getType(), 1 );
+                nbArrayElems = 1;
+                valueArray[0] = value;
+            }
+            else
+            {
+                // check that the value is not already present in the ValueHolder
+                int pos = findPos( value );
+    
+                if ( pos >= 0 )
+                {
+                    // The value exists : nothing to do
+                    return;
+                }
+    
+                // Ok, we just have to insert the new element at the right position
+                // We transform the position to a positive value 
+                pos = -( pos + 1 );
+                // First, copy the array
+                V[] newValueArray = ( V[] ) Array.newInstance( valueSerializer.getType(), valueArray.length + 1 );
+    
+                System.arraycopy( valueArray, 0, newValueArray, 0, pos );
+                newValueArray[pos] = value;
+                System.arraycopy( valueArray, pos, newValueArray, pos + 1, valueArray.length - pos );
+    
+                // And switch the arrays
+                valueArray = newValueArray;
             }
-
-            // Ok, we just have to insert the new element at the right position
-            // We transform the position to a positive value 
-            pos = -( pos + 1 );
-            // First, copy the array
-            V[] newValueArray = ( V[] ) Array.newInstance( valueSerializer.getType(), valueArray.length + 1 );
-
-            System.arraycopy( valueArray, 0, newValueArray, 0, pos );
-            newValueArray[pos] = value;
-            System.arraycopy( valueArray, pos, newValueArray, pos + 1, valueArray.length - pos );
-
-            // And switch the arrays
-            valueArray = newValueArray;
         }
     }
     
@@ -347,7 +361,7 @@ public abstract class AbstractValueHolde
      */
     public void add( V value )
     {
-        if ( valueArray != null )
+        if ( valueBtree == null )
         {
             addInArray( value );
         }

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/PageHolder.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/PageHolder.java?rev=1551187&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/PageHolder.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/PageHolder.java Mon Dec 16 11:46:32 2013
@@ -0,0 +1,55 @@
+/*
+ *  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.directory.mavibot.btree;
+
+import java.io.IOException;
+
+import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
+
+public class PageHolder<K, V>
+{
+    /** The BTree */
+    protected BTree<K, V> btree;
+    
+    /** The stored page */
+    private Page<K, V> page;
+
+    /**
+     * Create a new holder storing an offset and a SoftReference containing the element.
+     * 
+     * @param page The element to store into a SoftReference
+     **/
+    public PageHolder( BTree<K, V> btree, Page<K, V> page )
+    {
+        this.btree = btree;
+        this.page = page;
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     * @throws IOException 
+     * @throws EndOfFileExceededException 
+     */
+    public Page<K, V> getValue()
+    {
+        return page;
+    }
+}

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTreeFactory.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTreeFactory.java?rev=1551187&r1=1551186&r2=1551187&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTreeFactory.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTreeFactory.java Mon Dec 16 11:46:32 2013
@@ -23,8 +23,11 @@ package org.apache.directory.mavibot.btr
 import java.io.IOException;
 import java.util.LinkedList;
 
+import org.apache.directory.mavibot.btree.AbstractPage;
 import org.apache.directory.mavibot.btree.BTree;
+import org.apache.directory.mavibot.btree.KeyHolder;
 import org.apache.directory.mavibot.btree.Page;
+import org.apache.directory.mavibot.btree.PageHolder;
 import org.apache.directory.mavibot.btree.ParentPos;
 import org.apache.directory.mavibot.btree.ValueHolder;
 import org.apache.directory.mavibot.btree.serializer.ElementSerializer;
@@ -77,9 +80,9 @@ public class BTreeFactory
      * @param nbElems The number or elements in this leaf
      * @return A Leaf instance
      */
-    public static <K, V> Leaf<K, V> createLeaf( BTree<K, V> btree, long revision, int nbElems )
+    public static <K, V> InMemoryLeaf<K, V> createLeaf( BTree<K, V> btree, long revision, int nbElems )
     {
-        Leaf<K, V> leaf = new Leaf<K, V>( btree, revision, nbElems );
+        InMemoryLeaf<K, V> leaf = new InMemoryLeaf<K, V>( btree, revision, nbElems );
 
         return leaf;
     }
@@ -192,7 +195,8 @@ public class BTreeFactory
      */
     public static <K, V> void setKey( Page<K, V> page, int pos, K key )
     {
-        ( ( AbstractInMemoryPage<K, V> ) page ).setKey( pos, key );
+        KeyHolder<K> keyHolder = new KeyHolder<K>( key );
+        ( ( AbstractPage<K, V> ) page ).setKey( pos, keyHolder );
     }
 
 
@@ -201,7 +205,7 @@ public class BTreeFactory
      * @param pos The position in the values array
      * @param value the value to inject
      */
-    public static <K, V> void setValue( Leaf<K, V> page, int pos, InMemoryValueHolder<V> value )
+    public static <K, V> void setValue( InMemoryLeaf<K, V> page, int pos, InMemoryValueHolder<V> value )
     {
         page.setValue( pos, value );
     }
@@ -212,9 +216,9 @@ public class BTreeFactory
      * @param pos The position in the values array
      * @param value the value to inject
      */
-    public static <K, V> void setValue( Node<K, V> page, int pos, Page<K, V> value )
+    public static <K, V> void setValue( BTree<K, V> btree, Node<K, V> page, int pos, Page<K, V> value )
     {
-        page.children[pos] = value;
+        page.setPageHolder( pos, new PageHolder<K, V>( btree, value ) );
     }
 
 
@@ -232,9 +236,9 @@ public class BTreeFactory
         ParentPos<K, V> last = new ParentPos<K, V>( btree.getRootPage(), btree.getRootPage().getNbElems() );
         stack.push( last );
 
-        if ( btree.getRootPage() instanceof Leaf )
+        if ( btree.getRootPage() instanceof InMemoryLeaf )
         {
-            Leaf<K, V> leaf = ( Leaf<K, V> ) ( btree.getRootPage() );
+            InMemoryLeaf<K, V> leaf = ( InMemoryLeaf<K, V> ) ( btree.getRootPage() );
             ValueHolder<V> valueHolder = leaf.values[last.pos];
             last.valueCursor = valueHolder.getCursor();
         }
@@ -244,14 +248,14 @@ public class BTreeFactory
 
             while ( true )
             {
-                Page<K, V> p = node.children[node.getNbElems()];
+                Page<K, V> p = node.getPage( node.getNbElems() );
 
                 last = new ParentPos<K, V>( p, p.getNbElems() );
                 stack.push( last );
 
-                if ( p instanceof Leaf )
+                if ( p instanceof InMemoryLeaf )
                 {
-                    Leaf<K, V> leaf = ( Leaf<K, V> ) ( last.page );
+                    InMemoryLeaf<K, V> leaf = ( InMemoryLeaf<K, V> ) ( last.page );
                     ValueHolder<V> valueHolder = leaf.values[last.pos];
                     last.valueCursor = valueHolder.getCursor();
                     break;

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTree.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTree.java?rev=1551187&r1=1551186&r2=1551187&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTree.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTree.java Mon Dec 16 11:46:32 2013
@@ -154,7 +154,7 @@ public class InMemoryBTree<K, V> extends
 
         // 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>( this );
+        rootPage = new InMemoryLeaf<K, V>( this );
 
         // Now, initialize the BTree
         init();
@@ -265,7 +265,7 @@ public class InMemoryBTree<K, V> extends
 
         // 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>( this );
+        rootPage = new InMemoryLeaf<K, V>( this );
 
         // Now, call the init() method
         init();

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeBuilder.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeBuilder.java?rev=1551187&r1=1551186&r2=1551187&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeBuilder.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeBuilder.java Mon Dec 16 11:46:32 2013
@@ -36,6 +36,7 @@ import org.apache.directory.mavibot.btre
 import org.apache.directory.mavibot.btree.BTree;
 import org.apache.directory.mavibot.btree.KeyHolder;
 import org.apache.directory.mavibot.btree.Page;
+import org.apache.directory.mavibot.btree.PageHolder;
 import org.apache.directory.mavibot.btree.Tuple;
 import org.apache.directory.mavibot.btree.ValueHolder;
 import org.apache.directory.mavibot.btree.serializer.ElementSerializer;
@@ -77,7 +78,7 @@ public class InMemoryBTreeBuilder<K, V>
 
         int totalTupleCount = 0;
 
-        Leaf<K, V> leaf1 = createLeaf( btree, 0, numKeysInNode );
+        InMemoryLeaf<K, V> leaf1 = createLeaf( btree, 0, numKeysInNode );
         lstLeaves.add( leaf1 );
 
         int leafIndex = 0;
@@ -107,7 +108,7 @@ public class InMemoryBTreeBuilder<K, V>
         }
 
         // remove null keys and values from the last leaf and resize
-        Leaf<K, V> lastLeaf = ( Leaf<K, V> ) lstLeaves.get( lstLeaves.size() - 1 );
+        InMemoryLeaf<K, V> lastLeaf = ( InMemoryLeaf<K, V> ) lstLeaves.get( lstLeaves.size() - 1 );
         for ( int i = 0; i < lastLeaf.getNbElems(); i++ )
         {
             if ( lastLeaf.getKeys()[i] == null )
@@ -161,7 +162,7 @@ public class InMemoryBTreeBuilder<K, V>
                 setKey( node, i - 1, p.getLeftMostKey() );
             }
 
-            node.children[i] = p;
+            node.setPageHolder( i, new PageHolder<K, V>( btree, p ) );
 
             i++;
             totalNodes++;

Copied: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryLeaf.java (from r1551071, directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Leaf.java)
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryLeaf.java?p2=directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryLeaf.java&p1=directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Leaf.java&r1=1551071&r2=1551187&rev=1551187&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Leaf.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryLeaf.java Mon Dec 16 11:46:32 2013
@@ -23,11 +23,13 @@ package org.apache.directory.mavibot.btr
 import java.io.IOException;
 import java.lang.reflect.Array;
 
+import org.apache.directory.mavibot.btree.AbstractPage;
 import org.apache.directory.mavibot.btree.BTree;
 import org.apache.directory.mavibot.btree.BorrowedFromLeftResult;
 import org.apache.directory.mavibot.btree.BorrowedFromRightResult;
 import org.apache.directory.mavibot.btree.DeleteResult;
 import org.apache.directory.mavibot.btree.InsertResult;
+import org.apache.directory.mavibot.btree.KeyHolder;
 import org.apache.directory.mavibot.btree.ModifyResult;
 import org.apache.directory.mavibot.btree.NotPresentResult;
 import org.apache.directory.mavibot.btree.Page;
@@ -51,7 +53,7 @@ import org.apache.directory.mavibot.btre
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-/* No qualifier */class Leaf<K, V> extends AbstractInMemoryPage<K, V>
+/* No qualifier */class InMemoryLeaf<K, V> extends AbstractPage<K, V>
 {
     /** Values associated with keys */
     protected ValueHolder<V>[] values;
@@ -62,7 +64,7 @@ import org.apache.directory.mavibot.btre
      * 
      * @param btree The BTree this page belongs to.
      */
-    /* No qualifier */Leaf( BTree<K, V> btree )
+    InMemoryLeaf( BTree<K, V> btree )
     {
         super( btree );
     }
@@ -76,8 +78,7 @@ import org.apache.directory.mavibot.btre
      * @param nbElems The number of elements this page will contain
      */
     @SuppressWarnings("unchecked")
-    // Cannot create an array of generic objects
-    /* No qualifier */Leaf( BTree<K, V> btree, long revision, int nbElems )
+    InMemoryLeaf( BTree<K, V> btree, long revision, int nbElems )
     {
         super( btree, revision, nbElems );
 
@@ -193,15 +194,15 @@ import org.apache.directory.mavibot.btre
             }
         }
 
-        Leaf<K, V> newLeaf = null;
+        InMemoryLeaf<K, V> newLeaf = null;
 
         if ( keyRemoved )
         {
-            newLeaf = new Leaf<K, V>( btree, revision, nbElems - 1 );
+            newLeaf = new InMemoryLeaf<K, V>( btree, revision, nbElems - 1 );
         }
         else
         {
-            newLeaf = new Leaf<K, V>( btree, revision, nbElems );
+            newLeaf = new InMemoryLeaf<K, V>( btree, revision, nbElems );
         }
 
         // Create the result
@@ -231,7 +232,7 @@ import org.apache.directory.mavibot.btre
                 // Check in both next and previous page, if they have the same parent
                 // and select the biggest page with the same parent to borrow an element.
                 int siblingPos = selectSibling( ( Node<K, V> ) parent, parentPos );
-                Leaf<K, V> sibling = ( Leaf<K, V> ) ( ( ( Node<K, V> ) parent ).children[siblingPos] );
+                InMemoryLeaf<K, V> sibling = ( InMemoryLeaf<K, V> ) ( ( ( Node<K, V> ) parent ).getPage( siblingPos ) );
 
                 if ( sibling.getNbElems() == halfSize )
                 {
@@ -298,13 +299,13 @@ import org.apache.directory.mavibot.btre
      * @return The new created leaf containing the sibling and the old page.
      * @throws IOException If we have an error while trying to access the page
      */
-    private DeleteResult<K, V> mergeWithSibling( Tuple<K, V> removedElement, long revision, Leaf<K, V> sibling,
+    private DeleteResult<K, V> mergeWithSibling( Tuple<K, V> removedElement, long revision, InMemoryLeaf<K, V> sibling,
         boolean isLeft, int pos )
         throws EndOfFileExceededException, IOException
     {
         // Create the new page. It will contain N - 1 elements (the maximum number)
         // as we merge two pages that contain N/2 elements minus the one we remove
-        Leaf<K, V> newLeaf = new Leaf<K, V>( btree, revision, btree.getPageSize() - 1 );
+        InMemoryLeaf<K, V> newLeaf = new InMemoryLeaf<K, V>( btree, revision, btree.getPageSize() - 1 );
 
         if ( isLeft )
         {
@@ -359,7 +360,7 @@ import org.apache.directory.mavibot.btre
      * @return The resulting pages
      * @throws IOException If we have an error while trying to access the page 
      */
-    private DeleteResult<K, V> borrowFromLeft( Tuple<K, V> removedElement, long revision, Leaf<K, V> sibling, int pos )
+    private DeleteResult<K, V> borrowFromLeft( Tuple<K, V> removedElement, long revision, InMemoryLeaf<K, V> sibling, int pos )
         throws IOException
     {
         // The sibling is on the left, borrow the rightmost element
@@ -367,14 +368,14 @@ import org.apache.directory.mavibot.btre
         ValueHolder<V> siblingValue = sibling.values[sibling.getNbElems() - 1];
 
         // Create the new sibling, with one less element at the end
-        Leaf<K, V> newSibling = ( Leaf<K, V> ) sibling.copy( revision, sibling.getNbElems() - 1 );
+        InMemoryLeaf<K, V> newSibling = ( InMemoryLeaf<K, V> ) sibling.copy( revision, sibling.getNbElems() - 1 );
 
         // Create the new page and add the new element at the beginning
         // First copy the current page, with the same size
-        Leaf<K, V> newLeaf = new Leaf<K, V>( btree, revision, nbElems );
+        InMemoryLeaf<K, V> newLeaf = new InMemoryLeaf<K, V>( btree, revision, nbElems );
 
         // Insert the borrowed element
-        newLeaf.setKey( 0, siblingKey );
+        newLeaf.setKey( 0, new KeyHolder<K>( siblingKey ) );
         newLeaf.values[0] = siblingValue;
 
         // Copy the keys and the values up to the insertion position,
@@ -406,7 +407,7 @@ import org.apache.directory.mavibot.btre
      * @return The resulting pages
      * @throws IOException If we have an error while trying to access the page 
      */
-    private DeleteResult<K, V> borrowFromRight( Tuple<K, V> removedElement, long revision, Leaf<K, V> sibling, int pos )
+    private DeleteResult<K, V> borrowFromRight( Tuple<K, V> removedElement, long revision, InMemoryLeaf<K, V> sibling, int pos )
         throws IOException
     {
         // The sibling is on the left, borrow the rightmost element
@@ -414,7 +415,7 @@ import org.apache.directory.mavibot.btre
         ValueHolder<V> siblingHolder = sibling.values[0];
 
         // Create the new sibling
-        Leaf<K, V> newSibling = new Leaf<K, V>( btree, revision, sibling.getNbElems() - 1 );
+        InMemoryLeaf<K, V> newSibling = new InMemoryLeaf<K, V>( btree, revision, sibling.getNbElems() - 1 );
 
         // Copy the keys and the values from 1 to N in the new sibling
         System.arraycopy( sibling.getKeys(), 1, newSibling.getKeys(), 0, sibling.nbElems - 1 );
@@ -422,10 +423,10 @@ import org.apache.directory.mavibot.btre
 
         // Create the new page and add the new element at the end
         // First copy the current page, with the same size
-        Leaf<K, V> newLeaf = new Leaf<K, V>( btree, revision, nbElems );
+        InMemoryLeaf<K, V> newLeaf = new InMemoryLeaf<K, V>( btree, revision, nbElems );
 
         // Insert the borrowed element at the end
-        newLeaf.setKey( nbElems - 1, siblingKey );
+        newLeaf.setKey( nbElems - 1, new KeyHolder<K>( siblingKey ) );
         newLeaf.values[nbElems - 1] = siblingHolder;
 
         // Copy the keys and the values up to the deletion position,
@@ -454,7 +455,7 @@ import org.apache.directory.mavibot.btre
      * @param pos The position into the page of the element to remove
      * @throws IOException If we have an error while trying to access the page
      */
-    private void copyAfterRemovingElement( boolean keyRemoved, Leaf<K, V> newLeaf, int pos ) throws IOException
+    private void copyAfterRemovingElement( boolean keyRemoved, InMemoryLeaf<K, V> newLeaf, int pos ) throws IOException
     {
         if ( keyRemoved )
         {
@@ -703,7 +704,7 @@ import org.apache.directory.mavibot.btre
      */
     private Page<K, V> copy( long revision, int nbElems )
     {
-        Leaf<K, V> newLeaf = new Leaf<K, V>( btree, revision, nbElems );
+        InMemoryLeaf<K, V> newLeaf = new InMemoryLeaf<K, V>( btree, revision, nbElems );
 
         // Copy the keys and the values
         System.arraycopy( getKeys(), 0, newLeaf.getKeys(), 0, nbElems );
@@ -726,12 +727,12 @@ import org.apache.directory.mavibot.btre
     private InsertResult<K, V> replaceElement( long revision, K key, V value, int pos )
         throws IOException
     {
-        Leaf<K, V> newLeaf = this;
+        InMemoryLeaf<K, V> newLeaf = this;
 
         if ( this.revision != revision )
         {
             // The page hasn't been modified yet, we need to copy it first
-            newLeaf = ( Leaf<K, V> ) copy( revision, nbElems );
+            newLeaf = ( InMemoryLeaf<K, V> ) copy( revision, nbElems );
         }
 
         // Get the previous value from the leaf (it's a copy)
@@ -773,7 +774,7 @@ import org.apache.directory.mavibot.btre
     private Page<K, V> addElement( long revision, K key, V value, int pos )
     {
         // First copy the current page, but add one element in the copied page
-        Leaf<K, V> newLeaf = new Leaf<K, V>( btree, revision, nbElems + 1 );
+        InMemoryLeaf<K, V> newLeaf = new InMemoryLeaf<K, V>( btree, revision, nbElems + 1 );
 
         // Atm, store the value in memory
         InMemoryValueHolder<V> valueHolder = new InMemoryValueHolder<V>( btree, value );
@@ -781,7 +782,7 @@ import org.apache.directory.mavibot.btre
         // Deal with the special case of an empty page
         if ( nbElems == 0 )
         {
-            newLeaf.setKey( 0, key );
+            newLeaf.setKey( 0, new KeyHolder<K>( key ) );
             newLeaf.values[0] = valueHolder;
         }
         else
@@ -791,7 +792,7 @@ import org.apache.directory.mavibot.btre
             System.arraycopy( values, 0, newLeaf.values, 0, pos );
 
             // Add the new element
-            newLeaf.setKey( pos, key );
+            newLeaf.setKey( pos, new KeyHolder<K>( key ) );
             newLeaf.values[pos] = valueHolder;
 
             // And copy the remaining elements
@@ -821,22 +822,22 @@ import org.apache.directory.mavibot.btre
     private InsertResult<K, V> addAndSplit( long revision, K key, V value, int pos )
     {
         int middle = btree.getPageSize() >> 1;
-        Leaf<K, V> leftLeaf = null;
-        Leaf<K, V> rightLeaf = null;
+        InMemoryLeaf<K, V> leftLeaf = null;
+        InMemoryLeaf<K, V> rightLeaf = null;
         InMemoryValueHolder<V> valueHolder = new InMemoryValueHolder<V>( btree, value );
 
         // Determinate where to store the new value
         if ( pos <= middle )
         {
             // The left page will contain the new value
-            leftLeaf = new Leaf<K, V>( btree, revision, middle + 1 );
+            leftLeaf = new InMemoryLeaf<K, V>( btree, revision, middle + 1 );
 
             // Copy the keys and the values up to the insertion position
             System.arraycopy( getKeys(), 0, leftLeaf.getKeys(), 0, pos );
             System.arraycopy( values, 0, leftLeaf.values, 0, pos );
 
             // Add the new element
-            leftLeaf.setKey( pos, key );
+            leftLeaf.setKey( pos, new KeyHolder<K>( key ) );
             leftLeaf.values[pos] = valueHolder;
 
             // And copy the remaining elements
@@ -844,7 +845,7 @@ import org.apache.directory.mavibot.btre
             System.arraycopy( values, pos, leftLeaf.values, pos + 1, middle - pos );
 
             // Now, create the right page
-            rightLeaf = new Leaf<K, V>( btree, revision, middle );
+            rightLeaf = new InMemoryLeaf<K, V>( btree, revision, middle );
 
             // Copy the keys and the values in the right page
             System.arraycopy( getKeys(), middle, rightLeaf.getKeys(), 0, middle );
@@ -853,14 +854,14 @@ import org.apache.directory.mavibot.btre
         else
         {
             // Create the left page
-            leftLeaf = new Leaf<K, V>( btree, revision, middle );
+            leftLeaf = new InMemoryLeaf<K, V>( btree, revision, middle );
 
             // Copy all the element into the left page
             System.arraycopy( getKeys(), 0, leftLeaf.getKeys(), 0, middle );
             System.arraycopy( values, 0, leftLeaf.values, 0, middle );
 
             // Now, create the right page
-            rightLeaf = new Leaf<K, V>( btree, revision, middle + 1 );
+            rightLeaf = new InMemoryLeaf<K, V>( btree, revision, middle + 1 );
 
             int rightPos = pos - middle;
 
@@ -869,7 +870,7 @@ import org.apache.directory.mavibot.btre
             System.arraycopy( values, middle, rightLeaf.values, 0, rightPos );
 
             // Add the new element
-            rightLeaf.setKey( rightPos, key );
+            rightLeaf.setKey( rightPos, new KeyHolder<K>( key ) );
             rightLeaf.values[rightPos] = valueHolder;
 
             // And copy the remaining elements

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryValueHolder.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryValueHolder.java?rev=1551187&r1=1551186&r2=1551187&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryValueHolder.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryValueHolder.java Mon Dec 16 11:46:32 2013
@@ -29,6 +29,7 @@ import org.apache.directory.mavibot.btre
 import org.apache.directory.mavibot.btree.BTree;
 import org.apache.directory.mavibot.btree.Tuple;
 import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
+import org.apache.directory.mavibot.btree.persisted.PersistedBTree;
 
 
 /**
@@ -51,12 +52,17 @@ public class InMemoryValueHolder<V> exte
     InMemoryValueHolder( BTree<?, V> parentBtree, int nbValues )
     {
         valueSerializer = parentBtree.getValueSerializer();
+
+        if ( nbValues <= 1 )
+        {
+            valueArray = ( V[] ) Array.newInstance( valueSerializer.getType(), nbValues );
+        }
     }
 
 
     /**
      * Creates a new instance of a ValueHolder, containing Values. This constructor is called
-     * whe we need to create a new ValueHolder with deserialized values.
+     * when we need to create a new ValueHolder with deserialized values.
      * 
      * @param parentBtree The parent BTree
      * @param values The Values stored in the ValueHolder
@@ -69,10 +75,12 @@ public class InMemoryValueHolder<V> exte
         {
             int nbValues = values.length;
 
-            if ( nbValues < 2 )
+            if ( nbValues == 1 )
             {
                 // Store the value
-                valueArray = values;
+                valueArray = ( V[] ) Array.newInstance( valueSerializer.getType(), nbValues );
+                valueArray[0] = values[0];
+                nbArrayElems = nbValues;
             }
             else
             {
@@ -107,7 +115,7 @@ public class InMemoryValueHolder<V> exte
         }
         else
         {
-            return 1;
+            return nbArrayElems;
         }
     }
 
@@ -191,6 +199,7 @@ public class InMemoryValueHolder<V> exte
             {
                 valueArray = ( V[] ) Array.newInstance( valueSerializer.getType(), 1 );
                 valueArray[0] = valueBtree.browse().next().getKey();
+                nbArrayElems = 1;
                 valueBtree.close();
                 valueBtree = null;
             }
@@ -226,7 +235,7 @@ public class InMemoryValueHolder<V> exte
         else
         {
             V returnedValue = valueArray[0];
-            valueArray[0] = null;
+            nbArrayElems = 0;
           
             return returnedValue;
         }
@@ -278,7 +287,12 @@ public class InMemoryValueHolder<V> exte
         else
         {
             sb.append( ", {" );
-            sb.append( valueArray[0] );
+            
+            if ( size() != 0 )
+            {
+                sb.append( valueArray[0] );
+            }
+            
             sb.append( "}" );
         }
         

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Node.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Node.java?rev=1551187&r1=1551186&r2=1551187&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Node.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Node.java Mon Dec 16 11:46:32 2013
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.lang.reflect.Array;
 import java.util.List;
 
+import org.apache.directory.mavibot.btree.AbstractPage;
 import org.apache.directory.mavibot.btree.BTree;
 import org.apache.directory.mavibot.btree.BorrowedFromLeftResult;
 import org.apache.directory.mavibot.btree.BorrowedFromRightResult;
@@ -34,6 +35,7 @@ import org.apache.directory.mavibot.btre
 import org.apache.directory.mavibot.btree.ModifyResult;
 import org.apache.directory.mavibot.btree.NotPresentResult;
 import org.apache.directory.mavibot.btree.Page;
+import org.apache.directory.mavibot.btree.PageHolder;
 import org.apache.directory.mavibot.btree.ParentPos;
 import org.apache.directory.mavibot.btree.RemoveResult;
 import org.apache.directory.mavibot.btree.SplitResult;
@@ -54,12 +56,8 @@ import org.apache.directory.mavibot.btre
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-/* No qualifier */class Node<K, V> extends AbstractInMemoryPage<K, V>
+/* No qualifier */class Node<K, V> extends AbstractPage<K, V>
 {
-    /** Children pages associated with keys. */
-    protected Page<K, V>[] children;
-
-
     /**
      * Creates 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
@@ -75,7 +73,7 @@ import org.apache.directory.mavibot.btre
         super( btree, revision, nbElems );
 
         // Create the children array
-        children = ( Page<K, V>[] ) Array.newInstance( Page.class, nbElems + 1 );
+        children = ( PageHolder<K, V>[] ) Array.newInstance( PageHolder.class, nbElems + 1 );
     }
 
 
@@ -96,18 +94,17 @@ import org.apache.directory.mavibot.btre
         super( btree, revision, 1 );
 
         // Create the children array, and store the left and right children
-        children = ( Page[] ) Array.newInstance( Page.class,
-            btree.getPageSize() + 1 );
+        children = ( PageHolder[] ) Array.newInstance( PageHolder.class, btree.getPageSize() + 1 );
 
-        children[0] = leftPage;
-        children[1] = rightPage;
+        children[0] = new PageHolder<K, V>( btree, leftPage );
+        children[1] = new PageHolder<K, V>( btree, 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...
         setKeys( ( KeyHolder<K>[] ) Array.newInstance( KeyHolder.class, btree.getPageSize() ) );
 
-        setKey( 0, key );
+        setKey( 0, new KeyHolder<K>( key ) );
     }
 
 
@@ -127,7 +124,7 @@ import org.apache.directory.mavibot.btre
         }
 
         // Get the child page into which we will insert the <K, V> tuple
-        Page<K, V> child = children[pos];
+        Page<K, V> child = children[pos].getValue();
 
         // and insert the <K, V> into this child
         InsertResult<K, V> result = child.insert( revision, key, value );
@@ -190,16 +187,16 @@ import org.apache.directory.mavibot.btre
 
         if ( found )
         {
-            newPage.children[index + 1] = modifiedPage;
+            newPage.children[index + 1] = new PageHolder<K, V>( btree, modifiedPage );
         }
         else
         {
-            newPage.children[index] = modifiedPage;
+            newPage.children[index] = new PageHolder<K, V>( btree, modifiedPage );
         }
 
         if ( pos < 0 )
         {
-            newPage.setKey( index, removeResult.getModifiedPage().getLeftMostKey() );
+            newPage.setKey( index, new KeyHolder<K>( removeResult.getModifiedPage().getLeftMostKey() ) );
         }
 
         // Modify the result and return
@@ -261,7 +258,7 @@ import org.apache.directory.mavibot.btre
         // Create the new sibling, with one less element at the beginning
         Node<K, V> newSibling = new Node<K, V>( btree, revision, sibling.getNbElems() - 1 );
 
-        K siblingKey = sibling.children[0].getLeftMostKey();
+        K siblingKey = sibling.children[0].getValue().getLeftMostKey();
 
         // Copy the keys and children of the old sibling in the new sibling
         System.arraycopy( sibling.getKeys(), 1, newSibling.getKeys(), 0, newSibling.getNbElems() );
@@ -275,7 +272,7 @@ import org.apache.directory.mavibot.btre
         int index = Math.abs( pos );
 
         // Copy the key and children from sibling
-        newNode.setKey( nbElems - 1, siblingKey ); // 1
+        newNode.setKey( nbElems - 1, new KeyHolder<K>( siblingKey ) ); // 1
         newNode.children[nbElems] = sibling.children[0]; // 8
 
         if ( index < 2 )
@@ -285,7 +282,7 @@ import org.apache.directory.mavibot.btre
 
             // Inject the modified page
             Page<K, V> modifiedPage = mergedResult.getModifiedPage();
-            newNode.children[0] = modifiedPage;
+            newNode.children[0] = new PageHolder<K, V>( btree, modifiedPage );
 
             // Copy the children
             System.arraycopy( children, 2, newNode.children, 1, nbElems - 1 );
@@ -299,7 +296,7 @@ import org.apache.directory.mavibot.btre
             }
 
             // Inject the new modified page key
-            newNode.setKey( index - 2, mergedResult.getModifiedPage().getLeftMostKey() ); // 2
+            newNode.setKey( index - 2, new KeyHolder<K>( mergedResult.getModifiedPage().getLeftMostKey() ) ); // 2
 
             if ( index < nbElems )
             {
@@ -315,7 +312,7 @@ import org.apache.directory.mavibot.btre
 
             // Inject the modified page
             Page<K, V> modifiedPage = mergedResult.getModifiedPage();
-            newNode.children[index - 1] = modifiedPage; // 6
+            newNode.children[index - 1] = new PageHolder<K, V>( btree, modifiedPage ); // 6
         }
 
         // Create the result
@@ -344,7 +341,7 @@ import org.apache.directory.mavibot.btre
         Node<K, V> sibling, int pos ) throws IOException
     {
         // The sibling is on the left, borrow the rightmost element
-        Page<K, V> siblingChild = sibling.children[sibling.nbElems];
+        Page<K, V> siblingChild = sibling.children[sibling.nbElems].getValue();
 
         // Create the new sibling, with one less element at the end
         Node<K, V> newSibling = new Node<K, V>( btree, revision, sibling.getNbElems() - 1 );
@@ -358,23 +355,23 @@ import org.apache.directory.mavibot.btre
         Node<K, V> newNode = new Node<K, V>( btree, revision, nbElems );
 
         // Sets the first children
-        newNode.children[0] = siblingChild; //1
+        newNode.children[0] = new PageHolder<K, V>( btree, siblingChild ); //1
 
         int index = Math.abs( pos );
 
         if ( index < 2 )
         {
-            newNode.setKey( 0, mergedResult.getModifiedPage().getLeftMostKey() );
+            newNode.setKey( 0, new KeyHolder<K>( mergedResult.getModifiedPage().getLeftMostKey() ) );
             System.arraycopy( getKeys(), 1, newNode.getKeys(), 1, nbElems - 1 );
 
             Page<K, V> modifiedPage = mergedResult.getModifiedPage();
-            newNode.children[1] = modifiedPage;
+            newNode.children[1] = new PageHolder<K, V>( btree, modifiedPage );
             System.arraycopy( children, 2, newNode.children, 2, nbElems - 1 );
         }
         else
         {
             // Set the first key
-            newNode.setKey( 0, children[0].getLeftMostKey() ); //2
+            newNode.setKey( 0, new KeyHolder<K>( children[0].getValue().getLeftMostKey() ) ); //2
 
             if ( index > 2 )
             {
@@ -383,7 +380,7 @@ import org.apache.directory.mavibot.btre
             }
 
             // Inject the modified key
-            newNode.setKey( index - 1, mergedResult.getModifiedPage().getLeftMostKey() ); // 3
+            newNode.setKey( index - 1, new KeyHolder<K>( mergedResult.getModifiedPage().getLeftMostKey() ) ); // 3
 
             if ( index < nbElems )
             {
@@ -399,7 +396,7 @@ import org.apache.directory.mavibot.btre
 
             // Insert the modified page
             Page<K, V> modifiedPage = mergedResult.getModifiedPage();
-            newNode.children[index] = modifiedPage; // 7
+            newNode.children[index] = new PageHolder<K, V>( btree, modifiedPage ); // 7
         }
 
         // Create the result
@@ -445,18 +442,18 @@ import org.apache.directory.mavibot.btre
             // Then copy all the elements up to the deletion point
             if ( index < 2 )
             {
-                newNode.setKey( half, mergedResult.getModifiedPage().getLeftMostKey() );
+                newNode.setKey( half, new KeyHolder<K>( mergedResult.getModifiedPage().getLeftMostKey() ) );
                 System.arraycopy( getKeys(), 1, newNode.getKeys(), half + 1, half - 1 );
 
                 Page<K, V> modifiedPage = mergedResult.getModifiedPage();
-                newNode.children[half + 1] = modifiedPage;
+                newNode.children[half + 1] = new PageHolder<K, V>( btree, modifiedPage );
                 System.arraycopy( children, 2, newNode.children, half + 2, half - 1 );
             }
             else
             {
                 // Copy the left part of the node keys up to the deletion point
                 // Insert the new key
-                newNode.setKey( half, children[0].getLeftMostKey() ); // 3
+                newNode.setKey( half, new KeyHolder<K>( children[0].getValue().getLeftMostKey() ) ); // 3
 
                 if ( index > 2 )
                 {
@@ -464,7 +461,7 @@ import org.apache.directory.mavibot.btre
                 }
 
                 // Inject the new merged key
-                newNode.setKey( half + index - 1, mergedResult.getModifiedPage().getLeftMostKey() ); //5
+                newNode.setKey( half + index - 1, new KeyHolder<K>( mergedResult.getModifiedPage().getLeftMostKey() ) ); //5
 
                 if ( index < half )
                 {
@@ -477,7 +474,7 @@ import org.apache.directory.mavibot.btre
 
                 // Inject the new merged child
                 Page<K, V> modifiedPage = mergedResult.getModifiedPage();
-                newNode.children[half + index] = modifiedPage; //8
+                newNode.children[half + index] = new PageHolder<K, V>( btree, modifiedPage ); //8
             }
         }
         else
@@ -490,7 +487,7 @@ import org.apache.directory.mavibot.btre
 
                 // Insert the first child
                 Page<K, V> modifiedPage = mergedResult.getModifiedPage();
-                newNode.children[0] = modifiedPage;
+                newNode.children[0] = new PageHolder<K, V>( btree, modifiedPage );
 
                 // Copy the node children
                 System.arraycopy( children, 2, newNode.children, 1, half - 1 );
@@ -508,11 +505,11 @@ import org.apache.directory.mavibot.btre
                 System.arraycopy( children, 0, newNode.children, 0, index - 1 ); //6
 
                 // Inject the modified key
-                newNode.setKey( index - 2, mergedResult.getModifiedPage().getLeftMostKey() ); //2
+                newNode.setKey( index - 2, new KeyHolder<K>( mergedResult.getModifiedPage().getLeftMostKey() ) ); //2
 
                 // Inject the modified children
                 Page<K, V> modifiedPage = mergedResult.getModifiedPage();
-                newNode.children[index - 1] = modifiedPage; // 7
+                newNode.children[index - 1] = new PageHolder<K, V>( btree, modifiedPage ); // 7
 
                 // Add the remaining node's key if needed
                 if ( index < half )
@@ -525,7 +522,7 @@ import org.apache.directory.mavibot.btre
             }
 
             // Inject the new key from sibling
-            newNode.setKey( half - 1, sibling.findLeftMost().getKey() ); //3
+            newNode.setKey( half - 1, new KeyHolder<K>( sibling.findLeftMost().getKey() ) ); //3
 
             // Copy the sibling keys
             System.arraycopy( sibling.getKeys(), 0, newNode.getKeys(), half, half );
@@ -562,12 +559,12 @@ import org.apache.directory.mavibot.btre
         if ( found )
         {
             index = -( pos + 1 );
-            child = children[-pos];
+            child = children[-pos].getValue();
             deleteResult = child.delete( revision, key, value, this, -pos );
         }
         else
         {
-            child = children[pos];
+            child = children[pos].getValue();
             deleteResult = child.delete( revision, key, value, this, pos );
         }
 
@@ -631,7 +628,7 @@ import org.apache.directory.mavibot.btre
                 // a sibling, or we will have to merge two pages
                 int siblingPos = selectSibling( ( Node<K, V> ) parent, parentPos );
 
-                Node<K, V> sibling = ( Node<K, V> ) ( ( ( Node<K, V> ) parent ).children[siblingPos] );
+                Node<K, V> sibling = ( Node<K, V> ) ( ( ( Node<K, V> ) parent ).children[siblingPos].getValue() );
 
                 if ( sibling.getNbElems() > halfSize )
                 {
@@ -690,21 +687,21 @@ import org.apache.directory.mavibot.btre
             if ( borrowedResult.isFromRight() )
             {
                 // Update the keys
-                newPage.setKey( pos, modifiedPage.findLeftMost().getKey() );
-                newPage.setKey( pos + 1, modifiedSibling.findLeftMost().getKey() );
+                newPage.setKey( pos, new KeyHolder<K>( modifiedPage.findLeftMost().getKey() ) );
+                newPage.setKey( pos + 1, new KeyHolder<K>( modifiedSibling.findLeftMost().getKey() ) );
 
                 // Update the children
-                newPage.children[pos + 1] = modifiedPage;
-                newPage.children[pos + 2] = modifiedSibling;
+                newPage.children[pos + 1] = new PageHolder<K, V>( btree, modifiedPage );
+                newPage.children[pos + 2] = new PageHolder<K, V>( btree, modifiedSibling );
             }
             else
             {
                 // Update the keys
-                newPage.setKey( pos, modifiedPage.findLeftMost().getKey() );
+                newPage.setKey( pos, new KeyHolder<K>( modifiedPage.findLeftMost().getKey() ) );
 
                 // Update the children
-                newPage.children[pos] = modifiedSibling;
-                newPage.children[pos + 1] = modifiedPage;
+                newPage.children[pos] = new PageHolder<K, V>( btree, modifiedSibling );
+                newPage.children[pos + 1] = new PageHolder<K, V>( btree, modifiedPage );
             }
         }
         else
@@ -712,20 +709,20 @@ import org.apache.directory.mavibot.btre
             if ( borrowedResult.isFromRight() )
             {
                 // Update the keys
-                newPage.setKey( pos, modifiedSibling.findLeftMost().getKey() );
+                newPage.setKey( pos, new KeyHolder<K>( modifiedSibling.findLeftMost().getKey() ) );
 
                 // Update the children
-                newPage.children[pos] = modifiedPage;
-                newPage.children[pos + 1] = modifiedSibling;
+                newPage.children[pos] = new PageHolder<K, V>( btree, modifiedPage );
+                newPage.children[pos + 1] = new PageHolder<K, V>( btree, modifiedSibling );
             }
             else
             {
                 // Update the keys
-                newPage.setKey( pos - 1, modifiedPage.findLeftMost().getKey() );
+                newPage.setKey( pos - 1, new KeyHolder<K>( modifiedPage.findLeftMost().getKey() ) );
 
                 // Update the children
-                newPage.children[pos - 1] = modifiedSibling;
-                newPage.children[pos] = modifiedPage;
+                newPage.children[pos - 1] = new PageHolder<K, V>( btree, modifiedSibling );
+                newPage.children[pos] = new PageHolder<K, V>( btree, modifiedPage );
             }
         }
 
@@ -762,7 +759,7 @@ import org.apache.directory.mavibot.btre
             // Copy the keys and the children
             System.arraycopy( getKeys(), 1, newNode.getKeys(), 0, newNode.nbElems );
             Page<K, V> modifiedPage = mergedResult.getModifiedPage();
-            newNode.children[0] = modifiedPage;
+            newNode.children[0] = new PageHolder<K, V>( btree, modifiedPage );
             System.arraycopy( children, 2, newNode.children, 1, nbElems - 1 );
         }
         else
@@ -773,7 +770,7 @@ import org.apache.directory.mavibot.btre
                 System.arraycopy( getKeys(), 0, newNode.getKeys(), 0, index );
             }
 
-            newNode.setKey( index, mergedResult.getModifiedPage().findLeftMost().getKey() );
+            newNode.setKey( index, new KeyHolder<K>( mergedResult.getModifiedPage().findLeftMost().getKey() ) );
 
             if ( index < nbElems - 2 )
             {
@@ -784,7 +781,7 @@ import org.apache.directory.mavibot.btre
             System.arraycopy( children, 0, newNode.children, 0, index + 1 );
 
             Page<K, V> modifiedPage = mergedResult.getModifiedPage();
-            newNode.children[index + 1] = modifiedPage;
+            newNode.children[index + 1] = new PageHolder<K, V>( btree, modifiedPage );
 
             if ( index < nbElems - 2 )
             {
@@ -813,11 +810,11 @@ import org.apache.directory.mavibot.btre
         {
             // Here, if we have found the key in the node, then we must go down into
             // the right child, not the left one
-            return children[-pos].get( key );
+            return children[-pos].getValue().get( key );
         }
         else
         {
-            return children[pos].get( key );
+            return children[pos].getValue().get( key );
         }
     }
 
@@ -834,11 +831,11 @@ import org.apache.directory.mavibot.btre
         {
             // Here, if we have found the key in the node, then we must go down into
             // the right child, not the left one
-            return children[-pos].getValues( key );
+            return children[-pos].getValue().getValues( key );
         }
         else
         {
-            return children[pos].getValues( key );
+            return children[pos].getValue().getValues( key );
         }
     }
 
@@ -855,11 +852,11 @@ import org.apache.directory.mavibot.btre
         {
             // Here, if we have found the key in the node, then we must go down into
             // the right child, not the left one
-            return children[-pos].hasKey( key );
+            return children[-pos].getValue().hasKey( key );
         }
         else
         {
-            Page<K, V> page = children[pos];
+            Page<K, V> page = children[pos].getValue();
 
             if ( page == null )
             {
@@ -883,11 +880,11 @@ import org.apache.directory.mavibot.btre
         {
             // Here, if we have found the key in the node, then we must go down into
             // the right child, not the left one
-            return children[-pos].contains( key, value );
+            return children[-pos].getValue().contains( key, value );
         }
         else
         {
-            return children[pos].contains( key, value );
+            return children[pos].getValue().contains( key, value );
         }
     }
 
@@ -900,7 +897,7 @@ import org.apache.directory.mavibot.btre
      */
     public void setValue( int pos, Page<K, V> value )
     {
-        children[pos] = value;
+        children[pos] = new PageHolder<K, V>( btree, value );
     }
 
 
@@ -911,7 +908,7 @@ import org.apache.directory.mavibot.btre
     {
         if ( pos < nbElems + 1 )
         {
-            return children[pos];
+            return children[pos].getValue();
         }
         else
         {
@@ -936,7 +933,7 @@ import org.apache.directory.mavibot.btre
         // We first stack the current page
         stack[depth++] = new ParentPos<K, V>( this, pos );
         
-        Page<K, V> page = children[pos];
+        Page<K, V> page = children[pos].getValue();
 
         return page.browse( key, transaction, stack, depth );
     }
@@ -950,7 +947,7 @@ import org.apache.directory.mavibot.btre
     {
         stack[depth++] = new ParentPos<K, V>( this, 0 );
         
-        Page<K, V> page = children[0];
+        Page<K, V> page = children[0].getValue();
 
         return page.browse( transaction, stack, depth );
     }
@@ -976,7 +973,7 @@ import org.apache.directory.mavibot.btre
         // to point on the modified child
         Page<K, V> modifiedPage = result.getModifiedPage();
 
-        ( ( Node<K, V> ) newPage ).children[pos] = modifiedPage;
+        ( ( Node<K, V> ) newPage ).children[pos] = new PageHolder<K, V>( btree, modifiedPage );
 
         // We can return the result, where we update the modifiedPage,
         // to avoid the creation of a new object
@@ -1016,12 +1013,12 @@ import org.apache.directory.mavibot.btre
         }
 
         // Add the new key and children
-        newNode.setKey( pos, key );
+        newNode.setKey( pos, new KeyHolder<K>( key ) );
 
         // 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
-        newNode.children[pos] = leftPage;
-        newNode.children[pos + 1] = rightPage;
+        newNode.children[pos] = new PageHolder<K, V>( btree, leftPage );
+        newNode.children[pos + 1] = new PageHolder<K, V>( btree, rightPage );
 
         // And copy the remaining keys and children
         if ( nbElems > 0 )
@@ -1075,9 +1072,9 @@ import org.apache.directory.mavibot.btre
             System.arraycopy( children, 0, newLeftPage.children, 0, pos );
 
             // Add the new element
-            newLeftPage.setKey( pos, pivot );
-            newLeftPage.children[pos] = leftPage;
-            newLeftPage.children[pos + 1] = rightPage;
+            newLeftPage.setKey( pos, new KeyHolder<K>( pivot ) );
+            newLeftPage.children[pos] = new PageHolder<K, V>( btree, leftPage );
+            newLeftPage.children[pos + 1] = new PageHolder<K, V>( btree, rightPage );
 
             // And copy the remaining elements minus the new pivot
             System.arraycopy( getKeys(), pos, newLeftPage.getKeys(), pos + 1, middle - pos - 1 );
@@ -1100,12 +1097,12 @@ import org.apache.directory.mavibot.btre
             // Copy the keys and the children up to the insertion position (here, middle)
             System.arraycopy( getKeys(), 0, newLeftPage.getKeys(), 0, middle );
             System.arraycopy( children, 0, newLeftPage.children, 0, middle );
-            newLeftPage.children[middle] = leftPage;
+            newLeftPage.children[middle] = new PageHolder<K, V>( btree, leftPage );
 
             // And process the right page now
             System.arraycopy( getKeys(), middle, newRightPage.getKeys(), 0, middle );
             System.arraycopy( children, middle + 1, newRightPage.children, 1, middle );
-            newRightPage.children[0] = rightPage;
+            newRightPage.children[0] = new PageHolder<K, V>( btree, rightPage );
 
             // Create the result
             InsertResult<K, V> result = new SplitResult<K, V>( copiedPages, pivot, newLeftPage, newRightPage );
@@ -1124,9 +1121,9 @@ import org.apache.directory.mavibot.btre
             System.arraycopy( children, middle + 1, newRightPage.children, 0, pos - middle - 1 );
 
             // Add the new element
-            newRightPage.setKey( pos - middle - 1, pivot );
-            newRightPage.children[pos - middle - 1] = leftPage;
-            newRightPage.children[pos - middle] = rightPage;
+            newRightPage.setKey( pos - middle - 1, new KeyHolder<K>( pivot ) );
+            newRightPage.children[pos - middle - 1] = new PageHolder<K, V>( btree, leftPage );
+            newRightPage.children[pos - middle] = new PageHolder<K, V>( btree, rightPage );
 
             // And copy the remaining elements minus the new pivot
             System.arraycopy( getKeys(), pos, newRightPage.getKeys(), pos - middle, nbElems - pos );
@@ -1166,7 +1163,7 @@ import org.apache.directory.mavibot.btre
      */
     public K getLeftMostKey()
     {
-        return children[0].getLeftMostKey();
+        return children[0].getValue().getLeftMostKey();
     }
 
 
@@ -1179,10 +1176,10 @@ import org.apache.directory.mavibot.btre
 
         if ( children[index] != null )
         {
-            return children[index].getRightMostKey();
+            return children[index].getValue().getRightMostKey();
         }
 
-        return children[nbElems - 1].getRightMostKey();
+        return children[nbElems - 1].getValue().getRightMostKey();
     }
 
 
@@ -1191,7 +1188,7 @@ import org.apache.directory.mavibot.btre
      */
     public Tuple<K, V> findLeftMost() throws EndOfFileExceededException, IOException
     {
-        return children[0].findLeftMost();
+        return children[0].getValue().findLeftMost();
     }
 
 
@@ -1200,7 +1197,7 @@ import org.apache.directory.mavibot.btre
      */
     public Tuple<K, V> findRightMost() throws EndOfFileExceededException, IOException
     {
-        return children[nbElems].findRightMost();
+        return children[nbElems].getValue().findRightMost();
     }
 
 
@@ -1224,7 +1221,7 @@ import org.apache.directory.mavibot.btre
             }
             else
             {
-                sb.append( 'r' ).append( children[0].getRevision() );
+                sb.append( 'r' ).append( children[0].getValue().getRevision() );
             }
 
             for ( int i = 0; i < nbElems; i++ )
@@ -1237,7 +1234,7 @@ import org.apache.directory.mavibot.btre
                 }
                 else
                 {
-                    sb.append( 'r' ).append( children[i + 1].getRevision() );
+                    sb.append( 'r' ).append( children[i + 1].getValue().getRevision() );
                 }
             }
         }
@@ -1258,14 +1255,14 @@ import org.apache.directory.mavibot.btre
         if ( nbElems > 0 )
         {
             // Start with the first child
-            sb.append( children[0].dumpPage( tabs + "    " ) );
+            sb.append( children[0].getValue().dumpPage( tabs + "    " ) );
 
             for ( int i = 0; i < nbElems; i++ )
             {
                 sb.append( tabs );
                 sb.append( "<" );
                 sb.append( getKey( i ) ).append( ">\n" );
-                sb.append( children[i + 1].dumpPage( tabs + "    " ) );
+                sb.append( children[i + 1].getValue().dumpPage( tabs + "    " ) );
             }
         }
 

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/TupleCursorImpl.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/TupleCursorImpl.java?rev=1551187&r1=1551186&r2=1551187&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/TupleCursorImpl.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/TupleCursorImpl.java Mon Dec 16 11:46:32 2013
@@ -86,7 +86,7 @@ public class TupleCursorImpl<K, V> exten
                 parentPos.pos = parentPos.page.getNbElems();
             }
 
-            child = ((Node<K, V>)parentPos.page).children[parentPos.pos];
+            child = ((Node<K, V>)parentPos.page).getPage( parentPos.pos );
         }
         
         // and leaf
@@ -102,7 +102,7 @@ public class TupleCursorImpl<K, V> exten
             parentPos.pos = child.getNbElems() - 1;
         }
 
-        parentPos.valueCursor = ((Leaf<K, V>)parentPos.page).values[parentPos.pos].getCursor();
+        parentPos.valueCursor = ((InMemoryLeaf<K, V>)parentPos.page).values[parentPos.pos].getCursor();
         parentPos.valueCursor.afterLast();
         parentPos.pos = AFTER_LAST;
     }
@@ -131,7 +131,7 @@ public class TupleCursorImpl<K, V> exten
                 parentPos.page = child;
             }
 
-            child = ((Node<K, V>)parentPos.page).children[0];
+            child = ((Node<K, V>)parentPos.page).getPage( 0 );
         }
 
         // and leaf
@@ -145,7 +145,7 @@ public class TupleCursorImpl<K, V> exten
         
         if ( parentPos.valueCursor != null )
         {
-            parentPos.valueCursor = ((Leaf<K, V>)parentPos.page).values[0].getCursor();
+            parentPos.valueCursor = ((InMemoryLeaf<K, V>)parentPos.page).values[0].getCursor();
             parentPos.valueCursor.beforeFirst();
         }
     }
@@ -185,7 +185,7 @@ public class TupleCursorImpl<K, V> exten
             {
                 // We can pick the next element at this level
                 parentPos.pos++;
-                child = ((Node<K, V>)parentPos.page).children[parentPos.pos];
+                child = ((Node<K, V>)parentPos.page).getPage( parentPos.pos );
                 
                 // and go down the tree through the nodes
                 while ( currentDepth < depth - 1 )
@@ -194,14 +194,14 @@ public class TupleCursorImpl<K, V> exten
                     parentPos = stack[currentDepth];
                     parentPos.pos = 0;
                     parentPos.page = child;
-                    child = ((Node<K, V>)child).children[0];
+                    child = ((Node<K, V>)child).getPage( 0 );
                 }
 
                 // and the leaf
                 parentPos = stack[depth];
                 parentPos.page = child;
                 parentPos.pos = 0;
-                parentPos.valueCursor = ((Leaf<K, V>)child).values[0].getCursor();
+                parentPos.valueCursor = ((InMemoryLeaf<K, V>)child).values[0].getCursor();
 
                 return parentPos;
             }
@@ -245,7 +245,7 @@ public class TupleCursorImpl<K, V> exten
             {
                 // We can pick the next element at this level
                 parentPos.pos--;
-                child = ((Node<K, V>)parentPos.page).children[parentPos.pos];
+                child = ((Node<K, V>)parentPos.page).getPage( parentPos.pos );
                 
                 // and go down the tree through the nodes
                 while ( currentDepth < depth - 1 )
@@ -254,14 +254,14 @@ public class TupleCursorImpl<K, V> exten
                     parentPos = stack[currentDepth];
                     parentPos.pos = child.getNbElems();
                     parentPos.page = child;
-                    child = ((Node<K, V>)parentPos.page).children[parentPos.page.getNbElems()];
+                    child = ((Node<K, V>)parentPos.page).getPage( parentPos.page.getNbElems() );
                 }
 
                 // and the leaf
                 parentPos = stack[depth];
                 parentPos.pos = child.getNbElems() - 1;
                 parentPos.page = child;
-                ValueHolder<V> valueHolder = ((Leaf<K, V>)parentPos.page).values[parentPos.pos];
+                ValueHolder<V> valueHolder = ((InMemoryLeaf<K, V>)parentPos.page).values[parentPos.pos];
                 parentPos.valueCursor = valueHolder.getCursor();
                 parentPos.valueCursor.afterLast();
 
@@ -402,13 +402,13 @@ public class TupleCursorImpl<K, V> exten
             else
             {
                 // We can pick the next element at this level
-                child = ((Node<K, V>)parentPos.page).children[parentPos.pos + 1];
+                child = ((Node<K, V>)parentPos.page).getPage( parentPos.pos + 1 );
                 
                 // and go down the tree through the nodes
                 while ( currentDepth < depth - 1 )
                 {
                     currentDepth++;
-                    child = ((Node<K, V>)child).children[0];
+                    child = ((Node<K, V>)child).getPage( 0 );
                 }
 
                 return true;
@@ -548,13 +548,13 @@ public class TupleCursorImpl<K, V> exten
             else
             {
                 // We can pick the previous element at this level
-                child = ((Node<K, V>)parentPos.page).children[parentPos.pos - 1];
+                child = ((Node<K, V>)parentPos.page).getPage( parentPos.pos - 1 );
                 
                 // and go down the tree through the nodes
                 while ( currentDepth < depth - 1 )
                 {
                     currentDepth++;
-                    child = ((Node<K, V>)child).children[child.getNbElems()];
+                    child = ((Node<K, V>)child).getPage( child.getNbElems() );
                 }
 
                 return true;
@@ -630,7 +630,7 @@ public class TupleCursorImpl<K, V> exten
 
             try
             {
-                ValueHolder<V> valueHolder = ( ( Leaf<K, V> ) parentPos.page ).getValue( parentPos.pos );
+                ValueHolder<V> valueHolder = ( ( InMemoryLeaf<K, V> ) parentPos.page ).getValue( parentPos.pos );
                 
                 parentPos.valueCursor = valueHolder.getCursor();
                 
@@ -642,7 +642,7 @@ public class TupleCursorImpl<K, V> exten
             }
         }
         
-        Leaf<K, V> leaf = ( Leaf<K, V> ) ( parentPos.page );
+        InMemoryLeaf<K, V> leaf = ( InMemoryLeaf<K, V> ) ( parentPos.page );
         tuple.setKey( leaf.getKey( parentPos.pos ) );
         tuple.setValue( value );
 
@@ -681,7 +681,7 @@ public class TupleCursorImpl<K, V> exten
             if ( ( newParentPos == null ) || ( newParentPos.page == null ) )
             {
                 // This is the end : no more value
-                Leaf<K, V> leaf = ( Leaf<K, V> ) ( parentPos.page );
+                InMemoryLeaf<K, V> leaf = ( InMemoryLeaf<K, V> ) ( parentPos.page );
                 ValueHolder<V> valueHolder = leaf.values[parentPos.pos];
                 parentPos.pos = AFTER_LAST;
                 parentPos.valueCursor = valueHolder.getCursor();
@@ -701,7 +701,7 @@ public class TupleCursorImpl<K, V> exten
         }
 
         // The key
-        Leaf<K, V> leaf = ( Leaf<K, V> ) ( parentPos.page );
+        InMemoryLeaf<K, V> leaf = ( InMemoryLeaf<K, V> ) ( parentPos.page );
         tuple.setKey( leaf.getKey( parentPos.pos ) );
         
         // The value
@@ -778,7 +778,7 @@ public class TupleCursorImpl<K, V> exten
                 
                 try
                 {
-                    ValueHolder<V> valueHolder = ( ( Leaf<K, V> ) parentPos.page ).getValue( parentPos.pos );
+                    ValueHolder<V> valueHolder = ( ( InMemoryLeaf<K, V> ) parentPos.page ).getValue( parentPos.pos );
                     
                     parentPos.valueCursor = valueHolder.getCursor();
                     parentPos.valueCursor.afterLast();
@@ -793,7 +793,7 @@ public class TupleCursorImpl<K, V> exten
         }
 
 
-        Leaf<K, V> leaf = ( Leaf<K, V> ) ( parentPos.page );
+        InMemoryLeaf<K, V> leaf = ( InMemoryLeaf<K, V> ) ( parentPos.page );
         tuple.setKey( leaf.getKey( parentPos.pos ) );
         tuple.setValue( value );
 
@@ -846,7 +846,7 @@ public class TupleCursorImpl<K, V> exten
         }
         
         // Update the Tuple 
-        Leaf<K, V> leaf = ( Leaf<K, V> ) ( parentPos.page );
+        InMemoryLeaf<K, V> leaf = ( InMemoryLeaf<K, V> ) ( parentPos.page );
 
         // The key
         tuple.setKey( leaf.getKey( parentPos.pos ) );

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/BTreeFactory.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/BTreeFactory.java?rev=1551187&r1=1551186&r2=1551187&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/BTreeFactory.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/BTreeFactory.java Mon Dec 16 11:46:32 2013
@@ -23,7 +23,9 @@ package org.apache.directory.mavibot.btr
 import java.io.IOException;
 import java.util.LinkedList;
 
+import org.apache.directory.mavibot.btree.AbstractPage;
 import org.apache.directory.mavibot.btree.BTree;
+import org.apache.directory.mavibot.btree.KeyHolder;
 import org.apache.directory.mavibot.btree.Page;
 import org.apache.directory.mavibot.btree.ParentPos;
 import org.apache.directory.mavibot.btree.ValueHolder;
@@ -90,9 +92,9 @@ public class BTreeFactory
      * @param nbElems The number or elements in this leaf
      * @return A Leaf instance
      */
-    public static <K, V> Leaf<K, V> createLeaf( BTree<K, V> btree, long revision, int nbElems )
+    public static <K, V> PersistedLeaf<K, V> createLeaf( BTree<K, V> btree, long revision, int nbElems )
     {
-        Leaf<K, V> leaf = new Leaf<K, V>( btree, revision, nbElems );
+        PersistedLeaf<K, V> leaf = new PersistedLeaf<K, V>( btree, revision, nbElems );
 
         return leaf;
     }
@@ -232,9 +234,10 @@ public class BTreeFactory
      * @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, K key )
+    public static <K, V> void setKey( BTree<K, V> btree, Page<K, V> page, int pos, K key )
     {
-        ( ( AbstractPersistedPage<K, V> ) page ).setKey( pos, key );
+        KeyHolder<K> keyHolder = new PersistedKeyHolder<K>( btree.getKeySerializer(), key );
+        ( ( AbstractPage<K, V> ) page ).setKey( pos, keyHolder );
     }
 
 
@@ -244,9 +247,10 @@ public class BTreeFactory
      * @param pos the position of this key in the page
      * @param buffer the byte[] containing the serialized key
      */
-    public static <K, V> void setKey( Page<K, V> page, int pos, byte[] buffer )
+    public static <K, V> void setKey( BTree<K, V> btree, Page<K, V> page, int pos, byte[] buffer )
     {
-        ( ( AbstractPersistedPage<K, V> ) page ).setKey( pos, buffer );
+        KeyHolder<K> keyHolder = new PersistedKeyHolder<K>( btree.getKeySerializer(), buffer );
+        ( ( AbstractPage<K, V> ) page ).setKey( pos, keyHolder );
     }
 
 
@@ -255,7 +259,7 @@ public class BTreeFactory
      * @param pos The position in the values array
      * @param value the value to inject
      */
-    public static <K, V> void setValue( Leaf<K, V> page, int pos, PersistedValueHolder<V> value )
+    public static <K, V> void setValue( PersistedLeaf<K, V> page, int pos, PersistedValueHolder<V> value )
     {
         page.setValue( pos, value );
     }
@@ -266,7 +270,7 @@ public class BTreeFactory
      * @param pos The position in the values array
      * @param value the value to inject
      */
-    public static <K, V> void setValue( Node<K, V> page, int pos, PageHolder<K, V> value )
+    public static <K, V> void setValue( Node<K, V> page, int pos, PersistedPageHolder<K, V> value )
     {
         page.setValue( pos, value );
     }
@@ -286,9 +290,9 @@ public class BTreeFactory
         ParentPos<K, V> last = new ParentPos<K, V>( btree.getRootPage(), btree.getRootPage().getNbElems() );
         stack.push( last );
 
-        if ( btree.getRootPage() instanceof Leaf )
+        if ( btree.getRootPage() instanceof PersistedLeaf )
         {
-            Leaf<K, V> leaf = ( Leaf<K, V> ) ( btree.getRootPage() );
+            PersistedLeaf<K, V> leaf = ( PersistedLeaf<K, V> ) ( btree.getRootPage() );
             ValueHolder<V> valueHolder = leaf.values[last.pos];
             last.valueCursor = valueHolder.getCursor();
         }
@@ -298,14 +302,14 @@ public class BTreeFactory
 
             while ( true )
             {
-                Page<K, V> p = node.children[node.getNbElems()].getValue( btree );
+                Page<K, V> p = node.children[node.getNbElems()].getValue();
 
                 last = new ParentPos<K, V>( p, p.getNbElems() );
                 stack.push( last );
 
-                if ( p instanceof Leaf )
+                if ( p instanceof PersistedLeaf )
                 {
-                    Leaf<K, V> leaf = ( Leaf<K, V> ) ( last.page );
+                    PersistedLeaf<K, V> leaf = ( PersistedLeaf<K, V> ) ( last.page );
                     ValueHolder<V> valueHolder = leaf.values[last.pos];
                     last.valueCursor = valueHolder.getCursor();
                     break;
@@ -331,9 +335,9 @@ public class BTreeFactory
         ParentPos<K, V> first = new ParentPos<K, V>( btree.getRootPage(), 0 );
         stack.push( first );
 
-        if ( btree.getRootPage() instanceof Leaf )
+        if ( btree.getRootPage() instanceof PersistedLeaf )
         {
-            Leaf<K, V> leaf = ( Leaf<K, V> ) ( btree.getRootPage() );
+            PersistedLeaf<K, V> leaf = ( PersistedLeaf<K, V> ) ( btree.getRootPage() );
             ValueHolder<V> valueHolder = leaf.values[first.pos];
             first.valueCursor = valueHolder.getCursor();
         }
@@ -343,14 +347,14 @@ public class BTreeFactory
 
             while ( true )
             {
-                Page<K, V> page = node.children[0].getValue( btree );
+                Page<K, V> page = node.children[0].getValue();
 
                 first = new ParentPos<K, V>( page, 0 );
                 stack.push( first );
 
-                if ( page instanceof Leaf )
+                if ( page instanceof PersistedLeaf )
                 {
-                    Leaf<K, V> leaf = ( Leaf<K, V> ) ( page );
+                    PersistedLeaf<K, V> leaf = ( PersistedLeaf<K, V> ) ( page );
                     ValueHolder<V> valueHolder = leaf.values[first.pos];
                     first.valueCursor = valueHolder.getCursor();
                     break;



Mime
View raw message