directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1162132 - in /directory/apacheds/branches/apacheds-jdbm: jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/ jdbm/src/main/java/jdbm/btree/ jdbm/src/main/java/jdbm/recman/ jdbm/src/test/java/jdbm/btree/
Date Fri, 26 Aug 2011 14:57:58 GMT
Author: elecharny
Date: Fri Aug 26 14:57:57 2011
New Revision: 1162132

URL: http://svn.apache.org/viewvc?rev=1162132&view=rev
Log:
Applied last patch from Slcuk

Modified:
    directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmIndex.java
    directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmPartition.java
    directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmRdnIndex.java
    directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/btree/BPage.java
    directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/btree/BTree.java
    directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/recman/SnapshotRecordManager.java
    directory/apacheds/branches/apacheds-jdbm/jdbm/src/test/java/jdbm/btree/SnapshotBTree.java

Modified: directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmIndex.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmIndex.java?rev=1162132&r1=1162131&r2=1162132&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmIndex.java
(original)
+++ directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmIndex.java
Fri Aug 26 14:57:57 2011
@@ -26,9 +26,8 @@ import java.io.IOException;
 import java.net.URI;
 
 import jdbm.RecordManager;
-import jdbm.helper.MRU;
 import jdbm.recman.BaseRecordManager;
-import jdbm.recman.CacheRecordManager;
+import jdbm.recman.SnapshotRecordManager;
 
 import org.apache.directory.server.core.partition.impl.btree.IndexCursorAdaptor;
 import org.apache.directory.server.core.partition.impl.btree.LongComparator;
@@ -176,7 +175,7 @@ public class JdbmIndex<K, O> extends Abs
 
         BaseRecordManager base = new BaseRecordManager( path );
         base.disableTransactions();
-        this.recMan = new CacheRecordManager( base, new MRU( DEFAULT_INDEX_CACHE_SIZE ) );
+        this.recMan = new SnapshotRecordManager( base, DEFAULT_INDEX_CACHE_SIZE );
 
         try
         {

Modified: directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmPartition.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmPartition.java?rev=1162132&r1=1162131&r2=1162132&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmPartition.java
(original)
+++ directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmPartition.java
Fri Aug 26 14:57:57 2011
@@ -27,9 +27,8 @@ import java.util.Arrays;
 import java.util.List;
 
 import jdbm.RecordManager;
-import jdbm.helper.MRU;
 import jdbm.recman.BaseRecordManager;
-import jdbm.recman.CacheRecordManager;
+import jdbm.recman.SnapshotRecordManager;
 
 import org.apache.directory.server.constants.ApacheSchemaConstants;
 import org.apache.directory.server.core.partition.Partition;
@@ -141,7 +140,7 @@ public class JdbmPartition extends Abstr
             }
     
             // Now, create the entry cache for this partition
-            recMan = new CacheRecordManager( baseRecordManager, new MRU( cacheSize ) );
+            recMan = new SnapshotRecordManager( baseRecordManager, cacheSize );
     
             // Create the master table (the table containing all the entries)
             master = new JdbmMasterTable<Entry>( recMan, schemaManager );

Modified: directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmRdnIndex.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmRdnIndex.java?rev=1162132&r1=1162131&r2=1162132&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmRdnIndex.java
(original)
+++ directory/apacheds/branches/apacheds-jdbm/jdbm-partition/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmRdnIndex.java
Fri Aug 26 14:57:57 2011
@@ -27,9 +27,8 @@ import java.io.IOException;
 
 import javax.naming.NamingException;
 
-import jdbm.helper.MRU;
 import jdbm.recman.BaseRecordManager;
-import jdbm.recman.CacheRecordManager;
+import jdbm.recman.SnapshotRecordManager;
 
 import org.apache.directory.server.core.partition.impl.btree.LongComparator;
 import org.apache.directory.server.i18n.I18n;
@@ -95,7 +94,7 @@ public class JdbmRdnIndex<E> extends Jdb
         //System.out.println( "IDX Created index " + path );
         BaseRecordManager base = new BaseRecordManager( path );
         base.disableTransactions();
