HBASE-3316 Add support for Java Serialization to HbaseObjectWritable

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1043179 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2010-12-07 19:46:58 +00:00
parent 7142228dd2
commit 23c9501783
3 changed files with 100 additions and 13 deletions

View File

@ -23,6 +23,8 @@ Release 0.91.0 - Unreleased
HBASE-3292 Expose block cache hit/miss/evict counts into region server
metrics
HBASE-2936 Differentiate between daemon & restart sleep periods
HBASE-3316 Add support for Java Serialization to HbaseObjectWritable
(Ed Kohlwey via Stack)
NEW FEATURES
HBASE-3287 Add option to cache blocks on hfile write and evict blocks on

View File

@ -18,9 +18,15 @@
package org.apache.hadoop.hbase.io;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
@ -207,6 +213,9 @@ public class HbaseObjectWritable implements Writable, WritableWithSize, Configur
addToMap(KeyOnlyFilter.class, code++);
// serializable
addToMap(Serializable.class, code++);
}
private Class<?> declaredClass;
@ -310,6 +319,9 @@ public class HbaseObjectWritable implements Writable, WritableWithSize, Configur
else if (Writable.class.isAssignableFrom(c)) {
code = CLASS_TO_CODE.get(Writable.class);
}
else if (Serializable.class.isAssignableFrom(c)){
code = CLASS_TO_CODE.get(Serializable.class);
}
}
if (code == null) {
LOG.error("Unsupported type " + c);
@ -426,6 +438,28 @@ public class HbaseObjectWritable implements Writable, WritableWithSize, Configur
writeClassCode(out, c);
}
((Writable)instanceObj).write(out);
} else if (Serializable.class.isAssignableFrom(declClass)) {
Class <?> c = instanceObj.getClass();
Byte code = CLASS_TO_CODE.get(c);
if (code == null) {
out.writeByte(NOT_ENCODED);
Text.writeString(out, c.getName());
} else {
writeClassCode(out, c);
}
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
try{
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(instanceObj);
byte[] value = bos.toByteArray();
out.writeInt(value.length);
out.write(value);
} finally {
if(bos!=null) bos.close();
if(oos!=null) oos.close();
}
} else {
throw new IOException("Can't write: "+instanceObj+" as "+declClass);
}
@ -505,7 +539,7 @@ public class HbaseObjectWritable implements Writable, WritableWithSize, Configur
} else if (declaredClass.isEnum()) { // enum
instance = Enum.valueOf((Class<? extends Enum>) declaredClass,
Text.readString(in));
} else { // Writable
} else { // Writable or Serializable
Class instanceClass = null;
Byte b = in.readByte();
if (b.byteValue() == NOT_ENCODED) {
@ -519,6 +553,7 @@ public class HbaseObjectWritable implements Writable, WritableWithSize, Configur
} else {
instanceClass = CODE_TO_CLASS.get(b);
}
if(Writable.class.isAssignableFrom(instanceClass)){
Writable writable = WritableFactories.newInstance(instanceClass, conf);
try {
writable.readFields(in);
@ -531,6 +566,24 @@ public class HbaseObjectWritable implements Writable, WritableWithSize, Configur
declaredClass = ((NullInstance)instance).declaredClass;
instance = null;
}
} else {
int length = in.readInt();
byte[] objectBytes = new byte[length];
in.readFully(objectBytes);
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try {
bis = new ByteArrayInputStream(objectBytes);
ois = new ObjectInputStream(bis);
instance = ois.readObject();
} catch (ClassNotFoundException e) {
LOG.error("Error in readFields", e);
throw new IOException("Error in readFields", e);
} finally {
if(bis!=null) bis.close();
if(ois!=null) ois.close();
}
}
}
if (objectWritable != null) { // store values
objectWritable.declaredClass = declaredClass;

View File

@ -63,7 +63,7 @@ public class TestHbaseObjectWritable extends TestCase {
// Do unsupported type.
boolean exception = false;
try {
doType(conf, new File("a"), File.class);
doType(conf, new Object(), Object.class);
} catch (UnsupportedOperationException uoe) {
exception = true;
}
@ -122,6 +122,17 @@ public class TestHbaseObjectWritable extends TestCase {
assertEquals("mykey", ((CustomFilter)child).getKey());
}
public void testCustomSerializable() throws Exception {
Configuration conf = HBaseConfiguration.create();
// test proper serialization of un-encoded serialized java objects
CustomSerializable custom = new CustomSerializable("test phrase");
Object obj = doType(conf, custom, CustomSerializable.class);
assertTrue(obj instanceof Serializable);
assertTrue(obj instanceof CustomSerializable);
assertEquals("test phrase", ((CustomSerializable)obj).getValue());
}
private Object doType(final Configuration conf, final Object value,
final Class<?> clazz)
throws IOException {
@ -137,6 +148,27 @@ public class TestHbaseObjectWritable extends TestCase {
return product;
}
public static class CustomSerializable implements Serializable {
private static final long serialVersionUID = 1048445561865740632L;
private String value = null;
public CustomSerializable() {
}
public CustomSerializable(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public static class CustomWritable implements Writable {
private String value = null;