You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2013/12/18 12:01:11 UTC

svn commit: r1551908 [3/3] - in /directory/mavibot/trunk/mavibot/src: main/java/org/apache/directory/mavibot/btree/ main/java/org/apache/directory/mavibot/btree/persisted/ main/java/org/apache/directory/mavibot/btree/serializer/ test/java/org/apache/di...

Modified: directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/InMemoryBTreeTest.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/InMemoryBTreeTest.java?rev=1551908&r1=1551907&r2=1551908&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/InMemoryBTreeTest.java (original)
+++ directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/InMemoryBTreeTest.java Wed Dec 18 11:01:10 2013
@@ -35,15 +35,6 @@ import java.util.List;
 import java.util.Random;
 import java.util.Set;
 
-import org.apache.directory.mavibot.btree.BTree;
-import org.apache.directory.mavibot.btree.AbstractBTree;
-import org.apache.directory.mavibot.btree.InMemoryBTree;
-import org.apache.directory.mavibot.btree.InMemoryValueHolder;
-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.TupleCursor;
 import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
 import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
 import org.apache.directory.mavibot.btree.serializer.IntSerializer;
@@ -55,7 +46,7 @@ import org.junit.Test;
 
 /**
  * A unit test class for in-memory BTree
- * 
+ *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
 public class InMemoryBTreeTest
@@ -211,7 +202,7 @@ public class InMemoryBTreeTest
             if ( !checkTree( expected, btree ) )
             {
                 boolean isFirst = true;
-                
+
                 for ( Long key : added )
                 {
                     if ( isFirst )
@@ -222,7 +213,7 @@ public class InMemoryBTreeTest
                     {
                         System.out.print( ", " );
                     }
-                    
+
                     System.out.print( key );
                 }
             }
@@ -256,8 +247,8 @@ public class InMemoryBTreeTest
     /**
      * Test the deletion of elements in a BTree. We will try 1000 times to delete 1000
      * random elements in [0..1024], and check every tree to see if all the removed elements
-     * are absent. This pretty much validate the the deletion operation is valid, assuming 
-     * that due to the randomization of the deleted values, we will statically meet all the 
+     * are absent. This pretty much validate the the deletion operation is valid, assuming
+     * that due to the randomization of the deleted values, we will statically meet all the
      * use cases.
      * @throws Exception
      */
@@ -572,7 +563,7 @@ public class InMemoryBTreeTest
     public void testPageRemove() throws Exception
     {
         Long[] keys = new Long[]{  101L, 113L, 20L, 72L, 215L, 239L, 108L, 21L };
-        
+
         BTree<Long, String> btree = BTreeFactory.createInMemoryBTree( new LongComparator(), 8 );
         System.out.println( btree );
 
@@ -580,21 +571,21 @@ public class InMemoryBTreeTest
         {
             btree.insert( key, "V" + key );
         }
-        
+
         System.out.println( btree );
-        
+
         // Remove from the left
         btree.remove( 20L );
         System.out.println( btree );
-        
+
         // Remove from the right
         btree.remove( 239L );
         System.out.println( btree );
-        
+
         // Remove from the middle
         btree.remove( 72L );
         System.out.println( btree );
-        
+
         // Remove all the remaining elements
         btree.remove( 101L );
         System.out.println( btree );
@@ -606,7 +597,7 @@ public class InMemoryBTreeTest
         System.out.println( btree );
         btree.remove( 21L );
         System.out.println( btree );
-        
+
         btree.close();
     }
     */
