You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2014/03/05 03:56:07 UTC

svn commit: r1574327 - in /directory/mavibot/branches/with-txns/mavibot/src: main/java/org/apache/directory/mavibot/btree/ test/java/org/apache/directory/mavibot/btree/

Author: elecharny
Date: Wed Mar  5 02:56:07 2014
New Revision: 1574327

URL: http://svn.apache.org/r1574327
Log:
o Fixed a failing tests when we are reopening a BTree, as we should not see the old revisions
o Added the EmptyCursor used when we have no element to return
o Fixed a NPE

Added:
    directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/EmptyTupleCursor.java
Modified:
    directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java
    directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/ReadTransaction.java
    directory/mavibot/branches/with-txns/mavibot/src/test/java/org/apache/directory/mavibot/btree/RecordManagerTest.java

Modified: directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java
URL: http://svn.apache.org/viewvc/directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java?rev=1574327&r1=1574326&r2=1574327&view=diff
==============================================================================
--- directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java (original)
+++ directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractBTree.java Wed Mar  5 02:56:07 2014
@@ -118,11 +118,20 @@ import org.apache.directory.mavibot.btre
      */
     protected ReadTransaction<K, V> beginReadTransaction( long revision )
     {
-        ReadTransaction<K, V> readTransaction = new ReadTransaction<K, V>( getBtreeHeader( revision ) );
+        BTreeHeader<K, V> btreeHeader = getBtreeHeader( revision );
 
-        readTransactions.add( readTransaction );
+        if ( btreeHeader != null )
+        {
+            ReadTransaction<K, V> readTransaction = new ReadTransaction<K, V>( btreeHeader );
+
+            readTransactions.add( readTransaction );
 
-        return readTransaction;
+            return readTransaction;
+        }
+        else
+        {
+            return null;
+        }
     }
 
 
@@ -151,12 +160,19 @@ import org.apache.directory.mavibot.btre
     {
         ReadTransaction<K, V> transaction = beginReadTransaction( revision );
 
-        ParentPos<K, V>[] stack = (ParentPos<K, V>[]) Array.newInstance( ParentPos.class, 32 );
+        if ( transaction == null )
+        {
+            return new EmptyTupleCursor<K, V>( revision );
+        }
+        else
+        {
+            ParentPos<K, V>[] stack = (ParentPos<K, V>[]) Array.newInstance( ParentPos.class, 32 );
 
-        // And get the cursor
-        TupleCursor<K, V> cursor = transaction.getRootPage().browse( transaction, stack, 0 );
+            // And get the cursor
+            TupleCursor<K, V> cursor = transaction.getRootPage().browse( transaction, stack, 0 );
 
-        return cursor;
+            return cursor;
+        }
     }
 
 

