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/03 12:06:41 UTC

svn commit: r1463904 - in /labs/mavibot/branches/mavibot-multivalue-support/mavibot/src: main/java/org/apache/mavibot/btree/ test/java/org/apache/mavibot/btree/store/

Author: elecharny
Date: Wed Apr  3 10:06:41 2013
New Revision: 1463904

URL: http://svn.apache.org/r1463904
Log:
Removed the duplicated method exist(). We already have the hasKey() 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/main/java/org/apache/mavibot/btree/Leaf.java
    labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/Node.java
    labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/Page.java
    labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/store/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=1463904&r1=1463903&r2=1463904&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 Wed Apr  3 10:06:41 2013
@@ -128,7 +128,8 @@ public class BTree<K, V>
 
     /** Flag to enable duplicate key support */
     private boolean allowDuplicates;
-    
+
+
     /**
      * Create a thread that is responsible of cleaning the transactions when
      * they hit the timeout
@@ -857,7 +858,8 @@ public class BTree<K, V>
     {
         return delete( key, null, revision );
     }
-    
+
+
     /**
      * 
      * Deletes the given <key,value> pair if both key and value match. If the given value is null
@@ -920,19 +922,6 @@ public class BTree<K, V>
 
 
     /**
-     * Check if there is an element associated with the given key.
-     * 
-     * @param key The key we are looking at
-     * @return true if the Key exists in the BTree 
-     * @throws IOException 
-     */
-    public boolean exist( K key ) throws IOException
-    {
-        return rootPage.exist( key );
-    }
-
-
-    /**
      * Find a value in the tree, given its key. If the key is not found,
      * it will throw a KeyNotFoundException. <br/>
      * Note that we can get a null value stored, or many values.
@@ -953,14 +942,15 @@ public class BTree<K, V>
      *  
      * @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
      */
     public boolean hasKey( K key ) throws IOException
     {
-        if( key == null )
+        if ( key == null )
         {
             return false;
         }
-        
+
         return rootPage.hasKey( key );
     }
 
@@ -977,7 +967,7 @@ public class BTree<K, V>
         return rootPage.contains( key, value );
     }
 