@@ -645,7 +636,7 @@ public class InMemoryBTreeTest
         while ( cursor.hasNext() )
         {
             Tuple<Integer, String> tuple = cursor.next();
-            
+
             assertNotNull( tuple );
             Integer val = sortedValues[pos];
             Integer res = tuple.getKey();
@@ -682,7 +673,7 @@ public class InMemoryBTreeTest
         while ( cursor.hasNext() )
         {
             Tuple<Integer, String> tuple = cursor.next();
-            
+
             assertNotNull( tuple );
             Integer val = sortedValues[pos];
             Integer res = tuple.getKey();
@@ -923,7 +914,7 @@ public class InMemoryBTreeTest
 
         assertFalse( btree.hasKey( 0 ) );
         assertFalse( btree.hasKey( 21 ) );
-        
+
         btree.close();
     }
 
@@ -1019,7 +1010,7 @@ public class InMemoryBTreeTest
 
 
     /**
-     * Test the browse method with a non existing key 
+     * Test the browse method with a non existing key
      * @throws Exception
      */
     @Test
@@ -1043,7 +1034,7 @@ public class InMemoryBTreeTest
 
         TupleCursor<Integer, String> cursor = btree.browseFrom( 11 );
         assertFalse( cursor.hasNext() );
-        
+
         btree.close();
     }
 
@@ -1183,7 +1174,7 @@ public class InMemoryBTreeTest
 
 
     /**
-     * Remove an element from the tree, checking that the removal was successful 
+     * Remove an element from the tree, checking that the removal was successful
      * @param btree The btree on which we remove an element
      * @param element The removed element
      * @param expected The expected set of elements
@@ -1204,7 +1195,7 @@ public class InMemoryBTreeTest
     /**
      * Check that the tree contains all the elements in the expected set, and that
      * all the elements in the tree are also present in the set
-     * 
+     *
      * @param btree The tree to check
      * @param expected The set with the expected elements
      */
@@ -1244,7 +1235,7 @@ public class InMemoryBTreeTest
 
     /**
      * Remove a set of values from a btree
-     * 
+     *
      * @param btree The modified btree
      * @param expected The set of expected values to update
      * @param values The values to remove
@@ -1402,7 +1393,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 1: remove the leftmost element
      */
     @Test
@@ -1423,7 +1414,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 2: remove an element on the leftmost page but not the first one
      */
     @Test
@@ -1444,7 +1435,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 3: remove an element on the rightmost page on the leftmost node on the upper level
      */
     @Test
@@ -1465,7 +1456,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 4: remove the first element in a page in the middle of the first node
      */
     @Test
@@ -1486,7 +1477,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 5: remove the second element in a page in the middle of the first node
      */
     @Test
@@ -1507,7 +1498,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 1: remove the rightmost element
      */
     @Test
@@ -1528,7 +1519,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 1: remove the element before the rightmost element
      */
     @Test
@@ -1549,7 +1540,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 1: remove the leftmost element  of the rightmost leaf
      */
     @Test
@@ -1570,7 +1561,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 1: remove the second elemnt of the leftmost page on the rightmost second level node
      */
     @Test
@@ -1591,7 +1582,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 6: remove the first element of a leaf in the middle of the tree
      */
     @Test
@@ -1602,7 +1593,7 @@ public class InMemoryBTreeTest
         // deleting as many elements as necessary to get the node ready for a merge
         delete( btree, EXPECTED1, 42, 43, 46, 47 );
 
-        // delete 
+        // delete
         checkRemoval( btree, 50, EXPECTED1 );
 
         btree.close();
@@ -1612,7 +1603,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 7: remove the second element of a leaf in the middle of the tree
      */
     @Test
@@ -1623,7 +1614,7 @@ public class InMemoryBTreeTest
         // deleting as many elements as necessary to get the node ready for a merge
         delete( btree, EXPECTED1, 42, 43, 46, 47 );
 
-        // delete 
+        // delete
         checkRemoval( btree, 51, EXPECTED1 );
 
         btree.close();
@@ -1633,7 +1624,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 8: remove the last element of a leaf in the middle of the tree
      */
     @Test
@@ -1644,7 +1635,7 @@ public class InMemoryBTreeTest
         // deleting as many elements as necessary to get the node ready for a merge
         delete( btree, EXPECTED1, 42, 43, 46, 47 );
 
