You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by ra...@apache.org on 2011/10/20 20:21:49 UTC

svn commit: r1186976 - in /hbase/branches/0.92: CHANGES.txt src/main/java/org/apache/hadoop/hbase/io/HbaseObjectWritable.java src/test/java/org/apache/hadoop/hbase/io/TestHbaseObjectWritable.java

Author: ramkrishna
Date: Thu Oct 20 18:21:49 2011
New Revision: 1186976

URL: http://svn.apache.org/viewvc?rev=1186976&view=rev
Log:
HBASE-4459 HbaseObjectWritable code is a byte, we will eventually run out of codes

Modified:
    hbase/branches/0.92/CHANGES.txt
    hbase/branches/0.92/src/main/java/org/apache/hadoop/hbase/io/HbaseObjectWritable.java
    hbase/branches/0.92/src/test/java/org/apache/hadoop/hbase/io/TestHbaseObjectWritable.java

Modified: hbase/branches/0.92/CHANGES.txt
URL: http://svn.apache.org/viewvc/hbase/branches/0.92/CHANGES.txt?rev=1186976&r1=1186975&r2=1186976&view=diff
==============================================================================
--- hbase/branches/0.92/CHANGES.txt (original)
+++ hbase/branches/0.92/CHANGES.txt Thu Oct 20 18:21:49 2011
@@ -349,6 +349,7 @@ Release 0.92.0 - Unreleased
    HBASE-4621  TestAvroServer fails quite often intermittently (Akash Ashok)
    HBASE-4378  [hbck] Does not complain about regions with startkey==endkey.
                (Jonathan Hsieh)
+   HBASE-4459  HbaseObjectWritable code is a byte, we will eventually run out of codes
 
   TESTS
    HBASE-4492  TestRollingRestart fails intermittently

Modified: hbase/branches/0.92/src/main/java/org/apache/hadoop/hbase/io/HbaseObjectWritable.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.92/src/main/java/org/apache/hadoop/hbase/io/HbaseObjectWritable.java?rev=1186976&r1=1186975&r2=1186976&view=diff
==============================================================================
--- hbase/branches/0.92/src/main/java/org/apache/hadoop/hbase/io/HbaseObjectWritable.java (original)
+++ hbase/branches/0.92/src/main/java/org/apache/hadoop/hbase/io/HbaseObjectWritable.java Thu Oct 20 18:21:49 2011
@@ -92,6 +92,7 @@ import org.apache.hadoop.io.ObjectWritab
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.io.Writable;
 import org.apache.hadoop.io.WritableFactories;
