directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r635669 - in /directory/sandbox/akarasulu/bigbang/apacheds: core-avl/src/main/java/org/apache/directory/server/core/avltree/ core-avl/src/test/java/org/apache/directory/server/core/avltree/ jdbm-store/src/main/java/org/apache/directory/serv...
Date Mon, 10 Mar 2008 19:45:46 GMT
Author: akarasulu
Date: Mon Mar 10 12:45:38 2008
New Revision: 635669

URL: http://svn.apache.org/viewvc?rev=635669&view=rev
Log:
DIRSERVER-114: applying patch from Kiran Ayyagari

Modified:
    directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTree.java
    directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTreeMarshaller.java
    directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/Marshaller.java
    directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeMarshallerTest.java
    directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreePerfTest.java
    directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeTest.java
    directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/IntegerKeyMarshaller.java
    directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmDupsCursor.java
    directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java
    directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmDupsCursorTest.java
    directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTableWithDuplicatesTest.java

Modified: directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTree.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTree.java?rev=635669&r1=635668&r2=635669&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTree.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTree.java
Mon Mar 10 12:45:38 2008
@@ -69,7 +69,7 @@
 	 * @param key the item to be inserted.<br> 
 	 * Note: Ignores if a node with the given key already exists.
 	 */
-	public void insert( K key )
+	public K insert( K key )
 	{
 	    LinkedAvlNode<K> node, temp;
 	    LinkedAvlNode<K> parent = null;
@@ -80,7 +80,7 @@
 	      root = new LinkedAvlNode<K>( key );
 	      first = root;
 	      last = root;
-	      return;
+	      return null;
 	    }
 	    
 	    node = new LinkedAvlNode<K>( key );
@@ -98,7 +98,7 @@
 	        
 	        if( c == 0 )
 	        {
-	            return; // key already exists
+	            return key; // key already exists
 	        }
 	        
 	        if( c < 0 )
@@ -126,6 +126,8 @@
         
         treePath.add( 0, node );
 	    balance(treePath);
+	    
+	    return null;
 	}
 	
 	
@@ -200,7 +202,7 @@
      *
      * @param key the value of the node to be removed
      */
-    public void remove( K key )
+    public K remove( K key )
     {
         LinkedAvlNode<K> temp = null;
         LinkedAvlNode<K> y = null;
@@ -211,7 +213,7 @@
         
         if( treePath == null )
         {
-            return;
+            return null;
         }
         
         temp = treePath.remove( 0 );
@@ -224,7 +226,7 @@
             if( temp == root )
             {
               root = null;
-              return;
+              return key;
             }
             
             if( !treePath.isEmpty() )
@@ -296,6 +298,8 @@
        
        treePath.add( 0, y ); // y can be null but getBalance returns 0 so np
        balance( treePath );
+       
+       return key;
     }
     
     
@@ -422,6 +426,20 @@
         return root;
     }
     
+    
+    public List<K> getKeys()
+    {
+        List<K> keys = new ArrayList<K>();
+        LinkedAvlNode<K> node = first;
+        
+        while( node != null )
+        {
+            keys.add( node.key );
+            node = node.next;
+        }
+        
+        return keys;
+    }
 
     /**
      * Prints the contents of AVL tree in pretty format

Modified: directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTreeMarshaller.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTreeMarshaller.java?rev=635669&r1=635668&r2=635669&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTreeMarshaller.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/AvlTreeMarshaller.java
Mon Mar 10 12:45:38 2008
@@ -26,6 +26,8 @@
 import java.io.IOException;
 import java.util.Comparator;
 
+import jdbm.helper.Serializer;
+
 import org.apache.directory.server.core.avltree.AvlTree;
 import org.apache.directory.server.core.avltree.LinkedAvlNode;
 
@@ -41,7 +43,7 @@
 {
 
     /** marshaller to be used for marshalling the keys */
-    private Marshaller<E> keyMarshaller;
+    private Serializer keyMarshaller;
     
     /** key Comparator for the AvlTree */
     private Comparator<E> comparator;
