From 3f220e69e20f9382b943eddd6257e2617a828dd7 Mon Sep 17 00:00:00 2001 From: Aditya Kishore Date: Thu, 18 Jul 2013 10:25:59 -0700 Subject: [PATCH] HBASE-9032 - Result.getBytes() returns null if backed by KeyValue array --- .../java/org/apache/hadoop/hbase/client/Result.java | 17 +++++++++++++++++ .../java/org/apache/hadoop/hbase/client/TestResult.java | 16 ++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/main/java/org/apache/hadoop/hbase/client/Result.java b/src/main/java/org/apache/hadoop/hbase/client/Result.java index bf8bda3..05f528c 100644 --- a/src/main/java/org/apache/hadoop/hbase/client/Result.java +++ b/src/main/java/org/apache/hadoop/hbase/client/Result.java @@ -36,6 +36,7 @@ import org.apache.hadoop.hbase.KeyValue.SplitKeyValue; import org.apache.hadoop.hbase.io.ImmutableBytesWritable; import org.apache.hadoop.hbase.io.WritableWithSize; import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.io.DataOutputBuffer; import org.apache.hadoop.io.Writable; /** @@ -401,6 +402,22 @@ public class Result implements Writable, WritableWithSize { * @return pointer to raw binary of Result */ public ImmutableBytesWritable getBytes() { + if (this.bytes == null && this.kvs != null) { + int totalLen = 0; + for(KeyValue kv : kvs) { + totalLen += kv.getLength() + Bytes.SIZEOF_INT; + } + DataOutputBuffer out = new DataOutputBuffer(totalLen); + try { + for(KeyValue kv : kvs) { + kv.write(out); + } + out.close(); + } catch (IOException e) { + throw new RuntimeException("IOException in Result.getBytes()", e); + } + this.bytes = new ImmutableBytesWritable(out.getData(), 0, out.getLength()); + } return this.bytes; } diff --git a/src/test/java/org/apache/hadoop/hbase/client/TestResult.java b/src/test/java/org/apache/hadoop/hbase/client/TestResult.java index f9e29c2..70d1862 100644 --- a/src/test/java/org/apache/hadoop/hbase/client/TestResult.java +++ b/src/test/java/org/apache/hadoop/hbase/client/TestResult.java @@ -122,6 +122,22 @@ public class TestResult extends TestCase { } } + /** + * Verify that Result.getBytes(...) behaves correctly. + */ + public void testResultGetBytes() throws Exception { + byte [] value1 = Bytes.toBytes("value1"); + byte [] qual = Bytes.toBytes("qual"); + + KeyValue kv1 = new KeyValue(row, family, qual, value); + KeyValue kv2 = new KeyValue(row, family, qual, value1); + + Result r1 = new Result(new KeyValue[] {kv1, kv2}); + Result r2 = new Result(r1.getBytes()); + // no exception thrown + Result.compareResults(r1, r2); + } + @org.junit.Rule public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu = new org.apache.hadoop.hbase.ResourceCheckerJUnitRule(); -- 1.8.1.msysgit.1