-        this.recMan = new CacheRecordManager( base, new MRU( cacheSize ) );
+        this.recMan = new SnapshotRecordManager( base, cacheSize );
 
         try
         {

Modified: directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/btree/BPage.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/btree/BPage.java?rev=1162132&r1=1162131&r2=1162132&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/btree/BPage.java (original)
+++ directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/btree/BPage.java Fri
Aug 26 14:57:57 2011
@@ -47,6 +47,7 @@
 package jdbm.btree;
 
 
+import jdbm.btree.BTree.EmptyBrowser;
 import jdbm.helper.Serializer;
 import jdbm.helper.Tuple;
 import jdbm.helper.TupleBrowser;
@@ -198,6 +199,33 @@ public class BPage<K, V> implements Seri
 
         recordId = btree.recordManager.insert( this, this );
     }
+    
+    @SuppressWarnings("unchecked") // Cannot create an array of generic objects
+    BPage<K,V> copyOnWrite()
+    {
+        BPage<K, V> newPage = new BPage<K,V>();
+        
+        newPage.btree = this.btree;
+        newPage.isLeaf = this.isLeaf;
+        
+        newPage.first = this.first;
+        newPage.previous = this.previous;
+        newPage.next = this.next;
+        
+        newPage.keys = (K[])new Object[btree.pageSize];
+        newPage.values = (V[])new Object[btree.pageSize];
+        newPage.children = new long[btree.pageSize];
+        
+        newPage.recordId = this.recordId;
+        
+        if ( this.children != null )
+            this.copyChildren( this, 0, newPage, 0, btree.pageSize ); // this copies keys
as well
+        
+        if (this.values != null ) 
+            this.copyEntries( this, 0, newPage, 0, btree.pageSize ); // this copies keys
as well
+        
+        return newPage;
+    }
 
 
     /**
@@ -260,11 +288,10 @@ public class BPage<K, V> implements Seri
      *
      * @param height Height of the current BPage (zero is leaf page)
      * @param key The key
-     * @param context action specific context. not null if record manager is action capable
      * @return TupleBrowser positionned just before the given key, or before
      *                      next greater key if key isn't found.
      */
-    TupleBrowser<K, V> find( int height, K key, ActionContext context ) throws IOException
+    TupleBrowser<K, V> find( int height, K key) throws IOException
     {
         int index = this.findChildren( key );
         
@@ -287,27 +314,26 @@ public class BPage<K, V> implements Seri
             }
         }
 
-        return new Browser( child, index, context );
+        return new Browser( child, index);
     }
 
 
     /**
      * Find first entry and return a browser positioned before it.
      *
-     * @param context action specific context. not null if record manager is action capable
      * @return TupleBrowser positionned just before the first entry.
      */
-    TupleBrowser<K, V> findFirst(ActionContext context ) throws IOException
+    TupleBrowser<K, V> findFirst() throws IOException
     {
         if ( isLeaf )
         {
-            return new Browser( this, first, context );
+            return new Browser( this, first );
         }
         else
         {
             BPage<K, V> child = childBPage( first );
             
-            return child.findFirst( context );
+            return child.findFirst();
         }
     }
 