-        // delete 
+        // delete
         checkRemoval( btree, 59, EXPECTED1 );
 
         btree.close();
@@ -1654,7 +1645,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 9: remove the element before the last one of a leaf in the middle of the tree
      */
     @Test
@@ -1665,7 +1656,7 @@ public class InMemoryBTreeTest
         // deleting as many elements as necessary to get the node ready for a merge
         delete( btree, EXPECTED1, 42, 43, 46, 47 );
 
-        // delete 
+        // delete
         checkRemoval( btree, 58, EXPECTED1 );
 
         btree.close();
@@ -1675,7 +1666,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 10: remove the mid element of a leaf in the middle of the tree
      */
     @Test
@@ -1686,7 +1677,7 @@ public class InMemoryBTreeTest
         // deleting as many elements as necessary to get the node ready for a merge
         delete( btree, EXPECTED1, 42, 43, 46, 47 );
 
-        // delete 
+        // delete
         checkRemoval( btree, 54, EXPECTED1 );
 
         btree.close();
@@ -1696,7 +1687,7 @@ public class InMemoryBTreeTest
     /**
      * Test deletions in a tree with more than one level. We are specifically testing
      * the deletions that will make a node borrowing some element from a sibling.
-     * 
+     *
      * 11: remove the mid+1 element of a leaf in the middle of the tree
      */
     @Test
@@ -1707,7 +1698,7 @@ public class InMemoryBTreeTest
         // deleting as many elements as necessary to get the node ready for a merge
         delete( btree, EXPECTED1, 42, 43, 46, 47 );
 
-        // delete 
+        // delete
         checkRemoval( btree, 55, EXPECTED1 );
 
         btree.close();
@@ -1740,7 +1731,7 @@ public class InMemoryBTreeTest
 
         assertNotNull( deleted );
         assertNull( deleted.getValue() );
-        
+
         btree.close();
     }
 
@@ -1949,19 +1940,17 @@ public class InMemoryBTreeTest
         tuple = cursor.next();
         assertEquals( Integer.valueOf( 4 ), tuple.getKey() );
         assertEquals( Integer.valueOf( 4 ), tuple.getValue() );
-        
+
         cursor.close();
         btree.close();
     }
 
-    
+
     @Test
     public void testCheckRootPageContents() throws Exception
     {
         IntSerializer ser = new IntSerializer();
-        BTree<Integer, Integer> btree = BTreeFactory.createInMemoryBTree( "master1", ser, ser );
-        btree.setPageSize( 4 );
-        btree.init();
+        BTree<Integer, Integer> btree = BTreeFactory.createInMemoryBTree( "master1", ser, ser, 4 );
 
         for( int i=1; i < 8; i++ )
         {

Modified: directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/PersistedBTreeDuplicateKeyTest.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/PersistedBTreeDuplicateKeyTest.java?rev=1551908&r1=1551907&r2=1551908&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/PersistedBTreeDuplicateKeyTest.java (original)
+++ directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/PersistedBTreeDuplicateKeyTest.java Wed Dec 18 11:01:10 2013
@@ -33,10 +33,8 @@ import java.util.NoSuchElementException;
 import java.util.UUID;
 
 import org.apache.commons.io.FileUtils;
-import org.apache.directory.mavibot.btree.BTree;
-import org.apache.directory.mavibot.btree.RecordManager;
-import org.apache.directory.mavibot.btree.Tuple;
-import org.apache.directory.mavibot.btree.TupleCursor;
+import org.apache.directory.mavibot.btree.exception.BTreeAlreadyManagedException;
+import org.apache.directory.mavibot.btree.exception.DuplicateValueNotAllowedException;
 import org.apache.directory.mavibot.btree.serializer.IntSerializer;
 import org.apache.directory.mavibot.btree.serializer.LongSerializer;
 import org.apache.directory.mavibot.btree.serializer.StringSerializer;
@@ -74,20 +72,20 @@ public class PersistedBTreeDuplicateKeyT
         try
         {
             // Create a new BTree
-            btree = recordManager1.addBTree( "test", new LongSerializer(), new StringSerializer(), false );
+            btree = recordManager1.addBTree( "test", new LongSerializer(), new StringSerializer(), BTree.ALLOW_DUPLICATES );
         }
         catch ( Exception e )
         {
             throw new RuntimeException( e );
         }
     }
-    
-    
+
+
     @After
     public void cleanup() throws IOException
     {
         dataDir = new File( System.getProperty( "java.io.tmpdir" ) + "/recordman" );
-        
+
         btree.close();
 
         if ( dataDir.exists() )
@@ -96,7 +94,7 @@ public class PersistedBTreeDuplicateKeyT
         }
     }
 