-    
+
     /**
      * Creates a cursor starting on the given key
      * 
@@ -1586,7 +1576,7 @@ public class BTree<K, V>
         }
         else
         {
-            if( isAllowDuplicates() )
+            if ( isAllowDuplicates() )
             {
                 return new DuplicateKeyMemoryHolder<K, V>( this, ( V ) value );
             }

Modified: labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/Leaf.java
URL: http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/Leaf.java?rev=1463904&r1=1463903&r2=1463904&view=diff
==============================================================================
--- labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/Leaf.java (original)
+++ labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/Leaf.java Wed Apr  3 10:06:41 2013
@@ -66,9 +66,10 @@ public class Leaf<K, V> extends Abstract
     {
         super( btree, revision, nbElems );
 
-        if( btree.isAllowDuplicates() )
+        if ( btree.isAllowDuplicates() )
         {
-            this.values = ( DuplicateKeyMemoryHolder<K, V>[] ) Array.newInstance( DuplicateKeyMemoryHolder.class, nbElems );
+            this.values = ( DuplicateKeyMemoryHolder<K, V>[] ) Array.newInstance( DuplicateKeyMemoryHolder.class,
+                nbElems );
         }
         else
         {
@@ -144,34 +145,36 @@ public class Leaf<K, V> extends Abstract
 
         // Get the removed element
         Tuple<K, V> removedElement = null;
-        
+
         // flag to detect if a key was completely removed
         boolean keyRemoved = false;
 
         int index = -( pos + 1 );
-        
-        if( btree.isAllowDuplicates() )
+
+        if ( btree.isAllowDuplicates() )
         {
-            if( value == null );
-            BTree<V,V> dups = ( BTree<V,V> ) values[index].getValue( btree );
-            
-            if( dups.hasKey( value ) )
+            if ( value == null )
+                ;
+            BTree<V, V> dups = ( BTree<V, V> ) values[index].getValue( btree );
+
+            if ( dups.hasKey( value ) )
             {
                 dups.delete( value );
-                
-                if( dups.getNbElems() == 0 )
+
+                if ( dups.getNbElems() == 0 )
                 {
                     keyRemoved = true;
                 }
-                
+
                 removedElement = new Tuple<K, V>( keys[index], value ); // we deleted only one value (even if it is from a tree of size 1)
             }
-            else if( value == null ) // this is a case to delete entire <K,dupsTree> 
+            else if ( value == null ) // this is a case to delete entire <K,dupsTree> 
             {
                 removedElement = new Tuple<K, V>( keys[index], ( V ) dups ); // the entire tree was removed so pass it as the value
                 keyRemoved = true;
             }
-            else // value is not found
+            else
+            // value is not found
             {
                 return NotPresentResult.NOT_PRESENT;
             }
@@ -179,13 +182,13 @@ public class Leaf<K, V> extends Abstract
         else
         {
             V existing = values[index].getValue( btree );
-            
-            if( ( ( existing == null ) && ( value == null ) ) || ( value == null ) )
+
+            if ( ( ( existing == null ) && ( value == null ) ) || ( value == null ) )
             {
                 removedElement = new Tuple<K, V>( keys[index], existing );
                 keyRemoved = true;
             }
-            else if( btree.getValueSerializer().compare( value, existing ) == 0 )
+            else if ( btree.getValueSerializer().compare( value, existing ) == 0 )
             {
                 removedElement = new Tuple<K, V>( keys[index], value );
                 keyRemoved = true;
@@ -197,8 +200,8 @@ public class Leaf<K, V> extends Abstract
         }
 
         Leaf<K, V> newLeaf = null;
-        
-        if( keyRemoved )
+
+        if ( keyRemoved )
         {
             newLeaf = new Leaf<K, V>( btree, revision, nbElems - 1 );
         }
@@ -218,7 +221,7 @@ public class Leaf<K, V> extends Abstract
 
             return defaultResult;
         }
-        else if( keyRemoved )
+        else if ( keyRemoved )
         {
             // The current page is not the root. Check if the leaf has more than N/2
             // elements
@@ -236,7 +239,8 @@ public class Leaf<K, V> extends Abstract
                 if ( sibling.getNbElems() == halfSize )
                 {
                     // We will merge the current page with its sibling
-                    DeleteResult<K, V> result = mergeWithSibling( removedElement, revision, sibling, ( siblingPos < parentPos ), index );
+                    DeleteResult<K, V> result = mergeWithSibling( removedElement, revision, sibling,
+                        ( siblingPos < parentPos ), index );
 
                     return result;
                 }
@@ -269,7 +273,7 @@ public class Leaf<K, V> extends Abstract
                 return defaultResult;
             }
         }
-        
+
         return defaultResult;
     }
 
@@ -284,7 +288,8 @@ public class Leaf<K, V> extends Abstract
      * @return The new created leaf containing the sibling and the old page.
      * @throws IOException If we have an error while trying to access the page
      */
