directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r607005 - in /directory/sandbox/akarasulu/bigbang/apacheds/jdbm-cursor/src: main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/cursor/keyonly/ test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/cursor/key...
Date Thu, 27 Dec 2007 06:13:42 GMT
Author: akarasulu
Date: Wed Dec 26 22:13:40 2007
New Revision: 607005

URL: http://svn.apache.org/viewvc?rev=607005&view=rev
Log:
testing new version of Cursor interface with KeyCursor

Modified:
    directory/sandbox/akarasulu/bigbang/apacheds/jdbm-cursor/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/cursor/keyonly/KeyCursor.java
    directory/sandbox/akarasulu/bigbang/apacheds/jdbm-cursor/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/cursor/keyonly/KeyCursorTest.java

Modified: directory/sandbox/akarasulu/bigbang/apacheds/jdbm-cursor/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/cursor/keyonly/KeyCursor.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/jdbm-cursor/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/cursor/keyonly/KeyCursor.java?rev=607005&r1=607004&r2=607005&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/jdbm-cursor/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/cursor/keyonly/KeyCursor.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/jdbm-cursor/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/cursor/keyonly/KeyCursor.java
Wed Dec 26 22:13:40 2007
@@ -24,11 +24,12 @@
 import jdbm.helper.TupleBrowser;
 
 import org.apache.directory.server.core.cursor.AbstractCursor;
-import org.apache.directory.shared.ldap.NotImplementedException;
+import org.apache.directory.server.core.cursor.InvalidCursorPositionException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
+import java.util.Comparator;
 
 
 /**
@@ -43,75 +44,152 @@
     private static final Logger LOG = LoggerFactory.getLogger( KeyCursor.class );
     private final Tuple tuple = new Tuple();
 
-    private BTree btree;
+    private final BTree btree;
+    private final Comparator<E> comparator;
+    private boolean valueAvailable;
     private TupleBrowser browser;
 
 
     /**
      * Creates a Cursor over the keys of a JDBM BTree.
      *
-     * @param btree the JDBM BTree
+     * @param btree the JDBM BTree to build a Cursor over
+     * @param comparator the Comparator used to determine key ordering
      * @throws IOException of there are problems accessing the BTree
      */
-    KeyCursor( BTree btree ) throws IOException
+    public KeyCursor( BTree btree, Comparator<E> comparator ) throws IOException
     {
         this.btree = btree;
+        this.comparator = comparator;
     }
 
 
-    public void before( E element ) throws IOException
+    private void clearValue()
     {
-        browser = btree.browse( element );
         tuple.setKey( null );
         tuple.setValue( null );
+        valueAvailable = false;
+    }
+
+
+    public void before( E element ) throws IOException
+    {
+        browser = btree.browse( element );
+        clearValue();
     }
 
 
     public void after( E element ) throws IOException
     {
-        throw new NotImplementedException();
+        browser = btree.browse( element );
+
+        /*
+         * While the next value is less than or equal to the element keep
+         * advancing forward to the next item.  If we cannot advance any
+         * further then stop and return.  If we find a value greater than
+         * the element then we stop, backup, and return so subsequent calls
+         * to getNext() will return a value greater than the element.
+         */
+        while ( browser.getNext( tuple ) )
+        {
+            //noinspection unchecked
+            E next = ( E ) tuple.getKey();
+            int nextCompared = comparator.compare( next, element );
+
+            if ( nextCompared <= 0 )
+            {
+                // just continue
+            }
+            else if ( nextCompared > 0 )
+            {
+                /*
+                 * If we just have values greater than the element argument
+                 * then we are before the first element and cannot backup, and
+                 * the call below to getPrevious() will fail.  In this special
+                 * case we just reset the Cursor's browser and return.
+                 */
+                if ( browser.getPrevious( tuple ) )
+                {
+                }
+                else
+                {
+                    browser = btree.browse( element );
+                }
+
+                clearValue();
+                return;
+            }
+        }
+
+        clearValue();
+        // just return
     }
 
 
     public void beforeFirst() throws IOException
     {
-        throw new NotImplementedException();
+        browser = btree.browse();
+        clearValue();
     }
 
 
     public void afterLast() throws IOException
     {
-        throw new NotImplementedException();
+        browser = btree.browse( null );
     }
 
 
     public boolean first() throws IOException
     {
-        throw new NotImplementedException();
+        beforeFirst();
+        return next();
     }
 
 
     public boolean last() throws IOException
     {
-        throw new NotImplementedException();
+        afterLast();
+        return previous();
     }
 
 
     public boolean previous() throws IOException
     {
-        throw new NotImplementedException();
+        if ( browser.getPrevious( tuple ) )
+        {
+            return valueAvailable = true;
+        }
+        else
+        {
+            clearValue();
+            return false;
+        }
     }
 
 
     public boolean next() throws IOException
     {
-        throw new NotImplementedException();
+        if ( browser.getNext( tuple ) )
+        {
+            return valueAvailable = true;
+        }
+        else
+        {
+            clearValue();
+            return false;
+        }
     }
 
 
     public E get() throws IOException
     {
-        throw new NotImplementedException();
+        if ( valueAvailable )
+        {
+            //noinspection unchecked
+            return ( E ) tuple.getKey();
+        }
+
+        throw new InvalidCursorPositionException();
     }
 
 

