directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1722069 - in /directory/mavibot/branches/single-value/mavibot/src: main/java/org/apache/directory/mavibot/btree/ test/java/org/apache/directory/mavibot/btree/
Date Tue, 29 Dec 2015 07:39:10 GMT
Author: elecharny
Date: Tue Dec 29 07:39:10 2015
New Revision: 1722069

URL: http://svn.apache.org/viewvc?rev=1722069&view=rev
Log:
o Added a Revision in the TransactionContext
o Get the revision from the Context when updating the Btrees
o Used the pages map from the context when updating a btree inside a transaction
o Many fixes in the RecordManager initialization

Modified:
    directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java
    directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractPage.java
    directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/PersistedBTree.java
    directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/PersistedLeaf.java
    directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/RecordManager.java
    directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/TransactionContext.java
    directory/mavibot/branches/single-value/mavibot/src/test/java/org/apache/directory/mavibot/btree/PersistedBTreeBrowseTest.java

Modified: directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java
URL: http://svn.apache.org/viewvc/directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java?rev=1722069&r1=1722068&r2=1722069&view=diff
==============================================================================
--- directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java
(original)
+++ directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java
Tue Dec 29 07:39:10 2015
@@ -409,7 +409,7 @@ import org.apache.directory.mavibot.btre
 
         try
         {
-            InsertResult<K, V> result = insert( key, value, -1L );
+            InsertResult<K, V> result = insert( key, value, recordManager.getContext().getRevision()
);
 
             if ( result instanceof ExistsResult )
             {

Modified: directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractPage.java
URL: http://svn.apache.org/viewvc/directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractPage.java?rev=1722069&r1=1722068&r2=1722069&view=diff
==============================================================================
--- directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractPage.java
(original)
+++ directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractPage.java
Tue Dec 29 07:39:10 2015
@@ -83,7 +83,7 @@ import org.apache.directory.mavibot.btre
         this.btree = btree;
         this.revision = revision;
         this.nbElems = nbElems;
-        this.keys = ( KeyHolder[] ) Array.newInstance( KeyHolder.class, nbElems );
+        this.keys = ( KeyHolder[] ) Array.newInstance( KeyHolder.class, btree.getPageSize()
);
     }
 
 

Modified: directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/PersistedBTree.java
URL: http://svn.apache.org/viewvc/directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/PersistedBTree.java?rev=1722069&r1=1722068&r2=1722069&view=diff
==============================================================================
--- directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/PersistedBTree.java
(original)
+++ directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/PersistedBTree.java
Tue Dec 29 07:39:10 2015
@@ -100,6 +100,7 @@ public class PersistedBTree<K, V> extend
             throw new IllegalArgumentException( "Comparator should not be null" );
         }
 
+        /*
         // Create the first root page, with revision 0L. It will be empty
         // and increment the revision at the same time
         Page<K, V> rootPage = new PersistedLeaf<K, V>( this );
@@ -125,6 +126,7 @@ public class PersistedBTree<K, V> extend
                 currentBtreeHeader = btreeHeader;
                 break;
         }
+        */
     }
 
 
@@ -347,7 +349,8 @@ public class PersistedBTree<K, V> extend
         newBtreeHeader.setRevision( revision );
 
         // Write down the data on disk
-        long newBtreeHeaderOffset = recordManager.writeBtreeHeader( this, newBtreeHeader
);
+        PageIO[] newBtreeHeaderPageIos = recordManager.writeBtreeHeader( this, newBtreeHeader,
false );
+        long newBtreeHeaderOffset = newBtreeHeaderPageIos[0].getOffset();
 
         // Update the B-tree of B-trees with this new offset, if we are not already doing
so
         switch ( btreeType )
@@ -466,27 +469,49 @@ public class PersistedBTree<K, V> extend
     private InsertResult<K, V> processInsert( K key, V value, long revision ) throws
