HBASE-2209 Support of List [ ] in HBaseOutputWritable for serialization

git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@909109 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2010-02-11 19:04:59 +00:00
parent af9891965e
commit 7b328fbbc5
4 changed files with 50 additions and 1 deletions

View File

@ -355,6 +355,8 @@ Release 0.21.0 - Unreleased
(Ferdy via Stack)
HBASE-2189 HCM trashes meta cache even when not needed
HBASE-2190 HRS should report to master when HMsg are available
HBASE-2209 Support of List [ ] in HBaseOutputWritable for serialization
(Kay Kay via Stack)
NEW FEATURES
HBASE-1961 HBase EC2 scripts

View File

@ -22,7 +22,11 @@ import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
@ -156,6 +160,9 @@ public class HbaseObjectWritable implements Writable, Configurable {
addToMap(HLog.Entry.class, code++);
addToMap(HLog.Entry[].class, code++);
addToMap(HLogKey.class, code++);
// List
addToMap(List.class, code++);
}
private Class<?> declaredClass;
@ -248,6 +255,11 @@ public class HbaseObjectWritable implements Writable, Configurable {
static void writeClassCode(final DataOutput out, final Class<?> c)
throws IOException {
Byte code = CLASS_TO_CODE.get(c);
if (code == null ) {
if ( List.class.isAssignableFrom(c)) {
code = CLASS_TO_CODE.get(List.class);
}
}
if (code == null) {
LOG.error("Unsupported type " + c);
StackTraceElement[] els = new Exception().getStackTrace();
@ -299,6 +311,14 @@ public class HbaseObjectWritable implements Writable, Configurable {
declClass.getComponentType(), conf);
}
}
} else if (List.class.isAssignableFrom(declClass)) {
List list = (List)instanceObj;
int length = list.size();
out.writeInt(length);
for (int i = 0; i < length; i++) {
writeObject(out, list.get(i),
list.get(i).getClass(), conf);
}
} else if (declClass == String.class) { // String
Text.writeString(out, (String)instanceObj);
} else if (declClass.isPrimitive()) { // primitive type
@ -402,6 +422,12 @@ public class HbaseObjectWritable implements Writable, Configurable {
Array.set(instance, i, readObject(in, conf));
}
}
} else if (List.class.isAssignableFrom(declaredClass)) { // List
int length = in.readInt();
instance = new ArrayList(length);
for (int i = 0; i < length; i++) {
((ArrayList)instance).add(readObject(in, conf));
}
} else if (declaredClass == String.class) { // String
instance = Text.readString(in);
} else if (declaredClass.isEnum()) { // enum

View File

@ -73,7 +73,8 @@ public interface HBaseRPCProtocolVersion extends VersionedProtocol {
* <li>Version 19: Added getClusterStatus().</li>
* <li>Version 20: Backed Transaction HBase out of HBase core.</li>
* <li>Version 21: HBASE-1665.</li>
* <li>Version 22: HBASE-2209. Added List support to RPC</li>
* </ul>
*/
public static final long versionID = 21L;
public static final long versionID = 22L;
}

View File

@ -25,6 +25,8 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
@ -33,6 +35,7 @@ import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparator;
import org.junit.Assert;
public class TestHbaseObjectWritable extends TestCase {
@ -74,6 +77,22 @@ public class TestHbaseObjectWritable extends TestCase {
// Do 'known' Writable type.
obj = doType(conf, new Text(""), Text.class);
assertTrue(obj instanceof Text);
//List.class
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("universe");
obj = doType(conf, list, List.class);
assertTrue(obj instanceof List);
Assert.assertArrayEquals(list.toArray(), ((List)obj).toArray() );
//ArrayList.class
ArrayList<String> arr = new ArrayList<String>();
arr.add("hello");
arr.add("world");
arr.add("universe");
obj = doType(conf, arr, ArrayList.class);
assertTrue(obj instanceof ArrayList);
Assert.assertArrayEquals(list.toArray(), ((ArrayList)obj).toArray() );
// Check that filters can be serialized
obj = doType(conf, new PrefixFilter(HConstants.EMPTY_BYTE_ARRAY),
PrefixFilter.class);
@ -94,4 +113,5 @@ public class TestHbaseObjectWritable extends TestCase {
dis.close();
return product;
}
}