You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by el...@apache.org on 2013/03/07 00:14:10 UTC

svn commit: r1453609 - in /labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree: BTree.java BTreeFactory.java Leaf.java Node.java Page.java store/RecordManager.java

Author: elecharny
Date: Wed Mar  6 23:14:10 2013
New Revision: 1453609

URL: http://svn.apache.org/r1453609
Log:
o Added a BTreeFactory to create BTrees, Nodes and Leaf 
o Made some of the BTree setters package protected
o Node and Leaf class are now public
o Added the code taht create the BTrees in the RecordManager

Added:
    labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTreeFactory.java
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/Page.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=1453609&r1=1453608&r2=1453609&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 Wed Mar  6 23:14:10 2013
@@ -313,7 +313,7 @@ public class BTree<K, V>
     /**
      * Creates a new BTree, with no initialization. 
      */
-    public BTree() throws IOException
+    public BTree()
     {
     }
 
@@ -1413,7 +1413,7 @@ public class BTree<K, V>
     /**
      * @param revision the revision to set
      */
-    public void setRevision( long revision )
+    /* No qualifier */void setRevision( long revision )
     {
         this.revision.set( revision );
     }
@@ -1431,7 +1431,7 @@ public class BTree<K, V>
     /**
      * @param nbElems the nbElems to set
      */
-    public void setNbElems( long nbElems )
+    /* No qualifier */void setNbElems( long nbElems )
     {
         this.nbElems.set( nbElems );
     }

Added: labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTreeFactory.java
URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTreeFactory.java?rev=1453609&view=auto
==============================================================================
--- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTreeFactory.java (added)
+++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTreeFactory.java Wed Mar  6 23:14:10 2013
@@ -0,0 +1,163 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mavibot.btree;
+
+
+import org.apache.mavibot.btree.serializer.ElementSerializer;
+
+
+/**
+ * This cless construct a BTree from a serialized version of a BTree. We need it
+ * to avoid exposing all the methods of the BTree class.<br>
+ * 
+ * All its methods are static.
+ *  
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class BTreeFactory
+{
+    /**
+     * Create a new BTree.
+     * 
+     * @return The created BTree
+     */
+    public static BTree createBTree()
+    {
+        BTree btree = new BTree();
+
+        return btree;
+    }
+
+
+    /**
+     * Create a new Node for the give BTree.
+     * 
+     * @param btree The BTree which will contain this node
+     * @param revision The Node's revision
+     * @param nbElems The number or elements in this node
+     * @return A Node instance
+     */
+    public static Page createNode( BTree btree, long revision, int nbElems )
+    {
+        Page node = new Node( btree, revision, nbElems );
+
+        return node;
+    }
+
+
+    /**
+     * Create a new Leaf for the give BTree.
+     * 
+     * @param btree The BTree which will contain this leaf
+     * @param revision The Leaf's revision
+     * @param nbElems The number or elements in this leaf
+     * @return A Leaf instance
+     */
+    public static Page createLeaf( BTree btree, long revision, int nbElems )
+    {
+        Page leaf = new Leaf( btree, revision, nbElems );
+
+        return leaf;
+    }
+
+
+    /**
+     * Set the new root page for this tree. Used for debug purpose only. The revision
+     * will always be 0;
+     * 
+     * @param root the new root page.
+     */
+    public static void setRoot( BTree<?, ?> btree, Page root )
+    {
+        btree.setRoot( root );
+    }
+
+
+    /**
+     * @param nbElems the nbElems to set
+     */
+    public static void setNbElems( BTree<?, ?> btree, long nbElems )
+    {
+        btree.setNbElems( nbElems );
+    }
+
+
+    /**
+     * @param revision the revision to set
+     */
+    public static void setRevision( BTree<?, ?> btree, long revision )
+    {
+        btree.setRevision( revision );
+    }
+
+
+    /**
+     * @param name the name to set
+     */
+    public static void setName( BTree<?, ?> btree, String name )
+    {
+        btree.setName( name );
+    }
+
+
+    /**
+     * Sets the KeySerializer into the BTree
+     *  
+     * @param btree The BTree to update
+     * @param keySerializerFqcn the Key serializer FQCN to set
+     * @throws ClassNotFoundException
+     * @throws InstantiationException 
+     * @throws IllegalAccessException
+     */
+    public static void setKeySerializer( BTree<?, ?> btree, String keySerializerFqcn )
+        throws ClassNotFoundException, IllegalAccessException, InstantiationException
+    {
+        Class<?> keySerializer = Class.forName( keySerializerFqcn );
+        btree.setKeySerializer( ( ElementSerializer ) keySerializer.newInstance() );
+    }
+
+
+    /**
+     * Sets the ValueSerializer into the BTree
+     *  
+     * @param btree The BTree to update
+     * @param valueSerializerFqcn the Value serializer FQCN to set
+     * @throws ClassNotFoundException
+     * @throws InstantiationException 
+     * @throws IllegalAccessException
+     */
+    public static void setValueSerializer( BTree<?, ?> btree, String valueSerializerFqcn )
+        throws ClassNotFoundException, IllegalAccessException, InstantiationException
+    {
+        Class<?> valueSerializer = Class.forName( valueSerializerFqcn );
+        btree.setValueSerializer( ( ElementSerializer ) valueSerializer.newInstance() );
+    }
+
+
+    /**
+     * Set the maximum number of elements we can store in a page.
+     * 
+     * @param pageSize The requested page size
+     */
+    public static void setPageSize( BTree<?, ?> btree, int pageSize )
+    {
+        btree.setPageSize( pageSize );
+    }
+}

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=1453609&r1=1453608&r2=1453609&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 Wed Mar  6 23:14:10 2013
@@ -54,7 +54,7 @@ public class Leaf<K, V> extends Abstract
      */
     @SuppressWarnings("unchecked")
     // Cannot create an array of generic objects
