Author: kayyagari
Date: Thu Jul  4 11:13:09 2013
New Revision: 1499721

URL: http://svn.apache.org/r1499721
Log:
o fixed an issue with direction change in moveToNextNonDuplicateKey() when the 
cursor is on the last key
o added tests

Modified:
    
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Cursor.java
    
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeDuplicateKeyTest.java

Modified: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Cursor.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Cursor.java?rev=1499721&r1=1499720&r2=1499721&view=diff
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Cursor.java 
(original)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/Cursor.java 
Thu Jul  4 11:13:09 2013
@@ -461,14 +461,19 @@ public class Cursor<K, V>
             // parent, and down to the leaf
             // increment the position cause findNextParentPos checks 
"parentPos.pos == parentPos.page.getNbElems()"
             parentPos.pos++;
-            parentPos = findNextParentPos();
+            ParentPos<K, V> nextPos = findNextParentPos();
             
-            // if the returned value is a Node that means cursor is already at 
the last position
+            // if the returned value is a Node OR if it is same as the 
parentPos
+            // that means cursor is already at the last position
             // call afterLast() to restore the stack with the path to the 
right most element
-            if( parentPos.page instanceof Node )
+            if( ( nextPos.page instanceof Node ) || ( nextPos == parentPos ) )
             {
                 afterLast();
             }
+            else
+            {
+               parentPos = nextPos;
+            }
         }
         else
         {

Modified: 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeDuplicateKeyTest.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeDuplicateKeyTest.java?rev=1499721&r1=1499720&r2=1499721&view=diff
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeDuplicateKeyTest.java
 (original)
+++ 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/BTreeDuplicateKeyTest.java
 Thu Jul  4 11:13:09 2013
@@ -617,4 +617,81 @@ public class BTreeDuplicateKeyTest
         cursor.close();
 
     }
+    
+    
+    /**
+     * Test for moving after a key and traversing backwards.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testMoveToNextAndTraverseBackward() throws Exception
+    {
+        IntSerializer serializer = new IntSerializer();
+
+        BTreeConfiguration<Integer, Integer> config = new 
BTreeConfiguration<Integer, Integer>();
+        config.setAllowDuplicates( true );
+        config.setName( "master" );
+        config.setPageSize( 8 );
+        config.setSerializers( serializer, serializer );
+        BTree<Integer, Integer> btree = new BTree<Integer, Integer>( config );
+
+        int i = 5;
+        for ( int k=0; k < i; k++ )
+        {
+            btree.insert( k, k );
+        }
+        
+        // 4 is the last element in the tree
+        Cursor<Integer, Integer> cursor = btree.browseFrom(4);
+        cursor.moveToNextNonDuplicateKey();
+        
+        int currentKey = 4;
+        while( cursor.hasPrev() )
+        {
+               assertEquals( Integer.valueOf( currentKey ), 
cursor.prev().getKey() );
+               currentKey--;
+        }
+        
+        cursor.close();
+    }
+    
+    
+    /**
+     * Test for moving after a key and traversing backwards.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testMoveToPrevAndTraverseForward() throws Exception
+    {
+        IntSerializer serializer = new IntSerializer();
+
+        BTreeConfiguration<Integer, Integer> config = new 
BTreeConfiguration<Integer, Integer>();
+        config.setAllowDuplicates( true );
+        config.setName( "master" );
+        config.setPageSize( 8 );
+        config.setSerializers( serializer, serializer );
+        BTree<Integer, Integer> btree = new BTree<Integer, Integer>( config );
+
+        int i = 5;
+        for ( int k=0; k < i; k++ )
+        {
+            btree.insert( k, k );
+        }
+        
+        // 4 is the last element in the tree
+        Cursor<Integer, Integer> cursor = btree.browseFrom(0);
+        cursor.moveToPrevNonDuplicateKey();
+        
+        int currentKey = 0;
+        while( cursor.hasNext() )
+        {
+               assertEquals( Integer.valueOf( currentKey ), 
cursor.next().getKey() );
+               currentKey++;
+        }
+        
+        cursor.close();
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to