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/05 07:09:45 UTC

svn commit: r1464825 - 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: Fri Apr  5 05:09:45 2013
New Revision: 1464825

URL: http://svn.apache.org/r1464825
Log:
Implemented the BTree.hasKey(revision, K) method, with tests

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=1464825&r1=1464824&r2=1464825&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 Fri Apr  5 05:09:45 2013
@@ -977,7 +977,7 @@ public class BTree<K, V>
      * @param key The key we are looking at
      * @return The found value, or null if the key is not present in the tree
      * @throws KeyNotFoundException If the key is not found in the BTree
-     * @throws IOException TODO
+     * @throws IOException If there was an issue while fetching data from the disk
      */
     public V get( long revision, K key ) throws IOException, KeyNotFoundException
     {
@@ -1007,6 +1007,29 @@ public class BTree<K, V>
 
 
     /**
+     * Checks if the given key exists for a given revision.
+     *  
+     * @param revision The revision for which we want to find a key
+     * @param key The key we are looking at
+     * @return true if the key is present, false otherwise
+     * @throws IOException If we have an error while trying to access the page
+     * @throws KeyNotFoundException If the key is not found in the BTree
+     */
+    public boolean hasKey( long revision, K key ) throws IOException, KeyNotFoundException
+    {
+        if ( key == null )
+        {
+            return false;
+        }
+
+        // Fetch the root page for this revision
+        Page<K, V> revisionRootPage = getRootPage( revision );
+
+        return revisionRootPage.hasKey( key );
+    }
+
+
+    /**
      * Checks if the BTree contains the given key with the given value.
      * 
      * @param key The key we are looking for
@@ -1026,7 +1049,7 @@ public class BTree<K, V>
      * @param key The key we are looking for
      * @param value The value associated with the given key
      * @return true if the key and value are associated with each other, false otherwise
-     * @throws KeyNotFoundException 
+     * @throws KeyNotFoundException If the key is not found in the BTree
      */
     public boolean contains( long revision, K key, V value ) throws IOException, KeyNotFoundException
     {
@@ -1061,7 +1084,8 @@ public class BTree<K, V>
      * 
      * @param revision The revision we would like to browse
      * @return A cursor on the btree
-     * @throws IOException
+     * @throws IOException If we had an issue while fetching data from the disk
+     * @throws KeyNotFoundException If the key is not found in the BTree
      */
     public Cursor<K, V> browse( long revision ) throws IOException, KeyNotFoundException
     {
@@ -1105,7 +1129,7 @@ public class BTree<K, V>
      * 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
+     * @throws KeyNotFoundException  If we can't find a rootPage for this revision
      */
     public Cursor<K, V> browseFrom( long revision, K key ) throws IOException, KeyNotFoundException
     {

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=1464825&r1=1464824&r2=1464825&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 Fri Apr  5 05:09:45 2013
@@ -655,7 +655,7 @@ public class RecordManagerTest
 
 
     /**
-     * Test a get() from a given revision
+     * Test a contain() from a given revision
      */
     @Test
     public void testContainWithRevision() throws IOException, BTreeAlreadyManagedException,
@@ -761,4 +761,113 @@ public class RecordManagerTest
         assertFalse( btree.contains( rev4, 3L, "V3" ) );
         assertTrue( btree.contains( rev4, 5L, "V5" ) );
     }
+
+
+    /**
+     * Test a hasKey() from a given revision
+     */
+    @Test
+    public void testHasKeyWithRevision() throws IOException, BTreeAlreadyManagedException,
+        KeyNotFoundException
+    {
+        File tempFile = File.createTempFile( "mavibot", ".db" );
+        String tempFileName = tempFile.getAbsolutePath();
+        tempFile.deleteOnExit();
+
+        RecordManager recordManager = new RecordManager( tempFileName, 32 );
+
+        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();
+
+        // Delete one element
+        btree.delete( 3L );
+        long rev4 = btree.getRevision();
+
+        // Check that we can get a value from each revision
+        // revision 1
+        assertFalse( btree.hasKey( rev1, 1L ) );
+        assertTrue( btree.hasKey( rev1, 3L ) );
+        assertFalse( btree.hasKey( rev1, 5L ) );
+
+        // revision 2
+        assertTrue( btree.hasKey( rev2, 1L ) );
+        assertTrue( btree.hasKey( rev2, 3L ) );
+        assertFalse( btree.hasKey( rev2, 5L ) );
+
+        // revision 3
+        assertTrue( btree.hasKey( rev3, 1L ) );
+        assertTrue( btree.hasKey( rev3, 3L ) );
+        assertTrue( btree.hasKey( rev3, 5L ) );
+
+        // revision 4
+        assertTrue( btree.hasKey( rev4, 1L ) );
+        assertFalse( btree.hasKey( rev4, 3L ) );
+        assertTrue( btree.hasKey( rev4, 5L ) );
+
+        // Now, try to reload the file back
+        RecordManager recordManager1 = new RecordManager( tempFileName );
+
+        assertEquals( 1, recordManager1.getNbManagedTrees() );
+
+        Set<String> managedBTrees = recordManager1.getManagedTrees();
+
+        assertEquals( 1, managedBTrees.size() );
+        assertTrue( managedBTrees.contains( "test" ) );
+
+        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 ) );
+        assertFalse( btree1.hasKey( 3L ) );
+        assertTrue( btree1.hasKey( 5L ) );
+        assertEquals( "V1", btree1.get( 1L ) );
+        assertEquals( "V5", btree1.get( 5L ) );
+
+        // Check that we can get a value from each revision
+        // revision 1
+        assertFalse( btree.hasKey( rev1, 1L ) );
+        assertTrue( btree.hasKey( rev1, 3L ) );
+        assertFalse( btree.hasKey( rev1, 5L ) );
+
+        // revision 2
+        assertTrue( btree.hasKey( rev2, 1L ) );
+        assertTrue( btree.hasKey( rev2, 3L ) );
+        assertFalse( btree.hasKey( rev2, 5L ) );
+
+        // revision 3
+        assertTrue( btree.hasKey( rev3, 1L ) );
+        assertTrue( btree.hasKey( rev3, 3L ) );
+        assertTrue( btree.hasKey( rev3, 5L ) );
+
+        // revision 4
+        assertTrue( btree.hasKey( rev4, 1L ) );
+        assertFalse( btree.hasKey( rev4, 3L ) );
+        assertTrue( btree.hasKey( rev4, 5L ) );
+    }
 }



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