-    private DeleteResult<K, V> mergeWithSibling( Tuple<K, V> removedElement, long revision, Leaf<K, V> sibling, boolean isLeft, int pos )
+    private DeleteResult<K, V> mergeWithSibling( Tuple<K, V> removedElement, long revision, Leaf<K, V> sibling,
+        boolean isLeft, int pos )
         throws EndOfFileExceededException, IOException
     {
         // Create the new page. It will contain N - 1 elements (the maximum number)
@@ -430,24 +435,25 @@ public class Leaf<K, V> extends Abstract
      */
     private void copyAfterRemovingElement( boolean keyRemoved, Leaf<K, V> newLeaf, int pos ) throws IOException
     {
-        if( keyRemoved )
+        if ( keyRemoved )
         {
             // Deal with the special case of a page with only one element by skipping
             // the copy, as we won't have any remaining  element in the page
-            if( nbElems == 1 )
+            if ( nbElems == 1 )
             {
                 return;
             }
-            
+
             // Copy the keys and the values up to the insertion position
             System.arraycopy( keys, 0, newLeaf.keys, 0, pos );
             System.arraycopy( values, 0, newLeaf.values, 0, pos );
-            
+
             // And copy the elements after the position
             System.arraycopy( keys, pos + 1, newLeaf.keys, pos, keys.length - pos - 1 );
             System.arraycopy( values, pos + 1, newLeaf.values, pos, values.length - pos - 1 );
         }
-        else // one of the many values of the same key was removed, no change in the number of keys
+        else
+        // one of the many values of the same key was removed, no change in the number of keys
         {
             System.arraycopy( keys, 0, newLeaf.keys, 0, nbElems );
             System.arraycopy( values, 0, newLeaf.values, 0, nbElems );
@@ -458,24 +464,6 @@ public class Leaf<K, V> extends Abstract
     /**
      * {@inheritDoc}
      */
-    public boolean exist( K key )
-    {
-        int pos = findPos( key );
-
-        if ( pos < 0 )
-        {
-            return true;
-        }
-        else
-        {
-            return false;
-        }
-    }
-
-
-    /**
-     * {@inheritDoc}
-     */
     public V get( K key ) throws KeyNotFoundException, IOException
     {
         int pos = findPos( key );
@@ -483,14 +471,14 @@ public class Leaf<K, V> extends Abstract
         if ( pos < 0 )
         {
             V v = values[-( pos + 1 )].getValue( btree );
-            
-            if( btree.isAllowDuplicates() )
+
+            if ( btree.isAllowDuplicates() )
             {
                 // always return the first value for get(key) when duplicates are allowed
-                BTree<V,V> dupTree = ( ( BTree<V,V> ) v );
+                BTree<V, V> dupTree = ( ( BTree<V, V> ) v );
                 return dupTree.rootPage.getLeftMostKey();
             }
-            
+
             return v;
         }
         else
@@ -511,7 +499,7 @@ public class Leaf<K, V> extends Abstract
         {
             return true;
         }
-        
+
         return false;
     }
 
@@ -524,11 +512,11 @@ public class Leaf<K, V> extends Abstract
         if ( pos < 0 )
         {
             V v = values[-( pos + 1 )].getValue( btree );
-            
-            if( btree.isAllowDuplicates() )
+
+            if ( btree.isAllowDuplicates() )
             {
                 // always return the first value for get(key) when duplicates are allowed
-                BTree<V,V> dupTree = ( ( BTree<V,V> ) v );
+                BTree<V, V> dupTree = ( ( BTree<V, V> ) v );
                 return dupTree.hasKey( value );
             }
             else
@@ -586,7 +574,7 @@ public class Leaf<K, V> extends Abstract
             ParentPos<K, V> parentPos = new ParentPos<K, V>( this, index );
             setDupsContainer( parentPos );
             stack.push( parentPos );
-            
+
             cursor = new Cursor<K, V>( btree, transaction, stack );
         }
         else
@@ -632,9 +620,9 @@ public class Leaf<K, V> extends Abstract
         {
             // Start at the beginning of the page
             ParentPos<K, V> parentPos = new ParentPos<K, V>( this, pos );
-            
+
             setDupsContainer( parentPos );
-            
+
             stack.push( parentPos );
 
             cursor = new Cursor<K, V>( btree, transaction, stack );
@@ -685,19 +673,19 @@ public class Leaf<K, V> extends Abstract
         }
 
         V oldValue = null;
-        
-        if( btree.isAllowDuplicates() )
+
+        if ( btree.isAllowDuplicates() )
         {
-           BTree<V, V> dupValues = ( BTree<V, V> ) newLeaf.values[pos].getValue( btree );
-           // return value will always be null  here 
-           if( !dupValues.hasKey( value ) )
-           {
-               dupValues.insert( value, null, 0 );
-           }
-           else
-           {
-               oldValue = value;
-           }
+            BTree<V, V> dupValues = ( BTree<V, V> ) newLeaf.values[pos].getValue( btree );
+            // return value will always be null  here 
+            if ( !dupValues.hasKey( value ) )
+            {
+                dupValues.insert( value, null, 0 );
+            }
+            else
+            {
+                oldValue = value;
+            }
         }
         else
         {
@@ -705,10 +693,10 @@ public class Leaf<K, V> extends Abstract
             oldValue = newLeaf.values[pos].getValue( btree );
             newLeaf.values[pos] = btree.createHolder( value );
         }
-        
+
         // Create the result
         InsertResult<K, V> result = new ModifyResult<K, V>( newLeaf, oldValue );
-        
+
         return result;
     }
 
@@ -729,10 +717,10 @@ public class Leaf<K, V> extends Abstract
         Leaf<K, V> newLeaf = new Leaf<K, V>( btree, revision, nbElems + 1 );
 
         // Atm, store the value in memory
-        
+
         ElementHolder valueHolder = null;
-        
-        if( btree.isAllowDuplicates() )
+
+        if ( btree.isAllowDuplicates() )
         {
             valueHolder = new DuplicateKeyMemoryHolder<K, V>( btree, value );
         }
@@ -740,7 +728,7 @@ public class Leaf<K, V> extends Abstract
         {
             valueHolder = new MemoryHolder<K, V>( btree, value );
         }
-        
+
         //ValueHolder<K, V> valueHolder = btree.createHolder( value );
 
         // Deal with the special case of an empty page
@@ -877,17 +865,17 @@ public class Leaf<K, V> extends Abstract
     public Tuple<K, V> findLeftMost() throws IOException
     {
         V val = null;
-        
-        if( btree.isAllowDuplicates() )
+
+        if ( btree.isAllowDuplicates() )
         {
-            BTree<V,V> dupTree = ( BTree<V,V> ) values[0].getValue( btree );
+            BTree<V, V> dupTree = ( BTree<V, V> ) values[0].getValue( btree );
             val = dupTree.rootPage.getLeftMostKey();
         }
         else
         {
             val = values[0].getValue( btree );
         }
-        
+
         return new Tuple<K, V>( keys[0], val );
     }
 
@@ -898,17 +886,17 @@ public class Leaf<K, V> extends Abstract
     public Tuple<K, V> findRightMost() throws EndOfFileExceededException, IOException
     {
         V val = null;
-        
-        if( btree.isAllowDuplicates() )
+
+        if ( btree.isAllowDuplicates() )
         {
-            BTree<V,V> dupTree = ( BTree<V,V> ) values[nbElems - 1].getValue( btree );
+            BTree<V, V> dupTree = ( BTree<V, V> ) values[nbElems - 1].getValue( btree );
             val = dupTree.rootPage.getRightMostKey();
         }
         else
         {
             val = values[nbElems - 1].getValue( btree );
         }
-        
+
         return new Tuple<K, V>( keys[nbElems - 1], val );
     }
 
@@ -955,23 +943,23 @@ public class Leaf<K, V> extends Abstract
      *
      * @param parentPos The position we will add the sub-tree at
      */
-    private void setDupsContainer( ParentPos<K,V> parentPos )
+    private void setDupsContainer( ParentPos<K, V> parentPos )
     {
-        if( btree.isAllowDuplicates() )
+        if ( btree.isAllowDuplicates() )
         {
             try
             {
                 BTree<V, V> dupsContainer = ( BTree<V, V> ) values[parentPos.pos].getValue( btree );
                 parentPos.dupsContainer = dupsContainer;
             }
-            catch( IOException e )
+            catch ( IOException e )
             {
                 throw new RuntimeException( e );
             }
         }
     }
 
-    
+
     /**
      * {@inheritDoc}
      */

Modified: labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/Node.java
URL: http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/Node.java?rev=1463904&r1=1463903&r2=1463904&view=diff
==============================================================================
--- labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/Node.java (original)
+++ labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/Node.java Wed Apr  3 10:06:41 2013
@@ -560,7 +560,8 @@ public class Node<K, V> extends Abstract
     /**
      * {@inheritDoc}
      */
-    public DeleteResult<K, V> delete( long revision, K key, V value, Page<K, V> parent, int parentPos ) throws IOException
+    public DeleteResult<K, V> delete( long revision, K key, V value, Page<K, V> parent, int parentPos )
+        throws IOException
     {
         // We first try to delete the element from the child it belongs to
         // Find the key in the page
@@ -809,26 +810,6 @@ public class Node<K, V> extends Abstract
 
 
     /**
-     * {@inheritDoc} 
-     */
-    public boolean exist( K key ) throws IOException
-    {
-        int pos = findPos( key );
-
-        if ( pos < 0 )
-        {
-            // Here, if we have found the key in the node, then we must go down into
-            // the right child, not the left one
-            return children[-pos].getValue( btree ).exist( key );
-        }
-        else
-        {
-            return children[pos].getValue( btree ).exist( key );
-        }
-    }
-
-
-    /**
      * {@inheritDoc}
      */
     public V get( K key ) throws IOException, KeyNotFoundException

Modified: labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/Page.java
URL: http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/Page.java?rev=1463904&r1=1463903&r2=1463904&view=diff
==============================================================================
--- labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/Page.java (original)
+++ labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/Page.java Wed Apr  3 10:06:41 2013
@@ -83,16 +83,6 @@ public interface Page<K, V>
 
 
     /**
-     * Check if there is an element associated with the given key.
-     * 
-     * @param key The key we are looking at
-     * @return true if the Key exists in the BTree 
-     * @throws IOException If we have an error while trying to access the page
-     */
-    boolean exist( K key ) throws IOException;
-
-
-    /**
      * Gets the value associated with the given key, if any. If we don't have 
      * one, this method will throw a KeyNotFoundException.<br/>
      * Note that we may get back null if a null value has been associated 
@@ -114,8 +104,8 @@ public interface Page<K, V>
      * @return true if the key and value are associated with each other, false otherwise
      */
     boolean contains( K key, V value ) throws IOException;
-    
-    
+
+
     /**
      * Browses the tree, looking for the given key, and creates a Cursor on top
      * of the found result.
@@ -176,7 +166,7 @@ public interface Page<K, V>
      */
     K getRightMostKey() throws IOException;
 
-    
+
     /**
      * Finds the leftmost element in this page. If the page is a node, it will go
      * down in the leftmost children to recursively find the leftmost element.
@@ -203,8 +193,8 @@ public interface Page<K, V>
      * @return A pretty-print dump of the tree
      */
     String dumpPage( String tabs );
-    
-    
+
+
     /**
      * Find the position of the given key in the page. If we have found the key,
      * we will return its position as a negative value.
@@ -242,13 +232,14 @@ public interface Page<K, V>
      * @return The position in the page.
      */
     int findPos( K key );
-    
-    
+
+
     /**
      * Checks if the given key exists.
      *  
      * @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
      */
     boolean hasKey( K key ) throws IOException;
 }

Modified: labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/store/RecordManagerTest.java
URL: http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/store/RecordManagerTest.java?rev=1463904&r1=1463903&r2=1463904&view=diff
==============================================================================
--- labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/store/RecordManagerTest.java (original)
+++ labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/test/java/org/apache/mavibot/btree/store/RecordManagerTest.java Wed Apr  3 10:06:41 2013
@@ -143,9 +143,9 @@ public class RecordManagerTest
         assertEquals( btree.getValueSerializer().getClass().getName(), btree1.getValueSerializer().getClass().getName() );
 
         // Check the stored element
-        assertTrue( btree1.exist( 1L ) );
-        assertTrue( btree1.exist( 3L ) );
-        assertTrue( btree1.exist( 5L ) );
+        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 ) );
@@ -207,7 +207,7 @@ public class RecordManagerTest
         // Check the stored element
         for ( long i = 1L; i < 32L; i++ )
         {
-            assertTrue( btree1.exist( i ) );
+            assertTrue( btree1.hasKey( i ) );
             assertEquals( "V" + i, btree1.get( i ) );
         }
     }



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