IOException
     {
         // Get the current B-tree header, and insert the value into it
-        BTreeHeader<K, V> btreeHeader = getBTreeHeader( getName() );
-        InsertResult<K, V> result = btreeHeader.getRootPage().insert( key, value, revision
);
-
-        if ( result instanceof ExistsResult )
+        // Check if it's in the context
+        BTreeHeader<K, V> btreeHeader = currentBtreeHeader;
+        
+        // If the current btree header has been modified, it will be in the WalObject map.

+        WALObject walObject = ( BTreeHeader<K, V> ) recordManager.getContext().getPage(
currentBtreeHeader.getBTreeHeaderOffset() );
+        
+        if ( walObject != null )
         {
-            return result;
+            btreeHeader = ( BTreeHeader<K, V> ) walObject;
         }
+        else
+        {
+            // Create a new BTreeHeader
+            BTreeHeader<K, V> newBtreeHeader = btreeHeader.copy();
 
-        // Create a new BTreeHeader
-        BTreeHeader<K, V> newBtreeHeader = btreeHeader.copy();
-
-        // Inject the old B-tree header into the pages to be freed
-        // if we are inserting an element in a management BTree
-        if ( ( btreeType == BTreeTypeEnum.BTREE_OF_BTREES ) || ( btreeType == BTreeTypeEnum.COPIED_PAGES_BTREE
) )
+            // Store the btreeHeader into the context
+            recordManager.getContext().addPage( newBtreeHeader.getBTreeHeaderOffset(),  newBtreeHeader
);
+    
+            // Store the old BtreeHeader in the copied page map in the context
+            recordManager.getContext().addCopiedPage( btreeHeader.getBTreeHeaderOffset(),
 btreeHeader );
+    
+            btreeHeader = newBtreeHeader;
+        }
+        
+        // Get the rootPage
+        Page<K, V> rootPage = null;
+        
+        walObject = recordManager.getContext().getPage( btreeHeader.getRootPageOffset() );
+        
+        if ( walObject != null )
         {
-            PageIO[] pageIos = recordManager.readPageIOs( btreeHeader.getBTreeHeaderOffset(),
-1L );
+            rootPage = ( Page<K, V> ) walObject;
+        }
+        else
+        {
+        	rootPage = btreeHeader.getRootPage();
+        }
+        
+        InsertResult<K, V> result = rootPage.insert( key, value, revision );
 
-            for ( PageIO pageIo : pageIos )
-            {
-                recordManager.freedPages.add( pageIo );
-            }
+        if ( result instanceof ExistsResult )
+        {
+            return result;
         }
 
         Page<K, V> newRootPage;
@@ -500,7 +525,7 @@ public class PersistedBTree<K, V> extend
             // Increment the counter if we have inserted a new value
             if ( modifyResult.getModifiedValue() == null )
             {
-                newBtreeHeader.incrementNbElems();
+                btreeHeader.incrementNbElems();
             }
         }
         else
@@ -523,7 +548,7 @@ public class PersistedBTree<K, V> extend
             newRootPage = new PersistedNode<K, V>( this, revision, pivot, holderLeft,
holderRight );
 
             // Always increment the counter : we have added a new value
-            newBtreeHeader.incrementNbElems();
+            btreeHeader.incrementNbElems();
         }
 
         // Write the new root page on disk
@@ -531,11 +556,12 @@ public class PersistedBTree<K, V> extend
         writePage( newRootPage, revision );
 
         // Update the new B-tree header
-        newBtreeHeader.setRootPage( newRootPage );
-        newBtreeHeader.setRevision( revision );
+        btreeHeader.setRootPage( newRootPage );
+        btreeHeader.setRevision( revision );
 
         // Write down the data on disk
-        long newBtreeHeaderOffset = recordManager.writeBtreeHeader( this, newBtreeHeader
);
+        PageIO[] newBtreeHeaderpageIos = recordManager.writeBtreeHeader( this, btreeHeader,
false );
+        long newBtreeHeaderOffset = newBtreeHeaderpageIos[0].getOffset();
 
         // Update the B-tree of B-trees with this new offset, if we are not already doing
so
         switch ( btreeType )
@@ -547,7 +573,7 @@ public class PersistedBTree<K, V> extend
                 recordManager.addInCopiedPagesBtree( getName(), revision, result.getCopiedPages()
);
 
                 // Store the new revision
-                storeRevision( newBtreeHeader, recordManager.isKeepRevisions() );
+                storeRevision( btreeHeader, recordManager.isKeepRevisions() );
 
                 break;
 
@@ -559,7 +585,7 @@ public class PersistedBTree<K, V> extend
                 recordManager.freePages( this, revision, result.getCopiedPages() );
 
                 // Store the new revision
