directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1549961 [3/3] - in /directory/mavibot/trunk/mavibot/src: main/java/org/apache/directory/mavibot/btree/ main/java/org/apache/directory/mavibot/btree/managed/ main/java/org/apache/directory/mavibot/btree/memory/ test/java/org/apache/director...
Date Tue, 10 Dec 2013 20:42:17 GMT
Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/ValueHolder.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/ValueHolder.java?rev=1549961&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/ValueHolder.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/ValueHolder.java Tue Dec 10 20:42:16 2013
@@ -0,0 +1,750 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.directory.mavibot.btree.memory;
+
+
+import java.io.IOException;
+import java.util.Comparator;
+import java.util.UUID;
+
+import org.apache.directory.mavibot.btree.Tuple;
+import org.apache.directory.mavibot.btree.TupleCursor;
+import org.apache.directory.mavibot.btree.ValueCursor;
+import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
+
+
+/**
+ * A holder to store the Values
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @param <V> The value type
+ */
+public class ValueHolder<V> implements Cloneable
+{
+    /** The deserialized value */
+    private V value;
+
+    /** The BTree storing multiple value, if we have more than one value */
+    private BTree<V, V> valueBtree;
+
+    /** The RecordManager */
+    private BTree<?, V> btree;
+
+
+    /**
+     * A class that encapsulate one single value
+     */
+    private class ValueSingletonCursor implements ValueCursor<V>
+    {
+        /** Store the current position in the array or in the BTree */
+        private int currentPos;
+
+
+        /**
+         * Create an instance
+         */
+        private ValueSingletonCursor()
+        {
+            // Start at -1 to be positioned before the first element
+            currentPos = BEFORE_FIRST;
+        }
+
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public boolean hasNext()
+        {
+            return currentPos == BEFORE_FIRST;
+        }
+
+
+        /**
+         * {@inheritDoc}
+         */
+        public V next()
+        {
+            switch ( currentPos )
+            {
+                case AFTER_LAST :
+                    return null;
+                    
+                case BEFORE_FIRST :
+                    currentPos = 0;
+                    return value;
+                    
+                default :
+                    currentPos = AFTER_LAST;
+                    return null;
+            }
+        }
+
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public boolean hasPrev()
+        {
+            return currentPos > 0 || currentPos == AFTER_LAST;
+        }
+
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public void close()
+        {
+        }
+
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public void beforeFirst() throws IOException
+        {
+            currentPos = BEFORE_FIRST;
+        }
+
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public void afterLast() throws IOException
+        {
+            currentPos = AFTER_LAST;
+        }
+
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public V prev() throws EndOfFileExceededException, IOException
+        {
+            switch ( currentPos )
+            {
+                case AFTER_LAST :
+                    currentPos = 0;
+                    return value;
+                    
+                case BEFORE_FIRST :
+                    return null;
+                    
+                default :
+                    currentPos = BEFORE_FIRST;
+                    return null;
+            }
+        }
+
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public int size()
+        {
+            return 1;
+        }
+
+
+        /**
+         * @see Object#toString()
+         */
+        public String toString()
+        {
+            StringBuilder sb = new StringBuilder();
+            
+            sb.append( "SingletonCursor , currentpos =" );
+            
+            switch ( currentPos ) 
+            {
+                case BEFORE_FIRST :
+                    sb.append(  "BEFORE_FIRST" );
+                    break;
+                    
+                case AFTER_LAST :
+                    sb.append(  "AFTER_LAST" );
+                    break;
+                    
+                default :
+                    sb.append( "0/0" );
+                    break;
+            }
+            
+            return sb.toString();
+        }
+    }
+
+    
+    /**
+     * A class that encapsulate the values into an sub-btree
+     */
+    private class ValueBtreeCursor implements ValueCursor<V>
+    {
+        /** Store the current position in the array or in the BTree */
+        private TupleCursor<V, V> cursor;
+
+
+        /**
+         * Create an instance
+         */
+        private ValueBtreeCursor()
+        {
+            // Start at -1 to be positioned before the first element
+            try
+            {
+                if ( valueBtree != null )
+                {
+                    cursor = valueBtree.browse();
+                }
+            }
+            catch ( IOException e )
+            {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
+        }
+
+
+        /**
+         * {@inheritDoc}}
+         */
+        @Override
+        public boolean hasNext()
+        {
+            if ( cursor == null )
+            {
+                return false;
+            }
+            else
+            {
+                try
+                {
+                    return cursor.hasNext();
+                }
+                catch ( EndOfFileExceededException e )
+                {
+                    e.printStackTrace();
+                    return false;
+                }
+                catch ( IOException e )
+                {
+                    e.printStackTrace();
+                    return false;
+                }
+            }
+        }
+
+
+        /**
+         * {@inheritDoc}}
+         */
+        public V next()
+        {
+            try
+            {
+                return cursor.next().getKey();
+            }
+            catch ( EndOfFileExceededException e )
+            {
+                e.printStackTrace();
+                return null;
+            }
+            catch ( IOException e )
+            {
+                e.printStackTrace();
+                return null;
+            }
+        }
+
+
+        /**
+         * {@inheritDoc}}
+         */
+        @Override
+        public boolean hasPrev() throws EndOfFileExceededException, IOException
+        {
+            if ( cursor == null )
+            {
+                return false;
+            }
+            else
+            {
+                try
+                {
+                    return cursor.hasPrev();
+                }
+                catch ( EndOfFileExceededException e )
+                {
+                    e.printStackTrace();
+                    return false;
+                }
+                catch ( IOException e )
+                {
+                    e.printStackTrace();
+                    return false;
+                }
+            }
+        }
+
+
+        /**
+         * {@inheritDoc}}
+         */
+        @Override
+        public void close()
+        {
+            if ( cursor != null )
+            {
+                cursor.close();
+            }
+        }
+
+
+        /**
+         * {@inheritDoc}}
+         */
+        @Override
+        public void beforeFirst() throws IOException
+        {
+            if ( cursor != null )
+            {
+                cursor.beforeFirst();
+            }
+        }
+
+
+        /**
+         * {@inheritDoc}}
+         */
+        @Override
+        public void afterLast() throws IOException
+        {
+            if ( cursor != null )
+            {
+                cursor.afterLast();
+            }
+        }
+
+
+        /**
+         * {@inheritDoc}}
+         */
+        @Override
+        public V prev() throws EndOfFileExceededException, IOException
+        {
+            try
+            {
+                return cursor.prev().getKey();
+            }
+            catch ( EndOfFileExceededException e )
+            {
+                e.printStackTrace();
+                return null;
+            }
+            catch ( IOException e )
+            {
+                e.printStackTrace();
+                return null;
+            }
+        }
+
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public int size()
+        {
+            return ( int ) valueBtree.getNbElems();
+        }
+
+
+        /**
+         * @see Object#toString()
+         */
+        public String toString()
+        {
+            return "BTreeCursor";
+        }
+    }
+
+    /**
+     * Creates a new instance of a ValueHolder, containing the serialized values.
+     * 
+     * @param btree the container BTree
+     * @param valueSerializer The Value's serializer
+     * @param raw The raw data containing the values
+     * @param nbValues the number of stored values
+     * @param raw the byte[] containing either the serialized array of values or the sub-btree offset
+     */
+    /* No qualifier */ValueHolder( BTree<?, V> btree, int nbValues )
+    {
+        this.btree = btree;
+    }
+
+
+    /**
+     * Creates a new instance of a ValueHolder, containing Values. This constructor is called
+     * whe we need to create a new ValueHolder with deserialized values.
+     * 
+     * @param valueSerializer The Value's serializer
+     * @param values The Values stored in the ValueHolder
+     */
+    /* No qualifier */ValueHolder( BTree<?, V> btree, V... values )
+    {
+        this.btree = btree;
+
+        if ( ( values != null ) && ( values.length > 0 ) )
+        {
+            int nbValues = values.length;
+
+            if ( nbValues < 2 )
+            {
+                // Store the value
+                value = values[0];
+            }
+            else
+            {
+                // Use a sub btree, now that we have reached the threshold
+                createSubTree();
+
+                // Now inject all the values into it
+                for ( V value : values )
+                {
+                    try
+                    {
+                        valueBtree.insert( value, value );
+                    }
+                    catch ( IOException e )
+                    {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        }
+    }
+
+
+    /**
+     * @return a cursor on top of the values
+     */
+    public ValueCursor<V> getCursor()
+    {
+        ValueCursor<V> cursor;
+
+        if ( valueBtree != null )
+        {
+            cursor = new ValueBtreeCursor();
+        }
+        else
+        {
+            cursor = new ValueSingletonCursor();
+        }
+
+        return cursor;
+    }
+
+
+    /**
+     * @return the isSubBtree
+     */
+    public boolean isSubBtree()
+    {
+        return valueBtree != null;
+    }
+
+
+    /**
+     * @return the number of stored values
+     */
+    public int size()
+    {
+        if ( valueBtree != null )
+        {
+            return ( int ) valueBtree.getNbElems();
+        }
+        else
+        {
+            return 1;
+        }
+    }
+
+
+    /**
+     * Create a new Sub-BTree to store the values.
+     */
+    private void createSubTree()
+    {
+        try
+        {
+            BTreeConfiguration<V, V> configuration = new BTreeConfiguration<V, V>();
+            configuration.setAllowDuplicates( false );
+            configuration.setName( UUID.randomUUID().toString() );
+            
+            valueBtree = new BTree<V, V>( configuration );
+        }
+        catch ( IOException e )
+        {
+            throw new RuntimeException( e );
+        }
+    }
+
+
+    /**
+     * Set the subBtree in the ValueHolder
+     */
+    /* No qualifier*/void setSubBtree( BTree<V, V> subBtree )
+    {
+        valueBtree = subBtree;
+        value = null;
+    }
+    
+    
+    /**
+     * Add the value in an array
+     */
+    private void addInBTree( V newValue )
+    {
+        // Ok, create a sub-btree
+        try
+        {
+            valueBtree = new BTree<V, V>( UUID.randomUUID().toString(), btree.getValueSerializer(),
+                btree.getValueSerializer() );
+
+            valueBtree.insert( value, null, 0 );
+            valueBtree.insert( newValue, null, 0 );
+            value = null;
+        }
+        catch ( IOException e )
+        {
+            throw new RuntimeException( e );
+        }
+    }
+
+
+    /**
+     * Add a new value in the ValueHolder
+     * 
+     * @param newValue The added value
+     */
+    public void add( V newValue )
+    {
+        if ( value != null )
+        {
+            try
+            {
+                valueBtree = new BTree<V, V>( UUID.randomUUID().toString(), btree.getValueSerializer(),
+                    btree.getValueSerializer() );
+
+                valueBtree.insert( value, null, 0 );
+                valueBtree.insert( newValue, null, 0 );
+                value = null;
+            }
+            catch ( IOException e )
+            {
+                throw new RuntimeException( e );
+            }
+        }
+        else if ( valueBtree != null )
+        {
+            try
+            {
+                valueBtree.insert( newValue, null, 0 );
+            }
+            catch ( IOException e )
+            {
+                throw new RuntimeException( e );
+            }
+        }
+        else
+        {
+            this.value = newValue;
+        }
+    }
+    
+    
+    /**
+     * Remove a value from the ValueHolder
+     * 
+     * @param removedValue The value to remove
+     */
+    public V remove( V removedValue )
+    {
+        if ( valueBtree == null )
+        {
+            if ( removedValue == null )
+            {
+                return null; 
+            }
+            
+            Comparator<V> comparator = btree.getValueSerializer().getComparator();
+
+            int result = comparator.compare( removedValue, value );
+
+            if ( result != 0 )
+            {
+                // The value does not exists : nothing to do
+                return null;
+            }
+            else
+            {
+                V returnedValue = value;
+                value = null;
+                
+                return returnedValue;
+            }
+        }
+        else
+        {
+            V returnedValue = null;
+            
+            try
+            {
+                Tuple<V, V> removedTuple = valueBtree.delete( removedValue );
+                
+                if ( removedTuple != null )
+                {
+                    returnedValue = removedTuple.getKey();
+                }
+            }
+            catch ( IOException e )
+            {
+                throw new RuntimeException( e );
+            }
+
+            if ( valueBtree.getNbElems() == 1 )
+            {
+                try
+                {
+                    value = valueBtree.browse().next().getKey();
+                    valueBtree.close();
+                    valueBtree = null;
+                }
+                catch ( EndOfFileExceededException e )
+                {
+                    throw new RuntimeException( e );
+                }
+                catch ( IOException e )
+                {
+                    throw new RuntimeException( e );
+                }
+            }
+            
+            return returnedValue;
+        }
+    }
+    
+    
+    /**
+     * Check that a value exists in the ValueHolder
+     * 
+     * @param checkedValue The value to check
+     */
+    public boolean contains( V checkedValue )
+    {
+        if ( valueBtree != null )
+        {
+            try
+            {
+                return valueBtree.hasKey( checkedValue );
+            }
+            catch ( IOException e )
+            {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+                return false;
+            }
+        }
+        else
+        {
+            Comparator<V> comparator = btree.getValueSerializer().getComparator();
+
+            int result = comparator.compare( checkedValue, value );
+            
+            return result == 0;
+        }
+    }
+
+
+    /**
+     * Find the position of a given value in the array, or the position where we
+     * would insert the element (in this case, the position will be negative).
+     * As we use a 0-based array, the negative position for 0 is -1.
+     * -1 means the element can be added in position 0
+     * -2 means the element can be added in position 1
+     * ... 
+     */
+    private int findPos1( V findValue )
+    {
+        if ( findValue == null )
+        {
+            return -1;
+        }
+
+        Comparator<V> comparator = btree.getValueSerializer().getComparator();
+
+        int result = comparator.compare( findValue, value );
+        
+        return result;
+    }
+
+
+    /**
+     * Create a clone of this instance
+     */
+    public ValueHolder<V> clone() throws CloneNotSupportedException
+    {
+        ValueHolder<V> copy = ( ValueHolder<V> ) super.clone();
+
+        return copy;
+    }
+
+
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+
+        sb.append( "ValueHolder[" ).append( btree.getValueSerializer().getClass().getSimpleName() );
+
+        if ( valueBtree != null )
+        {
+            sb.append( ", SubBTree" );
+        }
+        else
+        {
+            sb.append( ", {" );
+            sb.append( value );
+            sb.append( "}" );
+        }
+        
+        sb.append( "]" );
+
+        return sb.toString();
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/BTreeDuplicateKeyTest.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/BTreeDuplicateKeyTest.java?rev=1549961&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/BTreeDuplicateKeyTest.java (added)
+++ directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/BTreeDuplicateKeyTest.java Tue Dec 10 20:42:16 2013
@@ -0,0 +1,772 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+package org.apache.directory.mavibot.btree.managed;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.NoSuchElementException;
+import java.util.UUID;
+
+import org.apache.directory.mavibot.btree.Tuple;
+import org.apache.directory.mavibot.btree.TupleCursor;
+import org.apache.directory.mavibot.btree.serializer.IntSerializer;
+import org.apache.directory.mavibot.btree.serializer.LongSerializer;
+import org.apache.directory.mavibot.btree.serializer.StringSerializer;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+
+/**
+ * TODO BTreeDuplicateKeyTest.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class BTreeDuplicateKeyTest
+{
+    private BTree<Long, String> btree = null;
+
+    private RecordManager recordManager1 = null;
+
+    @Rule
+    public TemporaryFolder tempFolder = new TemporaryFolder();
+
+    private File dataDir = null;
+
+
+    @Before
+    public void createBTree()
+    {
+        dataDir = tempFolder.newFolder( UUID.randomUUID().toString() );
+
+        openRecordManagerAndBtree();
+
+        try
+        {
+            // Create a new BTree
+            btree = recordManager1.addBTree( "test", new LongSerializer(), new StringSerializer(), false );
+        }
+        catch ( Exception e )
+        {
+            throw new RuntimeException( e );
+        }
+    }
+
+    
+    private void openRecordManagerAndBtree()
+    {
+        try
+        {
+            if ( recordManager1 != null )
+            {
+                recordManager1.close();
+            }
+
+            // Now, try to reload the file back
+            recordManager1 = new RecordManager( dataDir.getAbsolutePath() );
+
+            // load the last created btree
+            if ( btree != null )
+            {
+                btree = recordManager1.getManagedTree( btree.getName() );
+            }
+        }
+        catch ( Exception e )
+        {
+            throw new RuntimeException( e );
+        }
+    }
+
+    
+    @Test
+    public void testInsertNullValue() throws IOException
+    {
+        btree.insert( 1L, null );
+
+        TupleCursor<Long, String> cursor = btree.browse();
+        assertTrue( cursor.hasNext() );
+
+        Tuple<Long, String> t = cursor.next();
+
+        assertEquals( Long.valueOf( 1 ), t.getKey() );
+        assertEquals( null, t.getValue() );
+
+        cursor.close();
+        
+        btree.close();
+    }
+
+
+    @Test
+    public void testBrowseEmptyTree() throws IOException
+    {
+        IntSerializer serializer = new IntSerializer();
+
+        BTree<Integer, Integer> btree = new BTree<Integer, Integer>( "master", serializer, serializer );
+        btree.init();
+
+        TupleCursor<Integer, Integer> cursor = btree.browse();
+        assertFalse( cursor.hasNext() );
+        assertFalse( cursor.hasPrev() );
+
+        try
+        {
+            cursor.next();
+            fail( "Should not reach here" );
+        }
+        catch ( NoSuchElementException e )
+        {
+            assertTrue( true );
+        }
+
+        try
+        {
+            cursor.prev();
+            fail( "Should not reach here" );
+        }
+        catch ( NoSuchElementException e )
+        {
+            assertTrue( true );
+        }
+
+        cursor.close();
+        btree.close();
+    }
+
+
+    @Test
+    public void testDuplicateKey() throws IOException
+    {
+        btree.insert( 1L, "1" );
+        btree.insert( 1L, "2" );
+
+        TupleCursor<Long, String> cursor = btree.browse();
+        assertTrue( cursor.hasNext() );
+
+        Tuple<Long, String> t = cursor.next();
+
+        assertEquals( Long.valueOf( 1 ), t.getKey() );
+        assertEquals( "1", t.getValue() );
+
+        assertTrue( cursor.hasNext() );
+
+        t = cursor.next();
+
+        assertEquals( Long.valueOf( 1 ), t.getKey() );
+        assertEquals( "2", t.getValue() );
+
+        assertFalse( cursor.hasNext() );
+
+        // test backward move
+        assertTrue( cursor.hasPrev() );
+
+        t = cursor.prev();
+
+        assertEquals( Long.valueOf( 1 ), t.getKey() );
+        assertEquals( "1", t.getValue() );
+
+        assertFalse( cursor.hasPrev() );
+
+        // again forward
+        assertTrue( cursor.hasNext() );
+
+        t = cursor.next();
+
+        assertEquals( Long.valueOf( 1 ), t.getKey() );
+        assertEquals( "2", t.getValue() );
+
+        assertFalse( cursor.hasNext() );
+
+        cursor.close();
+        btree.close();
+    }
+
+
+    @Test
+    public void testGetDuplicateKey() throws Exception
+    {
+        String retVal = btree.insert( 1L, "1" );
+        assertNull( retVal );
+
+        retVal = btree.insert( 1L, "2" );
+        assertNull( retVal );
+
+        // check the return value when an existing value is added again
+        retVal = btree.insert( 1L, "2" );
+        assertEquals( "2", retVal );
+
+        assertEquals( "1", btree.get( 1L ) );
+        assertTrue( btree.contains( 1L, "1" ) );
+        assertTrue( btree.contains( 1L, "2" ) );
+
+        assertFalse( btree.contains( 1L, "0" ) );
+        assertFalse( btree.contains( 0L, "1" ) );
+        assertFalse( btree.contains( 0L, "0" ) );
+        assertFalse( btree.contains( null, "0" ) );
+        assertFalse( btree.contains( 0L, null ) );
+        assertFalse( btree.contains( null, null ) );
+        btree.close();
+    }
+
+
+    @Test
+    public void testRemoveDuplicateKey() throws Exception
+    {
+        btree.insert( 1L, "1" );
+        btree.insert( 1L, "2" );
+
+        assertEquals( 2, btree.getNbElems() );
+
+        Tuple<Long, String> t = btree.delete( 1L, "1" );
+        assertEquals( Long.valueOf( 1 ), t.getKey() );
+        assertEquals( "1", t.getValue() );
+
+        assertEquals( 1l, btree.getNbElems() );
+
+        t = btree.delete( 1L, "2" );
+        assertEquals( Long.valueOf( 1 ), t.getKey() );
+        assertEquals( "2", t.getValue() );
+
+        assertEquals( 0l, btree.getNbElems() );
+
+        t = btree.delete( 1L, "2" );
+        assertNull( t );
+        btree.close();
+    }
+
+
+    @Test
+    public void testFullPage() throws Exception
+    {
+        int i = 7;
+        for ( char ch = 'a'; ch <= 'z'; ch++ )
+        {
+            for ( int k = 0; k < i; k++ )
+            {
+                String val = ch + Integer.toString( k );
+                btree.insert( Long.valueOf( ch ), val );
+            }
+        }
+
+        TupleCursor<Long, String> cursor = btree.browse();
+
+        char ch = 'a';
+        int k = 0;
+
+        while ( cursor.hasNext() )
+        {
+            Tuple<Long, String> t = cursor.next();
+            assertEquals( Long.valueOf( ch ), t.getKey() );
+            k++;
+
+            if ( ( k % i ) == 0 )
+            {
+                ch++;
+            }
+        }
+
+        assertEquals( ( 'z' + 1 ), ch );
+
+        ch = 'z';
+        cursor.afterLast();
+
+        while ( cursor.hasPrev() )
+        {
+            Tuple<Long, String> t = cursor.prev();
+            assertEquals( Long.valueOf( ch ), t.getKey() );
+            k--;
+
+            if ( ( k % i ) == 0 )
+            {
+                ch--;
+            }
+        }
+
+        assertEquals( ( 'a' - 1 ), ch );
+        cursor.close();
+    }
+
+
+    @Test
+    public void testMoveFirst() throws Exception
+    {
+        for ( char ch = 'a'; ch <= 'z'; ch++ )
+        {
+            String val = Character.toString( ch );
+            btree.insert( Long.valueOf( ch ), val );
+        }
+
+        assertEquals( 26, btree.getNbElems() );
+
+        // 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
+        TupleCursor<Long, String> cursor = btree.browseFrom( Long.valueOf( 'c' ) );
+
+        int i = 0;
+
+        while ( cursor.hasNext() )
+        {
+            Tuple<Long, String> tuple = cursor.next();
+            assertNotNull( tuple );
+            i++;
+        }
+
+        assertEquals( 24, i );
+
+        // now move the cursor first
+        cursor.beforeFirst();
+        assertTrue( cursor.hasNext() );
+        Tuple<Long, String> tuple = cursor.next();
+
+        // We should be on the first position
+        assertEquals( Long.valueOf( 'a' ), tuple.getKey() );
+
+        // Count the number of element after the first one, we should have 26 only
+        i = 0;
+
+        while ( cursor.hasNext() )
+        {
+            tuple = cursor.next();
+            assertNotNull( tuple );
+            i++;
+        }
+        
+        assertEquals( 26, i );
+
+        cursor.close();
+
+        // Rebrowse
+        cursor = btree.browse();
+
+        i = 0;
+        
+        while ( cursor.hasNext() )
+        {
+            assertNotNull( cursor.next() );
+            i++;
+        }
+        
+        // again, we should see 27 elements
+        assertEquals( 27, i );
+
+        // now move the cursor first, but move forward the keys
+        cursor.beforeFirst();
+        assertTrue( cursor.hasNextKey() );
+        assertEquals( Long.valueOf( 'a' ), cursor.nextKey().getKey() );
+
+        i = 0;
+        
+        while ( cursor.hasNextKey() )
+        {
+            tuple = cursor.nextKey();
+            long key = tuple.getKey();
+            assertNotNull( key );
+            i++;
+        }
+        
+        // We should have 25 keys only, as we just moved forward the first one
+        assertEquals( 25, i );
+    }
+
+
+    @Test
+    public void testMoveLast() throws Exception
+    {
+        for ( char ch = 'a'; ch <= 'z'; ch++ )
+        {
+            String val = Character.toString( ch );
+            btree.insert( Long.valueOf( ch ), val );
+        }
+
+        assertEquals( 26, btree.getNbElems() );
+
+        // 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
+        TupleCursor<Long, String> cursor = btree.browseFrom( Long.valueOf( 'x' ) );
+
+        int i = 0;
+
+        while ( cursor.hasPrev() )
+        {
+            Tuple<Long, String> tuple = cursor.prev();
+            assertNotNull( tuple );
+            i++;
+        }
+
+        assertEquals( 23, i );
+
+        // now move the cursor to the last element
+        cursor.afterLast();
+        assertTrue( cursor.hasPrev() );
+        Tuple<Long, String> tuple = cursor.prev();
+
+        // We should be on the last position
+        assertEquals( Long.valueOf( 'z' ), tuple.getKey() );
+
+        // Count the number of element before the last one, we should have 26
+        i = 0;
+
+        while ( cursor.hasPrev() )
+        {
+            tuple = cursor.prev();
+            assertNotNull( tuple );
+            i++;
+        }
+        
+        assertEquals( 26, i );
+
+        cursor.close();
+
+        // Rebrowse
+        cursor = btree.browse();
+        cursor.afterLast();
+
+        i = 0;
+        
+        while ( cursor.hasPrev() )
+        {
+            assertNotNull( cursor.prev() );
+            i++;
+        }
+        
+        // again, we should see 27 elements
+        assertEquals( 27, i );
+
+        // now move the cursor first, but move backward the keys
+        cursor.afterLast();
+        assertTrue( cursor.hasPrevKey() );
+        assertEquals( Long.valueOf( 'z' ), cursor.prevKey().getKey() );
+
+        i = 0;
+        
+        while ( cursor.hasPrevKey() )
+        {
+            tuple = cursor.prevKey();
+            long key = tuple.getKey();
+            assertNotNull( key );
+            i++;
+        }
+        
+        // We should have 25 keys only, as we just moved forward the first one
+        assertEquals( 25, i );
+    }
+
+
+    @Test(expected = NoSuchElementException.class)
+    public void testMoveLast2() throws Exception
+    {
+        for ( char ch = 'a'; ch <= 'z'; ch++ )
+        {
+            btree.insert( Long.valueOf( ch ), UUID.randomUUID().toString() );
+        }
+
+        btree.insert( Long.valueOf( 'z' ), UUID.randomUUID().toString() );
+
+        TupleCursor<Long, String> cursor = btree.browseFrom( Long.valueOf( 'c' ) );
+        cursor.afterLast();
+
+        assertFalse( cursor.hasNext() );
+        assertTrue( cursor.hasPrev() );
+        assertEquals( Long.valueOf( 'z' ), cursor.prev().getKey() );
+        // the key, 'z', has two values
+        assertEquals( Long.valueOf( 'z' ), cursor.prev().getKey() );
+        assertEquals( Long.valueOf( 'y' ), cursor.prev().getKey() );
+
+        cursor.beforeFirst();
+        assertEquals( Long.valueOf( 'a' ), cursor.next().getKey() );
+
+        cursor.afterLast();
+        assertFalse( cursor.hasNext() );
+        // make sure it throws NoSuchElementException
+        cursor.next();
+    }
+
+
+    @Test(expected = NoSuchElementException.class)
+    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++ )
+        {
+            for ( int k = 0; k < i; k++ )
+            {
+                btree.insert( Long.valueOf( ch ), String.valueOf( k ) );
+            }
+        }
+
+        TupleCursor<Long, String> cursor = btree.browse();
+
+        assertTrue( cursor.hasNext() );
+        assertFalse( cursor.hasPrev() );
+        
+        for ( int k = 0; k < 2; k++ )
+        {
+            assertEquals( Long.valueOf( 'a' ), cursor.next().getKey() );
+        }
+
+        assertEquals( Long.valueOf( 'a' ), cursor.next().getKey() );
+
+        Tuple<Long, String> tuple = cursor.nextKey();
+
+        assertEquals( Long.valueOf( 'b' ), tuple.getKey() );
+
+        for ( char ch = 'b'; ch < 'z'; ch++ )
+        {
+            assertEquals( Long.valueOf( ch ), cursor.next().getKey() );
+            tuple = cursor.nextKey();
+            char t = ch;
+            assertEquals( Long.valueOf( ++t ), tuple.getKey() );
+        }
+
+        for ( int k = 0; k < i; k++ )
+        {
+            assertEquals( Long.valueOf( 'z' ), cursor.next().getKey() );
+        }
+
+        assertFalse( cursor.hasNextKey() );
+        assertTrue( cursor.hasPrevKey() );
+        tuple = cursor.prev();
+        assertEquals( Long.valueOf( 'z' ), tuple.getKey() );
+        assertEquals( "6", tuple.getValue() );
+
+        for ( char ch = 'z'; ch > 'a'; ch-- )
+        {
+            char t = ch;
+            t--;
+
+            assertEquals( Long.valueOf( ch ), cursor.prev().getKey() );
+
+            tuple = cursor.prevKey();
+
+            assertEquals( Long.valueOf( t ), tuple.getKey() );
+        }
+
+        for ( int k = 5; k >= 0; k-- )
+        {
+            tuple = cursor.prev();
+            assertEquals( Long.valueOf( 'a' ), tuple.getKey() );
+            assertEquals( String.valueOf( k ), tuple.getValue() );
+        }
+
+        assertTrue( cursor.hasNext() );
+        assertFalse( cursor.hasPrev() );
+        tuple = cursor.next();
+        assertEquals( Long.valueOf( 'a' ), tuple.getKey() );
+        assertEquals( "0", tuple.getValue() );
+
+        cursor.close();
+
+        cursor = btree.browseFrom( Long.valueOf( 'y' ) );
+        tuple = cursor.prevKey();
+        assertNotNull( tuple );
+        assertEquals( Long.valueOf( 'y' ), tuple.getKey() );
+        assertEquals( "6", tuple.getValue() );
+        cursor.close();
+
+        cursor = btree.browse();
+        cursor.beforeFirst();
+        assertFalse( cursor.hasPrev() );
+        // make sure it throws NoSuchElementException
+        cursor.prev();
+    }
+
+
+    /**
+     * Test for moving between two leaves. When moveToNextNonDuplicateKey is called
+     * and cursor is on the last element of the current leaf.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testMoveToNextAndPrevWithPageBoundaries() throws Exception
+    {
+        int i = 32;
+        for ( int k = 0; k < i; k++ )
+        {
+            btree.insert( (long)k, Long.toString( k ) );
+        }
+
+        // 15 is the last element of the first leaf
+        // Check that we correctly jump to the next page 
+        TupleCursor<Long, String> cursor = btree.browseFrom( 15L );
+        Tuple<Long, String> tuple = cursor.nextKey();
+
+        assertNotNull( tuple );
+        assertEquals( Long.valueOf( 16 ), tuple.getKey() );
+        assertEquals( "16", tuple.getValue() );
+        cursor.close();
+
+        // Do the same check, on the revert side : moving backward
+        cursor = btree.browseFrom( 16L );
+        tuple = cursor.prevKey();
+
+        assertNotNull( tuple );
+        assertEquals( Long.valueOf( 15 ), tuple.getKey() );
+        assertEquals( "15", tuple.getValue() );
+        cursor.close();
+
+        // Now do a next followed by a prev on the boundary of 2 pages
+        cursor = btree.browseFrom( 16L );
+        tuple = cursor.prevKey();
+
+        assertNotNull( tuple );
+        assertEquals( Long.valueOf( 15 ), tuple.getKey() );
+        assertEquals( "15", tuple.getValue() );
+
+        // Move next, we should be back to the initial value
+        assertTrue( cursor.hasNext() );
+        tuple = cursor.next();
+        assertEquals( Long.valueOf( 16 ), tuple.getKey() );
+        assertEquals( "16", tuple.getValue() );
+        cursor.close();
+
+        // test the extremes of the BTree instead of that of leaves
+        cursor = btree.browseFrom( 30L );
+        tuple = cursor.nextKey();
+        assertFalse( cursor.hasNext() );
+        assertTrue( cursor.hasPrev() );
+
+        assertEquals( Long.valueOf( 31 ), tuple.getKey() );
+        assertEquals( "31", tuple.getValue() );
+        cursor.close();
+
+        cursor = btree.browse();
+        assertTrue( cursor.hasNext() );
+        assertFalse( cursor.hasPrev() );
+
+        tuple = cursor.nextKey();
+        assertEquals( Long.valueOf( 0 ), tuple.getKey() );
+        assertEquals( "0", tuple.getValue() );
+        cursor.close();
+    }
+
+
+    @Test
+    public void testNextAfterPrev() throws Exception
+    {
+        int i = 32;
+
+        for ( int k = 0; k < i; k++ )
+        {
+            btree.insert( (long)k, String.valueOf( k ) );
+        }
+
+        // 15 is the last element of the first leaf
+        TupleCursor<Long, String> cursor = btree.browseFrom( 16L );
+
+        assertTrue( cursor.hasNext() );
+        Tuple<Long, String> tuple = cursor.next();
+        assertEquals( Long.valueOf( 16 ), tuple.getKey() );
+        assertEquals( "16", tuple.getValue() );
+
+        assertTrue( cursor.hasPrev() );
+        tuple = cursor.prev();
+        assertEquals( Long.valueOf( 15 ), tuple.getKey() );
+        assertEquals( "15", tuple.getValue() );
+
+        assertTrue( cursor.hasNext() );
+        tuple = cursor.next();
+        assertEquals( Long.valueOf( 16 ), tuple.getKey() );
+        assertEquals( "16", tuple.getValue() );
+        cursor.close();
+
+    }
+
+
+    /**
+     * Test for moving after a key and traversing backwards.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testMoveToNextAndTraverseBackward() throws Exception
+    {
+        int i = 5;
+        
+        for ( int k = 0; k < i; k++ )
+        {
+            btree.insert( (long)k, Long.toString( k ) );
+        }
+
+        // 4 is the last element in the tree
+        TupleCursor<Long, String> cursor = btree.browseFrom( 4L );
+        cursor.nextKey();
+
+        long currentKey = 4L;
+        
+        while ( cursor.hasPrev() )
+        {
+            assertEquals( Long.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
+    {
+        int i = 5;
+        
+        for ( int k = 0; k < i; k++ )
+        {
+            btree.insert( (long)k, Long.toString( k ) );
+        }
+
+        // 4 is the last element in the tree
+        TupleCursor<Long, String> cursor = btree.browseFrom( 0L );
+
+        long currentKey = 0L;
+        
+        while ( cursor.hasNext() )
+        {
+            assertEquals( Long.valueOf( currentKey ), cursor.next().getKey() );
+            currentKey++;
+        }
+
+        cursor.close();
+    }
+
+}

Modified: directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/BTreeDuplicateKeyTest.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/BTreeDuplicateKeyTest.java?rev=1549961&r1=1549960&r2=1549961&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/BTreeDuplicateKeyTest.java (original)
+++ directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/BTreeDuplicateKeyTest.java Tue Dec 10 20:42:16 2013
@@ -35,7 +35,6 @@ import org.apache.directory.mavibot.btre
 import org.apache.directory.mavibot.btree.TupleCursor;
 import org.apache.directory.mavibot.btree.serializer.IntSerializer;
 import org.apache.directory.mavibot.btree.serializer.StringSerializer;
-import org.junit.Ignore;
 import org.junit.Test;
 
 
@@ -144,13 +143,6 @@ public class BTreeDuplicateKeyTest
         t = cursor.prev();
 
         assertEquals( Integer.valueOf( 1 ), t.getKey() );
-        assertEquals( Integer.valueOf( 2 ), t.getValue() );
-
-        assertTrue( cursor.hasPrev() );
-
-        t = cursor.prev();
-
-        assertEquals( Integer.valueOf( 1 ), t.getKey() );
         assertEquals( Integer.valueOf( 1 ), t.getValue() );
 
         assertFalse( cursor.hasPrev() );
@@ -161,13 +153,6 @@ public class BTreeDuplicateKeyTest
         t = cursor.next();
 
         assertEquals( Integer.valueOf( 1 ), t.getKey() );
-        assertEquals( Integer.valueOf( 1 ), t.getValue() );
-
-        assertTrue( cursor.hasNext() );
-
-        t = cursor.next();
-
-        assertEquals( Integer.valueOf( 1 ), t.getKey() );
         assertEquals( Integer.valueOf( 2 ), t.getValue() );
 
         assertFalse( cursor.hasNext() );
@@ -226,6 +211,7 @@ public class BTreeDuplicateKeyTest
         btree.insert( 1, 1 );
         btree.insert( 1, 2 );
 
+        // We should have only one element in the tree (even if it has 2 values)
         assertEquals( 2l, btree.getNbElems() );
 
         Tuple<Integer, Integer> t = btree.delete( 1, 1 );
@@ -236,7 +222,7 @@ public class BTreeDuplicateKeyTest
 
         t = btree.delete( 1, 2 );
         assertEquals( Integer.valueOf( 1 ), t.getKey() );
-        assertEquals( Integer.valueOf( 2 ), t.getValue() );
+        assertNull( t.getValue() );
 
         assertEquals( 0l, btree.getNbElems() );
 
@@ -262,7 +248,8 @@ public class BTreeDuplicateKeyTest
         {
             for ( int k = 0; k < i; k++ )
             {
-                btree.insert( String.valueOf( ch ), UUID.randomUUID().toString() );
+                String val = ch + Integer.toString( k );
+                btree.insert( String.valueOf( ch ), val );
             }
         }
 
@@ -286,6 +273,7 @@ public class BTreeDuplicateKeyTest
         assertEquals( ( 'z' + 1 ), ch );
 
         ch = 'z';
+        cursor.afterLast();
 
         while ( cursor.hasPrev() )
         {
@@ -305,7 +293,6 @@ public class BTreeDuplicateKeyTest
 
 
     @Test
-    @Ignore
     public void testMoveFirst() throws Exception
     {
         StringSerializer serializer = new StringSerializer();
@@ -321,9 +308,13 @@ public class BTreeDuplicateKeyTest
             btree.insert( String.valueOf( ch ), UUID.randomUUID().toString() );
         }
 
+        assertEquals( 26, btree.getNbElems() );
+
         // add one more value for 'a'
         btree.insert( String.valueOf( 'a' ), UUID.randomUUID().toString() );
 
+        assertEquals( 27, btree.getNbElems() );
+
         TupleCursor<String, String> cursor = btree.browseFrom( "c" );
 
         int i = 0;
@@ -351,18 +342,22 @@ public class BTreeDuplicateKeyTest
             assertNotNull( tuple );
             i++;
         }
+        
         assertEquals( 26, i );
 
         cursor.close();
 
+        // Rebrowse
         cursor = btree.browse();
 
         i = 0;
+        
         while ( cursor.hasNext() )
         {
             assertNotNull( cursor.next() );
             i++;
         }
+        
         assertEquals( 27, i );
 
         // now move the cursor first
@@ -374,15 +369,17 @@ public class BTreeDuplicateKeyTest
         
         while ( cursor.hasNext() )
         {
-            assertNotNull( cursor.nextKey() );
+            tuple = cursor.nextKey();
+            String key = tuple.getKey();
+            assertNotNull( key );
             i++;
         }
-        assertEquals( 26, i );
+        
+        assertEquals( 25, i );
     }
 
 
     @Test(expected = NoSuchElementException.class)
-    @Ignore
     public void testMoveLast() throws Exception
     {
         StringSerializer serializer = new StringSerializer();
@@ -411,7 +408,7 @@ public class BTreeDuplicateKeyTest
         assertEquals( "y", cursor.prev().getKey() );
 
         cursor.beforeFirst();
-        assertEquals( "c", cursor.next().getKey() );
+        assertEquals( "a", cursor.next().getKey() );
 
         cursor.afterLast();
         assertFalse( cursor.hasNext() );
@@ -421,7 +418,6 @@ public class BTreeDuplicateKeyTest
 
 
     @Test(expected = NoSuchElementException.class)
-    @Ignore
     public void testNextPrevKey() throws Exception
     {
         StringSerializer serializer = new StringSerializer();
@@ -527,7 +523,6 @@ public class BTreeDuplicateKeyTest
      * @throws Exception
      */
     @Test
-    @Ignore
     public void testMoveToNextAndPrevWithPageBoundaries() throws Exception
     {
         IntSerializer serializer = new IntSerializer();
@@ -558,8 +553,8 @@ public class BTreeDuplicateKeyTest
         tuple = cursor.prevKey();
 
         assertNotNull( tuple );
-        assertEquals( Integer.valueOf( 3 ), tuple.getKey() );
-        assertEquals( Integer.valueOf( 3 ), tuple.getValue() );
+        assertEquals( Integer.valueOf( 2 ), tuple.getKey() );
+        assertEquals( Integer.valueOf( 2 ), tuple.getValue() );
         cursor.close();
 
         // 4 is the first element of the second leaf
@@ -579,7 +574,7 @@ public class BTreeDuplicateKeyTest
         cursor.close();
 
         // test the extremes of the BTree instead of that of leaves
-        cursor = btree.browseFrom( 6 );
+        cursor = btree.browseFrom( 5 );
         tuple = cursor.nextKey();
         assertFalse( cursor.hasNext() );
         assertTrue( cursor.hasPrev() );
@@ -589,10 +584,12 @@ public class BTreeDuplicateKeyTest
         cursor.close();
 
         cursor = btree.browse();
-        cursor.prevKey();
+        cursor.beforeFirst();
         assertTrue( cursor.hasNext() );
         assertFalse( cursor.hasPrev() );
 
+        tuple = cursor.next();
+        
         assertEquals( Integer.valueOf( 0 ), tuple.getKey() );
         assertEquals( Integer.valueOf( 0 ), tuple.getValue() );
         cursor.close();
@@ -627,8 +624,8 @@ public class BTreeDuplicateKeyTest
 
         assertTrue( cursor.hasPrev() );
         tuple = cursor.prev();
-        assertEquals( Integer.valueOf( 4 ), tuple.getKey() );
-        assertEquals( Integer.valueOf( 4 ), tuple.getValue() );
+        assertEquals( Integer.valueOf( 3 ), tuple.getKey() );
+        assertEquals( Integer.valueOf( 3 ), tuple.getValue() );
 
         assertTrue( cursor.hasNext() );
         tuple = cursor.next();
@@ -645,7 +642,6 @@ public class BTreeDuplicateKeyTest
      * @throws Exception
      */
     @Test
-    @Ignore
     public void testMoveToNextAndTraverseBackward() throws Exception
     {
         IntSerializer serializer = new IntSerializer();
@@ -684,7 +680,6 @@ public class BTreeDuplicateKeyTest
      * @throws Exception
      */
     @Test
-    @Ignore
     public void testMoveToPrevAndTraverseForward() throws Exception
     {
         IntSerializer serializer = new IntSerializer();
@@ -697,6 +692,7 @@ public class BTreeDuplicateKeyTest
         BTree<Integer, Integer> btree = new BTree<Integer, Integer>( config );
 
         int i = 5;
+        
         for ( int k = 0; k < i; k++ )
         {
             btree.insert( k, k );
@@ -704,9 +700,9 @@ public class BTreeDuplicateKeyTest
 
         // 4 is the last element in the tree
         TupleCursor<Integer, Integer> cursor = btree.browseFrom( 0 );
-        cursor.prevKey();
 
         int currentKey = 0;
+        
         while ( cursor.hasNext() )
         {
             assertEquals( Integer.valueOf( currentKey ), cursor.next().getKey() );
@@ -715,5 +711,4 @@ public class BTreeDuplicateKeyTest
 
         cursor.close();
     }
-
 }

Modified: directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTest.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTest.java?rev=1549961&r1=1549960&r2=1549961&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTest.java (original)
+++ directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTest.java Tue Dec 10 20:42:16 2013
@@ -35,6 +35,7 @@ import java.util.List;
 import java.util.Random;
 import java.util.Set;
 
+import org.apache.directory.mavibot.btree.Page;
 import org.apache.directory.mavibot.btree.Tuple;
 import org.apache.directory.mavibot.btree.TupleCursor;
 import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
@@ -628,7 +629,7 @@ public class InMemoryBTreeTest
         while ( cursor.hasNext() )
         {
             Tuple<Integer, String> tuple = cursor.next();
-
+            
             assertNotNull( tuple );
             Integer val = sortedValues[pos];
             Integer res = tuple.getKey();
@@ -665,7 +666,7 @@ public class InMemoryBTreeTest
         while ( cursor.hasNext() )
         {
             Tuple<Integer, String> tuple = cursor.next();
-
+            
             assertNotNull( tuple );
             Integer val = sortedValues[pos];
             Integer res = tuple.getKey();
@@ -812,10 +813,7 @@ public class InMemoryBTreeTest
 
         assertTrue( cursor.hasPrev() );
 
-        // Lets go backward. We should get the same value, as the next() call have incremented the counter
-        assertEquals( 12, cursor.prev().getKey().intValue() );
-
-        // Get 11
+        // Lets go backward.
         assertEquals( 11, cursor.prev().getKey().intValue() );
 
         // Get 10
@@ -1038,8 +1036,8 @@ public class InMemoryBTreeTest
         leaf.revision = revision;
         leaf.nbElems = tuples.length;
         leaf.keys = new Integer[leaf.nbElems];
-        leaf.values = ( MemoryHolder<Integer, String>[] ) Array
-            .newInstance( MemoryHolder.class, leaf.nbElems );
+        leaf.values = ( ValueHolder<String>[] ) Array
+            .newInstance( ValueHolder.class, leaf.nbElems );
 
         for ( Tuple<Integer, String> tuple : tuples )
         {
@@ -1062,7 +1060,7 @@ public class InMemoryBTreeTest
             node.keys[pos - 1] = leftmost.getKey();
         }
 
-        node.children[pos] = btree.createPageHolder( page );
+        node.children[pos] = page;
     }
 
 
@@ -1873,10 +1871,7 @@ public class InMemoryBTreeTest
         assertFalse( cursor.hasNext() );
         assertTrue( cursor.hasPrev() );
 
-        // Lets go backward. We should get the same value, as the next() call have incremented the counter
-        assertEquals( 12, cursor.prev().getKey().intValue() );
-
-        // Get 11
+        // Lets go backward.
         assertEquals( 11, cursor.prev().getKey().intValue() );
 
         // Get 10
@@ -1923,8 +1918,8 @@ public class InMemoryBTreeTest
 
         assertTrue( cursor.hasPrev() );
         tuple = cursor.prev();
-        assertEquals( Integer.valueOf( 4 ), tuple.getKey() );
-        assertEquals( Integer.valueOf( 4 ), tuple.getValue() );
+        assertEquals( Integer.valueOf( 3 ), tuple.getKey() );
+        assertEquals( Integer.valueOf( 3 ), tuple.getValue() );
 
         assertTrue( cursor.hasNext() );
         tuple = cursor.next();

Modified: directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTestOps.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTestOps.java?rev=1549961&r1=1549960&r2=1549961&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTestOps.java (original)
+++ directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTestOps.java Tue Dec 10 20:42:16 2013
@@ -109,6 +109,6 @@ public class InMemoryBTreeTestOps
         // Start the writer
         createTree();
         long t1 = System.currentTimeMillis();
-        System.out.println( "Time to create a tree with 500 in memory:" + ( ( t1 - t0 ) ) + " Mseconds" );
+        System.out.println( "Time to create a tree with 500 000 elements in memory:" + ( ( t1 - t0 ) ) + " milliseconds" );
     }
 }

Modified: directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/LeafTest.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/LeafTest.java?rev=1549961&r1=1549960&r2=1549961&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/LeafTest.java (original)
+++ directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/LeafTest.java Tue Dec 10 20:42:16 2013
@@ -26,6 +26,9 @@ import static org.junit.Assert.fail;
 
 import java.io.IOException;
 
+import org.apache.directory.mavibot.btree.DeleteResult;
+import org.apache.directory.mavibot.btree.InsertResult;
+import org.apache.directory.mavibot.btree.Page;
 import org.apache.directory.mavibot.btree.Tuple;
 import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
 import org.apache.directory.mavibot.btree.serializer.LongSerializer;
@@ -241,9 +244,9 @@ public class LeafTest
         right = insert( right, 12L, "v12" );
         right = insert( right, 13L, "v13" );
 
-        parent.children[0] = new MemoryHolder( btree, left );
-        parent.children[1] = new MemoryHolder( btree, target );
-        parent.children[2] = new MemoryHolder( btree, right );
+        parent.children[0] = left;
+        parent.children[1] = target;
+        parent.children[2] = right;
 
         // Update the parent
         parent.keys[0] = 6L;
@@ -311,9 +314,9 @@ public class LeafTest
         right = insert( right, 13L, "v13" );
         right = insert( right, 14L, "v14" );
 
-        parent.children[0] = new MemoryHolder( null, left );
-        parent.children[1] = new MemoryHolder( null, target );
-        parent.children[2] = new MemoryHolder( null, right );
+        parent.children[0] = left;
+        parent.children[1] = target;
+        parent.children[2] = right;
 
         // Update the parent
         parent.keys[0] = 6L;
@@ -381,9 +384,9 @@ public class LeafTest
         right = insert( right, 11L, "v11" );
         right = insert( right, 12L, "v12" );
 
-        parent.children[0] = new MemoryHolder( null, left );
-        parent.children[1] = new MemoryHolder( null, target );
-        parent.children[2] = new MemoryHolder( null, right );
+        parent.children[0] = left;
+        parent.children[1] = target;
+        parent.children[2] = right;
 
         // Update the parent
         parent.keys[0] = 5L;

Modified: directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/MultiThreadedBtreeTest.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/MultiThreadedBtreeTest.java?rev=1549961&r1=1549960&r2=1549961&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/MultiThreadedBtreeTest.java (original)
+++ directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/MultiThreadedBtreeTest.java Tue Dec 10 20:42:16 2013
@@ -137,7 +137,7 @@ public class MultiThreadedBtreeTest
 
 
     /**
-     * Chack that we can read the btree while it is being modified. We will start
+     * Check that we can read the btree while it is being modified. We will start
      * 100 readers for one writer.
      * 
      * @throws InterruptedException If the btree access failed.



Mime
View raw message