You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by ka...@apache.org on 2013/05/01 09:59:29 UTC

svn commit: r1477925 - in /labs/mavibot/trunk/mavibot/src: main/java/org/apache/mavibot/btree/ test/java/org/apache/mavibot/btree/

Author: kayyagari
Date: Wed May  1 07:59:29 2013
New Revision: 1477925

URL: http://svn.apache.org/r1477925
Log:
o moved the persist mode handling code to init() method
o many trivial javadoc fixes

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/PageIO.java
    labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/RecordManager.java
    labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeFlushTest.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=1477925&r1=1477924&r2=1477925&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 May  1 07:59:29 2013
@@ -130,6 +130,7 @@ public class BTree<K, V>
     /** A flag set to true if we want to keep old revisions */
     private boolean keepRevisions = false;
 
+    private File envDir;
 
     /**
      * Create a thread that is responsible of cleaning the transactions when
@@ -333,31 +334,13 @@ public class BTree<K, V>
         
         String filePath = configuration.getFilePath();
         
-        if ( filePath == null )
+        if ( filePath != null )
         {
-            type = BTreeTypeEnum.IN_MEMORY;
-        }
-        else
-        {
-            File dir = new File( filePath );
-            if( !dir.exists() )
-            {
-                boolean created = dir.mkdirs();
-                if( !created )
-                {
-                    throw new IllegalStateException( "Could not create the directory " + filePath + " for storing data" );
-                }
-            }
-            
-            this.file = new File( dir, configuration.getName() + DATA_SUFFIX );
-
-            this.journal = new File( dir, file.getName() + JOURNAL_SUFFIX );
-
-            type = BTreeTypeEnum.PERSISTENT;
+            envDir = new File( filePath );
         }
 
         btreeHeader = new BTreeHeader();
-        btreeHeader.setName( configuration.getName() );
+        btreeHeader.setName( name );
         btreeHeader.setPageSize( configuration.getPageSize() );
         keySerializer = configuration.getKeySerializer();
         valueSerializer = configuration.getValueSerializer();
@@ -417,41 +400,28 @@ public class BTree<K, V>
 
 
     /**
-     * Creates a new BTree with a specific page size and a comparator, with an associated file.
-     * @param pageSize The number of elements we can store in a page
-     * @param comparator The comparator to use
-     * @param serializer The serializer to use
+     * 
+     * Creates a new instance of BTree with the given name and store it under the given dataDir if provided.
+     *
+     * @param name the name of the BTree
+     * @param dataDir the name of the data directory with absolute path
+     * @param keySerializer key serializer
+     * @param valueSerializer value serializer
+     * @param pageSize size of the page
+     * @throws IOException
      */