Added: directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/EmptyTupleCursor.java
URL: http://svn.apache.org/viewvc/directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/EmptyTupleCursor.java?rev=1574327&view=auto
==============================================================================
--- directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/EmptyTupleCursor.java (added)
+++ directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/EmptyTupleCursor.java Wed Mar  5 02:56:07 2014
@@ -0,0 +1,234 @@
+/*
+ *  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.NoSuchElementException;
+
+import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
+
+
+/**
+ * A Cursor which is used when we have no element to return
+ * <p>
+ *
+ * @param <K> The type for the Key
+ * @param <V> The type for the stored value
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class EmptyTupleCursor<K, V> extends TupleCursor<K, V>
+{
+    private long revision;
+    private long creationDate;
+
+    /**
+     * Creates a new instance of Cursor, starting on a page at a given position.
+     *
+     * @param transaction The transaction this operation is protected by
+     * @param stack The stack of parent's from root to this page
+     */
+    public EmptyTupleCursor( long revision )
+    {
+        super();
+
+        this.revision = revision;
+        creationDate = System.currentTimeMillis();
+    }
+
+
+    /**
+     * Change the position in the current cursor to set it after the last key
+     */
+    public void afterLast() throws IOException
+    {
+    }
+
+
+    /**
+     * Change the position in the current cursor before the first key
+     */
+    public void beforeFirst() throws IOException
+    {
+    }
+
+
+    /**
+     * Tells if the cursor can return a next element
+     *
+     * @return true if there are some more elements
+     * @throws IOException
+     * @throws EndOfFileExceededException
+     */
+    public boolean hasNext() throws EndOfFileExceededException, IOException
+    {
+        return false;
+    }
+
+
+    /**
+     * Find the next key/value
+     *
+     * @return A Tuple containing the found key and value
+     * @throws IOException
+     * @throws EndOfFileExceededException
+     */
+    public Tuple<K, V> next() throws EndOfFileExceededException, IOException
+    {
+        throw new NoSuchElementException( "No tuple present" );
+    }
+
+
+    /**
+     * Get the next non-duplicate key.
+     * If the BTree contains :
+     *
+     *  <ul>
+     *    <li><1,0></li>
+     *    <li><1,1></li>
+     *    <li><1,2></li>
+     *    <li><2,0></li>
+     *    <li><2,1></li>
+     *  </ul>
+     *
+     *  and cursor is present at <1,1> then the returned tuple will be <2,0> (not <1,2>)
+     *
+     * @return A Tuple containing the found key and value
+     * @throws EndOfFileExceededException
+     * @throws IOException
+     */
+    public Tuple<K, V> nextKey() throws EndOfFileExceededException, IOException
+    {
+        // This is the end : no more value
+        throw new NoSuchElementException( "No more tuples present" );
+    }
+
+
+    /**
+     * Tells if the cursor can return a next key
+     *
+     * @return true if there are some more keys
+     * @throws IOException
+     * @throws EndOfFileExceededException
+     */
+    public boolean hasNextKey() throws EndOfFileExceededException, IOException
+    {
+        return false;
+    }
+
+
+    /**
+     * Tells if the cursor can return a previous element
+     *
+     * @return true if there are some more elements
+     * @throws IOException
+     * @throws EndOfFileExceededException
+     */
+    public boolean hasPrev() throws EndOfFileExceededException, IOException
+    {
+        return false;
+    }
+
+
+    /**
+     * Find the previous key/value
+     *
+     * @return A Tuple containing the found key and value
+     * @throws IOException
+     * @throws EndOfFileExceededException
+     */
+    public Tuple<K, V> prev() throws EndOfFileExceededException, IOException
+    {
+        throw new NoSuchElementException( "No more tuple present" );
+    }
+
+
+    /**
+     * Get the previous non-duplicate key.
+     * If the BTree contains :
+     *
+     *  <ul>
+     *    <li><1,0></li>
+     *    <li><1,1></li>
+     *    <li><1,2></li>
+     *    <li><2,0></li>
+     *    <li><2,1></li>
+     *  </ul>
+     *
+     *  and cursor is present at <2,1> then the returned tuple will be <1,0> (not <2,0>)
+     *
+     * @return A Tuple containing the found key and value
+     * @throws EndOfFileExceededException
+     * @throws IOException
+     */
+    public Tuple<K, V> prevKey() throws EndOfFileExceededException, IOException
+    {
+        throw new NoSuchElementException( "No more tuples present" );
+    }
+
+
+    /**
+     * Tells if the cursor can return a previous key
+     *
+     * @return true if there are some more keys
+     * @throws IOException
+     * @throws EndOfFileExceededException
+     */
+    public boolean hasPrevKey() throws EndOfFileExceededException, IOException
+    {
+        return false;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void close()
+    {
+    }
+
+
+    /**
+     * Get the creation date
+     * @return The creation date for this cursor
+     */
+    public long getCreationDate()
+    {
+        return creationDate;
+    }
+
+
+    /**
+     * Get the current revision
+     *
+     * @return The revision this cursor is based on
+     */
+    public long getRevision()
+    {
+        return revision;
+    }
+
+
+    public String toString()
+    {
+        return "EmptyTupleCursor";
+    }
+}

Modified: directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/ReadTransaction.java
URL: http://svn.apache.org/viewvc/directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/ReadTransaction.java?rev=1574327&r1=1574326&r2=1574327&view=diff
==============================================================================
--- directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/ReadTransaction.java (original)
+++ directory/mavibot/branches/with-txns/mavibot/src/main/java/org/apache/directory/mavibot/btree/ReadTransaction.java Wed Mar  5 02:56:07 2014
@@ -66,11 +66,14 @@ public class ReadTransaction<K, V>
      */
     public ReadTransaction( BTreeHeader<K, V> btreeHeader )
     {
-        this.revision = btreeHeader.getRevision();
-        this.creationDate = System.currentTimeMillis();
-        this.rootPage = btreeHeader.getRootPage();
-        this.btreeHeader = btreeHeader;
-        closed = false;
+        if ( btreeHeader != null )
+        {
+            this.revision = btreeHeader.getRevision();
+            this.creationDate = System.currentTimeMillis();
+            this.rootPage = btreeHeader.getRootPage();
+            this.btreeHeader = btreeHeader;
+            closed = false;
+        }
     }
 
 

Modified: directory/mavibot/branches/with-txns/mavibot/src/test/java/org/apache/directory/mavibot/btree/RecordManagerTest.java
URL: http://svn.apache.org/viewvc/directory/mavibot/branches/with-txns/mavibot/src/test/java/org/apache/directory/mavibot/btree/RecordManagerTest.java?rev=1574327&r1=1574326&r2=1574327&view=diff
==============================================================================
--- directory/mavibot/branches/with-txns/mavibot/src/test/java/org/apache/directory/mavibot/btree/RecordManagerTest.java (original)
+++ directory/mavibot/branches/with-txns/mavibot/src/test/java/org/apache/directory/mavibot/btree/RecordManagerTest.java Wed Mar  5 02:56:07 2014
@@ -454,10 +454,10 @@ public class RecordManagerTest
 
         // Check that we can read the revision again
         // revision 1
-        checkBTreeRevisionBrowse( btree, rev1, 3L );
+        checkBTreeRevisionBrowse( btree, rev1 );
 
         // Revision 2
-        checkBTreeRevisionBrowse( btree, rev2, 1L, 3L );
+        checkBTreeRevisionBrowse( btree, rev2 );
 
         // Revision 3
         checkBTreeRevisionBrowse( btree, rev3, 1L, 3L, 5L );