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/04/04 11:08:16 UTC

svn commit: r1464376 - in /labs/mavibot/branches/mavibot-multivalue-support/mavibot/src: main/java/org/apache/mavibot/btree/BTree.java test/java/org/apache/mavibot/btree/RecordManagerTest.java

Author: elecharny
Date: Thu Apr  4 09:08:16 2013
New Revision: 1464376

URL: http://svn.apache.org/r1464376
Log:
o Implemented the browseFrom( revision, K ) method
o Added a test for this method

Modified:
    labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java
    labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/RecordManagerTest.java

Modified: labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java
URL: http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java?rev=1464376&r1=1464375&r2=1464376&view=diff
==============================================================================
--- labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java (original)
+++ labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java Thu Apr  4 09:08:16 2013
@@ -970,60 +970,85 @@ public class BTree<K, V>
 
 
     /**
-     * Creates a cursor starting on the given key
+     * Creates a cursor starting at the beginning of the tree
      * 
-     * @param key The key which is the starting point. If the key is not found,
-     * then the cursor will always return null.
      * @return A cursor on the btree
      * @throws IOException
      */
-    public Cursor<K, V> browseFrom( K key ) throws IOException
+    public Cursor<K, V> browse() throws IOException
     {
         Transaction<K, V> transaction = beginReadTransaction();
 
         // Fetch the root page for this revision
-        Cursor<K, V> cursor = rootPage.browse( key, transaction, new LinkedList<ParentPos<K, V>>() );
+        LinkedList<ParentPos<K, V>> stack = new LinkedList<ParentPos<K, V>>();
+
+        Cursor<K, V> cursor = rootPage.browse( transaction, stack );
 
         return cursor;
     }
 
 
     /**
-     * Creates a cursor starting at the beginning of the tree
+     * Creates a cursor starting at the beginning of the tree, for a given revision
      * 
+     * @param revision The revision we would like to browse
      * @return A cursor on the btree
      * @throws IOException
      */
-    public Cursor<K, V> browse() throws IOException
+    public Cursor<K, V> browse( long revision ) throws IOException, KeyNotFoundException
     {
         Transaction<K, V> transaction = beginReadTransaction();
 
         // Fetch the root page for this revision
-        LinkedList<ParentPos<K, V>> stack = new LinkedList<ParentPos<K, V>>();
+        Page<K, V> revisionRootPage = getRootPage( revision );
 
-        Cursor<K, V> cursor = rootPage.browse( transaction, stack );
+        // And get the cursor
+        LinkedList<ParentPos<K, V>> stack = new LinkedList<ParentPos<K, V>>();
+        Cursor<K, V> cursor = revisionRootPage.browse( transaction, stack );
 
         return cursor;
     }
 
 
     /**
-     * Creates a cursor starting at the beginning of the tree, for a given revision
+     * Creates a cursor starting on the given key
      * 
-     * @param revision The revision we would like to browse
+     * @param key The key which is the starting point. If the key is not found,
+     * then the cursor will always return null.
      * @return A cursor on the btree
      * @throws IOException
      */
-    public Cursor<K, V> browse( long revision ) throws IOException, KeyNotFoundException
+    public Cursor<K, V> browseFrom( K key ) throws IOException
     {
         Transaction<K, V> transaction = beginReadTransaction();
 
         // Fetch the root page for this revision
-        LinkedList<ParentPos<K, V>> stack = new LinkedList<ParentPos<K, V>>();
+        Cursor<K, V> cursor = rootPage.browse( key, transaction, new LinkedList<ParentPos<K, V>>() );
+
+        return cursor;
+    }
+
 
+    /**
+     * Creates a cursor starting on the given key at the given revision
+     * 
+     * @param The revision we are looking for
+     * @param key The key which is the starting point. If the key is not found,
+     * then the cursor will always return null.
+     * @return A cursor on the btree
+     * @throws IOException If wxe had an issue reading the BTree from disk
+     * @throws KeyNotFoundException  If we cna't find a rootPage for this revision
+     */
+    public Cursor<K, V> browseFrom( long revision, K key ) throws IOException, KeyNotFoundException
+    {
+        Transaction<K, V> transaction = beginReadTransaction();
+
+        // Fetch the rootPage for this revision
         Page<K, V> revisionRootPage = getRootPage( revision );
 
-        Cursor<K, V> cursor = revisionRootPage.browse( transaction, stack );
+        // And get the cursor
+        LinkedList<ParentPos<K, V>> stack = new LinkedList<ParentPos<K, V>>();
+        Cursor<K, V> cursor = revisionRootPage.browse( key, transaction, stack );
 
         return cursor;
     }
@@ -1472,6 +1497,15 @@ public class BTree<K, V>
     }
 
 
