directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r439957 - in /directory/branches/apacheds/optimization/core/src: main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTableTest.java
Date Mon, 04 Sep 2006 07:09:39 GMT
Author: akarasulu
Date: Mon Sep  4 00:09:39 2006
New Revision: 439957

URL: http://svn.apache.org/viewvc?view=rev&rev=439957
Log:
many corrections for duplicate key handling

Added:
    directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTableTest.java
Modified:
    directory/branches/apacheds/optimization/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java

Modified: directory/branches/apacheds/optimization/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java
URL: http://svn.apache.org/viewvc/directory/branches/apacheds/optimization/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java?view=diff&rev=439957&r1=439956&r2=439957
==============================================================================
--- directory/branches/apacheds/optimization/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java
(original)
+++ directory/branches/apacheds/optimization/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTable.java
Mon Sep  4 00:09:39 2006
@@ -78,7 +78,7 @@
     /** */
     private TupleRenderer renderer;
 
-    private int numDupLimit = 1000;
+    private int numDupLimit = 1;
 
     // ------------------------------------------------------------------------
     // C O N S T R U C T O R
@@ -254,20 +254,18 @@
 
         Object values = getRaw( key );
         
+        if ( values == null )
+        {
+            return 0;
+        }
+        
         // -------------------------------------------------------------------
         // Handle the use of a TreeSet for storing duplicates
         // -------------------------------------------------------------------
 
         if ( values instanceof TreeSet )
         {
-            TreeSet set = ( TreeSet ) values;
-    
-            if ( set != null )
-            {
-                return set.size();
-            }
-
-            return 0;
+            return ( ( TreeSet ) values ).size();
         }
 
         // -------------------------------------------------------------------