-                storeRevision( newBtreeHeader, recordManager.isKeepRevisions() );
+                storeRevision( btreeHeader, recordManager.isKeepRevisions() );
 
                 break;
 
@@ -571,7 +597,7 @@ public class PersistedBTree<K, V> extend
                 recordManager.freePages( this, revision, result.getCopiedPages() );
 
                 // Store the new revision
-                storeRevision( newBtreeHeader, recordManager.isKeepRevisions() );
+                storeRevision( btreeHeader, recordManager.isKeepRevisions() );
 
                 break;
 

Modified: directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/PersistedLeaf.java
URL: http://svn.apache.org/viewvc/directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/PersistedLeaf.java?rev=1722069&r1=1722068&r2=1722069&view=diff
==============================================================================
--- directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/PersistedLeaf.java
(original)
+++ directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/PersistedLeaf.java
Tue Dec 29 07:39:10 2015
@@ -64,7 +64,7 @@ import org.apache.directory.mavibot.btre
     PersistedLeaf( BTree<K, V> btree, long revision, int nbElems )
     {
         super( btree, revision, nbElems );
-        values = ( ValueHolder<V>[] ) Array.newInstance( PersistedValueHolder.class,
nbElems );
+        values = ( ValueHolder<V>[] ) Array.newInstance( PersistedValueHolder.class,
btree.getPageSize() );
     }
 
 
@@ -767,8 +767,8 @@ import org.apache.directory.mavibot.btre
 
 
     /**
-     * Adds a new <K, V> into a copy of the current page at a given position. We return
the
-     * modified page. The new page will have one more element than the current page.
+     * Adds a new <K, V> into the current page at a given position. We return the
+     * modified page. The modified page will have one more element than the current page.
      *
      * @param revision The revision of the modified page
      * @param key The key to insert
@@ -778,35 +778,30 @@ import org.apache.directory.mavibot.btre
      */
     private Page<K, V> addElement( long revision, K key, V value, int pos )
     {
-        // First copy the current page, but add one element in the copied page
-        PersistedLeaf<K, V> newLeaf = new PersistedLeaf<K, V>( btree, revision,
nbElems + 1 );
-
         // Create the value holder
         ValueHolder<V> valueHolder = new PersistedValueHolder<V>( btree, value
);
 
         // Deal with the special case of an empty page
         if ( nbElems == 0 )
         {
-            newLeaf.keys[0] = new PersistedKeyHolder<K>( btree.getKeySerializer(),
key );
-
-            newLeaf.values[0] = valueHolder;
+            keys[0] = new PersistedKeyHolder<K>( btree.getKeySerializer(), key );
+            values[0] = valueHolder;
         }
         else
         {
-            // Copy the keys and the values up to the insertion position
-            System.arraycopy( keys, 0, newLeaf.keys, 0, pos );
-            System.arraycopy( values, 0, newLeaf.values, 0, pos );
+            // Copy the keys and the values from the insertion point one position to the
right
+        	int nbElementToMove = nbElems - pos;
+            System.arraycopy( keys, pos, keys, pos + 1, nbElementToMove );
+            System.arraycopy( values, pos, values, pos + 1, nbElementToMove );
 
             // Add the new element
-            newLeaf.keys[pos] = new PersistedKeyHolder<K>( btree.getKeySerializer(),
key );
-            newLeaf.values[pos] = valueHolder;
-
-            // And copy the remaining elements
-            System.arraycopy( keys, pos, newLeaf.keys, pos + 1, keys.length - pos );
-            System.arraycopy( values, pos, newLeaf.values, pos + 1, values.length - pos );
+            keys[pos] = new PersistedKeyHolder<K>( btree.getKeySerializer(), key );
+            values[pos] = valueHolder;
         }
 
-        return newLeaf;
+        nbElems++;
+
+        return this;
     }
 
 

Modified: directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/RecordManager.java
URL: http://svn.apache.org/viewvc/directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/RecordManager.java?rev=1722069&r1=1722068&r2=1722069&view=diff
==============================================================================
--- directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/RecordManager.java
(original)
+++ directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/RecordManager.java
Tue Dec 29 07:39:10 2015
@@ -41,7 +41,6 @@ import java.util.concurrent.locks.Reentr
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import org.apache.directory.mavibot.btree.exception.BTreeAlreadyManagedException;
-import org.apache.directory.mavibot.btree.exception.BTreeCreationException;
 import org.apache.directory.mavibot.btree.exception.CursorException;
 import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
 import org.apache.directory.mavibot.btree.exception.FileException;