-    private Leaf( BTree<K, V> btree, long revision, int nbElems )
+    /* No qualifier */Leaf( BTree<K, V> btree, long revision, int nbElems )
     {
         super( btree, revision, nbElems );
 

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=1453609&r1=1453608&r2=1453609&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 Wed Mar  6 23:14:10 2013
@@ -36,7 +36,7 @@ import org.apache.mavibot.btree.exceptio
  *
  * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
  */
-/* No qualifier */class Node<K, V> extends AbstractPage<K, V>
+public class Node<K, V> extends AbstractPage<K, V>
 {
     /** Children pages associated with keys. */
     protected Page<K, V>[] children;

Modified: labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Page.java
URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Page.java?rev=1453609&r1=1453608&r2=1453609&view=diff
==============================================================================
--- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Page.java (original)
+++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Page.java Wed Mar  6 23:14:10 2013
@@ -34,7 +34,7 @@ import org.apache.mavibot.btree.exceptio
  *
  * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
  */
-/* No qualifier */interface Page<K, V>
+public interface Page<K, V>
 {
     /**
      * @return The number of keys present in this page

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=1453609&r1=1453608&r2=1453609&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 Wed Mar  6 23:14:10 2013
@@ -31,6 +31,8 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.mavibot.btree.BTree;
+import org.apache.mavibot.btree.BTreeFactory;
+import org.apache.mavibot.btree.Page;
 import org.apache.mavibot.btree.exception.BTreeAlreadyManagedException;
 import org.apache.mavibot.btree.exception.EndOfFileExceededException;
 import org.apache.mavibot.btree.serializer.IntSerializer;
@@ -209,9 +211,9 @@ public class RecordManager
                 loadRecordManager();
             }
         }
-        catch ( IOException ioe )
+        catch ( Exception e )
         {
-            LOG.error( "Error while initializing the RecordManager : {}", ioe.getMessage() );
+            LOG.error( "Error while initializing the RecordManager : {}", e.getMessage() );
         }
     }
 
@@ -284,8 +286,12 @@ public class RecordManager
 
     /**
      * We will load the header and all the existing BTrees in this record manager.
+     * @throws InstantiationException 
+     * @throws IllegalAccessException 
+     * @throws ClassNotFoundException 
      */
-    public void loadRecordManager() throws IOException
+    public void loadRecordManager() throws IOException, ClassNotFoundException, IllegalAccessException,
+        InstantiationException
     {
         if ( fileChannel.size() != 0 )
         {
@@ -311,6 +317,10 @@ public class RecordManager
             // the pages that are stored in it, as we have restarted 
             // the RecordManager.
             PageIO[] pageIos = readPages( 0L );
+
+            // Create the BTree
+            copiedPageBTree = BTreeFactory.createBTree();
+
             long position = loadBTree( pageIos, 0L, copiedPageBTree );
 
             // Then process the next ones
@@ -363,8 +373,12 @@ public class RecordManager
      * @param position The position in the pageIos
      * @param btree The BTree we have to initialize
      * @return The new position in the list of given pages
+     * @throws InstantiationException 
+     * @throws IllegalAccessException 
+     * @throws ClassNotFoundException 
      */
-    private long loadBTree( PageIO[] pageIos, long position, BTree<?, ?> btree )
+    private long loadBTree( PageIO[] pageIos, long position, BTree<?, ?> btree ) throws EndOfFileExceededException,
+        IOException, ClassNotFoundException, IllegalAccessException, InstantiationException
     {
         // The tree name
         byte[] btreeNameBytes = readBytes( pageIos, position );
@@ -375,11 +389,11 @@ public class RecordManager
         {
             position += btreeNameBytes.length;
             String btreeName = Strings.utf8ToString( btreeNameBytes );
-            btree.setName( btreeName );
+            BTreeFactory.setName( btree, btreeName );
         }
         else
         {
-            btree.setName( "" );
+            BTreeFactory.setName( btree, "" );
         }
 
         // The keySerializer FQCN
@@ -398,6 +412,8 @@ public class RecordManager
             keySerializerFqcn = "";
         }
 
+        BTreeFactory.setKeySerializer( btree, keySerializerFqcn );
+
         // The valueSerialier FQCN
         byte[] valueSerializerBytes = readBytes( pageIos, position );
 
@@ -414,27 +430,47 @@ public class RecordManager
             valueSerializerFqcn = "";
         }
 
+        BTreeFactory.setValueSerializer( btree, valueSerializerFqcn );
+
         // The BTree page size
-        int pagSize = readInt( pageIos, position );
-        btree.setPageSize( pagSize );
+        int btreePageSize = readInt( pageIos, position );
+        BTreeFactory.setPageSize( btree, btreePageSize );
         position += INT_SIZE;
 
         // The BTree current revision
         long revision = readLong( pageIos, position );
-        btree.setRevision( revision );
+        BTreeFactory.setRevision( btree, revision );
         position += LONG_SIZE;
 
         // The nb elems in the tree
-        long nbElems = readLong( pageIos, position );
-        btree.setNbElems( nbElems );
+        int nbElems = readInt( pageIos, position );
+        BTreeFactory.setNbElems( btree, nbElems );
         position += LONG_SIZE;
 
         // The BTree rootPage offset
         long rootPageOffset = readLong( pageIos, position );
 
-        //List<PageIO> rootPage = readPages( rootPageOffset );
+        PageIO[] rootPage = readPages( rootPageOffset );
         position += LONG_SIZE;
 
+        // Now, load the rootPage, which can be a Leaf or a Node, depending 
+        // on the number of elements in the tree : if it's above the pageSize,
+        // it's a Node, otherwise it's a Leaf
+        Page btreeRoot = null;
+
+        if ( nbElems > btreePageSize )
+        {
+            // It's a Node
+            btreeRoot = BTreeFactory.createNode( btree, revision, nbElems );
+        }
+        else
+        {
+            // it's a leaf
+            btreeRoot = BTreeFactory.createLeaf( btree, revision, nbElems );
+        }
+
+        BTreeFactory.setRoot( btree, btreeRoot );
+
         return position;
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org