Author: elecharny
Date: Wed Jun 27 16:31:24 2012
New Revision: 1354619

URL: http://svn.apache.org/viewvc?rev=1354619&view=rev
Log:
Inferred the K type from the used comparator, so that we don't get some cast 
exceptions when storing keys in the arrays

Modified:
    
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/AbstractPage.java
    labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java

Modified: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/AbstractPage.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/AbstractPage.java?rev=1354619&r1=1354618&r2=1354619&view=diff
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/AbstractPage.java
 (original)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/AbstractPage.java
 Wed Jun 27 16:31:24 2012
@@ -19,6 +19,8 @@
  */
 package org.apache.mavibot.btree;
 
+import java.lang.reflect.Array;
+
 /**
  * A MVCC abstract Page. It stores the field and the methods shared by the 
Node and Leaf
  * classes.
@@ -66,7 +68,10 @@ public abstract class AbstractPage<K, V>
         this.btree = btree;
         this.revision = revision;
         this.nbElems = nbElems;
-        this.keys = (K[])new Object[nbElems];
+        
+        Class<?> keyType = btree.getKeyType();
+        this.keys = (K[])Array.newInstance( keyType, nbElems );
+        
         id = btree.generateRecordId();
     }
 

Modified: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java?rev=1354619&r1=1354618&r2=1354619&view=diff
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java 
(original)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/BTree.java 
Wed Jun 27 16:31:24 2012
@@ -20,6 +20,8 @@
 package org.apache.mavibot.btree;
 
 import java.io.IOException;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
 import java.util.Comparator;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -55,6 +57,9 @@ public class BTree<K, V>
 
     /** Number of entries in each Page. */
     protected int pageSize;
+    
+    /** The type to use to create the keys */
+    protected Class<?> keyType;
 
 
     /**
@@ -97,6 +102,16 @@ public class BTree<K, V>
         // and increment the revision at the same time
         rootPage = new Leaf<K, V>( this );
         roots.put( revision.getAndIncrement(), rootPage );
+        
+        // We will extract the Type to use for keys, using the comparator for 
that
+        Class<?> comparatorClass = comparator.getClass();
+        Type[] types = comparatorClass.getGenericInterfaces();
+        Type[] argumentTypes = 
((ParameterizedType)types[0]).getActualTypeArguments();
+        
+        if ( ( argumentTypes != null ) && ( argumentTypes.length > 0 ) && ( 
argumentTypes[0] instanceof Class<?> ) )
+        {
+            keyType = (Class<?>)argumentTypes[0];
+        }
     }
     
     
@@ -424,6 +439,15 @@ public class BTree<K, V>
         this.comparator = comparator;
     }
 
+
+    /**
+     * @return the type for the keys
+     */
+    public Class<?> getKeyType()
+    {
+        return keyType;
+    }
+
     
     /**
      * @see Object#toString()



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to