@@ -1387,10 +1413,7 @@ public class BPage<K, V> implements Seri
     {
         /** Current page. */
         private BPage<K, V> page;
-        
-        /** Browsing action's context in case of a action capable record manager */
-        ActionContext context;
-
+      
         /**
          * Current index in the page.  The index positionned on the next
          * tuple to return.
@@ -1402,14 +1425,12 @@ public class BPage<K, V> implements Seri
          * Create a browser.
          *
          * @param page Current page
-         * @param context Action specific context. Not null if part of an action
          * @param index Position of the next tuple to return.
          */
-        Browser( BPage<K, V> page, int index, ActionContext context )
+        Browser( BPage<K, V> page, int index)
         {
             this.page = page;
             this.index = index;
-            this.context = context;
         }
 
 
@@ -1423,7 +1444,6 @@ public class BPage<K, V> implements Seri
          */
         public boolean getNext( Tuple<K, V> tuple ) throws IOException
         {
-            btree.setAsCurrentAction( context );
             // First, check that we are within a page
             if ( index < page.btree.pageSize )
             {
@@ -1433,27 +1453,162 @@ public class BPage<K, V> implements Seri
                     // no : reached end of the tree.
                     return false;
                 }
+                
+                tuple.setKey( page.keys[index] );
+                tuple.setValue( btree.copyValue( page.values[index] ) );
+                index++;
+                return true;
             }
             // all the tuple for this page has been read. Move to the 
-            // next page, if we have one.
-            else if ( page.next != 0 )
+            // next page, if we have one. Start a new action while
+            // doing so.
+
+            ActionContext context = btree.beginAction( true );
+            boolean abortedAction = false;
+            try
             {
-                // move to next page
-                page = page.loadBPage( page.next );
-                index = page.first;
+                BPage<K, V> rootPage = btree.getRoot( true );
+
+                if ( rootPage == null )
+                {
+                    return false;
+                }
+
+                K prevKey = page.keys[index - 1];
+                Browser browser = ( Browser ) rootPage.find( rootPage.btree.bTreeHeight,
prevKey );
+
+                if ( browser.getNextWithinAction( tuple ) )
+                {
+                    if ( btree.comparator.compare( prevKey, tuple.getKey() ) == 0 )
+                    {
+                        // Do not return the same key again, move on to the next key
+                        if ( browser.getNextWithinAction( tuple ) )
+                        {
+                            page = browser.getCurrentPage();
+                            index = browser.getCurrentIndex();
+                            return true;
+                        }
+                        else
+                        {
+                            return false;
+                        }
+                    }
+                }
+                else
+                {
+                    return false;
+                }
+
+            }
+            catch ( IOException e )
+            {
+                abortedAction = true;
+                btree.abortAction( context );
+            }
+            finally
+            {
+                if ( !abortedAction )
+                    btree.endAction( context );
+            }
+
+            return false;
+        }
+
+
+        public boolean getPrevious( Tuple<K, V> tuple ) throws IOException
+        {
+            if ( index == page.first )
+            {
+                ActionContext context = btree.beginAction( true );
+                boolean abortedAction = false;
+                try
+                {
+                    BPage<K, V> rootPage = btree.getRoot( true );
+
+                    if ( rootPage == null )
+                    {
+                        return false;
+                    }
+
+                    K prevKey = page.keys[index];
+                    Browser browser = ( Browser ) rootPage.find( rootPage.btree.bTreeHeight,
prevKey );
+
+                    while ( browser.getPrevWithinAction( tuple ) )
+                    {
+                        if ( btree.comparator.compare( tuple.getKey(), prevKey ) < 0 )
+                        {
+
+                            page = browser.getCurrentPage();
+                            index = browser.getCurrentIndex();
+                            return true;
+
+                        }
+                    }
+                }
+                catch ( IOException e )
+                {
+                    abortedAction = true;
+                    btree.abortAction( context );
+                }
+                finally
+                {
+                    if ( !abortedAction )
+                        btree.endAction( context );
+                }
+
+                return false;
             }
             
+            index--;
             tuple.setKey( page.keys[index] );
-            tuple.setValue( page.values[index] );
-            index++;
+            tuple.setValue( btree.copyValue( page.values[index] ) );
             
             return true;
         }
 
 
-        public boolean getPrevious( Tuple<K, V> tuple ) throws IOException
+        BPage<K, V> getCurrentPage()
+        {
+            return page;
+        }
+
+
+        int getCurrentIndex()
+        {
+            return index;
+        }
+        
+        boolean getNextWithinAction( Tuple<K, V> tuple ) throws IOException
+        {
+            // First, check that we are within a page
+            if ( index < page.btree.pageSize )
+            {
+                // We are. Now check that we have a Tuple
+                if ( page.keys[index] == null )
+                {
+                    // no : reached end of the tree.
+                    return false;
+                }
+            }
+            // all the tuple for this page has been read. Move to the 
+            // next page,
+            else if ( page.next != 0 )
+            {
+                // move to next page                                                    
   
+                page = page.loadBPage( page.next );
+                index = page.first;
+            }
+
+            tuple.setKey( page.keys[index] );
+            tuple.setValue( btree.copyValue( page.values[index] ) );
+            index++;
+
+            return true;
+            
+        }
+        
+        boolean getPrevWithinAction( Tuple<K, V> tuple ) throws IOException
         {
-            btree.setAsCurrentAction( context );
             if ( index == page.first )
             {
                 if ( page.previous != 0 )
@@ -1467,19 +1622,14 @@ public class BPage<K, V> implements Seri
                     return false;
                 }
             }