-    
+
     private void openRecordManagerAndBtree()
     {
         try
@@ -121,7 +119,7 @@ public class PersistedBTreeDuplicateKeyT
         }
     }
 
-    
+
     @Test
     public void testInsertNullValue() throws IOException
     {
@@ -136,7 +134,7 @@ public class PersistedBTreeDuplicateKeyT
         assertEquals( null, t.getValue() );
 
         cursor.close();
-        
+
         btree.close();
     }
 
@@ -147,7 +145,6 @@ public class PersistedBTreeDuplicateKeyT
         IntSerializer serializer = new IntSerializer();
 
         BTree<Integer, Integer> btree = BTreeFactory.createPersistedBTree( "master", serializer, serializer );
-        btree.init();
 
         TupleCursor<Integer, Integer> cursor = btree.browse();
         assertFalse( cursor.hasNext() );
@@ -344,7 +341,7 @@ public class PersistedBTreeDuplicateKeyT
 
         // add one more value for 'a'
         btree.insert( Long.valueOf( 'a' ), "val" );
-        
+
         assertEquals( 27, btree.getNbElems() );
 
         // Start from c : we should have only 24 values
@@ -378,7 +375,7 @@ public class PersistedBTreeDuplicateKeyT
             assertNotNull( tuple );
             i++;
         }
-        
+
         assertEquals( 26, i );
 
         cursor.close();
@@ -387,13 +384,13 @@ public class PersistedBTreeDuplicateKeyT
         cursor = btree.browse();
 
         i = 0;
-        
+
         while ( cursor.hasNext() )
         {
             assertNotNull( cursor.next() );
             i++;
         }
-        
+
         // again, we should see 27 elements
         assertEquals( 27, i );
 
@@ -403,7 +400,7 @@ public class PersistedBTreeDuplicateKeyT
         assertEquals( Long.valueOf( 'a' ), cursor.nextKey().getKey() );
 
         i = 0;
-        
+
         while ( cursor.hasNextKey() )
         {
             tuple = cursor.nextKey();
@@ -411,7 +408,7 @@ public class PersistedBTreeDuplicateKeyT
             assertNotNull( key );
             i++;
         }
-        
+
         // We should have 25 keys only, as we just moved forward the first one
         assertEquals( 25, i );
     }
@@ -430,7 +427,7 @@ public class PersistedBTreeDuplicateKeyT
 
         // add one more value for 'z'
         btree.insert( Long.valueOf( 'z' ), "val" );
-        
+
         assertEquals( 27, btree.getNbElems() );
 
         // Start from x : we should have only 23 values
@@ -464,7 +461,7 @@ public class PersistedBTreeDuplicateKeyT
             assertNotNull( tuple );
             i++;
         }
-        
+
         assertEquals( 26, i );
 
         cursor.close();
@@ -474,13 +471,13 @@ public class PersistedBTreeDuplicateKeyT
         cursor.afterLast();
 
         i = 0;
-        
+
         while ( cursor.hasPrev() )
         {
             assertNotNull( cursor.prev() );
             i++;
         }
