directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r761491 - in /directory/apacheds/branches/ldif-partition/avl-partition/src: main/java/org/apache/directory/server/core/partition/avl/ test/java/org/apache/directory/server/core/partition/avl/
Date Fri, 03 Apr 2009 00:31:21 GMT
Author: akarasulu
Date: Fri Apr  3 00:31:21 2009
New Revision: 761491

URL: http://svn.apache.org/viewvc?rev=761491&view=rev
Log:
found a few bugs and fixed them while starting to test Table

Added:
    directory/apacheds/branches/ldif-partition/avl-partition/src/test/java/org/apache/directory/server/core/partition/avl/TableData.java
Modified:
    directory/apacheds/branches/ldif-partition/avl-partition/src/main/java/org/apache/directory/server/core/partition/avl/AvlTable.java
    directory/apacheds/branches/ldif-partition/avl-partition/src/main/java/org/apache/directory/server/core/partition/avl/AvlTableDupsCursor.java
    directory/apacheds/branches/ldif-partition/avl-partition/src/test/java/org/apache/directory/server/core/partition/avl/AvlTableTest.java

Modified: directory/apacheds/branches/ldif-partition/avl-partition/src/main/java/org/apache/directory/server/core/partition/avl/AvlTable.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/ldif-partition/avl-partition/src/main/java/org/apache/directory/server/core/partition/avl/AvlTable.java?rev=761491&r1=761490&r2=761491&view=diff
==============================================================================
--- directory/apacheds/branches/ldif-partition/avl-partition/src/main/java/org/apache/directory/server/core/partition/avl/AvlTable.java
(original)
+++ directory/apacheds/branches/ldif-partition/avl-partition/src/main/java/org/apache/directory/server/core/partition/avl/AvlTable.java
Fri Apr  3 00:31:21 2009
@@ -26,7 +26,8 @@
 import org.apache.directory.server.core.avltree.AvlTreeCursor;
 import org.apache.directory.server.core.avltree.AvlTreeMap;
 import org.apache.directory.server.core.avltree.AvlTreeMapImpl;
-import org.apache.directory.server.core.avltree.AvlTreeMapNoDupsCursor;
+import org.apache.directory.server.core.avltree.AvlSingletonOrOrderedSetCursor;
+import org.apache.directory.server.core.avltree.AvlTreeMapNoDupsWrapperCursor;
 import org.apache.directory.server.core.avltree.KeyTupleAvlCursor;
 import org.apache.directory.server.core.avltree.LinkedAvlMapNode;
 import org.apache.directory.server.core.avltree.SingletonOrOrderedSet;