-            
+
             index--;
             tuple.setKey( page.keys[index] );
-            tuple.setValue( page.values[index] );
-            
+            tuple.setValue( btree.copyValue( page.values[index] ) );
+
             return true;
         }
         
-        @Override
-        public void close()
-        {
-            btree.endAction( context );
-        }
     }
     
     

Modified: directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/btree/BTree.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/btree/BTree.java?rev=1162132&r1=1162131&r2=1162132&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/btree/BTree.java (original)
+++ directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/btree/BTree.java Fri
Aug 26 14:57:57 2011
@@ -87,7 +87,6 @@ import org.apache.directory.server.i18n.
  * user is responsible to supply a serializable <code>Comparator</code> object
  * to be used for the ordering of entries, which are also called <code>Tuple</code>.
  * The B+Tree allows traversing the keys in forward and reverse order using a
- * TupleBrowser obtained from the browse() methods.
  * <p>
  * This implementation does not directly support duplicate keys, but it is
  * possible to handle duplicates by inlining or referencing an object collection
@@ -116,7 +115,7 @@ public class BTree<K, V> implements Exte
     private transient long recordId;
 
     /** Comparator used to index entries. */
-    private Comparator<K> comparator;
+    Comparator<K> comparator;
 
     /** Serializer used to serialize index keys (optional) */
     protected Serializer keySerializer;
@@ -128,7 +127,7 @@ public class BTree<K, V> implements Exte
      * Height of the B+Tree.  This is the number of BPages you have to traverse
      * to get to a leaf BPage, starting from the root.
      */
-    private int bTreeHeight;
+    int bTreeHeight;
 
     /** Record id of the root BPage */
     private transient long rootId;
@@ -518,7 +517,7 @@ public class BTree<K, V> implements Exte
                 }
                 else
                 {
-                    return tuple.getValue();
+                   return this.copyValue( tuple.getValue() );
                 }
             }
             else
@@ -568,6 +567,7 @@ public class BTree<K, V> implements Exte
             
             if ( browser.getNext( tuple ) )
             {
+                tuple.setValue( this.copyValue( tuple.getValue() ) );
                 return tuple;
             }
             else
@@ -599,7 +599,8 @@ public class BTree<K, V> implements Exte
     public TupleBrowser<K, V> browse() throws IOException
     {
         TupleBrowser<K, V> browser = null;
-        ActionContext context = this.beginAction( true );      
+        ActionContext context = this.beginAction( true ); 
+        boolean abortedAction = false;
         try
         {
             BPage<K, V> rootPage = getRoot( true );
@@ -609,13 +610,21 @@ public class BTree<K, V> implements Exte
                 this.endAction( context );
                 return new EmptyBrowser(){};
             }
-            browser = rootPage.findFirst( context );         
+            browser = rootPage.findFirst();         
         }
         catch( IOException e )
         {
+            abortedAction = true;
             this.abortAction( context );
             throw e;
         }
+        finally
+        {
+            if ( !abortedAction )
+            {
+                this.endAction( context );
+            }
+        }
         
         return browser;
     }
@@ -637,7 +646,7 @@ public class BTree<K, V> implements Exte
     {
         TupleBrowser<K, V> browser = null;
         ActionContext context = this.beginAction( true );  
-        
+        boolean abortedAction = false;
         try
         {
             BPage<K, V> rootPage = getRoot( true );
@@ -648,13 +657,21 @@ public class BTree<K, V> implements Exte
                 return new EmptyBrowser(){};
             }
           
-            browser  = rootPage.find( rootPage.btree.bTreeHeight, key, context );
+            browser  = rootPage.find( rootPage.btree.bTreeHeight, key);
         }
         catch( IOException e )
         {
+            abortedAction = true;
             this.abortAction( context );
             throw e;
         }
+        finally
+        {
+            if ( !abortedAction )
+            {
+                this.endAction( context );
+            }
+        }
         
         return browser;
     }
@@ -682,13 +699,19 @@ public class BTree<K, V> implements Exte
     /**
      * Return the root BPage<Object, Object>, or null if it doesn't exist.
      */
