You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by el...@apache.org on 2013/03/13 15:59:47 UTC
svn commit: r1455978 - in
/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree:
BTree.java store/RecordManager.java
Author: elecharny
Date: Wed Mar 13 14:59:46 2013
New Revision: 1455978
URL: http://svn.apache.org/r1455978
Log:
o Incremented the revision before using it, as we start at revision 0
o Moved the flush on disk in the public insert() method, so that the BTree number of elements is updated when we update the BTree header
o Fixed a bug in the flushPages() method : we were writing the pages at the end of the file, no matter what...
Modified:
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java
Modified: labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java
URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java?rev=1455978&r1=1455977&r2=1455978&view=diff
==============================================================================
--- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java (original)
+++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java Wed Mar 13 14:59:46 2013
@@ -699,7 +699,7 @@ public class BTree<K, V>
/** No qualifier */
long generateRevision()
{
- return revision.getAndIncrement();
+ return revision.incrementAndGet();
}
@@ -718,13 +718,33 @@ public class BTree<K, V>
{
long revision = generateRevision();
- V existingValue = insert( key, value, revision );
+ V existingValue = null;
- // Increase the number of element in the current tree if the insertion is successful
- // and does not replace an element
- if ( existingValue == null )
+ try
{
- nbElems.getAndIncrement();
+ // Commented atm, we will have to play around the idea of transactions later
+ writeLock.lock();
+
+ existingValue = insert( key, value, revision );
+
+ // Increase the number of element in the current tree if the insertion is successful
+ // and does not replace an element
+ if ( existingValue == null )
+ {
+ nbElems.getAndIncrement();
+ }
+
+ // If the BTree is managed, we have to update the rootPage on disk
+ if ( isManaged() )
+ {
+ // Update the BTree header now
+ recordManager.updateBtreeHeader( this, ( ( AbstractPage<K, V> ) rootPage ).getOffset() );
+ }
+ }
+ finally
+ {
+ // See above
+ writeLock.unlock();
}
return existingValue;
@@ -938,65 +958,47 @@ public class BTree<K, V>
throw new IllegalArgumentException( "Key must not be null" );
}
- // Commented atm, we will have to play around the idea of transactions later
- writeLock.lock();
-
- try
- {
- // If the key exists, the existing value will be replaced. We store it
- // to return it to the caller.
- V modifiedValue = null;
-
- // Try to insert the new value in the tree at the right place,
- // starting from the root page. Here, the root page may be either
- // a Node or a Leaf
- InsertResult<K, V> result = rootPage.insert( revision, key, value );
-
- if ( result instanceof ModifyResult )
- {
- ModifyResult<K, V> modifyResult = ( ( ModifyResult<K, V> ) result );
+ // If the key exists, the existing value will be replaced. We store it
+ // to return it to the caller.
+ V modifiedValue = null;
- // The root has just been modified, we haven't split it
- // Get it and make it the current root page
- rootPage = modifyResult.getModifiedPage();
+ // Try to insert the new value in the tree at the right place,
+ // starting from the root page. Here, the root page may be either
+ // a Node or a Leaf
+ InsertResult<K, V> result = rootPage.insert( revision, key, value );
- modifiedValue = modifyResult.getModifiedValue();
+ if ( result instanceof ModifyResult )
+ {
+ ModifyResult<K, V> modifyResult = ( ( ModifyResult<K, V> ) result );
- // If the BTree is managed, we have to update the rootPage on disk
- if ( isManaged() )
- {
- // Update the BTree header now
- recordManager.updateBtreeHeader( this, btreeOffset );
- }
- }
- else
- {
- // We have split the old root, create a new one containing
- // only the pivotal we got back
- SplitResult<K, V> splitResult = ( ( SplitResult<K, V> ) result );
-
- K pivot = splitResult.getPivot();
- Page<K, V> leftPage = splitResult.getLeftPage();
- Page<K, V> rightPage = splitResult.getRightPage();
+ // The root has just been modified, we haven't split it
+ // Get it and make it the current root page
+ rootPage = modifyResult.getModifiedPage();
- // Create the new rootPage
- rootPage = new Node<K, V>( this, revision, pivot, leftPage, rightPage );
- }
+ modifiedValue = modifyResult.getModifiedValue();
+ }
+ else
+ {
+ // We have split the old root, create a new one containing
+ // only the pivotal we got back
+ SplitResult<K, V> splitResult = ( ( SplitResult<K, V> ) result );
- // Inject the modification into the modification queue
- if ( type == BTreeTypeEnum.PERSISTENT )
- {
- modificationsQueue.add( new Addition<K, V>( key, value ) );
- }
+ K pivot = splitResult.getPivot();
+ Page<K, V> leftPage = splitResult.getLeftPage();
+ Page<K, V> rightPage = splitResult.getRightPage();
- // Return the value we have found if it was modified
- return modifiedValue;
+ // Create the new rootPage
+ rootPage = new Node<K, V>( this, revision, pivot, leftPage, rightPage );
}
- finally
+
+ // Inject the modification into the modification queue
+ if ( type == BTreeTypeEnum.PERSISTENT )
{
- // See above
- writeLock.unlock();
+ modificationsQueue.add( new Addition<K, V>( key, value ) );
}
+
+ // Return the value we have found if it was modified
+ return modifiedValue;
}
Modified: labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java
URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java?rev=1455978&r1=1455977&r2=1455978&view=diff
==============================================================================
--- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java (original)
+++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/store/RecordManager.java Wed Mar 13 14:59:46 2013
@@ -944,7 +944,7 @@ public class RecordManager
dataSize += buffer.length;
}
- // Nodes have one mor evalue to serialize
+ // Nodes have one more value to serialize
if ( page instanceof Node )
{
// TODO
@@ -1044,12 +1044,12 @@ public class RecordManager
if ( fileChannel.size() <= ( pageIo.getOffset() + pageSize ) )
{
- fileChannel.write( pageIo.getData(), pageIo.getOffset() );
+ // This is a page we have to add to the file
+ fileChannel.write( pageIo.getData(), fileChannel.size() );
}
else
{
- // This is a page we have to add to the file
- fileChannel.write( pageIo.getData(), fileChannel.size() );
+ fileChannel.write( pageIo.getData(), pageIo.getOffset() );
}
pageIo.getData().rewind();
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org