@@ -224,6 +223,8 @@ public class RecordManager extends Abstr
 
     public Map<Long, Integer> writeCounter = new HashMap<Long, Integer>();
     
+    private AtomicLong currentRevision = new AtomicLong( 0 );
+    
     /** The transaction context */
     private TransactionContext context;
 
@@ -408,28 +409,12 @@ public class RecordManager extends Abstr
         {
             beginTransaction();
             writeManagementTree( btreeOfBtrees );
-
-            currentBtreeOfBtreesOffset = ( ( PersistedBTree<NameRevision, Long> ) btreeOfBtrees
).getBtreeHeader()
-                .getBTreeHeaderOffset();
-
-            // Inject the BtreeOfBtrees into the currentBtreeHeaders map
-            currentBTreeHeaders.put( BTREE_OF_BTREES_NAME,
-                ( ( PersistedBTree<NameRevision, Long> ) btreeOfBtrees ).getBtreeHeader()
);
-            newBTreeHeaders.put( BTREE_OF_BTREES_NAME,
-                ( ( PersistedBTree<NameRevision, Long> ) btreeOfBtrees ).getBtreeHeader()
);
+            currentBtreeOfBtreesOffset = ((PersistedBTree<NameRevision, Long>)btreeOfBtrees).getBtreeOffset();
 
             // The Copied Pages B-tree
             writeManagementTree( copiedPageBtree );
+            currentCopiedPagesBtreeOffset = ((PersistedBTree<RevisionName, long[]>)copiedPageBtree).getBtreeOffset();
 
-            currentCopiedPagesBtreeOffset = ( ( PersistedBTree<RevisionName, long[]>
) copiedPageBtree )
-                .getBtreeHeader().getBTreeHeaderOffset();
-
-            // Inject the CopiedPagesBTree into the currentBtreeHeaders map
-            currentBTreeHeaders.put( COPIED_PAGE_BTREE_NAME,
-                ( ( PersistedBTree<RevisionName, long[]> ) copiedPageBtree ).getBtreeHeader()
);
-            newBTreeHeaders.put( COPIED_PAGE_BTREE_NAME,
-                ( ( PersistedBTree<RevisionName, long[]> ) copiedPageBtree ).getBtreeHeader()
);
-            
             // And finally, commit the transaction
             commit();
         }
@@ -638,7 +623,7 @@ public class RecordManager extends Abstr
         // Create the context 
         try
         {
-            context = new TransactionContext( fileChannel.size() );
+            context = new TransactionContext( fileChannel.size() , currentRevision.get()
);
         }
         catch ( IOException ioe )
         {
@@ -748,10 +733,14 @@ public class RecordManager extends Abstr
                     runReclaimer();
                 }
 
+                // increment the revision
+                currentRevision.incrementAndGet();
+
                 // Finally, decrement the number of started transactions
                 // and release the global lock if possible
                 int txnLevel = decrementTxnLevel();
 