+    /**
+     * Get the rootPzge associated to a give revision.
+     * 
+     * @param revision The revision we are looking for
+     * @return The rootPage associated to this revision
+     * @throws IOException If we had an issue while accessing the underlying file
+     * @throws KeyNotFoundException If the revision does not exist for this Btree
+     */
+    @SuppressWarnings("unchecked")
     private Page<K, V> getRootPage( long revision ) throws IOException, KeyNotFoundException
     {
         if ( isManaged() )

Modified: labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/RecordManagerTest.java
URL: http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/RecordManagerTest.java?rev=1464376&r1=1464375&r2=1464376&view=diff
==============================================================================
--- labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/RecordManagerTest.java (original)
+++ labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/RecordManagerTest.java Thu Apr  4 09:08:16 2013
@@ -21,11 +21,15 @@ package org.apache.mavibot.btree;
 
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 import org.apache.mavibot.btree.exception.BTreeAlreadyManagedException;
@@ -294,11 +298,76 @@ public class RecordManagerTest
     }
 
 
+    private void checkBTreeRevisionBrowse( BTree btree, long revision, long... values ) throws IOException,
+        KeyNotFoundException
+    {
+        Cursor<Long, String> cursor = btree.browse( revision );
+        List<Long> expected = new ArrayList<Long>( values.length );
+        Set<Long> found = new HashSet<Long>( values.length );
+
+        for ( long value : values )
+        {
+            expected.add( value );
+        }
+
+        int nb = 0;
+
+        while ( cursor.hasNext() )
+        {
+            Tuple<Long, String> res = cursor.next();
+
+            long key = res.getKey();
+            assertEquals( expected.get( nb ), ( Long ) key );
+            assertFalse( found.contains( key ) );
+            found.add( key );
+            assertEquals( "V" + key, res.getValue() );
+            nb++;
+        }
+
+        assertEquals( values.length, nb );
+        cursor.close();
+    }
+
+
+    private void checkBTreeRevisionBrowseFrom( BTree btree, long revision, long from, long... values )
+        throws IOException,
+        KeyNotFoundException
+    {
+        Cursor<Long, String> cursor = btree.browseFrom( revision, from );
+        List<Long> expected = new ArrayList<Long>( values.length );
+        Set<Long> found = new HashSet<Long>( values.length );
+
+        for ( long value : values )
+        {
+            expected.add( value );
+        }
+
+        int nb = 0;
+
+        while ( cursor.hasNext() )
+        {
+            Tuple<Long, String> res = cursor.next();
+
+            long key = res.getKey();
+            assertEquals( expected.get( nb ), ( Long ) key );
+            assertFalse( found.contains( key ) );
+            found.add( key );
+            assertEquals( "V" + key, res.getValue() );
+            nb++;
+        }
+
+        assertEquals( values.length, nb );
+        cursor.close();
+
+    }
+
+
     /**
-     * Test the creation of a RecordManager with a BTree containing data, where we keep the revisions.
+     * Test the creation of a RecordManager with a BTree containing data, where we keep the revisions, 
+     * and browse the BTree.
      */
     @Test
-    public void testRecordManagerWithBTreeKeepRevisions() throws IOException, BTreeAlreadyManagedException,
+    public void testRecordManagerBrowseWithKeepRevisions() throws IOException, BTreeAlreadyManagedException,
         KeyNotFoundException
     {
         File tempFile = File.createTempFile( "mavibot", ".db" );
@@ -328,80 +397,98 @@ public class RecordManagerTest
 
         // Check that we can browse each revision
         // revision 1
-        Cursor<Long, String> cursor = btree.browse( rev1 );
+        checkBTreeRevisionBrowse( btree, rev1, 3L );
 
-        int nb = 0;
-        while ( cursor.hasNext() )
-        {
-            Tuple<Long, String> res = cursor.next();
-            nb++;
+        // Revision 2
+        checkBTreeRevisionBrowse( btree, rev2, 1L, 3L );
 
-            assertEquals( 3L, ( long ) res.getKey() );
-            assertEquals( "V3", res.getValue() );
-        }
+        // Revision 3
+        checkBTreeRevisionBrowse( btree, rev3, 1L, 3L, 5L );
 
-        assertEquals( 1, nb );
-        cursor.close();
+        // Now, try to reload the file back
+        RecordManager recordManager1 = new RecordManager( tempFileName );
 
-        // Revision 2
-        cursor = btree.browse( rev2 );
+        assertEquals( 1, recordManager1.getNbManagedTrees() );
 
-        nb = 0;
-        while ( cursor.hasNext() )
-        {
-            Tuple<Long, String> res = cursor.next();
-            nb++;
+        Set<String> managedBTrees = recordManager1.getManagedTrees();
 
-            switch ( nb )
-            {
-                case 1:
-                    assertEquals( 1L, ( long ) res.getKey() );
-                    assertEquals( "V1", res.getValue() );
-                    break;
-
-                case 2:
-                    assertEquals( 3L, ( long ) res.getKey() );
-                    assertEquals( "V3", res.getValue() );
-                    break;
-            }
-        }
+        assertEquals( 1, managedBTrees.size() );
+        assertTrue( managedBTrees.contains( "test" ) );
 
-        assertEquals( 2, nb );
-        cursor.close();
+        BTree<Long, String> btree1 = recordManager1.getManagedTree( "test" );
+
+        assertNotNull( btree1 );
+        assertEquals( btree.getComparator().getClass().getName(), btree1.getComparator().getClass().getName() );
+        assertEquals( btree.getFile(), btree1.getFile() );
+        assertEquals( btree.getKeySerializer().getClass().getName(), btree1.getKeySerializer().getClass().getName() );
+        assertEquals( btree.getName(), btree1.getName() );
+        assertEquals( btree.getNbElems(), btree1.getNbElems() );
+        assertEquals( btree.getPageSize(), btree1.getPageSize() );
+        assertEquals( btree.getRevision(), btree1.getRevision() );
+        assertEquals( btree.getValueSerializer().getClass().getName(), btree1.getValueSerializer().getClass().getName() );
+
+        // Check the stored element
+        assertTrue( btree1.hasKey( 1L ) );
+        assertTrue( btree1.hasKey( 3L ) );
+        assertTrue( btree1.hasKey( 5L ) );
+        assertEquals( "V1", btree1.get( 1L ) );
+        assertEquals( "V3", btree1.get( 3L ) );
+        assertEquals( "V5", btree1.get( 5L ) );
+
+        // Check that we can read the revision again
+        // revision 1
+        checkBTreeRevisionBrowse( btree, rev1, 3L );
+
+        // Revision 2
+        checkBTreeRevisionBrowse( btree, rev2, 1L, 3L );
 
         // Revision 3
-        cursor = btree.browse( rev3 );
+        checkBTreeRevisionBrowse( btree, rev3, 1L, 3L, 5L );
+    }
 