-    private BPage<K, V> getRoot( boolean readOnlyAction ) throws IOException
+    BPage<K, V> getRoot( boolean readOnlyAction ) throws IOException
     {
         BTree<K, V> bTreeCopy;
         
-        if ( readOnlyAction && isActionCapable )
+        if ( readOnlyAction )
         {
             bTreeCopy = ( BTree<K, V> )recordManager.fetch( recordId );
+            bTreeCopy.comparator = this.comparator;
+            bTreeCopy.keySerializer = this.keySerializer;
+            bTreeCopy.valueSerializer = this.valueSerializer;
+            bTreeCopy.recordManager = this.recordManager;
+            bTreeCopy.recordId = this.recordId;
+            bTreeCopy.bpageSerializer = this.bpageSerializer;
         }
         else
         {
@@ -826,62 +849,59 @@ public class BTree<K, V> implements Exte
     
     BPage<K,V> copyOnWrite( BPage<K,V> page) throws IOException
     {
-    	if ( !isActionCapable )
-    	{
-    		return page;
-    	}
-    	else
-    	{
-    		byte[] array;
-    		array = this.bpageSerializer.serialize( page );
-    		BPage<K,V> pageCopy = this.bpageSerializer.deserialize( array );
-    		pageCopy.recordId = page.recordId;
-    		pageCopy.btree = page.btree;
-    		return pageCopy;
-    	}
-    }
-    
-    @SuppressWarnings("unchecked") 
-    private BTree<K,V> copyOnWrite() throws IOException
-    {
-        ObjectOutputStream out = null;
-        ObjectInputStream in = null;
-        ByteArrayOutputStream bout = null;
-        ByteArrayInputStream bin = null;
-        
-        BTree<K,V> tree;
-        
-        try 
-        {
-            bout = new ByteArrayOutputStream();
-            out = new ObjectOutputStream( bout );
-            out.writeObject( this );
-            out.flush();
-            byte[]  arr = bout.toByteArray();
-            bin = new ByteArrayInputStream( arr );
-            in =new ObjectInputStream( bin );
-            tree = ( BTree<K, V> )in.readObject();
-        }
-        catch ( ClassNotFoundException e ) 
+        return page.copyOnWrite();
+    }
+   
+
+    V copyValue( V value) throws IOException 
+    {
+        byte[] array;
+        V valueCopy = null;
+
+        if ( this.valueSerializer != null ) 
         {
-            throw new WrappedRuntimeException( e );
+            array = this.valueSerializer.serialize( value );
+            valueCopy = (V) this.valueSerializer.deserialize( array );
         }
-        finally
+        else
         {
-            if ( bout != null )
-                bout.close();
-            
-            if ( out != null )
-                out.close();
-            
-            if ( bin != null )
-                bin.close();
-           
-            if ( in != null )
-                in.close();
+            ObjectOutputStream out = null;
+            ObjectInputStream in = null;
+            ByteArrayOutputStream bout = null;
+            ByteArrayInputStream bin = null;
+
+            try
+            {
+                bout = new ByteArrayOutputStream();
+                out = new ObjectOutputStream( bout );
+                out.writeObject( value );
+                out.flush();
+                byte[]  arr = bout.toByteArray();
+                bin = new ByteArrayInputStream( arr );
+                in =new ObjectInputStream( bin );
+                valueCopy = ( V )in.readObject();
+            }
+            catch ( ClassNotFoundException e )
+            {
+                throw new WrappedRuntimeException( e );
+            }
+            finally
+            {
+                if ( bout != null )
+                    bout.close();
+
+                if ( out != null )
+                    out.close();
+
+                if ( bin != null )
+                    bin.close();
+
+                if ( in != null )
+                    in.close();
+            }
         }
         
-        return tree;
+        return valueCopy;
     }
     
     

Modified: directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/recman/SnapshotRecordManager.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/recman/SnapshotRecordManager.java?rev=1162132&r1=1162131&r2=1162132&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/recman/SnapshotRecordManager.java
(original)
+++ directory/apacheds/branches/apacheds-jdbm/jdbm/src/main/java/jdbm/recman/SnapshotRecordManager.java
Fri Aug 26 14:57:57 2011
@@ -169,18 +169,45 @@ public class SnapshotRecordManager imple
         checkIfClosed();
         
         ActionContext actionContext = actionContextVar.get();
-        assert( actionContext.isWriteAction() == true );
-
-        long recid = recordManager.insert( obj, serializer );
+        boolean startedAction = false;
+        boolean abortedAction = false;
+        if ( actionContext.isWriteAction() == false )
+        {
+            actionContext = this.beginAction( false );
+            this.setCurrentActionContext( actionContext );
+            startedAction = true;
+        }
         
-        try 
+        long recid = 0;
+        try
         {
+            recid = recordManager.insert( obj, serializer );
+            
             versionedCache.put( new Long( recid ), obj, actionContext.getVersion().getVersion(),
                 serializer );
         } 
+        catch ( IOException e )
+        {
+            if ( startedAction )
+            {
+                this.abortAction( actionContext );
+                abortedAction = true;
+            }
+            throw e;
+        }
         catch ( CacheEvictionException except ) 
         {
+            if ( startedAction )
+            {
+                this.abortAction( actionContext );
+                abortedAction = true;
+            }
             throw new IOException( except.getLocalizedMessage() );
+        }       
+        finally
+        {
+            if ( startedAction && !abortedAction )
+                this.endAction ( actionContext );
         }
         
         return recid;
@@ -198,18 +225,45 @@ public class SnapshotRecordManager imple
         checkIfClosed();
         
         ActionContext actionContext = actionContextVar.get();
-        assert( actionContext.isWriteAction() == true );
-
+        boolean startedAction = false;
+        boolean abortedAction = false;
+        if ( actionContext.isWriteAction() == false )
+        {
+            actionContext = this.beginAction( false );
+            this.setCurrentActionContext( actionContext );
+            startedAction = true;
+        }       
+        
         // Update the cache
         try 
         {
             versionedCache.put( new Long( recid ), null, actionContext.getVersion().getVersion(),
                 null );
-        } 
+        }
+        catch ( IOException e )
+        {
+            if ( startedAction )
+            {
+                this.abortAction( actionContext );
+                abortedAction = true;
+            }
+            throw e;
+        }
         catch ( CacheEvictionException except ) 
         {
+            if ( startedAction )
+            {
+                this.abortAction( actionContext );
+                abortedAction = true;
+            }
             throw new IOException( except.getLocalizedMessage() );
-        } 
+        }       
+        finally
+        {
+            if ( startedAction && !abortedAction )
+                this.endAction ( actionContext );
+        }
+         
     }
 
 