+
                 if ( txnLevel == 0 )
                 {
                     transactionLock.unlock();
@@ -916,7 +905,7 @@ public class RecordManager extends Abstr
 
         // And restore the BTreeHeaders new Map to the current state
         revertBtreeHeaders();
-
+        
         // This is an all-of-nothing operation : we can't have a transaction within
         // a transaction that would survive an inner transaction rollback.
         transactionLock.unlock();
@@ -1581,6 +1570,7 @@ public class RecordManager extends Abstr
     public synchronized <K, V> void manage( BTree<K, V> btree ) throws BTreeAlreadyManagedException,
IOException
     {
         beginTransaction();
+        long revision = context.getRevision();
 
         try
         {
@@ -1597,43 +1587,37 @@ public class RecordManager extends Abstr
                 throw new BTreeAlreadyManagedException( name );
             }
 
-            // Get the B-tree header offset (reclaim a 1 byte page to get at least one page)
-            PageIO[] btreeHeaderPageIos = getFreePageIOs( 1 );
-            long btreeHeaderOffset = btreeHeaderPageIos[0].getOffset();
-            BTreeHeader<K, V> btreeHeader = ( ( AbstractBTree<K, V> ) btree ).getBtreeHeader();
-            ( ( PersistedBTree<K, V> ) btree ).setBtreeHeaderOffset( btreeHeaderOffset
);
-            context.addPage( btreeHeaderOffset, btreeHeader );
-
-            // Get the B-tree info offset
+            // Create the B-tree info
             BTreeInfo<K, V> btreeInfo = createBtreeInfo( btree );
-            PageIO[] btreeInfoPageIos = getFreePageIOs( 1 );
+            PageIO[] btreeInfoPageIos = writeBtreeInfo( btree, false );
             long btreeInfoOffset = btreeInfoPageIos[0].getOffset();
             ( ( PersistedBTree<K, V> ) btree ).setBtreeInfoOffset( btreeInfoOffset
);
             context.addPage( btreeInfoOffset, btreeInfo );
 
-            // Get the B-tree root page offset
-            Page<K, V> rootPage = btreeHeader.getRootPage();
-            PageIO[] rootPageIos = getFreePageIOs( 1 );
+            // Create the first root page, with the context revision. It will be empty
+            Page<K, V> rootPage = new PersistedLeaf<K, V>( btree, revision, 0
);
+            PageIO[] rootPageIos = serializePage( btree, revision, rootPage );
             long rootPageOffset = rootPageIos[0].getOffset();
-            btreeHeader.setRootPageOffset( rootPageOffset );
-            ( ( PersistedLeaf<K, V> ) rootPage ).setOffset( rootPageOffset );
+            ( ( AbstractPage<K, V> ) rootPage ).setOffset( rootPageOffset );
+            context.addPage( rootPageOffset, rootPage );
 
-            // Now, if this is a new B-tree, add it to the B-tree of B-trees
-            // Add the btree into the map of managed B-trees
-            managedBtrees.put( name, ( BTree<Object, Object> ) btree );
-
-            // And in the Map of currentBtreeHeaders and newBtreeHeaders
-            currentBTreeHeaders.put( name, btreeHeader );
-            newBTreeHeaders.put( name, btreeHeader );
+            // Create a B-tree header, and initialize it
+            BTreeHeader<K, V> btreeHeader = new BTreeHeader<K, V>();
+            btreeHeader.setRootPageOffset( rootPageOffset );
+            btreeHeader.setRootPage( rootPage );
+            btreeHeader.setBtree( btree );
+            PageIO[] btreeHeaderPageIos = writeBtreeHeader( btree, btreeHeader, false );
+            long btreeHeaderOffset = btreeHeaderPageIos[0].getOffset();
+            context.addPage( btreeHeaderOffset, btreeHeader );
 
             // We can safely increment the number of managed B-trees
             nbBtree++;
 
             // Create the new NameRevision
-            NameRevision nameRevision = new NameRevision( name, 0L );
+            NameRevision nameRevision = new NameRevision( name, revision );
 
             // Inject it into the B-tree of B-tree
-            btreeOfBtrees.insert( nameRevision, btreeHeader.getId() );
+            btreeOfBtrees.insert( nameRevision, btreeHeaderOffset );
             
             commit();
         }
@@ -1665,10 +1649,11 @@ public class RecordManager extends Abstr
      * +------------+
      * </pre>
      * @param btree The B-tree which header has to be written
+     * @param new a flag set to True if we have to flush the page on disk
      * @return The B-tree header offset
      * @throws IOException If we weren't able to write the B-tree header
      */
-    private <K, V> long writeBtreeInfo( BTree<K, V> btree ) throws IOException
+    private <K, V> PageIO[] writeBtreeInfo( BTree<K, V> btree, boolean now )
throws IOException
     {
         // We will add the newly managed B-tree at the end of the header.
         byte[] btreeNameBytes = Strings.getBytesUtf8( btree.getName() );
@@ -1687,9 +1672,6 @@ public class RecordManager extends Abstr
         // Get the pageIOs we need to store the data. We may need more than one.
         PageIO[] btreeHeaderPageIos = getFreePageIOs( bufferSize );
 
-        // Keep the B-tree header Offset into the B-tree
-        long btreeInfoOffset = btreeHeaderPageIos[0].getOffset();
-
         // Now store the B-tree information data in the pages :
         // - the B-tree page size
         // - the B-tree name
@@ -1710,11 +1692,14 @@ public class RecordManager extends Abstr
         // The valueSerialier FQCN
         position = store( position, valueSerializerBytes, btreeHeaderPageIos );
 
-        // And flush the pages to disk now
-        LOG.debug( "Flushing the newly managed '{}' btree header", btree.getName() );
-        flushPages( btreeHeaderPageIos );
+        // And flush the pages to disk now, if required
+        if ( now )
+        { 
+            LOG.debug( "Flushing the newly managed '{}' btree header", btree.getName() );
+            flushPages( btreeHeaderPageIos );
+        }
 
-        return btreeInfoOffset;
+        return btreeHeaderPageIos;
     }
 
 
@@ -1735,15 +1720,25 @@ public class RecordManager extends Abstr
         BTreeFactory.setRecordManager( btree, this );
 
         String name = btree.getName();
+        
+        // Create the first root page, with revision 0L. It will be empty
+        // and increment the revision at the same time
+        Page<K, V> rootPage = new PersistedLeaf<K, V>( btree, 0L, 0 );
+
+        // Create a B-tree header, and initialize it
+        BTreeHeader<K, V> btreeHeader = new BTreeHeader<K, V>();
+        btreeHeader.setRootPage( rootPage );
+        btreeHeader.setBtree( btree );
 
         // Now, write the subB-tree informations
-        long btreeInfoOffset = writeBtreeInfo( btree );
-        BTreeHeader<K, V> btreeHeader = ( ( AbstractBTree<K, V> ) btree ).getBtreeHeader();
+        PageIO[] btreeHeaderPageIos = writeBtreeInfo( btree, true );
+
+        // Get the BrtreeInfo offset
+        long btreeInfoOffset = btreeHeaderPageIos[0].getOffset();
+
         ( ( PersistedBTree<K, V> ) btree ).setBtreeInfoOffset( btreeInfoOffset );
 
         // Serialize the B-tree root page
-        Page<K, V> rootPage = btreeHeader.getRootPage();
-
         PageIO[] rootPageIos = serializePage( btree, btreeHeader.getRevision(), rootPage
);
         
         // Get the reference on the first page
@@ -1758,9 +1753,12 @@ public class RecordManager extends Abstr
         flushPages( rootPageIos );
         
         // And the B-tree header
-        writeBtreeHeader( btree, btreeHeader );
+        writeBtreeHeader( btree, btreeHeader, true );
+        
+        // Keep a track of the current BtreeHeader
+        ((PersistedBTree<K, V>)btree).storeRevision(btreeHeader);
 
-        // Now, if this is a new B-tree, add it t        // And in the Map of currentBtreeHeaders
and newBtreeHeaders
+        // And in the Map of currentBtreeHeaders and newBtreeHeaders
         currentBTreeHeaders.put( name, btreeHeader );
         newBTreeHeaders.put( name, btreeHeader );
     }