@@ -276,14 +274,7 @@
 
         if ( values instanceof BTree )
         {
-            BTree tree = ( BTree ) values;
-            
-            if ( values != null )
-            {
-                return tree.size();
-            }
-            
-            return 0;
+            return ( ( BTree ) values ).size();
         }
         
         throw new IllegalStateException( "When using duplicate keys either a TreeSet or BTree
is used for values." );
@@ -315,10 +306,16 @@
 
         Object values = getRaw( key );
         
+        if ( values == null )
+        {
+            return null;
+        }
+        
         if ( values instanceof TreeSet )
         {
             TreeSet set = ( TreeSet ) values;
-            if ( null == set || set.size() == 0 )
+            
+            if ( set.size() == 0 )
             {
                 return null;
             }
@@ -332,7 +329,7 @@
         {
             BTree tree = ( BTree ) values;
             
-            if ( null == tree || tree.size() == 0 )
+            if ( tree.size() == 0 )
             {
                 return null;
             }
@@ -410,12 +407,17 @@
 
         Object values = getRaw( key );
         
+        if ( values == null )
+        {
+            return false;
+        }
+        
         if ( values instanceof TreeSet )
         {
             TreeSet set = ( TreeSet ) values;
             SortedSet subset = null;
     
-            if ( null == set || set.size() == 0 )
+            if ( set.size() == 0 )
             {
                 return false;
             }
@@ -440,7 +442,7 @@
         if ( values instanceof BTree )
         {
             BTree tree = ( BTree ) values;
-            if ( null == tree || tree.size() == 0 )
+            if ( tree.size() == 0 )
             {
                 return false;
             }
@@ -609,28 +611,19 @@
         
         Object values = getRaw( key );
         
+        if ( values == null )
+        {
+            return false;
+        }
+        
         if ( values instanceof TreeSet )
         {
-            TreeSet set = ( TreeSet ) values;
-
-            if ( null == set )
-            {
-                return false;
-            }
-
-            return set.contains( value );
+            return ( ( TreeSet ) values ).contains( value );
         }
         
         if ( values instanceof BTree )
         {
-            BTree tree = ( BTree ) values;
-            
-            if ( null == tree )
-            {
-                return  false;
-            }
-            
-            return btreeHas( tree, value );
+            return btreeHas( ( BTree ) values, value );
         }
         
         throw new IllegalStateException( "When using duplicate keys either a TreeSet or BTree
is used for values." );
@@ -695,34 +688,36 @@
         
         Object values = getRaw( key );
         
+        if ( values == null )
+        {
+            values = new TreeSet( comparator.getValueComparator() );
+        }
+        
         if ( values instanceof TreeSet )
         {
             TreeSet set = ( TreeSet ) values;
-
-            if ( null == set )
-            {
-                set = new TreeSet( comparator.getValueComparator() );
-            }
-            else if ( set.contains( value ) )
+            
+            if ( set.contains( value ) )
             {
                 return value;
             }
-
+            
             boolean addSuccessful = set.add( value );
             
             if ( set.size() > numDupLimit )
             {
                 BTree tree = convertToBTree( set );
-                putRaw( key, tree, true );
+                replaced = putRaw( key, tree, true );
             }
             else
             {
-                putRaw( key, set, true );
+                replaced = putRaw( key, set, true );
             }
             
             if ( addSuccessful )
             {
                 count++;
+                return replaced;
             }
             return null;
         }
@@ -733,7 +728,9 @@
             if ( insertDupIntoBTree( tree, value ) )
             {
                 count++;
+                return putRaw( key, tree, true );
             }
+            return null;
         }
         
         throw new IllegalStateException( "When using duplicate keys either a TreeSet or BTree
is used for values." );
@@ -828,6 +825,11 @@
 
         Object storedValues = getRaw( key );
         
+        if ( values == null )
+        {
+            storedValues = new TreeSet( comparator.getValueComparator() );
+        }
+        
         if ( storedValues instanceof TreeSet )
         {
             /*
@@ -837,12 +839,6 @@
              * if it is we add it and increment the table entry counter.
              */
             TreeSet set = ( TreeSet ) storedValues;
-    
-            if ( null == set )
-            {
-                set = new TreeSet( comparator.getValueComparator() );
-            }
-    
             while ( values.hasMore() )
             {
                 Object val = values.next();
@@ -907,14 +903,14 @@
 
         Object values = getRaw( key );
         
+        if ( values == null )
+        {
+            return null;
+        }
+        
         if ( values instanceof TreeSet )
         {
-            TreeSet set = ( TreeSet ) getRaw( key );
-
-            if ( null == set )
-            {
-                return null;
-            }
+            TreeSet set = ( TreeSet ) values;
 
             // If removal succeeds then remove if set is empty else replace it
             if ( set.remove( value ) )
@@ -942,11 +938,6 @@
         {
             BTree tree = ( BTree ) values;
             
-            if ( null == tree )
-            {
-                return null;
-            }
-            
             if ( removeDupFromBTree( tree, value ) )
             {
                 if ( tree.size() == 0 )
@@ -1001,6 +992,11 @@
 
         Object storedValues = getRaw( key );
         
+        if ( storedValues == null )
+        {
+            return null;
+        }
+        
         if ( storedValues instanceof TreeSet )
         {
             /*
@@ -1009,10 +1005,6 @@
              * does not exist for key - nothing to do here.
              */
             TreeSet set = ( TreeSet ) storedValues;
-            if ( null == set )
-            {
-                return null;
-            }
     
             /*
              * So we have a valid TreeSet with values in it.  We check if each value
@@ -1039,10 +1031,6 @@
         if ( storedValues instanceof BTree )
         {
             BTree tree = ( BTree ) storedValues;
-            if ( null == tree )
-            {
-                return null;
-            }
             
             while ( values.hasMore() )
             {
@@ -1073,15 +1061,49 @@
             return null;
         }
 
-        if ( allowsDuplicates )
+        if ( ! allowsDuplicates )
+        {
+            this.count--;
+            return returned;
+        }
+
+        if ( returned instanceof TreeSet )
         {
             TreeSet set = ( TreeSet ) returned;
             this.count -= set.size();
             return set.first();
         }
-
-        this.count--;
-        return returned;
+        
+        if ( returned instanceof BTree )
+        {
+            BTree tree = ( BTree ) returned;
+            this.count -= tree.size();
+            removeAll( tree );
+        }
+        
+        throw new IllegalStateException( "When using duplicate keys either a TreeSet or BTree
is used for values." );
+    }
+    
+    
+    private void removeAll( BTree tree ) throws NamingException
+    {
+        jdbm.helper.Tuple jdbmTuple = new jdbm.helper.Tuple();
+        TupleBrowser browser;
+        try
+        {
+            browser = tree.browse();
+            while( browser.getNext( jdbmTuple ) )
+            {
+                tree.remove( jdbmTuple.getKey() );
+            }
+        }
+        catch ( IOException e )
+        {
+            LdapNamingException lne = new LdapNamingException( "Failed to remove all keys
in BTree",
+                ResultCodeEnum.OTHER );
+            lne.setRootCause( e );
+            throw lne;
+        }
     }
 
 
@@ -1106,14 +1128,14 @@
 
         Object values = getRaw( key );
         
+        if ( values == null )
+        {
+            return new EmptyEnumeration();
+        }
+        
         if ( values instanceof TreeSet )
         {
             TreeSet set = ( TreeSet ) values;
-            if ( null == set )
-            {
-                return new EmptyEnumeration();
-            }
-    
             final Iterator list = set.iterator();
             return new NamingEnumeration()
             {
@@ -1150,11 +1172,6 @@
         if ( values instanceof BTree )
         {
             BTree tree = ( BTree ) values;
-            if ( null == tree )
-            {
-                return new EmptyEnumeration();
-            }
-            
             return new BTreeEnumeration( tree );
         }
         
@@ -1215,15 +1232,15 @@
         }
 
         Object values = getRaw( key );
+
+        if ( values == null )
+        {
+            return new EmptyEnumeration();
+        }
         
         if ( values instanceof TreeSet )
         {
             TreeSet set = ( TreeSet ) values;
-            if ( set == null )
-            {
-                return new EmptyEnumeration();
-            }
-    
             Object[] objs = new Object[set.size()];
             objs = set.toArray( objs );
             ArrayIterator iterator = new ArrayIterator( objs );
@@ -1232,12 +1249,7 @@
         
         if ( values instanceof BTree )
         {
-            BTree tree = ( BTree ) values;
-            if ( null == tree )
-            {
-                return new EmptyEnumeration();
-            }
-            return new BTreeTupleEnumeration( tree, key );
+            return new BTreeTupleEnumeration( ( BTree ) values, key );
         }
 
         throw new IllegalStateException( "When using duplicate keys either a TreeSet or BTree
is used for values." );
@@ -1340,13 +1352,14 @@
 
         Object values = getRaw( key );
         
+        if ( values == null )
+        {
+            return new EmptyEnumeration();
+        }
+
         if ( values instanceof TreeSet )
         {
             TreeSet set = ( TreeSet ) values;
-            if ( set == null )
-            {
-                return new EmptyEnumeration();
-            }
     
             if ( isGreaterThan )
             {
@@ -1382,13 +1395,7 @@
         
         if ( values instanceof BTree )
         {
-            BTree tree = ( BTree ) values;
-            if ( tree == null )
-            {
-                return new EmptyEnumeration();
-            }
-
-            return new BTreeTupleEnumeration( tree, comparator.getValueComparator(), key,
val, isGreaterThan );
+            return new BTreeTupleEnumeration( ( BTree ) values, comparator.getValueComparator(),
key, val, isGreaterThan );
         }
 
         throw new IllegalStateException( "When using duplicate keys either a TreeSet or BTree
is used for values." );

Added: directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTableTest.java
URL: http://svn.apache.org/viewvc/directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTableTest.java?view=auto&rev=439957
==============================================================================
--- directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTableTest.java
(added)
+++ directory/branches/apacheds/optimization/core/src/test/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmTableTest.java
Mon Sep  4 00:09:39 2006
@@ -0,0 +1,102 @@
+/*
+ *  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.impl.btree.jdbm;
+
+
+import java.io.File;
+import java.io.Serializable;
+import java.math.BigInteger;
+
+import javax.naming.NamingException;
+
+import jdbm.RecordManager;
+import jdbm.recman.BaseRecordManager;
+
+import org.apache.directory.server.core.partition.impl.btree.TupleComparator;
+import org.apache.directory.server.core.schema.SerializableComparator;
+import org.apache.directory.shared.ldap.util.BigIntegerComparator;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Test cases for JdbmTable. 
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class JdbmTableTest extends TestCase implements Serializable
+{
+    private static final long serialVersionUID = 1L;
+    private transient File tempFile = null;
+    private transient RecordManager rm = null;
+    private final BigIntegerComparator biComparator = new BigIntegerComparator();
+    private final SerializableComparator serializableComparator = new SerializableComparator(
"integerMatchingRule" )
+    {
+        private static final long serialVersionUID = 1L;
+
+        public int compare( Object o1, Object o2 )
+        {
+            return biComparator.compare( o1, o2 );
+        }
+    };
+    private TupleComparator comparator = new TupleComparator()
+    {
+        private static final long serialVersionUID = 1L;
+
+        public int compareKey( Object key1, Object key2 )
+        {
+            return biComparator.compare( key1, key2 );
+        }
+
+        public int compareValue( Object value1, Object value2 )
+        {
+            return biComparator.compare( value1, value2 );
+        }
+
+        public SerializableComparator getKeyComparator()
+        {
+            return serializableComparator;
+        }
+
+        public SerializableComparator getValueComparator()
+        {
+            return serializableComparator;
+        }
+    };
+
+    
+    
+    public void setUp() throws Exception 
+    {
+        tempFile = File.createTempFile( "jdbm", "test" );
+        rm = new BaseRecordManager( tempFile.getAbsolutePath() );
+    }
+
+    
+    public void testCreateTableWithDups() throws NamingException
+    {
+        JdbmTable table = new JdbmTable( "test", true, rm, comparator );
+        for ( BigInteger ii = BigInteger.ZERO; ii.intValue() < 1001; ii = ii.add( BigInteger.ONE
) )
+        {
+            table.put( BigInteger.ONE, ii );
+        }
+    }
+}



Mime
View raw message