@@ -238,16 +292,42 @@ public class SnapshotRecordManager imple
     {
         checkIfClosed();
         ActionContext actionContext = actionContextVar.get();
-        assert( actionContext.isWriteAction() == true );
+        boolean startedAction = false;
+        boolean abortedAction = false;
+        if ( actionContext.isWriteAction() == false )
+        {
+            actionContext = this.beginAction( false );
+            this.setCurrentActionContext( actionContext );
+            startedAction = true;
+        }
 
         try 
         {
            versionedCache.put( new Long( recid ), obj, actionContext.getVersion().getVersion(),
                serializer );       
-        } 
+        }
+        catch ( IOException e )
+        {
+            if ( startedAction )
+            {
+                this.abortAction( actionContext );
+                abortedAction = true;
+            }
+            throw e;
+        }
         catch ( CacheEvictionException except ) 
         {
+            if ( startedAction )
+            {
+                this.abortAction( actionContext );
+                abortedAction = true;
+            }
             throw new IOException( except.getLocalizedMessage() );
+        }       
+        finally
+        {
+            if ( startedAction && !abortedAction )
+                this.endAction ( actionContext );
         }
     }
 
@@ -278,16 +358,43 @@ public class SnapshotRecordManager imple
         checkIfClosed();
         Object obj;
         ActionContext actionContext = actionContextVar.get();
-        assert( actionContext.isActive() == true );
+        
+        boolean startedAction = false;
+        boolean abortedAction = false;
+        if ( actionContext.isActive() == false )
+        {
+            actionContext = this.beginAction( false );
+            this.setCurrentActionContext( actionContext );
+            startedAction = true;
+        }
         
         try 
         {
            obj = versionedCache.get( new Long( recid ), actionContext.getVersion().getVersion(),
                serializer );       
         } 