-    public BTree( String name, String path, ElementSerializer<K> keySerializer, ElementSerializer<V> valueSerializer,
+    public BTree( String name, String dataDir, ElementSerializer<K> keySerializer, ElementSerializer<V> valueSerializer,
         int pageSize )
         throws IOException
     {
         btreeHeader = new BTreeHeader();
         btreeHeader.setName( name );
 
-        if ( path == null )
+        if( dataDir != null )
         {
-            type = BTreeTypeEnum.IN_MEMORY;
+            envDir = new File( dataDir );
         }
-        else
-        {
-            File dir = new File( path );
-            if( !dir.exists() )
-            {
-                boolean created = dir.mkdirs();
-                if( !created )
-                {
-                    throw new IllegalStateException( "Could not create the directory " + path + " for storing data" );
-                }
-            }
-            
-            this.file = new File( dir, name + DATA_SUFFIX );
-
-            this.journal = new File( dir, file.getName() + JOURNAL_SUFFIX );
-
-            type = BTreeTypeEnum.PERSISTENT;
-        }
-
+        
         setPageSize( pageSize );
         writeBufferSize = DEFAULT_WRITE_BUFFER_SIZE;
 
@@ -481,15 +451,34 @@ public class BTree<K, V>
      */
     public void init() throws IOException
     {
-        // Create the queue containing the pending read transactions
-        readTransactions = new ConcurrentLinkedQueue<Transaction<K, V>>();
-
-        // Create the queue containing the modifications, if it's not a in-memory btree
-        if ( type == BTreeTypeEnum.PERSISTENT )
+        if ( envDir == null )
         {
-            modificationsQueue = new LinkedBlockingDeque<Modification<K, V>>();
+            type = BTreeTypeEnum.IN_MEMORY;
+        }
+        else
+        {
+            if( !envDir.exists() )
+            {
+                boolean created = envDir.mkdirs();
+                if( !created )
+                {
+                    throw new IllegalStateException( "Could not create the directory " + envDir + " for storing data" );
+                }
+            }
+            
+            this.file = new File( envDir, btreeHeader.getName() + DATA_SUFFIX );
+
+            // if not in-memory then default to persist mode instead of managed
+            if( type != BTreeTypeEnum.MANAGED )
+            {
+                this.journal = new File( envDir, file.getName() + JOURNAL_SUFFIX );
+                type = BTreeTypeEnum.PERSISTENT;
+            }
         }
 
+        // Create the queue containing the pending read transactions
+        readTransactions = new ConcurrentLinkedQueue<Transaction<K, V>>();
+
         // We will extract the Type to use for keys, using the comparator for that
         Class<?> comparatorClass = comparator.getClass();
         Type[] types = comparatorClass.getGenericInterfaces();
@@ -503,64 +492,32 @@ public class BTree<K, V>
         writeLock = new ReentrantLock();
 
         // Check the files and create them if missing
-        if ( file != null )
+        // Create the queue containing the modifications, if it's not a in-memory btree
+        if ( type == BTreeTypeEnum.PERSISTENT )
         {
-            if ( !file.exists() )
+            modificationsQueue = new LinkedBlockingDeque<Modification<K, V>>();
+            
+            if ( file.length() > 0 )
             {
-                file.createNewFile();
-
-                if ( journal == null )
-                {
-                    journal = new File( file.getParentFile(), BTree.DEFAULT_JOURNAL );
-                }
-
-                journal.createNewFile();
-                withJournal = true;
-
-                // If the journal is not empty, we have to read it
-                // and to apply all the modifications to the current file
-                if ( journal.length() > 0 )
-                {
-                    applyJournal();
-                }
+                // We have some existing file, load it 
+                load( file );
             }
-            else
+            
+            withJournal = true;
+            
+            // If the journal is not empty, we have to read it
+            // and to apply all the modifications to the current file
+            if ( journal.length() > 0 )
             {
-                if ( file.length() > 0 )
-                {
-                    // We have some existing file, load it 
-                    load( file );
-                }
-
-                if ( journal == null )
-                {
-                    journal = new File( file.getParentFile(), BTree.DEFAULT_JOURNAL );
-                }
-
-                journal.createNewFile();
-                withJournal = true;
-
-                // If the journal is not empty, we have to read it
-                // and to apply all the modifications to the current file
-                if ( journal.length() > 0 )
-                {
-                    applyJournal();
-                }
+                applyJournal();
             }
-        }
-        else
-        {
-            withJournal = false;
+            
+            // Initialize the Journal manager thread if it's not a in-memory btree
+            createJournalManager();
         }
 
         // Initialize the txnManager thread
         createTransactionManager();
-
-        // Initialize the Journal manager thread if it's not a in-memory btree
-        if ( ( type == BTreeTypeEnum.PERSISTENT ) && withJournal )
-        {
-            createJournalManager();
-        }
     }
 
 

Modified: labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/PageIO.java
URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/PageIO.java?rev=1477925&r1=1477924&r2=1477925&view=diff
==============================================================================
--- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/PageIO.java (original)
+++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/PageIO.java Wed May  1 07:59:29 2013
@@ -26,7 +26,7 @@ import org.apache.mavibot.btree.util.Str
 
 
 /**
- * A structure containing a Page on disk. It's a byte[PageSize] plus a few informations like
+ * A structure containing a Page on disk. It's a byte[PageSize] plus a few more details like
  * the page offset on disk and a link to the next page.</br>
  * As we may need more than one Page to store some data, the PageIO are linked so that
  * the list of all the PageIO contain the full data.</br>

Modified: labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/RecordManager.java
URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/RecordManager.java?rev=1477925&r1=1477924&r2=1477925&view=diff
==============================================================================
--- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/RecordManager.java (original)
+++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/RecordManager.java Wed May  1 07:59:29 2013
@@ -48,8 +48,8 @@ import org.slf4j.LoggerFactory;
  * A RecordManager will manage more than one BTree.<br/>
  * 
  * It stores data in fixed size pages (default size is 4Kb), which may be linked one to 
- * the other if the data we want to store is too bug for a page.
- * if 
+ * the other if the data we want to store is too big for a page.
+ *  
  * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
  */
 public class RecordManager
@@ -78,7 +78,7 @@ public class RecordManager
 
     /** 
      * A Btree used to manage the page that has been copied in a new version.
-     * Those page can be reclaimed when the associated version is dead. 
+     * Those pages can be reclaimed when the associated version is dead. 
      **/
     private BTree<Integer, long[]> copiedPageBTree;
 
@@ -134,7 +134,7 @@ public class RecordManager
 
     /**
      * Create a Record manager which will either create the underlying file
-     * or load an existing one. If a folder is provider, then we will create
+     * or load an existing one. If a folder is provided, then we will create
      * a file with a default name : mavibot.db
      * 
      * @param name The file name, or a folder name
@@ -223,8 +223,8 @@ public class RecordManager
         }
         catch ( Exception e )
         {
-            e.printStackTrace();
             LOG.error( "Error while initializing the RecordManager : {}", e.getMessage() );
+            LOG.error( "", e );
         }
     }
 
@@ -234,7 +234,7 @@ public class RecordManager
      * a BTree to manage the old revisions we want to keep and
      * a BTree used to manage pages associated with old versions.
      * <br/>
-     * The Header contains the following informations :
+     * The Header contains the following details :
      * <pre>
      * +-----------+
      * | PageSize  | 4 bytes : The size of a physical page (default to 4096)
@@ -941,7 +941,7 @@ public class RecordManager
 
         lastAddedBTreeOffset = btreeOffset;
 
-        // Last, not last, update the number of managed BTrees in the header
+        // Last, not least, update the number of managed BTrees in the header
         updateRecordManagerHeader();
     }
 
@@ -1590,7 +1590,7 @@ public class RecordManager
 
 
     /**
-     * Get as many pages as needed to store the data which size is provided
+     * Get as many pages as needed to store the data of the given size
      *  
      * @param dataSize The data size
      * @return An array of pages, enough to store the full data
@@ -1624,7 +1624,7 @@ public class RecordManager
 
 
     /**
-     * Return a new Page. We take one of the existing free page, or we create
+     * Return a new Page. We take one of the existing free pages, or we create
      * a new page at the end of the file.
      * 
      * @return The fetched PageIO

Modified: labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeFlushTest.java
URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeFlushTest.java?rev=1477925&r1=1477924&r2=1477925&view=diff
==============================================================================
--- labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeFlushTest.java (original)
+++ labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeFlushTest.java Wed May  1 07:59:29 2013
@@ -218,7 +218,6 @@ public class BTreeFlushTest
         tempFile.delete();
 
         BTree<Integer, String> btree = new BTree<Integer, String>( "test", path, new IntSerializer(), new StringSerializer() );
-        btree.setName( "flush" );
         btree.setPageSize( 8 );
         
         File journal = btree.getJournal();



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