directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1550580 [1/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 Thu, 12 Dec 2013 23:08:29 GMT
Author: elecharny
Date: Thu Dec 12 23:08:28 2013
New Revision: 1550580

URL: http://svn.apache.org/r1550580
Log:
Some more big refactoring :
o Created a BTree interface, and an AbstractBTree class to gather the common methods of the managed and inMemory BTree
o Renamed the managed and inMemory BTree to PersistedBTree and InMemoryBTree

Added:
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/BTree.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueBTreeCursor.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/PersistedBTree.java
      - copied, changed from r1550413, directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTree.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/PersistedBTreeBuilder.java
      - copied, changed from r1550046, directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/ManagedBTreeBuilder.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/TupleCursorImpl.java
      - copied, changed from r1550046, directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/TupleCursorImpl.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTree.java
      - copied, changed from r1550209, directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTree.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeBuilder.java
      - copied, changed from r1550046, directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTreeBuilder.java
Removed:
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTree.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/ManagedBTreeBuilder.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTree.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTreeBuilder.java
Modified:
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/AbstractPage.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeConfiguration.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeFactory.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BulkDataSorter.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/ElementHolder.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Leaf.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Node.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/PageHolder.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/RecordManager.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/ValueHolder.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/AbstractPage.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTreeConfiguration.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTreeFactory.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BulkDataSorter.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Leaf.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Node.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/TupleCursorImpl.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/ValueHolder.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/BTreeDuplicateKeyTest.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/ManagedBTreeBrowseTest.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/ManagedBTreeBuilderTest.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/RecordManagerFreePageTest.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/RecordManagerPrivateMethodTest.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/RecordManagerTest.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/managed/RecordManagerWithDuplicatesTest.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/BTreeBuilderTest.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/BTreeConfigurationTest.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/BTreeDuplicateKeyTest.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/BTreeFlushTest.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTest.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTestOps.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/LeafTest.java
    directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/MultiThreadedBtreeTest.java

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java?rev=1550580&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java Thu Dec 12 23:08:28 2013
@@ -0,0 +1,657 @@
+/*
+ *  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;
+
+import java.io.IOException;
+import java.util.Comparator;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.locks.ReentrantLock;
+
+import net.sf.ehcache.Cache;
+
+import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
+import org.apache.directory.mavibot.btree.serializer.ElementSerializer;
+
+/**
+ * A BTree abstract class containing the methods shared by the PersistedBTree or the InMemoryBTree
+ * implementations.
+ *
+ * @param <K> The Key type
+ * @param <V> The Value type
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public abstract class AbstractBTree<K, V> implements BTree<K, V>
+{
+    /** The read transaction timeout */
+    protected long readTimeOut = DEFAULT_READ_TIMEOUT;
+
+    /** The Header for a managed BTree */
+    protected BTreeHeader btreeHeader;
+
+    /** The current rootPage */
+    protected volatile Page<K, V> rootPage;
+
+    /** The Key serializer used for this tree.*/
+    protected ElementSerializer<K> keySerializer;
+
+    /** The Value serializer used for this tree. */
+    protected ElementSerializer<V> valueSerializer;
+
+    /** The list of read transactions being executed */
+    protected ConcurrentLinkedQueue<Transaction<K, V>> readTransactions;
+
+    /** The size of the buffer used to write data in disk */
+    protected int writeBufferSize;
+
+    /** A lock used to protect the write operation against concurrent access */
+    protected ReentrantLock writeLock;
+
+    /** Flag to enable duplicate key support */
+    private boolean allowDuplicates;
+
+    /** The cache associated with this BTree */
+    protected Cache cache;
+
+    /** The cache size, default to 1000 elements */
+    protected int cacheSize = DEFAULT_CACHE_SIZE;
+
+    /** The thread responsible for the cleanup of timed out reads */
+    protected Thread readTransactionsThread;
+
+    /**
+     * Starts a Read Only transaction. If the transaction is not closed, it will be 
+     * automatically closed after the timeout
+     * 
+     * @return The created transaction
+     */
+    protected Transaction<K, V> beginReadTransaction()
+    {
+        Transaction<K, V> readTransaction = new Transaction<K, V>( rootPage, btreeHeader.getRevision() - 1,
+            System.currentTimeMillis() );
+
+        readTransactions.add( readTransaction );
+
+        return readTransaction;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public TupleCursor<K, V> browse() throws IOException
+    {
+        Transaction<K, V> transaction = beginReadTransaction();
+
+        // Fetch the root page for this revision
+        ParentPos<K, V>[] stack = new ParentPos[32];
+        
+        TupleCursor<K, V> cursor = rootPage.browse( transaction, stack, 0 );
+        
+        // Set the position before the first element
+        cursor.beforeFirst();
+
+        return cursor;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public TupleCursor<K, V> browse( long revision ) throws IOException, KeyNotFoundException
+    {
+        Transaction<K, V> transaction = beginReadTransaction();
+
+        // Fetch the root page for this revision
+        Page<K, V> revisionRootPage = getRootPage( revision );
+        
+        ParentPos<K, V>[] stack = new ParentPos[32];
+
+        // And get the cursor
+        TupleCursor<K, V> cursor = revisionRootPage.browse( transaction, stack, 0 );
+
+        return cursor;
+    }
+    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public TupleCursor<K, V> browseFrom( K key ) throws IOException
+    {
+        Transaction<K, V> transaction = beginReadTransaction();
+
+        // Fetch the root page for this revision
+        ParentPos<K, V>[] stack = new ParentPos[32];
+
+        TupleCursor<K, V> cursor = rootPage.browse( key, transaction, stack, 0 );
+
+        return cursor;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public TupleCursor<K, V> browseFrom( long revision, K key ) throws IOException, KeyNotFoundException
+    {
+        Transaction<K, V> transaction = beginReadTransaction();
+
+        // Fetch the rootPage for this revision
+        Page<K, V> revisionRootPage = getRootPage( revision );
+
+        ParentPos<K, V>[] stack = new ParentPos[32];
+
+        // And get the cursor
+        TupleCursor<K, V> cursor = revisionRootPage.browse( key, transaction, stack, 0 );
+
+        return cursor;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean contains( K key, V value ) throws IOException
+    {
+        return rootPage.contains( key, value );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean contains( long revision, K key, V value ) throws IOException, KeyNotFoundException
+    {
+        // Fetch the root page for this revision
+        Page<K, V> revisionRootPage = getRootPage( revision );
+
+        return revisionRootPage.contains( key, value );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Tuple<K, V> delete( K key ) throws IOException
+    {
+        if ( key == null )
+        {
+            throw new IllegalArgumentException( "Key must not be null" );
+        }
+
+        long revision = generateRevision();
+
+        Tuple<K, V> deleted = delete( key, revision );
+
+        return deleted;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Tuple<K, V> delete( K key, V value ) throws IOException
+    {
+        if ( key == null )
+        {
+            throw new IllegalArgumentException( "Key must not be null" );
+        }
+
+        if ( value == null )
+        {
+            throw new IllegalArgumentException( "Value must not be null" );
+        }
+
+        long revision = generateRevision();
+
+        Tuple<K, V> deleted = delete( key, value, revision );
+
+        return deleted;
+    }
+
+
+    /**
+     * Delete the entry which key is given as a parameter. If the entry exists, it will
+     * be removed from the tree, the old tuple will be returned. Otherwise, null is returned.
+     * 
+     * @param key The key for the entry we try to remove
+     * @return A Tuple<K, V> containing the removed entry, or null if it's not found.
+     */
+    protected Tuple<K, V> delete( K key, long revision ) throws IOException
+    {
+        return delete( key, null, revision );
+    }
+
+
+    abstract protected Tuple<K, V> delete( K key, V value, long revision ) throws IOException;
+    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public V insert( K key, V value ) throws IOException
+    {
+        long revision = generateRevision();
+
+        V existingValue = null;
+
+        try
+        {
+            // Commented atm, we will have to play around the idea of transactions later
+            writeLock.lock();
+
+            InsertResult<K, V> result = insert( key, value, revision );
+
+            if ( result instanceof ModifyResult )
+            {
+                existingValue = ( ( ModifyResult<K, V> ) result ).getModifiedValue();
+            }
+        }
+        finally
+        {
+            // See above
+            writeLock.unlock();
+        }
+
+        return existingValue;
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    abstract public InsertResult<K, V> insert( K key, V value, long revision ) throws IOException;
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public V get( K key ) throws IOException, KeyNotFoundException
+    {
+        return rootPage.get( key );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public V get( long revision, K key ) throws IOException, KeyNotFoundException
+    {
+        // Fetch the root page for this revision
+        Page<K, V> revisionRootPage = getRootPage( revision );
+
+        return revisionRootPage.get( key );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Page<K, V> getRootPage()
+    {
+        return rootPage;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setRootPage( Page<K, V> root )
+    {
+        rootPage = root;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public ValueCursor<V> getValues( K key ) throws IOException, KeyNotFoundException
+    {
+        return rootPage.getValues( key );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean hasKey( K key ) throws IOException
+    {
+        if ( key == null )
+        {
+            return false;
+        }
+
+        return rootPage.hasKey( key );
+    }
+    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public boolean hasKey( long revision, K key ) throws IOException, KeyNotFoundException
+    {
+        if ( key == null )
+        {
+            return false;
+        }
+
+        // Fetch the root page for this revision
+        Page<K, V> revisionRootPage = getRootPage( revision );
+
+        return revisionRootPage.hasKey( key );
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public ElementSerializer<K> getKeySerializer()
+    {
+        return keySerializer;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setKeySerializer( ElementSerializer<K> keySerializer )
+    {
+        this.keySerializer = keySerializer;
+        btreeHeader.setKeySerializerFQCN( keySerializer.getClass().getName() );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getKeySerializerFQCN()
+    {
+        return btreeHeader.getKeySerializerFQCN();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public ElementSerializer<V> getValueSerializer()
+    {
+        return valueSerializer;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setValueSerializer( ElementSerializer<V> valueSerializer )
+    {
+        this.valueSerializer = valueSerializer;
+        btreeHeader.setValueSerializerFQCN( valueSerializer.getClass().getName() );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getValueSerializerFQCN()
+    {
+        return btreeHeader.getValueSerializerFQCN();
+    }
+
+
+    /** 
+     * {@inheritDoc}
+     */
+    public long getRevision()
+    {
+        return btreeHeader.getRevision();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setRevision( long revision )
+    {
+        btreeHeader.setRevision( revision );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public long generateRevision()
+    {
+        return btreeHeader.incrementRevision();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public long getReadTimeOut()
+    {
+        return readTimeOut;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setReadTimeOut( long readTimeOut )
+    {
+        this.readTimeOut = readTimeOut;
+    }
+
+
+    /** 
+     * {@inheritDoc}
+     */
+    public long getNbElems()
+    {
+        return btreeHeader.getNbElems();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setNbElems( long nbElems )
+    {
+        btreeHeader.setNbElems( nbElems );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public int getPageSize()
+    {
+        return btreeHeader.getPageSize();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setPageSize( int pageSize )
+    {
+        if ( pageSize <= 2 )
+        {
+            btreeHeader.setPageSize( DEFAULT_PAGE_SIZE );
+        }
+        else
+        {
+            btreeHeader.setPageSize( getPowerOf2( pageSize ) );
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getName()
+    {
+        return btreeHeader.getName();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setName( String name )
+    {
+        btreeHeader.setName( name );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Comparator<K> getComparator()
+    {
+        return keySerializer.getComparator();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public int getWriteBufferSize()
+    {
+        return writeBufferSize;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setWriteBufferSize( int writeBufferSize )
+    {
+        this.writeBufferSize = writeBufferSize;
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isAllowDuplicates()
+    {
+        return btreeHeader.isAllowDuplicates();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setAllowDuplicates( boolean allowDuplicates )
+    {
+        btreeHeader.setAllowDuplicates( allowDuplicates );
+    }
+
+
+    /**
+     * Gets the number which is a power of 2 immediately above the given positive number.
+     */
+    private int getPowerOf2( int size )
+    {
+        int newSize = --size;
+        newSize |= newSize >> 1;
+        newSize |= newSize >> 2;
+        newSize |= newSize >> 4;
+        newSize |= newSize >> 8;
+        newSize |= newSize >> 16;
+        newSize++;
+
+        return newSize;
+    }
+
+
+    /**
+     * Create a thread that is responsible of cleaning the transactions when
+     * they hit the timeout
+     */
+    protected void createTransactionManager()
+    {
+        Runnable readTransactionTask = new Runnable()
+        {
+            public void run()
+            {
+                try
+                {
+                    Transaction<K, V> transaction = null;
+
+                    while ( !Thread.currentThread().isInterrupted() )
+                    {
+                        long timeoutDate = System.currentTimeMillis() - readTimeOut;
+                        long t0 = System.currentTimeMillis();
+                        int nbTxns = 0;
+
+                        // Loop on all the transactions from the queue
+                        while ( ( transaction = readTransactions.peek() ) != null )
+                        {
+                            nbTxns++;
+
+                            if ( transaction.isClosed() )
+                            {
+                                // The transaction is already closed, remove it from the queue
+                                readTransactions.poll();
+                                continue;
+                            }
+
+                            // Check if the transaction has timed out
+                            if ( transaction.getCreationDate() < timeoutDate )
+                            {
+                                transaction.close();
+                                readTransactions.poll();
+                                continue;
+                            }
+
+                            // We need to stop now
+                            break;
+                        }
+
+                        long t1 = System.currentTimeMillis();
+
+                        if ( nbTxns > 0 )
+                        {
+                            System.out.println( "Processing old txn : " + nbTxns + ", " + ( t1 - t0 ) + "ms" );
+                        }
+
+                        // Wait until we reach the timeout
+                        Thread.sleep( readTimeOut );
+                    }
+                }
+                catch ( InterruptedException ie )
+                {
+                    //System.out.println( "Interrupted" );
+                }
+                catch ( Exception e )
+                {
+                    throw new RuntimeException( e );
+                }
+            }
+        };
+
+        readTransactionsThread = new Thread( readTransactionTask );
+        readTransactionsThread.setDaemon( true );
+        readTransactionsThread.start();
+    }
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/BTree.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/BTree.java?rev=1550580&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/BTree.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/BTree.java Thu Dec 12 23:08:28 2013
@@ -0,0 +1,418 @@
+/*
+ *  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;
+
+import java.io.IOException;
+import java.util.Comparator;
+
+import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
+import org.apache.directory.mavibot.btree.serializer.ElementSerializer;
+
+/**
+ * A BTree interface, to be implemented by the PersistedBTree or the InMemoryBTree
+ *
+ * @param <K> The Key type
+ * @param <V> The Value type
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public interface BTree<K, V>
+{
+    /** Default page size (number of entries per node) */
+    static final int DEFAULT_PAGE_SIZE = 16;
+
+    /** Default size of the buffer used to write data on disk. Around 1Mb */
+    static final int DEFAULT_WRITE_BUFFER_SIZE = 4096 * 250;
+
+    /** Define a default delay for a read transaction. This is 10 seconds */
+    static final long DEFAULT_READ_TIMEOUT = 10 * 1000L;
+
+    /** The default number of pages to keep in memory */
+    static final int DEFAULT_CACHE_SIZE = 1000;
+
+
+    /**
+     * Initialize the BTree.
+     * 
+     * @throws IOException If we get some exception while initializing the BTree
+     */
+    void init() throws IOException;
+
+
+    /**
+     * Close the BTree, cleaning up all the data structure
+     */
+    void close() throws IOException;
+
+
+    /**
+     * Set the maximum number of elements we can store in a page. This must be a
+     * number greater than 1, and a power of 2. The default page size is 16.
+     * <br/>
+     * If the provided size is below 2, we will default to DEFAULT_PAGE_SIZE.<br/>
+     * If the provided size is not a power of 2, we will select the closest power of 2
+     * higher than the given number<br/>
+     * 
+     * @param pageSize The requested page size
+     */
+    void setPageSize( int pageSize );
+
+
+    /**
+     * @return the pageSize
+     */
+    int getPageSize();
+
+
+    /**
+     * Generates a new revision number. It's only used by the Page instances.
+     * 
+     * @return a new incremental revision number
+     */
+    long generateRevision();
+
+
+    /**
+     * Insert an entry in the BTree.
+     * <p>
+     * We will replace the value if the provided key already exists in the
+     * btree.
+     *
+     * @param key Inserted key
+     * @param value Inserted value
+     * @return Existing value, if any.
+     * @throws IOException TODO
+     */
+    V insert( K key, V value ) throws IOException;
+
+
+    /**
+     * Insert an entry in the BTree.
+     * <p>
+     * We will replace the value if the provided key already exists in the
+     * btree.
+     *
+     * @param key Inserted key
+     * @param value Inserted value
+     * @param revision the revision
+     * @return Existing value, if any.
+     * @throws IOException TODO
+     */
+    InsertResult<K, V> insert( K key, V value, long revision ) throws IOException;
+
+
+    /**
+     * Delete the entry which key is given as a parameter. If the entry exists, it will
+     * be removed from the tree, the old tuple will be returned. Otherwise, null is returned.
+     * 
+     * @param key The key for the entry we try to remove
+     * @return A Tuple<K, V> containing the removed entry, or null if it's not found.
+     */
+    Tuple<K, V> delete( K key ) throws IOException;
+
+
+    /**
+     * Delete the value from an entry associated with the given key. If the value
+     * If the value is present, it will be deleted first, later if there are no other 
+     * values associated with this key(which can happen when duplicates are enabled), 
+     * we will remove the key from the tree.
+     * 
+     * @param key The key for the entry we try to remove
+     * @param value The value to delete (can be null)
+     * @return A Tuple<K, V> containing the removed entry, or null if it's not found.
+     */
+    Tuple<K, V> delete( K key, V value ) throws IOException;
+
+
+    /**
+     * 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.
+     * 
+     * @param key The key we are looking at
+     * @return The found value, or null if the key is not present in the tree
+     * @throws KeyNotFoundException If the key is not found in the BTree
+     * @throws IOException TODO
+     */
+    V get( K key ) throws IOException, KeyNotFoundException;
+
+
+    /**
+     * Get the rootPage associated to a given revision.
+     * 
+     * @param revision The revision we are looking for
+     * @return The rootPage associated to this revision
+     * @throws IOException If we had an issue while accessing the underlying file
+     * @throws KeyNotFoundException If the revision does not exist for this Btree
+     */
+    Page<K, V> getRootPage( long revision ) throws IOException, KeyNotFoundException;
+
+
+    /**
+     * Get the current rootPage
+     * 
+     * @return The current rootPage
+     */
+    Page<K, V> getRootPage();
+
+
+    /**
+     * Set the new root page for this tree. Used for debug purpose only. The revision
+     * will always be 0;
+     * 
+     * @param root the new root page.
+     */
+    void setRootPage( Page<K, V> root );
+
+
+    /**
+     * @see Page#getValues(Object)
+     */
+    ValueCursor<V> getValues( K key ) throws IOException, KeyNotFoundException;
+
+
+    /**
+     * Find a value in the tree, given its key, at a specific revision. If the key is not found,
+     * it will throw a KeyNotFoundException. <br/>
+     * Note that we can get a null value stored, or many values.
+     * 
+     * @param revision The revision for which we want to find a key
+     * @param key The key we are looking at
+     * @return The found value, or null if the key is not present in the tree
+     * @throws KeyNotFoundException If the key is not found in the BTree
+     * @throws IOException If there was an issue while fetching data from the disk
+     */
+    V get( long revision, K key ) throws IOException, KeyNotFoundException;
+
+
+    /**
+     * 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;
+
+
+    /**
+     * Checks if the given key exists for a given revision.
+     *  
+     * @param revision The revision for which we want to find a key
+     * @param key The key we are looking at
+     * @return true if the key is present, false otherwise
+     * @throws IOException If we have an error while trying to access the page
+     * @throws KeyNotFoundException If the key is not found in the BTree
+     */
+    boolean hasKey( long revision, K key ) throws IOException, KeyNotFoundException;
+
+
+    /**
+     * Checks if the BTree contains the given key with the given value.
+     * 
+     * @param key The key we are looking for
+     * @param value The value associated with the given key
+     * @return true if the key and value are associated with each other, false otherwise
+     */
+    boolean contains( K key, V value ) throws IOException;
+
+
+    /**
+     * Checks if the BTree contains the given key with the given value for a given revision
+     * 
+     * @param revision The revision we would like to browse
+     * @param key The key we are looking for
+     * @param value The value associated with the given key
+     * @return true if the key and value are associated with each other, false otherwise
+     * @throws KeyNotFoundException If the key is not found in the BTree
+     */
+    boolean contains( long revision, K key, V value ) throws IOException, KeyNotFoundException;
+
+
+    /**
+     * Creates a cursor starting at the beginning of the tree
+     * 
+     * @return A cursor on the btree
+     * @throws IOException
+     */
+    TupleCursor<K, V> browse() throws IOException;
+
+
+    /**
+     * Creates a cursor starting at the beginning of the tree, for a given revision
+     * 
+     * @param revision The revision we would like to browse
+     * @return A cursor on the btree
+     * @throws IOException If we had an issue while fetching data from the disk
+     * @throws KeyNotFoundException If the key is not found in the BTree
+     */
+    TupleCursor<K, V> browse( long revision ) throws IOException, KeyNotFoundException;
+
+
+    /**
+     * Creates a cursor starting on the given key
+     * 
+     * @param key The key which is the starting point. If the key is not found,
+     * then the cursor will always return null.
+     * @return A cursor on the btree
+     * @throws IOException
+     */
+    TupleCursor<K, V> browseFrom( K key ) throws IOException;
+
+
+    /**
+     * Creates a cursor starting on the given key at the given revision
+     * 
+     * @param The revision we are looking for
+     * @param key The key which is the starting point. If the key is not found,
+     * then the cursor will always return null.
+     * @return A cursor on the btree
+     * @throws IOException If wxe had an issue reading the BTree from disk
+     * @throws KeyNotFoundException  If we can't find a rootPage for this revision
+     */
+    TupleCursor<K, V> browseFrom( long revision, K key ) throws IOException, KeyNotFoundException;
+
+
+    /**
+     * @return the comparator
+     */
+    Comparator<K> getComparator();
+
+
+    /**
+     * @return the type for the keys
+     */
+    Class<?> getKeyType();
+
+
+    /**
+     * @param keySerializer the Key serializer to set
+     */
+    void setKeySerializer( ElementSerializer<K> keySerializer );
+
+
+    /**
+     * @param valueSerializer the Value serializer to set
+     */
+    void setValueSerializer( ElementSerializer<V> valueSerializer );
+
+
+    /**
+     * Flush the latest revision to disk. We will replace the current file by the new one, as
+     * we flush in a temporary file.
+     */
+    void flush() throws IOException;
+
+
+    /**
+     * @return the readTimeOut
+     */
+    long getReadTimeOut();
+
+
+    /**
+     * @param readTimeOut the readTimeOut to set
+     */
+    void setReadTimeOut( long readTimeOut );
+
+
+    /**
+     * @return the name
+     */
+    String getName();
+
+
+    /**
+     * @param name the name to set
+     */
+    void setName( String name );
+
+
+    /**
+     * @return the writeBufferSize
+     */
+    int getWriteBufferSize();
+
+
+    /**
+     * @param writeBufferSize the writeBufferSize to set
+     */
+    void setWriteBufferSize( int writeBufferSize );
+
+
+    /**
+     * @return the keySerializer
+     */
+    ElementSerializer<K> getKeySerializer();
+
+
+    /**
+     * @return the keySerializer FQCN
+     */
+    String getKeySerializerFQCN();
+
+
+    /**
+     * @return the valueSerializer
+     */
+    ElementSerializer<V> getValueSerializer();
+
+
+    /**
+     * @return the valueSerializer FQCN
+     */
+    String getValueSerializerFQCN();
+
+
+    /** 
+     * @return The current BTree revision
+     */
+    long getRevision();
+
+
+    /** 
+     * @param The current BTree revision
+     */
+    void setRevision( long revision);
+
+
+    /** 
+     * @return The current number of elements in the BTree
+     */
+    long getNbElems();
+
+
+    /** 
+     * @return The current number of elements in the BTree
+     */
+    void setNbElems( long nbElems);
+
+
+    /**
+     * @return true if this BTree allow duplicate values
+     */
+    boolean isAllowDuplicates();
+
+
+    /**
+     * @param allowDuplicates True if the BTree will allow duplicate values
+     */
+    void setAllowDuplicates( boolean allowDuplicates );
+}

Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueBTreeCursor.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueBTreeCursor.java?rev=1550580&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueBTreeCursor.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueBTreeCursor.java Thu Dec 12 23:08:28 2013
@@ -0,0 +1,204 @@
+package org.apache.directory.mavibot.btree;
+
+import java.io.IOException;
+
+import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
+import org.apache.directory.mavibot.btree.BTree;
+
+/**
+ * A class that encapsulate the values into an sub-btree
+ */
+public class ValueBTreeCursor<V> implements ValueCursor<V>
+{
+    /** Store the current position in the array or in the BTree */
+    private TupleCursor<V, V> cursor;
+    
+    /** The Value sub-btree */
+    private BTree<V, V> valueBtree;
+
+
+    /**
+     * Create an instance
+     */
+    public ValueBTreeCursor( BTree<V, V> valueBtree )
+    {
+        this.valueBtree = valueBtree;
+        
+        // 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";
+    }
+}

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/AbstractPage.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/AbstractPage.java?rev=1550580&r1=1550579&r2=1550580&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/AbstractPage.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/AbstractPage.java Thu Dec 12 23:08:28 2013
@@ -23,6 +23,7 @@ package org.apache.directory.mavibot.btr
 import java.io.IOException;
 import java.lang.reflect.Array;
 
+import org.apache.directory.mavibot.btree.BTree;
 import org.apache.directory.mavibot.btree.Page;
 import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
 

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeConfiguration.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeConfiguration.java?rev=1550580&r1=1550579&r2=1550580&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeConfiguration.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeConfiguration.java Thu Dec 12 23:08:28 2013
@@ -20,6 +20,7 @@
 package org.apache.directory.mavibot.btree.managed;
 
 
+import org.apache.directory.mavibot.btree.BTree;
 import org.apache.directory.mavibot.btree.serializer.ElementSerializer;
 
 
@@ -61,25 +62,7 @@ public class BTreeConfiguration<K, V>
      * The default value is 10000 (10 seconds). If the value is 0 or below,
      * the delay is considered as infinite
      */
-    private long readTimeOut = BTree.DEFAULT_READ_TIMEOUT;
-
-    /** The maximal size of the journal. When this size is reached, the tree is 
-     * flushed on disk.
-     * The default size is 10 Mb
-     */
-    private long journalSize = 10 * 1024 * 1024L;
-
-    /**
-     * The journal's name. Default to "mavibot.log".
-     */
-    private String journalName = BTree.DEFAULT_JOURNAL;
-
-    /** 
-     * The delay between two checkpoints. When we reach the maximum delay,
-     * the BTree is flushed on disk, but only if we have had some modifications.
-     * The default value is 60 seconds.
-     */
-    private long checkPointDelay = 60 * 1000L;
+    private long readTimeOut = PersistedBTree.DEFAULT_READ_TIMEOUT;
 
     /** Flag to enable duplicate key support */
     private boolean allowDuplicates;
@@ -177,42 +160,6 @@ public class BTreeConfiguration<K, V>
 
 
     /**
-     * @return the journalSize
-     */
-    public long getJournalSize()
-    {
-        return journalSize;
-    }
-
-
-    /**
-     * @param journalSize the journalSize to set
-     */
-    public void setJournalSize( long journalSize )
-    {
-        this.journalSize = journalSize;
-    }
-
-
-    /**
-     * @return the checkPointDelay
-     */
-    public long getCheckPointDelay()
-    {
-        return checkPointDelay;
-    }
-
-
-    /**
-     * @param checkPointDelay the checkPointDelay to set
-     */
-    public void setCheckPointDelay( long checkPointDelay )
-    {
-        this.checkPointDelay = checkPointDelay;
-    }
-
-
-    /**
      * @return the filePath
      */
     public String getFilePath()
@@ -231,24 +178,6 @@ public class BTreeConfiguration<K, V>
 
 
     /**
-     * @return the journal name
-     */
-    public String getJournalName()
-    {
-        return journalName;
-    }
-
-
-    /**
-     * @param journalName the journal name to set
-     */
-    public void setJournalName( String journalName )
-    {
-        this.journalName = journalName;
-    }
-
-
-    /**
      * @return the writeBufferSize
      */
     public int getWriteBufferSize()

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeFactory.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeFactory.java?rev=1550580&r1=1550579&r2=1550580&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeFactory.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeFactory.java Thu Dec 12 23:08:28 2013
@@ -23,6 +23,7 @@ package org.apache.directory.mavibot.btr
 import java.io.IOException;
 import java.util.LinkedList;
 
+import org.apache.directory.mavibot.btree.BTree;
 import org.apache.directory.mavibot.btree.Page;
 import org.apache.directory.mavibot.btree.ParentPos;
 import org.apache.directory.mavibot.btree.serializer.ElementSerializer;
@@ -45,7 +46,20 @@ public class BTreeFactory
      */
     public static <K, V> BTree<K, V> createBTree()
     {
-        BTree<K, V> btree = new BTree<K, V>();
+        BTree<K, V> btree = new PersistedBTree<K, V>();
+
+        return btree;
+    }
+    
+    
+    /**
+     * Create a new BTree.
+     * 
+     * @return The created BTree
+     */
+    public static <K, V> BTree<K, V> createBTree( BTreeConfiguration<K, V> configuration)
+    {
+        BTree<K, V> btree = new PersistedBTree<K, V>( configuration );
 
         return btree;
     }
@@ -89,9 +103,9 @@ public class BTreeFactory
      * 
      * @param root the new root page.
      */
-    public static <K, V> void setRoot( BTree<K, V> btree, Page<K, V> root )
+    public static <K, V> void setRootPage( BTree<K, V> btree, Page<K, V> root )
     {
-        btree.setRoot( root );
+        btree.setRootPage( root );
     }
 
 
@@ -103,7 +117,7 @@ public class BTreeFactory
      */
     public static <K, V> Page<K, V> getRoot( BTree<K, V> btree )
     {
-        return btree.rootPage;
+        return btree.getRootPage();
     }
 
 
@@ -112,7 +126,7 @@ public class BTreeFactory
      */
     public static <K, V> void setNbElems( BTree<K, V> btree, long nbElems )
     {
-        btree.setNbElems( nbElems );
+        ((PersistedBTree<K, V>)btree).setNbElems( nbElems );
     }
 
 
@@ -121,7 +135,7 @@ public class BTreeFactory
      */
     public static <K, V> void setRevision( BTree<K, V> btree, long revision )
     {
-        btree.setRevision( revision );
+        ((PersistedBTree<K, V>)btree).setRevision( revision );
     }
 
 
@@ -130,7 +144,7 @@ public class BTreeFactory
      */
     public static <K, V> void setRootPageOffset( BTree<K, V> btree, long rootPageOffset )
     {
-        btree.setRootPageOffset( rootPageOffset );
+        ((PersistedBTree<K, V>)btree).setRootPageOffset( rootPageOffset );
     }
 
 
@@ -139,7 +153,7 @@ public class BTreeFactory
      */
     public static <K, V> void setNextBTreeOffset( BTree<K, V> btree, long nextBTreeOffset )
     {
-        btree.setNextBTreeOffset( nextBTreeOffset );
+        ((PersistedBTree<K, V>)btree).setNextBTreeOffset( nextBTreeOffset );
     }
 
 
@@ -208,7 +222,7 @@ public class BTreeFactory
      */
     public static <K, V> void setRecordManager( BTree<K, V> btree, RecordManager recordManager )
     {
-        btree.setRecordManager( recordManager );
+        ((PersistedBTree<K, V>)btree).setRecordManager( recordManager );
     }
 
 
@@ -268,18 +282,18 @@ public class BTreeFactory
     {
         LinkedList<ParentPos<K, V>> stack = new LinkedList<ParentPos<K, V>>();
 
-        ParentPos<K, V> last = new ParentPos<K, V>( btree.rootPage, btree.rootPage.getNbElems() );
+        ParentPos<K, V> last = new ParentPos<K, V>( btree.getRootPage(), btree.getRootPage().getNbElems() );
         stack.push( last );
 
-        if ( btree.rootPage instanceof Leaf )
+        if ( btree.getRootPage() instanceof Leaf )
         {
-            Leaf<K, V> leaf = ( Leaf<K, V> ) ( btree.rootPage );
+            Leaf<K, V> leaf = ( Leaf<K, V> ) ( btree.getRootPage() );
             ValueHolder<V> valueHolder = leaf.values[last.pos];
             last.valueCursor = valueHolder.getCursor();
         }
         else
         {
-            Node<K, V> node = ( Node<K, V> ) btree.rootPage;
+            Node<K, V> node = ( Node<K, V> ) btree.getRootPage();
 
             while ( true )
             {
@@ -313,18 +327,18 @@ public class BTreeFactory
     {
         LinkedList<ParentPos<K, V>> stack = new LinkedList<ParentPos<K, V>>();
 
-        ParentPos<K, V> first = new ParentPos<K, V>( btree.rootPage, 0 );
+        ParentPos<K, V> first = new ParentPos<K, V>( btree.getRootPage(), 0 );
         stack.push( first );
 
-        if ( btree.rootPage instanceof Leaf )
+        if ( btree.getRootPage() instanceof Leaf )
         {
-            Leaf<K, V> leaf = ( Leaf<K, V> ) ( btree.rootPage );
+            Leaf<K, V> leaf = ( Leaf<K, V> ) ( btree.getRootPage() );
             ValueHolder<V> valueHolder = leaf.values[first.pos];
             first.valueCursor = valueHolder.getCursor();
         }
         else
         {
-            Node<K, V> node = ( Node<K, V> ) btree.rootPage;
+            Node<K, V> node = ( Node<K, V> ) btree.getRootPage();
 
             while ( true )
             {

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BulkDataSorter.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BulkDataSorter.java?rev=1550580&r1=1550579&r2=1550580&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BulkDataSorter.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BulkDataSorter.java Thu Dec 12 23:08:28 2013
@@ -39,7 +39,7 @@ import org.apache.directory.mavibot.btre
 
 
 /**
- * A utility class for sorting a large number of keys before building a BTree using {@link ManagedBTreeBuilder}.
+ * A utility class for sorting a large number of keys before building a BTree using {@link PersistedBTreeBuilder}.
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/ElementHolder.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/ElementHolder.java?rev=1550580&r1=1550579&r2=1550580&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/ElementHolder.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/ElementHolder.java Thu Dec 12 23:08:28 2013
@@ -22,6 +22,7 @@ package org.apache.directory.mavibot.btr
 
 import java.io.IOException;
 
+import org.apache.directory.mavibot.btree.BTree;
 import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
 
 

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Leaf.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Leaf.java?rev=1550580&r1=1550579&r2=1550580&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Leaf.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Leaf.java Thu Dec 12 23:08:28 2013
@@ -23,6 +23,7 @@ package org.apache.directory.mavibot.btr
 import java.io.IOException;
 import java.lang.reflect.Array;
 
+import org.apache.directory.mavibot.btree.BTree;
 import org.apache.directory.mavibot.btree.BorrowedFromLeftResult;
 import org.apache.directory.mavibot.btree.BorrowedFromRightResult;
 import org.apache.directory.mavibot.btree.DeleteResult;
@@ -844,7 +845,7 @@ import org.apache.directory.mavibot.btre
         Leaf<K, V> newLeaf = new Leaf<K, V>( btree, revision, nbElems + 1 );
 
         // Create the value holder
-        ValueHolder<V> valueHolder = btree.createValueHolder( value );
+        ValueHolder<V> valueHolder = new ValueHolder<V>( btree, value );
 
         // Deal with the special case of an empty page
         if ( nbElems == 0 )
@@ -892,7 +893,7 @@ import org.apache.directory.mavibot.btre
         int middle = btree.getPageSize() >> 1;
         Leaf<K, V> leftLeaf = null;
         Leaf<K, V> rightLeaf = null;
-        ValueHolder<V> valueHolder = btree.createValueHolder( value );
+        ValueHolder<V> valueHolder = new ValueHolder<V>( btree, value );
 
         // Determinate where to store the new value
         if ( pos <= middle )

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Node.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Node.java?rev=1550580&r1=1550579&r2=1550580&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Node.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/Node.java Thu Dec 12 23:08:28 2013
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.lang.reflect.Array;
 import java.util.List;
 
+import org.apache.directory.mavibot.btree.BTree;
 import org.apache.directory.mavibot.btree.BorrowedFromSiblingResult;
 import org.apache.directory.mavibot.btree.BorrowedFromLeftResult;
 import org.apache.directory.mavibot.btree.BorrowedFromRightResult;
@@ -97,8 +98,8 @@ import org.apache.directory.mavibot.btre
         children = ( PageHolder<K, V>[] ) Array.newInstance( PageHolder.class,
             btree.getPageSize() + 1 );
 
-        children[0] = btree.createPageHolder( leftPage );
-        children[1] = btree.createPageHolder( rightPage );
+        children[0] = new PageHolder<K, V>( btree, leftPage );
+        children[1] = new PageHolder<K, V>( btree, rightPage );
 
         // Create the keys array and store the pivot into it
         // We get the type of array to create from the btree
@@ -1053,7 +1054,7 @@ import org.apache.directory.mavibot.btre
      */
     private PageHolder<K, V> createHolder( Page<K, V> page ) throws IOException
     {
-        PageHolder<K, V> holder = btree.getRecordManager().writePage( btree,
+        PageHolder<K, V> holder = ((PersistedBTree<K, V>)btree).getRecordManager().writePage( btree,
             page,
             revision );
 

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/PageHolder.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/PageHolder.java?rev=1550580&r1=1550579&r2=1550580&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/PageHolder.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/PageHolder.java Thu Dec 12 23:08:28 2013
@@ -24,6 +24,7 @@ import java.io.IOException;
 
 import net.sf.ehcache.Element;
 
+import org.apache.directory.mavibot.btree.BTree;
 import org.apache.directory.mavibot.btree.Page;
 import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
 
@@ -54,22 +55,42 @@ public class PageHolder<K, V>
     /**
      * Create a new holder storing an offset and a SoftReference containing the element.
      * 
-     * @param offset The offset in disk for this value
-     * @param element The element to store into a SoftReference
+     * @param page The element to store into a SoftReference
      */
-    public PageHolder( BTree<K, V> btree, Page<K, V> element, long offset, long lastOffset )
+    public PageHolder( BTree<K, V> btree, Page<K, V> page )
+    {
+        this.btree = btree;
+        this.offset = page.getOffset();
+        this.lastOffset = page.getLastOffset();
+
+        if ( page instanceof Page<?, ?> )
+        {
+            ( ( AbstractPage<K, V> ) page ).setOffset( offset );
+            ( ( AbstractPage<K, V> ) page ).setLastOffset( lastOffset );
+        }
+
+        ((PersistedBTree<K, V>)btree).getCache().put( new Element( offset, page ) );
+    }
+
+
+    /**
+     * Create a new holder storing an offset and a SoftReference containing the element.
+     * 
+     * @param page The element to store into a SoftReference
+     */
+    public PageHolder( BTree<K, V> btree, Page<K, V> page, long offset, long lastOffset )
     {
         this.btree = btree;
         this.offset = offset;
         this.lastOffset = lastOffset;
 
-        if ( element instanceof Page<?, ?> )
+        if ( page instanceof Page<?, ?> )
         {
-            ( ( AbstractPage<K, V> ) element ).setOffset( offset );
-            ( ( AbstractPage<K, V> ) element ).setLastOffset( lastOffset );
+            ( ( AbstractPage<K, V> ) page ).setOffset( offset );
+            ( ( AbstractPage<K, V> ) page ).setLastOffset( lastOffset );
         }
 
-        btree.getCache().put( new Element( offset, element ) );
+        ((PersistedBTree<K, V>)btree).getCache().put( new Element( offset, page ) );
     }
 
 
@@ -80,7 +101,7 @@ public class PageHolder<K, V>
      */
     public Page<K, V> getValue( BTree<K, V> btree ) throws EndOfFileExceededException, IOException
     {
-        Element element = btree.getCache().get( offset );
+        Element element = ((PersistedBTree<K, V>)btree).getCache().get( offset );
 
         if ( element == null )
         {
@@ -88,12 +109,12 @@ public class PageHolder<K, V>
             // We have to fetch the element from disk, using the offset now
             Page<K, V> page = fetchElement( btree );
 
-            btree.getCache().put( new Element( offset, page ) );
+            ((PersistedBTree<K, V>)btree).getCache().put( new Element( offset, page ) );
 
             return page;
         }
 
-        Page<K, V> page = (org.apache.directory.mavibot.btree.Page<K, V> ) element.getObjectValue();
+        Page<K, V> page = (Page<K, V> ) element.getObjectValue();
 
         if ( page == null )
         {
@@ -106,7 +127,7 @@ public class PageHolder<K, V>
                 ( ( AbstractPage<K, V> ) page ).setLastOffset( lastOffset );
             }
 
-            btree.getCache().put( new Element( offset, page ) );
+            ((PersistedBTree<K, V>)btree).getCache().put( new Element( offset, page ) );
         }
 
         return page;
@@ -121,7 +142,7 @@ public class PageHolder<K, V>
      */
     private Page<K, V> fetchElement( BTree<K, V> btree ) throws EndOfFileExceededException, IOException
     {
-        Page<K, V> element = btree.getRecordManager().deserialize( btree, offset );
+        Page<K, V> element = ((PersistedBTree<K, V>)btree).getRecordManager().deserialize( btree, offset );
 
         return element;
     }



Mime
View raw message