+        catch ( IOException e )
+        {
+            if ( startedAction )
+            {
+                this.abortAction( actionContext );
+                abortedAction = true;
+            }
+            throw e;
+        }
         catch ( CacheEvictionException except ) 
         {
+            if ( startedAction )
+            {
+                this.abortAction( actionContext );
+                abortedAction = true;
+            }
             throw new IOException( except.getLocalizedMessage() );
+        }       
+        finally
+        {
+            if ( startedAction && !abortedAction )
+                this.endAction ( actionContext );
         }
         
         return obj;

Modified: directory/apacheds/branches/apacheds-jdbm/jdbm/src/test/java/jdbm/btree/SnapshotBTree.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-jdbm/jdbm/src/test/java/jdbm/btree/SnapshotBTree.java?rev=1162132&r1=1162131&r2=1162132&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-jdbm/jdbm/src/test/java/jdbm/btree/SnapshotBTree.java
(original)
+++ directory/apacheds/branches/apacheds-jdbm/jdbm/src/test/java/jdbm/btree/SnapshotBTree.java
Fri Aug 26 14:57:57 2011
@@ -1,4 +1,4 @@
-package jdbm.btree;
+    package jdbm.btree;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
@@ -108,6 +108,8 @@ public class SnapshotBTree
         {
             int count = 0;
             int idx;
+            boolean sawXXX = false;
+            
             TupleBrowser<Integer, String> browser = btree.browse();
             Tuple<Integer, String> tuple = new Tuple();
             browseSem.release();
@@ -128,24 +130,48 @@ public class SnapshotBTree
                 if ( (count & 7) == 0 )
                     Thread.sleep( 1 );
                 
-                assertTrue( !tuple.getValue().equals( "xxx" ) );
+                if ( !sawXXX && tuple.getValue().equals( "xxx" ) )
+                    sawXXX = true;
+                
+                if ( sawXXX )
+                {
+                    assertTrue( tuple.getValue().equals( "xxx" ) );
+                }
+                else
+                {
+                    assertTrue( tuple.getValue().equals( "value" + tuple.getKey().intValue()
) );
+                }                    
             }
             
             
             System.out.println( "count is " + count );
-            assertEquals( count, 1024 );            
+            assertTrue( count <= 2048 );            
             browser.close();
             
             updateSem.acquireUninterruptibly();
             browser = btree.browse( new Integer( 10 ) );
-            
+            assertTrue( browser.getNext( tuple ) );
+            assertEquals( tuple.getKey().intValue(), 20 );
+            browser = btree.browse( new Integer( 2047 ) );
+            assertTrue( browser.getNext( tuple ) );
+            assertTrue( tuple.getValue().equals("xxx") );
+            boolean sawNonXXX = false;
             browseSem.release();
-            for ( idx = 20; idx < 1024; idx++ )
+
+            while ( browser.getPrevious( tuple ) )
             {
-                assertTrue( browser.getNext( tuple ) );
                 
-                System.out.println( "key:"+ tuple.getKey().intValue() + " idx:" + idx );
-                assertTrue( tuple.getKey().intValue() == idx );
+                if ( !sawNonXXX && !tuple.getValue().equals( "xxx" ) )
+                    sawNonXXX = true;
+                
+                if ( sawNonXXX )
+                {
+                    assertTrue( tuple.getValue().equals( "value" + tuple.getKey().intValue()
) );
+                }
+                else 
+                {
+                    assertTrue( tuple.getValue().equals( "xxx") );
+                }
             }
             browser.close();
             
@@ -158,14 +184,16 @@ public class SnapshotBTree
             for ( idx = 0; idx < numReadThreads; idx++ )
                 browseSem.acquireUninterruptibly();
             
-            btree.insert( new Integer(1024), "xxx", true );
+            for ( idx = 512; idx < 1024; idx++ )
+            {
+                btree.insert( new Integer( idx ), "xxx", true );
+            }
+            
             for ( idx = 1024; idx < 2048; idx++ )
             {
-                btree.insert( new Integer( 0 ), "value" + idx, true );
+                btree.insert( new Integer( idx ), "xxx", true );
             }
            
-            btree.insert( new Integer(1), "xxx", true );
-            btree.insert( new Integer(1024), "xxx", true );
             for ( idx = 10; idx < 20; idx++ )
             {
                 btree.remove( new Integer( idx ) );



Mime
View raw message