@@ -58,16 +60,30 @@
      */
     public AvlTreeMarshaller(Comparator<E> comparator, Marshaller keyMarshaller)
     {
+        this( comparator, ( Serializer )keyMarshaller );
+    }
+
+    /**
+     * 
+     * Creates a new instance of AvlTreeMarshaller.
+     *
+     * @param comparator Comparator to be used for key comparision
+     * @param keySerializer marshaller for keys
+     */
+    public AvlTreeMarshaller(Comparator<E> comparator, Serializer keySerializer)
+    {
         this.comparator = comparator;
-        this.keyMarshaller = keyMarshaller;
+        this.keyMarshaller = keySerializer;
     }
     
     /**
      * Marshals the given tree to bytes
      * @param tree the tree to be marshalled
      */
-    public byte[] marshal( AvlTree<E> tree )
+    public byte[] serialize( Object avlTreeObj )
     {
+        AvlTree<E> tree = ( AvlTree<E> ) avlTreeObj;
+        
         if( tree.isEmpty() )
         {
             return null;
@@ -105,7 +121,7 @@
      */
     private void writeTree( LinkedAvlNode<E> node, DataOutputStream out ) throws IOException
     {
-        byte[] data = keyMarshaller.marshal( node.getKey() );
+        byte[] data = keyMarshaller.serialize( node.getKey() );
         
         out.writeInt( data.length ); // data-length
         out.write( data ); // data
@@ -139,7 +155,7 @@
      * 
      * @param data byte array to be converted into AVLTree  
      */
-    public AvlTree<E> unMarshal( byte[] data )
+    public AvlTree<E> deserialize( byte[] data )
     {
         ByteArrayInputStream bin = new ByteArrayInputStream( data );
         DataInputStream din = new DataInputStream( bin );
@@ -191,7 +207,7 @@
       byte[] data = new byte[ dLen ];
       in.read( data );
 
-      E key = keyMarshaller.unMarshal( data );
+      E key = ( E ) keyMarshaller.deserialize( data );
       node = new LinkedAvlNode( key );
       
       int index = in.readInt();

Modified: directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/Marshaller.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/Marshaller.java?rev=635669&r1=635668&r2=635669&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/Marshaller.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/main/java/org/apache/directory/server/core/avltree/Marshaller.java
Mon Mar 10 12:45:38 2008
@@ -19,6 +19,8 @@
  */
 package org.apache.directory.server.core.avltree;
 
+import jdbm.helper.Serializer;
+
 /**
  * 
  * An interface to marshall/unmarshal the AVLTree keys.
@@ -26,8 +28,6 @@
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public interface Marshaller<E>
+public interface Marshaller<E> extends Serializer
 {
-    byte[] marshal(E e);
-    E unMarshal(byte[] data);
 }

Modified: directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeMarshallerTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeMarshallerTest.java?rev=635669&r1=635668&r2=635669&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeMarshallerTest.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeMarshallerTest.java
Mon Mar 10 12:45:38 2008
@@ -80,7 +80,7 @@
         tree.insert( 9 );
 
         FileOutputStream fout = new FileOutputStream( treeFile );
-        fout.write( treeMarshaller.marshal( tree ) );
+        fout.write( treeMarshaller.serialize( tree ) );
         fout.close();
         
         savedTree = tree; // to reference in other tests
@@ -100,7 +100,7 @@
         byte[] data = new byte[ ( int )treeFile.length() ];
         fin.read( data );
         
-        AvlTree<Integer> unmarshalledTree = treeMarshaller.unMarshal( data );
+        AvlTree<Integer> unmarshalledTree = treeMarshaller.deserialize( data );
         
         System.out.println("\nunmarshalled tree\n---------------");
         unmarshalledTree.printTree();
@@ -109,7 +109,7 @@
 
         unmarshalledTree.insert( 6 ); // will change the root as part of balancing
         
-        assertFalse( savedTree.getRoot().getKey() == unmarshalledTree.getRoot().getKey()
);
+        assertTrue( savedTree.getRoot().getKey() == unmarshalledTree.getRoot().getKey() );
         assertTrue( 8 == unmarshalledTree.getRoot().getKey() ); // new root
         
         assertTrue( 37 == unmarshalledTree.getLast().getKey() );

Modified: directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreePerfTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreePerfTest.java?rev=635669&r1=635668&r2=635669&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreePerfTest.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreePerfTest.java
Mon Mar 10 12:45:38 2008
@@ -263,7 +263,7 @@
     
       start = System.nanoTime();
       
-      fout.write( treeMarshaller.marshal( tree ) );
+      fout.write( treeMarshaller.serialize( tree ) );
       fout.flush();
       fout.close();
       
@@ -283,7 +283,7 @@
         start = System.nanoTime();
         
         fin.read(data);
-        tree = (AvlTree<Integer>) treeMarshaller.unMarshal( data );
+        tree = (AvlTree<Integer>) treeMarshaller.deserialize( data );
         
         end = System.nanoTime();
         System.out.println("total time taken for reconstructing a serialized AVLTree ->"
+ getTime( start, end ) );

Modified: directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeTest.java?rev=635669&r1=635668&r2=635669&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeTest.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/AvlTreeTest.java
Mon Mar 10 12:45:38 2008
@@ -102,10 +102,10 @@
     @Test
     public void testInsert()
     {
-        tree.insert( 3 );
+        assertNull( tree.insert( 3 ) );
         assertFalse( tree.isEmpty() );
         
-        tree.insert( 3 );// should be ignored
+        assertTrue( 3 == tree.insert( 3 ) );// should be ignored
         assertTrue( 1 == tree.getSize() );
 
         assertNotNull( tree.getFirst() );
@@ -210,7 +210,8 @@
         assertEquals("3", getInorderForm());
         assertTrue( 3 == tree.getRoot().key );
         
-        tree.remove( 3 );
+        assertNull( tree.remove( 777 ) );// key not present
+        assertTrue( 3 == tree.remove( 3 ) );
         assertTrue(tree.isEmpty());
         
         tree.insert( 37 );
@@ -337,6 +338,21 @@
         tree.remove( 79 ); // should call findMin internally
         assertTrue( 11 == tree.getRoot().key );
     }
+    
+    @Test
+    public void testGetKeys()
+    {
+        tree.insert( 72 );
+        tree.insert( 79 );
+        tree.insert( 1 );
+        tree.insert( 2 );
+        tree.insert( 3 );
+        tree.insert( 7 );
+        tree.insert( 34 );
+        
+        assertTrue( 7 == tree.getKeys().size() );
+    }
+    
     
     private String getLinkedText() 
     {

Modified: directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/IntegerKeyMarshaller.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/IntegerKeyMarshaller.java?rev=635669&r1=635668&r2=635669&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/IntegerKeyMarshaller.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/core-avl/src/test/java/org/apache/directory/server/core/avltree/IntegerKeyMarshaller.java
Mon Mar 10 12:45:38 2008
@@ -4,8 +4,9 @@
 public class IntegerKeyMarshaller implements Marshaller<Integer>
 {
 
-    public byte[] marshal( Integer i )
+    public byte[] serialize( Object integerObj )
     {
+        Integer i = ( Integer ) integerObj;
         int y = i.intValue();
         byte[] data = new byte[4];
         data[0] = (byte)((y & 0xFF) >>> 24);
@@ -17,7 +18,7 @@
     }
 
 
-    public Integer unMarshal( byte[] data )
+    public Integer deserialize( byte[] data )
     {
         if( data == null || data.length == 0)
         {

Modified: directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmDupsCursor.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmDupsCursor.java?rev=635669&r1=635668&r2=635669&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmDupsCursor.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmDupsCursor.java
Mon Mar 10 12:45:38 2008
@@ -1,18 +1,20 @@
 package org.apache.directory.server.core.partition.impl.btree.jdbm;
 
 
+import java.util.Comparator;
+
 import jdbm.btree.BTree;
-import org.apache.directory.server.core.cursor.InvalidCursorPositionException;
-import org.apache.directory.server.core.cursor.Cursor;
-import org.apache.directory.server.core.cursor.ListCursor;
+
+import org.apache.directory.server.core.avltree.AvlTree;
+import org.apache.directory.server.core.avltree.AvlTreeCursor;
 import org.apache.directory.server.core.cursor.AbstractCursor;
+import org.apache.directory.server.core.cursor.Cursor;
+import org.apache.directory.server.core.cursor.InvalidCursorPositionException;
 import org.apache.directory.server.core.partition.impl.btree.Tuple;
 import org.apache.directory.shared.ldap.NotImplementedException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.*;
-
 
 /**
  * A Cursor over a BTree which manages duplicate keys.
@@ -39,11 +41,11 @@
      * pairs in the btree of the JDBM Table.  It does not return different
      * values for the same key: hence it is not duplicate key aware.  So for
      * tables supporting duplicate keys, it's Tuple vlaues may contain
-     * TreeSet or BTreeRedirect objects.  These objects are processed by
+     * AvlTree or BTreeRedirect objects.  These objects are processed by
      * this outer Cursor to mimic traversal over the Table as if duplicate
      * keys are natively allowed by JDBM.
      *
-     * In essence the TreeSet and the BTreeRedirect are used to store multiple
+     * In essence the AvlTree and the BTreeRedirect are used to store multiple
      * values for the same key.
      */
     private final JdbmNoDupsCursor<K,V> noDupsCursor;
@@ -51,7 +53,7 @@
     /**
      * A Cursor over a set of value objects for the current key.  A new Cursor
      * will be created for each new key as we traverse table's that allow for
-     * duplicate keys.  The Cursor traverses over either a TreeSet object full
+     * duplicate keys.  The Cursor traverses over either a AvlTree object full
      * of values in a multi-valued key or it traverses over a BTree which
      * contains the values in it's keys.
      */
@@ -59,8 +61,8 @@
 
     /**
      * The current Tuple returned from the underlying JdbmNoDupsCursor which
-     * may contain a TreeSet or BTreeRedirect for Tuple values.  A
-     * JdbmNoDupsCursor on a Table that allows duplicates returns TreeSets or
+     * may contain a AvlTree or BTreeRedirect for Tuple values.  A
+     * JdbmNoDupsCursor on a Table that allows duplicates returns AvlTrees or
      * BTreeRedirect objects for Tuple values.
      */
     private Tuple<K,V> noDupsTuple;
@@ -101,10 +103,10 @@
         {
             noDupsTuple = noDupsCursor.get();
             
-            if ( noDupsTuple.getValue() instanceof TreeSet )
+            if ( noDupsTuple.getValue() instanceof byte[] )
             {
-                LOG.debug( "Duplicates tuple {} stored in a TreeSet", noDupsTuple );
-                TreeSet<V> set = ( TreeSet<V> ) noDupsTuple.getValue();
+                LOG.debug( "Duplicates tuple {} stored in a AvlTree", noDupsTuple );
+                AvlTree<V> set = ( AvlTree<V> ) table.getMarshaller().deserialize(
( byte[] ) noDupsTuple.getValue() );
             }
             else 
             {
@@ -157,13 +159,11 @@
             noDupsTuple = noDupsCursor.get();
             Object values = noDupsTuple.getValue();
 
-            if ( values instanceof TreeSet)
+            if ( values instanceof AvlTree)
             {
                 //noinspection unchecked
-                TreeSet<V> set = ( TreeSet<V> ) noDupsTuple.getValue();
-                List<V> list = new ArrayList<V>( set.size() );
-                list.addAll( set );
-                dupCursor = new ListCursor<V>( list );
+                AvlTree<V> set = ( AvlTree<V> ) noDupsTuple.getValue();
+                dupCursor = new AvlTreeCursor<V>( set );
                 if ( ! dupCursor.previous() )
                 {
                     clearValue();
@@ -185,7 +185,7 @@
             /*
              * If we get to this point then cursor has more elements and
              * noDupsTuple holds the Tuple containing the key and the btree or
-             * TreeSet of values for that key which the Cursor traverses.  All we
+             * AvlTree of values for that key which the Cursor traverses.  All we
              * need to do is populate our tuple object with the key and the value
              * in the cursor.
              */
@@ -209,7 +209,7 @@
         {
             /*
              * If the underlying cursor has more elements we get the previous
-             * key/TreeSet Tuple to work with and get a cursor over it's
+             * key/AvlTree Tuple to work with and get a cursor over it's
              * values.
              */
             if ( noDupsCursor.previous() )
@@ -217,13 +217,11 @@
                 noDupsTuple = noDupsCursor.get();
                 Object values = noDupsTuple.getValue();
 
-                if ( values instanceof TreeSet )
+                if ( values instanceof AvlTree )
                 {
                     //noinspection unchecked
-                    TreeSet<V> set = ( TreeSet ) noDupsTuple.getValue();
-                    List<V> list = new ArrayList<V>( set.size() );
-                    list.addAll( set );
-                    dupCursor = new ListCursor<V>( list );
+                    AvlTree<V> set = ( AvlTree ) noDupsTuple.getValue();
+                    dupCursor = new AvlTreeCursor<V>( set );
                     dupCursor.previous();
                 }
                 else if ( values instanceof BTreeRedirect )
@@ -243,7 +241,7 @@
         /*
          * If we get to this point then cursor has more elements and
          * noDupsTuple holds the Tuple containing the key and the btree or
-         * TreeSet of values for that key which the Cursor traverses.  All we
+         * AvlTree of values for that key which the Cursor traverses.  All we
          * need to do is populate our tuple object with the key and the value
          * in the cursor.
          */
@@ -263,20 +261,18 @@
         {
             /*
              * If the underlying cursor has more elements we get the next
-             * key/TreeSet Tuple to work with and get a cursor over it.
+             * key/AvlTree Tuple to work with and get a cursor over it.
              */
             if ( noDupsCursor.next() )
             {
                 noDupsTuple = noDupsCursor.get();
                 Object values = noDupsTuple.getValue();
 
-                if ( values instanceof TreeSet)
+                if ( values instanceof AvlTree)
                 {
                     //noinspection unchecked
-                    TreeSet<V> set = ( TreeSet<V> ) noDupsTuple.getValue();
-                    List<V> list = new ArrayList<V>( set.size() );
-                    list.addAll( set );
-                    dupCursor = new ListCursor<V>( list );
+                    AvlTree<V> set = ( AvlTree<V> ) noDupsTuple.getValue();
+                    dupCursor = new AvlTreeCursor<V>( set );
                     dupCursor.next();
                 }
                 else if ( values instanceof BTreeRedirect )
@@ -296,7 +292,7 @@
         /*
          * If we get to this point then cursor has more elements and
          * noDupsTuple holds the Tuple containing the key and the btree or
-         * TreeSet of values for that key which the Cursor traverses.  All we
+         * AvlTree of values for that key which the Cursor traverses.  All we
          * need to do is populate our tuple object with the key and the value
          * in the cursor.
          */

Modified: directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java?rev=635669&r1=635668&r2=635669&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java
Mon Mar 10 12:45:38 2008
@@ -24,6 +24,11 @@
 import jdbm.btree.BTree;
 import jdbm.helper.Serializer;
 import jdbm.helper.TupleBrowser;
+
+import org.apache.directory.server.core.avltree.AvlTree;
+import org.apache.directory.server.core.avltree.AvlTreeMarshaller;
+import org.apache.directory.server.core.avltree.IntegerKeyMarshaller;
+import org.apache.directory.server.core.avltree.Marshaller;
 import org.apache.directory.server.core.cursor.Cursor;
 import org.apache.directory.server.core.partition.impl.btree.*;
 import org.apache.directory.server.schema.SerializableComparator;
@@ -67,7 +72,7 @@
     /** @TODO should really be a cache of duplicate BTrees */
     private Map<Long, BTree> duplicateBtrees = new HashMap<Long, BTree>();
     
-    
+    AvlTreeMarshaller<V> marshaller;
     // ------------------------------------------------------------------------
     // C O N S T R U C T O R
     // ------------------------------------------------------------------------
@@ -80,7 +85,7 @@
      * @param name the name of the table
      * @param allowsDuplicates whether or not duplicates are enabled 
      * @param numDupLimit the size limit of duplicates before switching to
-     * BTrees for values instead of TreeSets
+     * BTrees for values instead of AvlTrees
      * @param manager the record manager to be used for this table
      * @param comparator a tuple comparator
      * @param keySerializer a serializer to use for the keys instead of using
@@ -98,6 +103,20 @@
             (keySerializer == null ? "null" : keySerializer.getClass().getName()) +
             ", valueSerializer = " + 
             (valueSerializer == null ? "null" : valueSerializer.getClass().getName()) );*/
+        
+        if( allowsDuplicates && valueSerializer == null )
+        {
+          throw new IllegalArgumentException("Value serializer cannot be null when duplicates
are allowed in JdbmTable");  
+        }
+
+        if( allowsDuplicates )
+        {
+            //TODO the IntegerKeyMarshaller should be replaced with the appropriate marshaller
for type V
+            marshaller = new AvlTreeMarshaller<V>( comparator.getValueComparator(),
valueSerializer );
+            // the value serializer causes problems between BTree and AvlTree cause each
use it in a different way
+            valueSerializer = null; // set this to null
+        }
+        
         this.numDupLimit = numDupLimit;
         this.name = name;
         this.recMan = manager;
@@ -124,6 +143,7 @@
             recId = recMan.insert( 0 );
             recMan.setNamedObject( name + SZSUFFIX, recId );
         }
+        
     }
 
 
@@ -255,12 +275,12 @@
         }
         
         // -------------------------------------------------------------------
-        // Handle the use of a TreeSet for storing duplicates
+        // Handle the use of a AvlTree for storing duplicates
         // -------------------------------------------------------------------
 
-        if ( values instanceof TreeSet )
+        if ( values instanceof AvlTree )
         {
-            return ( ( TreeSet ) values ).size();
+            return ( ( AvlTree ) values ).getSize();
         }
 
         // -------------------------------------------------------------------
@@ -299,11 +319,11 @@
             return null;
         }
         
-        if ( values instanceof TreeSet )
+        if ( values instanceof AvlTree )
         {
             //noinspection unchecked
-            TreeSet<V> set = ( TreeSet<V> ) values;
-            return set.first();
+            AvlTree<V> set = ( AvlTree<V> ) values;
+            return set.getFirst().getKey();
         }
 
         // Handle values if they are stored in another BTree
@@ -378,21 +398,21 @@
             return false;
         }
         
-        if ( values instanceof TreeSet )
+        if ( values instanceof AvlTree )
         {
-            TreeSet set = ( TreeSet ) values;
-            SortedSet subset;
+            AvlTree set = ( AvlTree ) values;
+            Object result;
     
             if ( isGreaterThan )
             {
-                subset = set.tailSet( val );
+                result = set.findGreater( val );
             }
             else
             {
-                subset = set.headSet( val );
+                result = set.findLess( val );
             }
 
-            return subset.size() > 0 || set.contains( val );
+            return ( result != null );
         }
         
         // last option is to try a btree with BTreeRedirects
@@ -505,9 +525,9 @@
             return false;
         }
         
-        if ( values instanceof TreeSet )
+        if ( values instanceof AvlTree )
         {
-            return ( ( TreeSet ) values ).contains( value );
+            return ( ( AvlTree ) values ).find( value ) != null;
         }
         
         return btreeHas( getBTree( ( BTreeRedirect ) values ), value );
@@ -553,21 +573,23 @@
         
         if ( values == null )
         {
-            values = new TreeSet<V>( comparator.getValueComparator() );
+            values = new AvlTree<V>( comparator.getValueComparator() );
         }
         
-        if ( values instanceof TreeSet )
+        if ( values instanceof AvlTree )
         {
-            TreeSet<V> set = ( TreeSet ) values;
+            AvlTree<V> set = ( AvlTree ) values;
+            
+            V result = set.insert( value );
             
-            if ( set.contains( value ) )
+            if ( result != null )// if the value already present returns the same value
             {
                 return value;
             }
             
-            boolean addSuccessful = set.add( value );
+            boolean addSuccessful = true;
             
-            if ( set.size() > numDupLimit )
+            if ( set.getSize() > numDupLimit )
             {
                 BTree tree = convertToBTree( set );
                 BTreeRedirect redirect = new BTreeRedirect( tree.getRecid() );
@@ -575,7 +597,7 @@
             }
             else
             {
-                replaced = ( V ) bt.insert( key, set, true );
+                replaced = ( V ) bt.insert( key, marshaller.serialize( set ), true );
             }
             
             if ( addSuccessful )
@@ -624,12 +646,12 @@
             return null;
         }
         
-        if ( values instanceof TreeSet )
+        if ( values instanceof AvlTree )
         {
-            TreeSet set = ( TreeSet ) values;
+            AvlTree set = ( AvlTree ) values;
 
             // If removal succeeds then remove if set is empty else replace it
-            if ( set.remove( value ) )
+            if ( set.remove( value ) != null )
             {
                 if ( set.isEmpty() )
                 {
@@ -637,7 +659,7 @@
                 }
                 else
                 {
-                    bt.insert( key, set, true );
+                    bt.insert( key, marshaller.serialize( set ), true );
                 }
                 count--;
                 return value;
@@ -646,7 +668,7 @@
             return null;
         }
 
-        // TODO might be nice to add code here that reverts to a TreeSet
+        // TODO might be nice to add code here that reverts to a AvlTree
         // if the number of duplicates falls below the numDupLimit value
         BTree tree = getBTree( ( BTreeRedirect ) values );
         if ( removeDupFromBTree( tree, value ) )
@@ -682,12 +704,12 @@
             return returned;
         }
 
-        if ( returned instanceof TreeSet )
+        if ( returned instanceof byte[] )
         {
             //noinspection unchecked
-            TreeSet<V> set = ( TreeSet<V> ) returned;
-            this.count -= set.size();
-            return set.first();
+            AvlTree<V> set = ( AvlTree<V> ) marshaller.deserialize( ( byte[]
) returned );
+            this.count -= set.getSize();
+            return set.getFirst().getKey();
         }
         
         BTree tree = getBTree( ( BTreeRedirect ) returned );
@@ -741,6 +763,11 @@
         }
     }
 
+    
+    public Marshaller getMarshaller()
+    {
+        return marshaller;
+    }
 
     // ------------------------------------------------------------------------
     // Private Utility Methods 
@@ -772,6 +799,12 @@
         {
             //noinspection unchecked
             val = ( V ) bt.find( key );
+
+            if( val != null && val instanceof byte[])
+            {
+                val = (V) marshaller.deserialize( ( byte[] ) val );
+            }
+            
         }
 
         return val;
@@ -907,10 +940,11 @@
     }
     
 
-    private BTree convertToBTree( TreeSet set ) throws IOException
+    private BTree convertToBTree( AvlTree set ) throws IOException
     {
         BTree tree = BTree.createInstance( recMan, comparator.getValueComparator() );
-        for ( Object element : set )
+        List<V> keys = set.getKeys();
+        for ( V element : keys )
         {
             tree.insert( element, EMPTY_BYTES, true );
         }

Modified: directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmDupsCursorTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmDupsCursorTest.java?rev=635669&r1=635668&r2=635669&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmDupsCursorTest.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmDupsCursorTest.java
Mon Mar 10 12:45:38 2008
@@ -25,6 +25,7 @@
 import org.apache.directory.server.core.partition.impl.btree.Table;
 import org.apache.directory.server.core.partition.impl.btree.Tuple;
 import org.apache.directory.server.core.partition.impl.btree.TupleComparator;
+import org.apache.directory.server.core.avltree.IntegerKeyMarshaller;
 import org.apache.directory.server.core.cursor.Cursor;
 import org.apache.directory.server.schema.SerializableComparator;
 import org.apache.directory.server.schema.registries.ComparatorRegistry;
@@ -82,7 +83,7 @@
                 new DefaultTupleComparator<Integer,Integer>(
                         new SerializableComparator<Integer>( "" ),
                         new SerializableComparator<Integer>( "" ) );
-        table = new JdbmTable<Integer,Integer>( "test", true, SIZE, recman, comparator,
null, null );
+        table = new JdbmTable<Integer,Integer>( "test", true, SIZE, recman, comparator,
null, new IntegerKeyMarshaller() );
         LOG.debug( "Created new table and populated it with data" );
     }
 

Modified: directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTableWithDuplicatesTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTableWithDuplicatesTest.java?rev=635669&r1=635668&r2=635669&view=diff
==============================================================================
--- directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTableWithDuplicatesTest.java
(original)
+++ directory/sandbox/akarasulu/bigbang/apacheds/jdbm-store/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTableWithDuplicatesTest.java
Mon Mar 10 12:45:38 2008
@@ -26,6 +26,7 @@
 import org.junit.Test;
 import static org.junit.Assert.*;
 
+import org.apache.directory.server.core.avltree.IntegerKeyMarshaller;
 import org.apache.directory.server.core.partition.impl.btree.Table;
 import org.apache.directory.server.core.partition.impl.btree.TupleComparator;
 import org.apache.directory.server.core.partition.impl.btree.DefaultTupleComparator;
@@ -80,7 +81,7 @@
                 new DefaultTupleComparator<Integer,Integer>(
                         new SerializableComparator<Integer>( "" ),
                         new SerializableComparator<Integer>( "" ) );
-        table = new JdbmTable<Integer,Integer>( "test", true, SIZE, recman, comparator,
null, null );
+        table = new JdbmTable<Integer,Integer>( "test", true, SIZE, recman, comparator,
null, new IntegerKeyMarshaller() );
         LOG.debug( "Created new table and populated it with data" );
     }
 
@@ -106,7 +107,7 @@
             new DefaultTupleComparator<Integer,Integer>(
                     new SerializableComparator<Integer>( "" ),
                     new SerializableComparator<Integer>( "" ) );
-        table = new JdbmTable<Integer,Integer>( "test", true, SIZE, recman, comparator,
null, null );
+        table = new JdbmTable<Integer,Integer>( "test", true, SIZE, recman, comparator,
null, new IntegerKeyMarshaller() );
         assertTrue( 2 == table.get( 1 ) );
     }
 



Mime
View raw message