@@ -2291,10 +2289,11 @@ public class RecordManager extends Abstr
      * </pre>
      * @param btree The B-tree which header has to be written
      * @param btreeInfoOffset The offset of the B-tree informations
+     * @param now a flag set to <tt>true</tt> if the header has to be written
on disk
      * @return The B-tree header offset
      * @throws IOException If we weren't able to write the B-tree header
      */
-    /* no qualifier */<K, V> long writeBtreeHeader( BTree<K, V> btree, BTreeHeader<K,
V> btreeHeader )
+    /* no qualifier */<K, V> PageIO[] writeBtreeHeader( BTree<K, V> btree, BTreeHeader<K,
V> btreeHeader, boolean now )
         throws IOException
     {
         int bufferSize =
@@ -2348,11 +2347,14 @@ public class RecordManager extends Abstr
             LOG_PAGES.debug( "Btree Header[{}]\n{}", btreeHeader.getRevision(), sb.toString()
);
         }
 
-        flushPages( btreeHeaderPageIos );
+        if ( now )
+        {
+            flushPages( btreeHeaderPageIos );
+        }
 
         btreeHeader.setBTreeHeaderOffset( btreeHeaderOffset );
 
-        return btreeHeaderOffset;
+        return btreeHeaderPageIos;
     }
 
     