@@ -366,7 +367,7 @@
     {
         if ( ! avl.isDupsAllowed() )
         {
-            return new AvlTreeMapNoDupsCursor<K,V>( avl );
+            return new AvlTreeMapNoDupsWrapperCursor<K, V>( new AvlSingletonOrOrderedSetCursor<K,V>(
avl ) );
         }
 
         return new AvlTableDupsCursor<K, V>( this );

Modified: directory/apacheds/branches/ldif-partition/avl-partition/src/main/java/org/apache/directory/server/core/partition/avl/AvlTableDupsCursor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/ldif-partition/avl-partition/src/main/java/org/apache/directory/server/core/partition/avl/AvlTableDupsCursor.java?rev=761491&r1=761490&r2=761491&view=diff
==============================================================================
--- directory/apacheds/branches/ldif-partition/avl-partition/src/main/java/org/apache/directory/server/core/partition/avl/AvlTableDupsCursor.java
(original)
+++ directory/apacheds/branches/ldif-partition/avl-partition/src/main/java/org/apache/directory/server/core/partition/avl/AvlTableDupsCursor.java
Fri Apr  3 00:31:21 2009
@@ -22,7 +22,8 @@
 
 import org.apache.directory.server.core.avltree.AvlTree;
 import org.apache.directory.server.core.avltree.AvlTreeCursor;
-import org.apache.directory.server.core.avltree.AvlTreeMapNoDupsCursor;
+import org.apache.directory.server.core.avltree.AvlSingletonOrOrderedSetCursor;
+import org.apache.directory.server.core.avltree.SingletonOrOrderedSet;
 import org.apache.directory.server.core.cursor.Cursor;
 import org.apache.directory.server.core.cursor.InvalidCursorPositionException;
 import org.apache.directory.server.core.cursor.SingletonCursor;
@@ -52,7 +53,7 @@
      * The underlying wrapped cursor which returns Tuples whose values are
      * either V objects or AvlTree objects.
      */
-    private final TupleCursor<K,V> wrappedCursor;
+    private final TupleCursor<K,SingletonOrOrderedSet<V>> wrappedCursor;
     
     /**
      * A Cursor over a set of value objects for the current key held in the
@@ -64,7 +65,7 @@
     private Cursor<V> dupsCursor;
 
     /** The current Tuple returned from the wrapped cursor. */
-    private final Tuple<K,V> wrappedTuple = new Tuple<K, V>();
+    private final Tuple<K,SingletonOrOrderedSet<V>> wrappedTuple = new Tuple<K,
SingletonOrOrderedSet<V>>();
 
     /**
      * The Tuple that is used to return values via the get() method. This
@@ -85,7 +86,7 @@
     public AvlTableDupsCursor( AvlTable<K,V> table )
     {
         this.table = table;
-        this.wrappedCursor = new AvlTreeMapNoDupsCursor<K, V>( table.getAvlTreeMap()
);
+        this.wrappedCursor = new AvlSingletonOrOrderedSetCursor<K, V>( table.getAvlTreeMap()
);
         LOG.debug( "Created on table {}", table.getName() );
     }
 
@@ -111,24 +112,23 @@
     /**
      * {@inheritDoc}
      */
-    @SuppressWarnings("unchecked")
     public void beforeValue( K key, V value ) throws Exception
     {
         checkNotClosed( "beforeValue()" );
-        wrappedCursor.before( new Tuple<K,V>( key, null ) );
+        wrappedCursor.before( new Tuple<K, SingletonOrOrderedSet<V>>( key, null
) );
         
         if ( wrappedCursor.next() )
         {
             wrappedTuple.setBoth( wrappedCursor.get() );
             
-            if ( wrappedTuple.getValue() instanceof AvlTree )
+            if ( wrappedTuple.getValue().isOrderedSet() )
             {
-                AvlTree<V> avlTree = ( AvlTree<V> ) wrappedTuple.getValue();
+                AvlTree<V> avlTree = wrappedTuple.getValue().getOrderedSet();
                 dupsCursor = new AvlTreeCursor<V>( avlTree );
             }
             else
             {
-                dupsCursor = new SingletonCursor<V>( wrappedTuple.getValue() );
+                dupsCursor = new SingletonCursor<V>( wrappedTuple.getValue().getSingleton()
);
             }
             
             if ( value == null )
@@ -168,7 +168,6 @@
     /**
      * {@inheritDoc}
      */
-    @SuppressWarnings("unchecked")
     public void afterValue( K key, V value ) throws Exception
     {
         checkNotClosed( "afterValue()" );
@@ -195,26 +194,26 @@
 
         if ( value == null )
         {
-            wrappedCursor.after( new Tuple<K,V>( key, null ) );
+            wrappedCursor.after( new Tuple<K, SingletonOrOrderedSet<V>>( key,
null ) );
         }
         else
         {
-            wrappedCursor.before( new Tuple<K,V>( key, null ) );
+            wrappedCursor.before( new Tuple<K, SingletonOrOrderedSet<V>>( key,
null ) );
         }
 
         if ( wrappedCursor.next() )
         {
             wrappedTuple.setBoth( wrappedCursor.get() );
-            V values = wrappedTuple.getValue();
+            SingletonOrOrderedSet<V> values = wrappedTuple.getValue();
 
-            if ( values instanceof AvlTree )
+            if ( values.isOrderedSet() )
             {
-                AvlTree<V> set = ( AvlTree<V> ) values;
+                AvlTree<V> set = values.getOrderedSet();
                 dupsCursor = new AvlTreeCursor<V>( set );
             }
             else
             {
-                dupsCursor = new SingletonCursor<V>( values );
+                dupsCursor = new SingletonCursor<V>( values.getSingleton() );
             }
 
             if ( value == null )
@@ -286,7 +285,6 @@
     /**
      * {@inheritDoc}
      */
-    @SuppressWarnings("unchecked")
     public boolean first() throws Exception
     {
         checkNotClosed( "first()" );
@@ -296,15 +294,15 @@
         if ( wrappedCursor.first() )
         {
             wrappedTuple.setBoth( wrappedCursor.get() );
-            V values = wrappedTuple.getValue();
+            SingletonOrOrderedSet<V> values = wrappedTuple.getValue();
 
-            if ( values instanceof AvlTree )
+            if ( values.isOrderedSet() )
             {
-                dupsCursor = new AvlTreeCursor<V>( ( AvlTree<V> ) values );
+                dupsCursor = new AvlTreeCursor<V>( values.getOrderedSet() );
             }
             else
             {
-                dupsCursor = new SingletonCursor<V>( values );
+                dupsCursor = new SingletonCursor<V>( values.getSingleton() );
             }
 
             /*
@@ -351,7 +349,6 @@
     /**
      * {@inheritDoc}
      */
-    @SuppressWarnings("unchecked")
     public boolean last() throws Exception
     {
         checkNotClosed( "last()" );
@@ -361,15 +358,15 @@
         if ( wrappedCursor.last() )
         {
             wrappedTuple.setBoth( wrappedCursor.get() );
-            V values = wrappedTuple.getValue();
+            SingletonOrOrderedSet<V> values = wrappedTuple.getValue();
 
-            if ( values instanceof AvlTree )
+            if ( values.isOrderedSet() )
             {
-                dupsCursor = new AvlTreeCursor<V>( ( AvlTree<V> ) values );
+                dupsCursor = new AvlTreeCursor<V>( values.getOrderedSet() );
             }
             else
             {
-                dupsCursor = new SingletonCursor<V>( values );
+                dupsCursor = new SingletonCursor<V>( values.getSingleton() );
             }
 
             /*
@@ -391,7 +388,6 @@
     /**
      * {@inheritDoc}
      */
-    @SuppressWarnings("unchecked")
     public boolean next() throws Exception
     {
         checkNotClosed( "next()" );
@@ -408,15 +404,15 @@
             if ( wrappedCursor.next() )
             {
                 wrappedTuple.setBoth( wrappedCursor.get() );
-                V values = wrappedTuple.getValue();
+                SingletonOrOrderedSet<V> values = wrappedTuple.getValue();
 
-                if ( values instanceof AvlTree )
+                if ( values.isOrderedSet())
                 {
-                    dupsCursor = new AvlTreeCursor<V>( ( AvlTree<V> ) values
);
+                    dupsCursor = new AvlTreeCursor<V>( values.getOrderedSet() );
                 }
                 else
                 {
-                    dupsCursor = new SingletonCursor<V>( values );
+                    dupsCursor = new SingletonCursor<V>( values.getSingleton() );
                 }
 
                 /*
@@ -451,7 +447,6 @@
     /**
      * {@inheritDoc}
      */
-    @SuppressWarnings("unchecked")
     public boolean previous() throws Exception
     {
         checkNotClosed( "previous()" );
@@ -469,15 +464,15 @@
             if ( wrappedCursor.previous() )
             {
                 wrappedTuple.setBoth( wrappedCursor.get() );
-                V values = wrappedTuple.getValue();
+                SingletonOrOrderedSet<V> values = wrappedTuple.getValue();
 
-                if ( values instanceof AvlTree )
+                if ( values.isOrderedSet() )
                 {
-                    dupsCursor = new AvlTreeCursor<V>( ( AvlTree<V> ) values
);
+                    dupsCursor = new AvlTreeCursor<V>( values.getOrderedSet() );
                 }
                 else
                 {
-                    dupsCursor = new SingletonCursor<V>( values );
+                    dupsCursor = new SingletonCursor<V>( values.getSingleton() );
                 }
 
                 /*

Modified: directory/apacheds/branches/ldif-partition/avl-partition/src/test/java/org/apache/directory/server/core/partition/avl/AvlTableTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/ldif-partition/avl-partition/src/test/java/org/apache/directory/server/core/partition/avl/AvlTableTest.java?rev=761491&r1=761490&r2=761491&view=diff
==============================================================================
--- directory/apacheds/branches/ldif-partition/avl-partition/src/test/java/org/apache/directory/server/core/partition/avl/AvlTableTest.java
(original)
+++ directory/apacheds/branches/ldif-partition/avl-partition/src/test/java/org/apache/directory/server/core/partition/avl/AvlTableTest.java
Fri Apr  3 00:31:21 2009
@@ -22,9 +22,12 @@
 
 import java.util.Comparator;
 
-import org.junit.Ignore;
+import org.apache.directory.server.core.cursor.Cursor;
+import org.apache.directory.server.xdbm.Tuple;
+import org.junit.Before;
 
 import junit.framework.TestCase;
+import static org.apache.directory.server.core.partition.avl.TableData.*;
 
 
 /**
@@ -35,40 +38,193 @@
  */
 public class AvlTableTest extends TestCase
 {
+    private AvlTable<Integer,Integer> dups;
+    private AvlTable<Integer,Integer> nodups;
+    
+    
+    @Before
+    public void setUp()
+    {
+        IntComparator comparator = new IntComparator();
+        dups = new AvlTable<Integer,Integer>( "dups", comparator, comparator, true
);
+        nodups = new AvlTable<Integer,Integer>( "nodups", comparator, comparator, false
);
+    }
+    
+    
+    public void testGetName()
+    {
+        assertEquals( "dups", dups.getName() );
+        assertEquals( "nodups", nodups.getName() );
+    }
+    
+    
+    public void testCursor() throws Exception
+    {
+        injectNoDupsData( nodups );
+        Cursor<Tuple<Integer, Integer>> cursor = nodups.cursor();
+        
+        // position at first element (0,3)
+        assertTrue( cursor.first() );
+        Tuple<Integer, Integer> tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 0, tuple.getKey().intValue() );
+        assertEquals( 3, tuple.getValue().intValue() );
+        
+        // move to next element (1,2)
+        assertTrue( cursor.next() );
+        tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 1, tuple.getKey().intValue() );
+        assertEquals( 2, tuple.getValue().intValue() );
+        
+        // move to next element (2,1)
+        assertTrue( cursor.next() );
+        tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 2, tuple.getKey().intValue() );
+        assertEquals( 1, tuple.getValue().intValue() );
+        
+        // move to next element (3,0)
+        assertTrue( cursor.next() );
+        tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 3, tuple.getKey().intValue() );
+        assertEquals( 0, tuple.getValue().intValue() );
+        
+        // move to next element (23,8934)
+        assertTrue( cursor.next() );
+        tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 23, tuple.getKey().intValue() );
+        assertEquals( 8934, tuple.getValue().intValue() );
+        
+        assertFalse( cursor.next() );
+        cursor.close();
+        
+        // work with duplicates now
+        
+        injectDupsData( dups );
+        cursor = dups.cursor();
+        
+        // position at first element (0,3)
+        assertTrue( cursor.first() );
+        tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 0, tuple.getKey().intValue() );
+        assertEquals( 3, tuple.getValue().intValue() );
+        
+        // move to next element (1,2)
+        assertTrue( cursor.next() );
+        tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 1, tuple.getKey().intValue() );
+        assertEquals( 2, tuple.getValue().intValue() );
+        
+        // move to next element (1,4)
+        assertTrue( cursor.next() );
+        tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 1, tuple.getKey().intValue() );
+        assertEquals( 4, tuple.getValue().intValue() );
+        
+        // move to next element (1,6)
+        assertTrue( cursor.next() );
+        tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 1, tuple.getKey().intValue() );
+        assertEquals( 6, tuple.getValue().intValue() );
+        
+        // move to next element (2,1)
+        assertTrue( cursor.next() );
+        tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 2, tuple.getKey().intValue() );
+        assertEquals( 1, tuple.getValue().intValue() );
+        
+        // move to next element (3,0)
+        assertTrue( cursor.next() );
+        tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 3, tuple.getKey().intValue() );
+        assertEquals( 0, tuple.getValue().intValue() );
+        
+        // move to next element (3,8)
+        assertTrue( cursor.next() );
+        tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 3, tuple.getKey().intValue() );
+        assertEquals( 8, tuple.getValue().intValue() );
+        
+        // move to next element (3,9)
+        assertTrue( cursor.next() );
+        tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 3, tuple.getKey().intValue() );
+        assertEquals( 9, tuple.getValue().intValue() );
+        
+        // move to next element (3,10)
+        assertTrue( cursor.next() );
+        tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 3, tuple.getKey().intValue() );
+        assertEquals( 10, tuple.getValue().intValue() );
+        
+        // move to next element (23,8934)
+        assertTrue( cursor.next() );
+        tuple = cursor.get();
+        assertNotNull( tuple );
+        assertEquals( 23, tuple.getKey().intValue() );
+        assertEquals( 8934, tuple.getValue().intValue() );
+        
+        assertFalse( cursor.next() );
+        cursor.close();
+    }
+    
+    
     /**
-     * Tests the put() and get() methods on an AvlTable with duplicate keys 
-     * disabled.
+     * Tests the put() and get() methods on an AvlTable.
      */