Modified: directory/sandbox/akarasulu/bigbang/apacheds/jdbm-cursor/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/cursor/keyonly/KeyCursorTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/jdbm-cursor/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/cursor/keyonly/KeyCursorTest.java?rev=607005&r1=607004&r2=607005&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/jdbm-cursor/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/cursor/keyonly/KeyCursorTest.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/jdbm-cursor/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/cursor/keyonly/KeyCursorTest.java
Wed Dec 26 22:13:40 2007
@@ -27,8 +27,6 @@
 import static org.junit.Assert.fail;
 import static org.junit.Assert.assertNull;
 
-import org.apache.directory.server.core.cursor.Cursor;
-import org.apache.directory.server.core.partition.impl.btree.jdbm.cursor.keyonly.KeyCursor;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -60,7 +58,7 @@
     File dbFile;
     RecordManager recman;
     BTree bt;
-    Comparator comparator;
+    Comparator<String> comparator;
 
     KeyCursor<String> cursor;
 
@@ -91,7 +89,8 @@
         bt.insert( "8", EMPTY_BYTES, true );
         bt.insert( "9", EMPTY_BYTES, true );
 
-        cursor = new KeyCursor<String>( bt );
+        cursor = new KeyCursor<String>( bt, comparator );
+        LOG.debug( "Created new KeyCursor and populated it's btree" );
     }
 
 
@@ -106,7 +105,88 @@
 
 
     @Test
-    public void testJdbmBehaviorBrowse() throws Exception
+    public void testAfter() throws IOException
+    {
+        // test initial setup, advances after, and before inside elements
+        assertInvalidCursor();
+
+        cursor.after( "5" );
+        assertInvalidCursor();
+        assertTrue( cursor.next() );
+        assertEquals( "6", cursor.get() );
+
+        cursor.before( "2" );
+        assertInvalidCursor();
+        assertTrue( cursor.next() );
+        assertEquals( "2", cursor.get() );
+
+        // test advances up to and past the tail end
+        cursor.after( "9" );
+        assertInvalidCursor();
+        assertFalse( cursor.next() );
+        assertTrue( cursor.previous() );
+        assertEquals( "9", cursor.get() );
+
+        cursor.after( "a" );
+        assertInvalidCursor();
+        assertFalse( cursor.next() );
+        assertTrue( cursor.previous() );
+        assertEquals( "9", cursor.get() );
+
+        cursor.before( "a" );
+        assertInvalidCursor();
+        assertFalse( cursor.next() );
+        assertTrue( cursor.previous() );
+        assertEquals( "9", cursor.get() );
+
+        // test advances up to and past the head
+        cursor.before( "0" );
+        assertInvalidCursor();
+        assertFalse( cursor.previous() );
+        assertTrue( cursor.next() );
+        assertEquals( "0", cursor.get() );
+
+        cursor.after( "*" );
+        assertInvalidCursor();
+        assertFalse( cursor.previous() );
+        assertTrue( cursor.next() );
+        assertEquals( "0", cursor.get() );
+
+        cursor.before( "*" );
+        assertInvalidCursor();
+        assertFalse( cursor.previous() );
+        assertTrue( cursor.next() );
+        assertEquals( "0", cursor.get() );
+
+        bt.remove( "0" );
+        bt.remove( "1" );
+        bt.remove( "2" );
+        bt.remove( "3" );
+        bt.remove( "4" );
+        bt.remove( "6" );
+        bt.remove( "7" );
+        bt.remove( "8" );
+        bt.remove( "9" );
+
+        // now test with only one element: "5" remains now with others deleted
+        cursor.before( "5" );
+        assertInvalidCursor();
+        assertFalse( cursor.previous() );
+        assertTrue( cursor.next() );
+        assertEquals( "5", cursor.get() );
+        assertFalse( cursor.next() );
+
+        cursor.after( "5" );
+        assertInvalidCursor();
+        assertFalse( cursor.next() );
+        assertTrue( cursor.previous() );
+        assertEquals( "5", cursor.get() );
+        assertFalse( cursor.previous() );
+    }
+
+
+    @Test
+    public void testJdbmBrowse() throws Exception
     {
         bt.remove( "0" );
         bt.remove( "5" );
@@ -162,7 +242,7 @@
     }
 
 
-    private void assertInvalid( Cursor<String> cursor )
+    private void assertInvalidCursor()
     {
         try
         {



Mime
View raw message