-        
+
         // again, we should see 27 elements
         assertEquals( 27, i );
 
@@ -490,7 +487,7 @@ public class PersistedBTreeDuplicateKeyT
         assertEquals( Long.valueOf( 'z' ), cursor.prevKey().getKey() );
 
         i = 0;
-        
+
         while ( cursor.hasPrevKey() )
         {
             tuple = cursor.prevKey();
@@ -498,7 +495,7 @@ public class PersistedBTreeDuplicateKeyT
             assertNotNull( key );
             i++;
         }
-        
+
         // We should have 25 keys only, as we just moved forward the first one
         assertEquals( 25, i );
     }
@@ -538,7 +535,7 @@ public class PersistedBTreeDuplicateKeyT
     public void testNextPrevKey() throws Exception
     {
         int i = 7;
-        
+
         // Insert keys from a to z with 7 values for each key
         for ( char ch = 'a'; ch <= 'z'; ch++ )
         {
@@ -552,7 +549,7 @@ public class PersistedBTreeDuplicateKeyT
 
         assertTrue( cursor.hasNext() );
         assertFalse( cursor.hasPrev() );
-        
+
         for ( int k = 0; k < 2; k++ )
         {
             assertEquals( Long.valueOf( 'a' ), cursor.next().getKey() );
@@ -641,7 +638,7 @@ public class PersistedBTreeDuplicateKeyT
         }
 
         // 15 is the last element of the first leaf
-        // Check that we correctly jump to the next page 
+        // Check that we correctly jump to the next page
         TupleCursor<Long, String> cursor = btree.browseFrom( 15L );
         Tuple<Long, String> tuple = cursor.nextKey();
 
@@ -736,7 +733,7 @@ public class PersistedBTreeDuplicateKeyT
     public void testMoveToNextAndTraverseBackward() throws Exception
     {
         int i = 5;
-        
+
         for ( int k = 0; k < i; k++ )
         {
             btree.insert( (long)k, Long.toString( k ) );
@@ -747,7 +744,7 @@ public class PersistedBTreeDuplicateKeyT
         cursor.nextKey();
 
         long currentKey = 4L;
-        
+
         while ( cursor.hasPrev() )
         {
             assertEquals( Long.valueOf( currentKey ), cursor.prev().getKey() );
@@ -767,7 +764,7 @@ public class PersistedBTreeDuplicateKeyT
     public void testMoveToPrevAndTraverseForward() throws Exception
     {
         int i = 5;
-        
+
         for ( int k = 0; k < i; k++ )
         {
             btree.insert( (long)k, Long.toString( k ) );
@@ -777,7 +774,7 @@ public class PersistedBTreeDuplicateKeyT
         TupleCursor<Long, String> cursor = btree.browseFrom( 0L );
 
         long currentKey = 0L;
-        
+
         while ( cursor.hasNext() )
         {
             assertEquals( Long.valueOf( currentKey ), cursor.next().getKey() );
@@ -787,4 +784,29 @@ public class PersistedBTreeDuplicateKeyT
         cursor.close();
     }
 
+
+    /**
+     * Test that a BTree which forbid duplicate values does not accept them
+     */
+    @Test(expected=DuplicateValueNotAllowedException.class)
+    public void testBTreeForbidDups() throws IOException, BTreeAlreadyManagedException
+    {
+        BTree<Long, String> singleValueBtree = recordManager1.addBTree( "test2", new LongSerializer(),
+            new StringSerializer(), BTree.FORBID_DUPLICATES );
+
+        for ( long i = 0; i < 64; i++ )
+        {
+            singleValueBtree.insert( i, Long.toString( i  ) );
+        }
+
+        try
+        {
+            singleValueBtree.insert( 18L, "Duplicate" );
+            fail();
+        }
+        finally
+        {
+            singleValueBtree.close();
+        }
+    }
 }