Return-Path: X-Original-To: apmail-directory-commits-archive@www.apache.org Delivered-To: apmail-directory-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8A42E10ADA for ; Thu, 12 Dec 2013 23:09:02 +0000 (UTC) Received: (qmail 37882 invoked by uid 500); 12 Dec 2013 23:09:02 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 37823 invoked by uid 500); 12 Dec 2013 23:09:02 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 37816 invoked by uid 99); 12 Dec 2013 23:09:02 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Dec 2013 23:09:02 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Dec 2013 23:08:53 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 26606238889B; Thu, 12 Dec 2013 23:08:31 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@directory.apache.org From: elecharny@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131212230831.26606238889B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 The Key type + * @param The Value type + * + * @author Apache Directory Project + */ +public abstract class AbstractBTree implements BTree +{ + /** 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 rootPage; + + /** The Key serializer used for this tree.*/ + protected ElementSerializer keySerializer; + + /** The Value serializer used for this tree. */ + protected ElementSerializer valueSerializer; + + /** The list of read transactions being executed */ + protected ConcurrentLinkedQueue> 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 beginReadTransaction() + { + Transaction readTransaction = new Transaction( rootPage, btreeHeader.getRevision() - 1, + System.currentTimeMillis() ); + + readTransactions.add( readTransaction ); + + return readTransaction; + } + + + /** + * {@inheritDoc} + */ + public TupleCursor browse() throws IOException + { + Transaction transaction = beginReadTransaction(); + + // Fetch the root page for this revision + ParentPos[] stack = new ParentPos[32]; + + TupleCursor cursor = rootPage.browse( transaction, stack, 0 ); + + // Set the position before the first element + cursor.beforeFirst(); + + return cursor; + } + + + /** + * {@inheritDoc} + */ + public TupleCursor browse( long revision ) throws IOException, KeyNotFoundException + { + Transaction transaction = beginReadTransaction(); + + // Fetch the root page for this revision + Page revisionRootPage = getRootPage( revision ); + + ParentPos[] stack = new ParentPos[32]; + + // And get the cursor + TupleCursor cursor = revisionRootPage.browse( transaction, stack, 0 ); + + return cursor; + } + + + /** + * {@inheritDoc} + */ + public TupleCursor browseFrom( K key ) throws IOException + { + Transaction transaction = beginReadTransaction(); + + // Fetch the root page for this revision + ParentPos[] stack = new ParentPos[32]; + + TupleCursor cursor = rootPage.browse( key, transaction, stack, 0 ); + + return cursor; + } + + + /** + * {@inheritDoc} + */ + public TupleCursor browseFrom( long revision, K key ) throws IOException, KeyNotFoundException + { + Transaction transaction = beginReadTransaction(); + + // Fetch the rootPage for this revision + Page revisionRootPage = getRootPage( revision ); + + ParentPos[] stack = new ParentPos[32]; + + // And get the cursor + TupleCursor 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 revisionRootPage = getRootPage( revision ); + + return revisionRootPage.contains( key, value ); + } + + + /** + * {@inheritDoc} + */ + public Tuple delete( K key ) throws IOException + { + if ( key == null ) + { + throw new IllegalArgumentException( "Key must not be null" ); + } + + long revision = generateRevision(); + + Tuple deleted = delete( key, revision ); + + return deleted; + } + + + /** + * {@inheritDoc} + */ + public Tuple 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 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 containing the removed entry, or null if it's not found. + */ + protected Tuple delete( K key, long revision ) throws IOException + { + return delete( key, null, revision ); + } + + + abstract protected Tuple 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 result = insert( key, value, revision ); + + if ( result instanceof ModifyResult ) + { + existingValue = ( ( ModifyResult ) result ).getModifiedValue(); + } + } + finally + { + // See above + writeLock.unlock(); + } + + return existingValue; + } + + + /** + * {@inheritDoc} + */ + abstract public InsertResult 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 revisionRootPage = getRootPage( revision ); + + return revisionRootPage.get( key ); + } + + + /** + * {@inheritDoc} + */ + public Page getRootPage() + { + return rootPage; + } + + + /** + * {@inheritDoc} + */ + public void setRootPage( Page root ) + { + rootPage = root; + } + + + /** + * {@inheritDoc} + */ + public ValueCursor 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 revisionRootPage = getRootPage( revision ); + + return revisionRootPage.hasKey( key ); + } + + + /** + * {@inheritDoc} + */ + public ElementSerializer getKeySerializer() + { + return keySerializer; + } + + + /** + * {@inheritDoc} + */ + public void setKeySerializer( ElementSerializer keySerializer ) + { + this.keySerializer = keySerializer; + btreeHeader.setKeySerializerFQCN( keySerializer.getClass().getName() ); + } + + + /** + * {@inheritDoc} + */ + public String getKeySerializerFQCN() + { + return btreeHeader.getKeySerializerFQCN(); + } + + + /** + * {@inheritDoc} + */ + public ElementSerializer getValueSerializer() + { + return valueSerializer; + } + + + /** + * {@inheritDoc} + */ + public void setValueSerializer( ElementSerializer 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 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 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 The Key type + * @param The Value type + * + * @author Apache Directory Project + */ +public interface BTree +{ + /** 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. + *
+ * If the provided size is below 2, we will default to DEFAULT_PAGE_SIZE.
+ * If the provided size is not a power of 2, we will select the closest power of 2 + * higher than the given number
+ * + * @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. + *

+ * 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. + *

+ * 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 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 containing the removed entry, or null if it's not found. + */ + Tuple 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 containing the removed entry, or null if it's not found. + */ + Tuple 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.
+ * 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 getRootPage( long revision ) throws IOException, KeyNotFoundException; + + + /** + * Get the current rootPage + * + * @return The current rootPage + */ + Page 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 root ); + + + /** + * @see Page#getValues(Object) + */ + ValueCursor 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.
+ * 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 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 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 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 browseFrom( long revision, K key ) throws IOException, KeyNotFoundException; + + + /** + * @return the comparator + */ + Comparator getComparator(); + + + /** + * @return the type for the keys + */ + Class getKeyType(); + + + /** + * @param keySerializer the Key serializer to set + */ + void setKeySerializer( ElementSerializer keySerializer ); + + + /** + * @param valueSerializer the Value serializer to set + */ + void setValueSerializer( ElementSerializer 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 getKeySerializer(); + + + /** + * @return the keySerializer FQCN + */ + String getKeySerializerFQCN(); + + + /** + * @return the valueSerializer + */ + ElementSerializer 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 implements ValueCursor +{ + /** Store the current position in the array or in the BTree */ + private TupleCursor cursor; + + /** The Value sub-btree */ + private BTree valueBtree; + + + /** + * Create an instance + */ + public ValueBTreeCursor( BTree 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 * 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 /** - * @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 /** - * @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 BTree createBTree() { - BTree btree = new BTree(); + BTree btree = new PersistedBTree(); + + return btree; + } + + + /** + * Create a new BTree. + * + * @return The created BTree + */ + public static BTree createBTree( BTreeConfiguration configuration) + { + BTree btree = new PersistedBTree( configuration ); return btree; } @@ -89,9 +103,9 @@ public class BTreeFactory * * @param root the new root page. */ - public static void setRoot( BTree btree, Page root ) + public static void setRootPage( BTree btree, Page root ) { - btree.setRoot( root ); + btree.setRootPage( root ); } @@ -103,7 +117,7 @@ public class BTreeFactory */ public static Page getRoot( BTree btree ) { - return btree.rootPage; + return btree.getRootPage(); } @@ -112,7 +126,7 @@ public class BTreeFactory */ public static void setNbElems( BTree btree, long nbElems ) { - btree.setNbElems( nbElems ); + ((PersistedBTree)btree).setNbElems( nbElems ); } @@ -121,7 +135,7 @@ public class BTreeFactory */ public static void setRevision( BTree btree, long revision ) { - btree.setRevision( revision ); + ((PersistedBTree)btree).setRevision( revision ); } @@ -130,7 +144,7 @@ public class BTreeFactory */ public static void setRootPageOffset( BTree btree, long rootPageOffset ) { - btree.setRootPageOffset( rootPageOffset ); + ((PersistedBTree)btree).setRootPageOffset( rootPageOffset ); } @@ -139,7 +153,7 @@ public class BTreeFactory */ public static void setNextBTreeOffset( BTree btree, long nextBTreeOffset ) { - btree.setNextBTreeOffset( nextBTreeOffset ); + ((PersistedBTree)btree).setNextBTreeOffset( nextBTreeOffset ); } @@ -208,7 +222,7 @@ public class BTreeFactory */ public static void setRecordManager( BTree btree, RecordManager recordManager ) { - btree.setRecordManager( recordManager ); + ((PersistedBTree)btree).setRecordManager( recordManager ); } @@ -268,18 +282,18 @@ public class BTreeFactory { LinkedList> stack = new LinkedList>(); - ParentPos last = new ParentPos( btree.rootPage, btree.rootPage.getNbElems() ); + ParentPos last = new ParentPos( btree.getRootPage(), btree.getRootPage().getNbElems() ); stack.push( last ); - if ( btree.rootPage instanceof Leaf ) + if ( btree.getRootPage() instanceof Leaf ) { - Leaf leaf = ( Leaf ) ( btree.rootPage ); + Leaf leaf = ( Leaf ) ( btree.getRootPage() ); ValueHolder valueHolder = leaf.values[last.pos]; last.valueCursor = valueHolder.getCursor(); } else { - Node node = ( Node ) btree.rootPage; + Node node = ( Node ) btree.getRootPage(); while ( true ) { @@ -313,18 +327,18 @@ public class BTreeFactory { LinkedList> stack = new LinkedList>(); - ParentPos first = new ParentPos( btree.rootPage, 0 ); + ParentPos first = new ParentPos( btree.getRootPage(), 0 ); stack.push( first ); - if ( btree.rootPage instanceof Leaf ) + if ( btree.getRootPage() instanceof Leaf ) { - Leaf leaf = ( Leaf ) ( btree.rootPage ); + Leaf leaf = ( Leaf ) ( btree.getRootPage() ); ValueHolder valueHolder = leaf.values[first.pos]; first.valueCursor = valueHolder.getCursor(); } else { - Node node = ( Node ) btree.rootPage; + Node node = ( Node ) 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 Apache Directory Project */ 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 newLeaf = new Leaf( btree, revision, nbElems + 1 ); // Create the value holder - ValueHolder valueHolder = btree.createValueHolder( value ); + ValueHolder valueHolder = new ValueHolder( 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 leftLeaf = null; Leaf rightLeaf = null; - ValueHolder valueHolder = btree.createValueHolder( value ); + ValueHolder valueHolder = new ValueHolder( 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[] ) Array.newInstance( PageHolder.class, btree.getPageSize() + 1 ); - children[0] = btree.createPageHolder( leftPage ); - children[1] = btree.createPageHolder( rightPage ); + children[0] = new PageHolder( btree, leftPage ); + children[1] = new PageHolder( 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 createHolder( Page page ) throws IOException { - PageHolder holder = btree.getRecordManager().writePage( btree, + PageHolder holder = ((PersistedBTree)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 /** * 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 btree, Page element, long offset, long lastOffset ) + public PageHolder( BTree btree, Page page ) + { + this.btree = btree; + this.offset = page.getOffset(); + this.lastOffset = page.getLastOffset(); + + if ( page instanceof Page ) + { + ( ( AbstractPage ) page ).setOffset( offset ); + ( ( AbstractPage ) page ).setLastOffset( lastOffset ); + } + + ((PersistedBTree)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 btree, Page page, long offset, long lastOffset ) { this.btree = btree; this.offset = offset; this.lastOffset = lastOffset; - if ( element instanceof Page ) + if ( page instanceof Page ) { - ( ( AbstractPage ) element ).setOffset( offset ); - ( ( AbstractPage ) element ).setLastOffset( lastOffset ); + ( ( AbstractPage ) page ).setOffset( offset ); + ( ( AbstractPage ) page ).setLastOffset( lastOffset ); } - btree.getCache().put( new Element( offset, element ) ); + ((PersistedBTree)btree).getCache().put( new Element( offset, page ) ); } @@ -80,7 +101,7 @@ public class PageHolder */ public Page getValue( BTree btree ) throws EndOfFileExceededException, IOException { - Element element = btree.getCache().get( offset ); + Element element = ((PersistedBTree)btree).getCache().get( offset ); if ( element == null ) { @@ -88,12 +109,12 @@ public class PageHolder // We have to fetch the element from disk, using the offset now Page page = fetchElement( btree ); - btree.getCache().put( new Element( offset, page ) ); + ((PersistedBTree)btree).getCache().put( new Element( offset, page ) ); return page; } - Page page = (org.apache.directory.mavibot.btree.Page ) element.getObjectValue(); + Page page = (Page ) element.getObjectValue(); if ( page == null ) { @@ -106,7 +127,7 @@ public class PageHolder ( ( AbstractPage ) page ).setLastOffset( lastOffset ); } - btree.getCache().put( new Element( offset, page ) ); + ((PersistedBTree)btree).getCache().put( new Element( offset, page ) ); } return page; @@ -121,7 +142,7 @@ public class PageHolder */ private Page fetchElement( BTree btree ) throws EndOfFileExceededException, IOException { - Page element = btree.getRecordManager().deserialize( btree, offset ); + Page element = ((PersistedBTree)btree).getRecordManager().deserialize( btree, offset ); return element; }