-    @Ignore ( "Test is failing due to failure of AvlTreeMap.insert to replace a tuple." )
-    public void testPutNoDups() throws Exception
+    public void testPutGetCount() throws Exception
     {
-        AvlTable<Integer,Integer> table = new AvlTable<Integer,Integer>( 
-            "test", new IntComparator(), new IntComparator(), false );
-
         // ---------------------------------------------------------
         // normal operation 
         // ---------------------------------------------------------
+
+        injectNoDupsData( nodups );
+        
+        assertEquals( 5, nodups.count() );
         
-        table.put( 0, 3 );
-        table.put( 1, 2 );
-        table.put( 2, 1 );
-        table.put( 3, 0 );
-        table.put( 23, 8934 );
-        
-        assertEquals( 3, table.get( 0 ).intValue() );
-        assertEquals( 2, table.get( 1 ).intValue() );
-        assertEquals( 1, table.get( 2 ).intValue() );
-        assertEquals( 0, table.get( 3 ).intValue() );
-        assertEquals( 8934, table.get( 23 ).intValue() );
+        assertEquals( 3, nodups.get( 0 ).intValue() );
+        assertEquals( 2, nodups.get( 1 ).intValue() );
+        assertEquals( 1, nodups.get( 2 ).intValue() );
+        assertEquals( 0, nodups.get( 3 ).intValue() );
+        assertEquals( 8934, nodups.get( 23 ).intValue() );
 
         // ---------------------------------------------------------
         // try adding duplicates when not supported
         // ---------------------------------------------------------
         
-        table.put( 23, 34 );
-        assertEquals( 34, table.get( 23 ).intValue() );
+        nodups.put( 23, 34 );
+        assertEquals( 34, nodups.get( 23 ).intValue() );
+        assertEquals( 5, nodups.count() );
+
+        // ---------------------------------------------------------
+        // now with duplicates
+        // ---------------------------------------------------------
+        
+        assertEquals( 0, dups.count() );
+        
+        injectDupsData( dups );
+        
+        // [3,0] was put twice so only 10 of 11 should have been put in
+        assertEquals( 10, dups.count() );
+        
+        assertEquals( 3, dups.get( 0 ).intValue() );
+        assertEquals( 2, dups.get( 1 ).intValue() );
+        assertEquals( 1, dups.get( 2 ).intValue() );
+        assertEquals( 0, dups.get( 3 ).intValue() );
     }