+import org.apache.hadoop.io.WritableUtils;
 
 /**
  * This is a customized version of the polymorphic hadoop
@@ -114,15 +115,15 @@ public class HbaseObjectWritable impleme
   // Here we maintain two static maps of classes to code and vice versa.
   // Add new classes+codes as wanted or figure way to auto-generate these
   // maps from the HMasterInterface.
-  static final Map<Byte, Class<?>> CODE_TO_CLASS =
-    new HashMap<Byte, Class<?>>();
-  static final Map<Class<?>, Byte> CLASS_TO_CODE =
-    new HashMap<Class<?>, Byte>();
+  static final Map<Integer, Class<?>> CODE_TO_CLASS =
+    new HashMap<Integer, Class<?>>();
+  static final Map<Class<?>, Integer> CLASS_TO_CODE =
+    new HashMap<Class<?>, Integer>();
   // Special code that means 'not-encoded'; in this case we do old school
   // sending of the class name using reflection, etc.
   private static final byte NOT_ENCODED = 0;
   static {
-    byte code = NOT_ENCODED + 1;
+    int code = NOT_ENCODED + 1;
     // Primitive types.
     addToMap(Boolean.TYPE, code++);
     addToMap(Byte.TYPE, code++);
@@ -317,7 +318,7 @@ public class HbaseObjectWritable impleme
     }
 
     public void readFields(DataInput in) throws IOException {
-      this.declaredClass = CODE_TO_CLASS.get(in.readByte());
+      this.declaredClass = CODE_TO_CLASS.get(WritableUtils.readVInt(in));
     }
 
     public void write(DataOutput out) throws IOException {
@@ -333,7 +334,7 @@ public class HbaseObjectWritable impleme
    */
   static void writeClassCode(final DataOutput out, final Class<?> c)
   throws IOException {
-    Byte code = CLASS_TO_CODE.get(c);
+    Integer code = CLASS_TO_CODE.get(c);
     if (code == null ) {
       if ( List.class.isAssignableFrom(c)) {
         code = CLASS_TO_CODE.get(List.class);
@@ -351,11 +352,9 @@ public class HbaseObjectWritable impleme
       for(StackTraceElement elem : els) {
         LOG.error(elem.getMethodName());
       }
-//          new Exception().getStackTrace()[0].getMethodName());
-//      throw new IOException(new Exception().getStackTrace()[0].getMethodName());
       throw new UnsupportedOperationException("No code for unexpected " + c);
     }
-    out.writeByte(code);
+    WritableUtils.writeVInt(out, code);
   }
 
 
@@ -452,7 +451,7 @@ public class HbaseObjectWritable impleme
       Text.writeString(out, ((Enum)instanceObj).name());
     } else if (Writable.class.isAssignableFrom(declClass)) { // Writable
       Class <?> c = instanceObj.getClass();
-      Byte code = CLASS_TO_CODE.get(c);
+      Integer code = CLASS_TO_CODE.get(c);
       if (code == null) {
         out.writeByte(NOT_ENCODED);
         Text.writeString(out, c.getName());
@@ -462,7 +461,7 @@ public class HbaseObjectWritable impleme
       ((Writable)instanceObj).write(out);
     } else if (Serializable.class.isAssignableFrom(declClass)) {
       Class <?> c = instanceObj.getClass();
-      Byte code = CLASS_TO_CODE.get(c);
+      Integer code = CLASS_TO_CODE.get(c);
       if (code == null) {
         out.writeByte(NOT_ENCODED);
         Text.writeString(out, c.getName());
@@ -514,7 +513,7 @@ public class HbaseObjectWritable impleme
   public static Object readObject(DataInput in,
       HbaseObjectWritable objectWritable, Configuration conf)
   throws IOException {
-    Class<?> declaredClass = CODE_TO_CLASS.get(in.readByte());
+    Class<?> declaredClass = CODE_TO_CLASS.get(WritableUtils.readVInt(in));
     Object instance;
     if (declaredClass.isPrimitive()) {            // primitive types
       if (declaredClass == Boolean.TYPE) {             // boolean
@@ -550,7 +549,7 @@ public class HbaseObjectWritable impleme
           Array.set(instance, i, readObject(in, conf));
         }
       }
-    } else if (List.class.isAssignableFrom(declaredClass)) {              // List
+    } else if (List.class.isAssignableFrom(declaredClass)) {            // List
       int length = in.readInt();
       instance = new ArrayList(length);
       for (int i = 0; i < length; i++) {
@@ -563,8 +562,8 @@ public class HbaseObjectWritable impleme
         Text.readString(in));
     } else {                                      // Writable or Serializable
       Class instanceClass = null;
-      Byte b = in.readByte();
-      if (b.byteValue() == NOT_ENCODED) {
+      int b = (byte)WritableUtils.readVInt(in);
+      if (b == NOT_ENCODED) {
         String className = Text.readString(in);
         try {
           instanceClass = getClassByName(conf, className);
@@ -627,7 +626,7 @@ public class HbaseObjectWritable impleme
     return Class.forName(className, true, cl);
   }
 
-  private static void addToMap(final Class<?> clazz, final byte code) {
+  private static void addToMap(final Class<?> clazz, final int code) {
     CLASS_TO_CODE.put(clazz, code);
     CODE_TO_CLASS.put(code, clazz);
   }

Modified: hbase/branches/0.92/src/test/java/org/apache/hadoop/hbase/io/TestHbaseObjectWritable.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.92/src/test/java/org/apache/hadoop/hbase/io/TestHbaseObjectWritable.java?rev=1186976&r1=1186975&r2=1186976&view=diff
==============================================================================
--- hbase/branches/0.92/src/test/java/org/apache/hadoop/hbase/io/TestHbaseObjectWritable.java (original)
+++ hbase/branches/0.92/src/test/java/org/apache/hadoop/hbase/io/TestHbaseObjectWritable.java Thu Oct 20 18:21:49 2011
@@ -32,6 +32,7 @@ import org.apache.hadoop.hbase.filter.Fi
 import org.apache.hadoop.hbase.filter.FilterBase;
 import org.apache.hadoop.hbase.filter.FilterList;
 import org.apache.hadoop.hbase.filter.PrefixFilter;
+import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.io.Writable;
 import org.apache.hadoop.io.WritableComparator;
@@ -50,6 +51,54 @@ public class TestHbaseObjectWritable ext
   }
 
   @SuppressWarnings("boxing")
+  public void testReadOldObjectDataInput() throws IOException {
+    Configuration conf = HBaseConfiguration.create();
+    /*
+     * This is the code used to generate byte[] where
+     *  HbaseObjectWritable used byte for code
+     * 
+    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
+    DataOutputStream out = new DataOutputStream(byteStream);
+    HbaseObjectWritable.writeObject(out, bytes, byte[].class, conf);
+    byte[] ba = byteStream.toByteArray();
+    out.close();
+    */
+    
+    /*
+     * byte array generated by the folowing call
+     *  HbaseObjectWritable.writeObject(out, new Text("Old"), Text.class, conf);
+     */
+    byte[] baForText = {13, 13, 3, 79, 108, 100};
+    Text txt = (Text)readByteArray(conf, baForText);
+    Text oldTxt = new Text("Old");
+    assertEquals(txt, oldTxt);
+    
+    final byte A = 'A';
+    byte [] bytes = new byte[1];
+    bytes[0] = A;
+    /*
+     * byte array generated by the folowing call
+     *  HbaseObjectWritable.writeObject(out, bytes, byte[].class, conf);
+     */
+    byte[] baForByteArray = { 11, 1, 65 };
+    byte[] baOut = (byte[])readByteArray(conf, baForByteArray);
+    assertTrue(Bytes.equals(baOut, bytes));
+  }
+  
+  /*
+   * helper method which reads byte array using HbaseObjectWritable.readObject()
+   */
+  private Object readByteArray(final Configuration conf, final byte[] ba)
+  throws IOException {
+    ByteArrayInputStream bais =
+      new ByteArrayInputStream(ba);
+    DataInputStream dis = new DataInputStream(bais);
+    Object product = HbaseObjectWritable.readObject(dis, conf);
+    dis.close();
+    return product;
+  }
+  
+  @SuppressWarnings("boxing")
   public void testReadObjectDataInputConfiguration() throws IOException {
     Configuration conf = HBaseConfiguration.create();
     // Do primitive type
@@ -212,4 +261,4 @@ public class TestHbaseObjectWritable ext
       this.key = Text.readString(in);
     }
   }
-}
\ No newline at end of file
+}