-        nb = 0;
-        while ( cursor.hasNext() )
-        {
-            Tuple<Long, String> res = cursor.next();
-            nb++;
 
-            switch ( nb )
-            {
-                case 1:
-                    assertEquals( 1L, ( long ) res.getKey() );
-                    assertEquals( "V1", res.getValue() );
-                    break;
-
-                case 2:
-                    assertEquals( 3L, ( long ) res.getKey() );
-                    assertEquals( "V3", res.getValue() );
-                    break;
-
-                case 3:
-                    assertEquals( 5L, ( long ) res.getKey() );
-                    assertEquals( "V5", res.getValue() );
-                    break;
-            }
-        }
+    /**
+     * Test the creation of a RecordManager with a BTree containing data, where we keep the revision, and 
+     * we browse from a key
+     */
+    @Test
+    public void testRecordManagerBrowseFromWithRevision() throws IOException, BTreeAlreadyManagedException,
+        KeyNotFoundException
+    {
+        File tempFile = File.createTempFile( "mavibot", ".db" );
+        String tempFileName = tempFile.getAbsolutePath();
+        tempFile.deleteOnExit();
 
-        assertEquals( 3, nb );
-        cursor.close();
+        RecordManager recordManager = new RecordManager( tempFileName, 32 );
 
-        // Close the recordManager
-        recordManager.close();
+        assertNotNull( recordManager );
+
+        // Create a new BTree
+        BTree<Long, String> btree = new BTree<Long, String>( "test", new LongSerializer(), new StringSerializer() );
+        btree.setKeepRevisions( true );
+
+        // And make it managed by the RM
+        recordManager.manage( btree );
+
+        // Now, add some elements in the BTree
+        btree.insert( 3L, "V3" );
+        long rev1 = btree.getRevision();
+
+        btree.insert( 1L, "V1" );
+        long rev2 = btree.getRevision();
+
+        btree.insert( 5L, "V5" );
+        long rev3 = btree.getRevision();
+
+        // Check that we can browse each revision
+        // revision 1
+        checkBTreeRevisionBrowseFrom( btree, rev1, 3L, 3L );
+
+        // Revision 2
+        checkBTreeRevisionBrowseFrom( btree, rev2, 3L, 3L );
+
+        // Revision 3
+        checkBTreeRevisionBrowseFrom( btree, rev3, 3L, 3L, 5L );
 
         // Now, try to reload the file back
         RecordManager recordManager1 = new RecordManager( tempFileName );
@@ -432,5 +519,15 @@ public class RecordManagerTest
         assertEquals( "V1", btree1.get( 1L ) );
         assertEquals( "V3", btree1.get( 3L ) );
         assertEquals( "V5", btree1.get( 5L ) );
+
+        // Check that we can read the revision again
+        // revision 1
+        checkBTreeRevisionBrowseFrom( btree, rev1, 3L, 3L );
+
+        // Revision 2
+        checkBTreeRevisionBrowseFrom( btree, rev2, 3L, 3L );
+
+        // Revision 3
+        checkBTreeRevisionBrowseFrom( btree, rev3, 3L, 3L, 5L );
     }
 }



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