-    
+        
     
     class IntComparator implements Comparator<Integer>
     {

Added: directory/apacheds/branches/ldif-partition/avl-partition/src/test/java/org/apache/directory/server/core/partition/avl/TableData.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/ldif-partition/avl-partition/src/test/java/org/apache/directory/server/core/partition/avl/TableData.java?rev=761491&view=auto
==============================================================================
--- directory/apacheds/branches/ldif-partition/avl-partition/src/test/java/org/apache/directory/server/core/partition/avl/TableData.java
(added)
+++ directory/apacheds/branches/ldif-partition/avl-partition/src/test/java/org/apache/directory/server/core/partition/avl/TableData.java
Fri Apr  3 00:31:21 2009
@@ -0,0 +1,75 @@
+/*
+ *   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.server.core.partition.avl;
+
+
+import org.apache.directory.server.xdbm.Table;
+
+
+/**
+ * Some data for a table.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class TableData
+{
+    /**
+     * Used to inject data into a Table not supporting duplicates.
+     *
+     * @return
+     */
+    public static void injectNoDupsData( Table<Integer,Integer> table ) throws Exception
+    {
+        if ( table.isDupsEnabled() )
+        {
+            throw new IllegalArgumentException( "table must NOT support duplicates" );
+        }
+        
+        table.put( 0 , 3 );
+        table.put( 1 , 2 );
+        table.put( 2 , 0 ); // overwritten
+        table.put( 2 , 1 );
+        table.put( 2 , 1 ); // has no effect
+        table.put( 3 , 0 );
+        table.put( 23, 8934 );
+    }
+
+
+    public static void injectDupsData( Table<Integer,Integer> table ) throws Exception
+    {
+        if ( ! table.isDupsEnabled() )
+        {
+            throw new IllegalArgumentException( "table MUST support duplicates" );
+        }
+        
+        table.put( 0 , 3 ); // 1 
+        table.put( 1 , 2 ); // 2 - 3 duplicates
+        table.put( 1 , 4 ); // 3
+        table.put( 1 , 6 ); // 4
+        table.put( 2 , 1 ); // 5
+        table.put( 3 , 0 ); // 6
+        table.put( 3 , 0 ); // has no effect
+        table.put( 3 , 8 ); // 7
+        table.put( 3 , 9 ); // 8
+        table.put( 3 , 10 ); // 9
+        table.put( 23, 8934 ); // 10
+    }
+}



Mime
View raw message