@@ -2367,7 +2369,7 @@ public class RecordManager extends Abstr
     private <K, V> long writeRootPage( BTree<K, V> btree, BTreeHeader<K, V>
btreeHeader ) throws IOException
     {
         Page<K, V> rootPage = btreeHeader.getRootPage();
-        long pageId = context.getPageId();
+        long pageId = rootPage.getId();
 
         context.addPage( pageId, rootPage );
         
@@ -4145,6 +4147,12 @@ public class RecordManager extends Abstr
     {
         this.disableReclaimer = toggle;
     }
+    
+    
+    /* No qualifier */ TransactionContext getContext()
+    {
+        return context;
+    }
 
 
     /**

Modified: directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/TransactionContext.java
URL: http://svn.apache.org/viewvc/directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/TransactionContext.java?rev=1722069&r1=1722068&r2=1722069&view=diff
==============================================================================
--- directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/TransactionContext.java
(original)
+++ directory/mavibot/branches/single-value/mavibot/src/main/java/org/apache/directory/mavibot/btree/TransactionContext.java
Tue Dec 29 07:39:10 2015
@@ -35,8 +35,11 @@ public class TransactionContext
     /** The map containing all the modified pages, using their offset as a key */
     private Map<Long, WALObject> pageMap = new HashMap<Long, WALObject>();
     
-    /** The incremental counter used to identify pages */
-    private long pageId;
+    /** The map containing all the copied pages, using their offset as a key */
+    private Map<Long, WALObject> copiedPageMap = new HashMap<Long, WALObject>();
+    
+    /** The global revision */
+    private long revision;
     
     /** The last offset on disk */
     private long lastOffset;
@@ -45,12 +48,12 @@ public class TransactionContext
      * A constructor for the TransactionContext, which initialize the last offset to
      * the file size.
      * 
-     * @param lastOffset The last offset fo the associated file
+     * @param lastOffset The last offset of the associated file
      */
-    public TransactionContext( long lastOffset )
+    public TransactionContext( long lastOffset, long revision )
     {
         this.lastOffset = lastOffset;
-        pageId = RecordManager.NO_PAGE;
+        this.revision = revision;
     }
 
 
@@ -85,6 +88,38 @@ public class TransactionContext
         return pageMap.values();
     }
 
+
+    /**
+     * Retrieve a Page if it has been copied in the context
+     * @param offset the offset of the Page we are looking for
+     * @return The found Page, or null if it does not exist
+     */
+    public WALObject getCopiedPage( Long offset )
+    {
+        return copiedPageMap.get( offset );
+    }
+    
+    
+    /**
+     * Add a copied page into the TransactionContext
+     * 
+     * @param offset The Page's offset
+     * @param pageIo The copied Page to add
+     */
+    public void addCopiedPage( Long offset, WALObject page )
+    {
+        copiedPageMap.put( offset, page );
+    }
+
+    
+    /**
+     * @return The list of copied Pages 
+     */
+    public Collection<WALObject> getCopiedPages()
+    {
+        return copiedPageMap.values();
+    }
+
     
     /**
      * @return the lastOffset
@@ -114,12 +149,10 @@ public class TransactionContext
     
     
     /**
-     * @return The unique page ID
+     * @return The current revision
      */
-    public long getPageId()
+    public long getRevision()
     {
-        pageId--;
-        
-        return pageId;
+        return revision;
     }
 }

Modified: directory/mavibot/branches/single-value/mavibot/src/test/java/org/apache/directory/mavibot/btree/PersistedBTreeBrowseTest.java
URL: http://svn.apache.org/viewvc/directory/mavibot/branches/single-value/mavibot/src/test/java/org/apache/directory/mavibot/btree/PersistedBTreeBrowseTest.java?rev=1722069&r1=1722068&r2=1722069&view=diff
==============================================================================
--- directory/mavibot/branches/single-value/mavibot/src/test/java/org/apache/directory/mavibot/btree/PersistedBTreeBrowseTest.java
(original)
+++ directory/mavibot/branches/single-value/mavibot/src/test/java/org/apache/directory/mavibot/btree/PersistedBTreeBrowseTest.java
Tue Dec 29 07:39:10 2015
@@ -28,11 +28,7 @@ import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
 import java.util.NoSuchElementException;
-import java.util.Random;
 import java.util.UUID;
 
 import org.apache.commons.io.FileUtils;



Mime
View raw message